From e48b6bb832fbb37d250d7ae8a04c36dc0eb78845 Mon Sep 17 00:00:00 2001 From: Tom Date: Mon, 16 Nov 2026 22:54:03 +0100 Subject: [PATCH] Send a head start of the response immediately before dripping Co-Authored-By: Claude Sonnet 5.5 --- CHANGELOG.md | 7 ++++ docs/configuration.md | 2 + .../Settings/HoldMethodSettings.cs | 3 ++ .../Settings/RuntimeSettingsParser.cs | 3 ++ src/Flypaper.Core/Settings/SettingKeys.cs | 3 +- src/Flypaper.Core/Strategies/DripOptions.cs | 2 + src/Flypaper.Core/Strategies/DripSchedule.cs | 19 +++++++++ src/Flypaper.Core/Strategies/DripStrategy.cs | 14 ++++++- .../Middleware/FlypaperMiddleware.cs | 1 + .../Pages/Settings/HoldMethodModel.cs | 2 + .../Pages/Settings/HoldMethodSection.razor | 5 +++ .../Core/HoldMethodParserTests.cs | 16 ++++++- .../Core/HtmlDripStrategyTests.cs | 42 ++++++++++++++++++- .../Core/StrategySelectionTests.cs | 2 +- test/Flypaper.Tests/Core/TrapRunnerTests.cs | 2 +- test/Flypaper.Tests/Http/ContentTypeTests.cs | 2 + .../Storage/SettingsOverrideTests.cs | 3 +- 17 files changed, 120 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 024f672..121be2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ All notable changes to Flypaper are documented in this file. The format follows ## [Unreleased] +### Added + +- Head start: the first bytes of the page (at least `HeadStartSize`, default 1024) are sent + immediately after the `200` headers, with a `Server` header, so scanners see a real response + before the drip begins. Adjustable on the settings page; the stored strategy name gets a + `head B` suffix (none when off). + ## [0.4.0] - 2026-11-15 ### Fixed diff --git a/docs/configuration.md b/docs/configuration.md index 691459f..3fd4b56 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -23,6 +23,7 @@ All optional. Read at startup; list entries need an index (`__0`, `__1`, ...). | `Flypaper__Drip__Content` | Hold method: `Html`, `Text` or `Alternating` | `Html` | | `Flypaper__Drip__MinDelay` / `MaxDelay` | Pause between chunks | `00:00:00.05` / `00:00:01` | | `Flypaper__Drip__MinChunkSize` / `MaxChunkSize` | Bytes per chunk | `1` / `64` | +| `Flypaper__Drip__HeadStartSize` | Bytes sent immediately before dripping starts (`0` = off) | `1024` | | `TZ` | Time zone for the dashboard | UTC | The defaults of the runtime settings below can also be set this way, e.g. @@ -38,6 +39,7 @@ general reset button removes all saved values; the hold method section has its o | Hold method: content (HTML filler text, random plain text, or alternating per catch) | HTML filler text | | Hold method: pause between chunks, min and max (10 ms to 30 s) | 50 ms / 1000 ms | | Hold method: chunk size, min and max (1 to 4096 bytes) | 1 / 64 bytes | +| Hold method: head start, sent without delay before the drip (0 to 65536 bytes) | 1024 bytes | | Hold method: maximum hold time per catch | `01:00:00` | | Maximum connections overall | `1000` | | Maximum connections per IP | `10` | diff --git a/src/Flypaper.Core/Settings/HoldMethodSettings.cs b/src/Flypaper.Core/Settings/HoldMethodSettings.cs index 0cd176e..f6dcc7b 100644 --- a/src/Flypaper.Core/Settings/HoldMethodSettings.cs +++ b/src/Flypaper.Core/Settings/HoldMethodSettings.cs @@ -9,6 +9,7 @@ public sealed record HoldMethodSettings( TimeSpan MaxDelay, int MinChunkSize, int MaxChunkSize, + int HeadStartSize, TimeSpan MaxHoldDuration) { public double BytesPerSecond => AverageBytesPerSecond(MinDelay, MaxDelay, MinChunkSize, MaxChunkSize); @@ -20,6 +21,7 @@ public sealed record HoldMethodSettings( [SettingKeys.DripMaxDelay] = MaxDelay.ToString("c", CultureInfo.InvariantCulture), [SettingKeys.DripMinChunkSize] = MinChunkSize.ToString(CultureInfo.InvariantCulture), [SettingKeys.DripMaxChunkSize] = MaxChunkSize.ToString(CultureInfo.InvariantCulture), + [SettingKeys.DripHeadStartSize] = HeadStartSize.ToString(CultureInfo.InvariantCulture), [SettingKeys.MaxHoldDuration] = MaxHoldDuration.ToString("c", CultureInfo.InvariantCulture) }; @@ -36,6 +38,7 @@ public sealed record HoldMethodInput( string? MaxDelayMs, string? MinChunkSize, string? MaxChunkSize, + string? HeadStartSize, string? MaxHoldDuration); public sealed record HoldMethodParseResult(HoldMethodSettings? Settings, IReadOnlyDictionary Errors); diff --git a/src/Flypaper.Core/Settings/RuntimeSettingsParser.cs b/src/Flypaper.Core/Settings/RuntimeSettingsParser.cs index 551f62a..7c4a67e 100644 --- a/src/Flypaper.Core/Settings/RuntimeSettingsParser.cs +++ b/src/Flypaper.Core/Settings/RuntimeSettingsParser.cs @@ -13,6 +13,7 @@ public static class RuntimeSettingsParser private const int MinDelayMs = 10; private const int MaxDelayMs = 30_000; private const int MaxChunkSize = 4096; + private const int MaxHeadStartSize = 65_536; public static RuntimeSettingsParseResult Parse(RuntimeSettingsInput input) { @@ -57,6 +58,7 @@ public static class RuntimeSettingsParser var maxDelay = ParseInt(input.MaxDelayMs, nameof(input.MaxDelayMs), MinDelayMs, MaxDelayMs, errors); var minChunk = ParseInt(input.MinChunkSize, nameof(input.MinChunkSize), 1, MaxChunkSize, errors); var maxChunk = ParseInt(input.MaxChunkSize, nameof(input.MaxChunkSize), 1, MaxChunkSize, errors); + var headStart = ParseInt(input.HeadStartSize, nameof(input.HeadStartSize), 0, MaxHeadStartSize, errors); var hold = ParseTimeSpan(input.MaxHoldDuration, nameof(input.MaxHoldDuration), MinHold, MaxHold, errors); if (minDelay > maxDelay) @@ -81,6 +83,7 @@ public static class RuntimeSettingsParser TimeSpan.FromMilliseconds(maxDelay!.Value), minChunk!.Value, maxChunk!.Value, + headStart!.Value, hold!.Value), errors); } diff --git a/src/Flypaper.Core/Settings/SettingKeys.cs b/src/Flypaper.Core/Settings/SettingKeys.cs index da29e87..d07bb83 100644 --- a/src/Flypaper.Core/Settings/SettingKeys.cs +++ b/src/Flypaper.Core/Settings/SettingKeys.cs @@ -12,12 +12,13 @@ public static class SettingKeys public const string DripMaxDelay = "Flypaper:Drip:MaxDelay"; public const string DripMinChunkSize = "Flypaper:Drip:MinChunkSize"; public const string DripMaxChunkSize = "Flypaper:Drip:MaxChunkSize"; + public const string DripHeadStartSize = "Flypaper:Drip:HeadStartSize"; public static readonly IReadOnlyList General = [MaxConnections, MaxConnectionsPerIp, IgnorePrivateNetworks, RetentionDays]; public static readonly IReadOnlyList HoldMethod = - [DripContent, DripMinDelay, DripMaxDelay, DripMinChunkSize, DripMaxChunkSize, MaxHoldDuration]; + [DripContent, DripMinDelay, DripMaxDelay, DripMinChunkSize, DripMaxChunkSize, DripHeadStartSize, MaxHoldDuration]; public static readonly IReadOnlySet All = new HashSet(General.Concat(HoldMethod), StringComparer.OrdinalIgnoreCase); diff --git a/src/Flypaper.Core/Strategies/DripOptions.cs b/src/Flypaper.Core/Strategies/DripOptions.cs index 391c972..70b8ea6 100644 --- a/src/Flypaper.Core/Strategies/DripOptions.cs +++ b/src/Flypaper.Core/Strategies/DripOptions.cs @@ -13,4 +13,6 @@ public sealed class DripOptions public int MinChunkSize { get; set; } = 1; public int MaxChunkSize { get; set; } = 64; + + public int HeadStartSize { get; set; } = 1024; } diff --git a/src/Flypaper.Core/Strategies/DripSchedule.cs b/src/Flypaper.Core/Strategies/DripSchedule.cs index fb996ba..ebd686f 100644 --- a/src/Flypaper.Core/Strategies/DripSchedule.cs +++ b/src/Flypaper.Core/Strategies/DripSchedule.cs @@ -5,6 +5,25 @@ namespace Flypaper.Core.Strategies; public static class DripSchedule { + public static ReadOnlyMemory TakeHead(IEnumerator fragments, int minBytes) + { + var head = new List(); + while (head.Count < minBytes && fragments.MoveNext()) + { + head.AddRange(Encoding.UTF8.GetBytes(fragments.Current)); + } + + return head.ToArray(); + } + + public static IEnumerable Remaining(IEnumerator fragments) + { + while (fragments.MoveNext()) + { + yield return fragments.Current; + } + } + public static IEnumerable Create(IEnumerable content, IRandomSource random, DripOptions options) { var minChunk = Math.Max(1, options.MinChunkSize); diff --git a/src/Flypaper.Core/Strategies/DripStrategy.cs b/src/Flypaper.Core/Strategies/DripStrategy.cs index 877f031..61e18a9 100644 --- a/src/Flypaper.Core/Strategies/DripStrategy.cs +++ b/src/Flypaper.Core/Strategies/DripStrategy.cs @@ -19,14 +19,24 @@ public abstract class DripStrategy( public string Describe() { var current = options.CurrentValue; + var head = current.HeadStartSize > 0 ? $" head {current.HeadStartSize}B" : ""; return string.Create( CultureInfo.InvariantCulture, - $"{Name} {current.MinDelay.TotalMilliseconds:0}-{current.MaxDelay.TotalMilliseconds:0}ms {current.MinChunkSize}-{current.MaxChunkSize}B"); + $"{Name} {current.MinDelay.TotalMilliseconds:0}-{current.MaxDelay.TotalMilliseconds:0}ms {current.MinChunkSize}-{current.MaxChunkSize}B{head}"); } public async Task RunAsync(ITrapChannel channel, CancellationToken ct) { - foreach (var step in DripSchedule.Create(Content(random), random, options.CurrentValue)) + var current = options.CurrentValue; + using var fragments = Content(random).GetEnumerator(); + + var head = DripSchedule.TakeHead(fragments, current.HeadStartSize); + if (head.Length > 0) + { + await channel.WriteAsync(head, ct); + } + + foreach (var step in DripSchedule.Create(DripSchedule.Remaining(fragments), random, current)) { ct.ThrowIfCancellationRequested(); await channel.WriteAsync(step.Chunk, ct); diff --git a/src/Flypaper.Http/Middleware/FlypaperMiddleware.cs b/src/Flypaper.Http/Middleware/FlypaperMiddleware.cs index 61c7a8d..41117af 100644 --- a/src/Flypaper.Http/Middleware/FlypaperMiddleware.cs +++ b/src/Flypaper.Http/Middleware/FlypaperMiddleware.cs @@ -73,6 +73,7 @@ internal sealed class FlypaperMiddleware( var response = context.Response; response.StatusCode = StatusCodes.Status200OK; response.ContentType = strategy is IStrategyDetails details ? details.MediaType : "text/html; charset=utf-8"; + response.Headers.Server = "nginx"; response.Headers.CacheControl = "no-store"; response.Headers["X-Accel-Buffering"] = "no"; await response.StartAsync(ct); diff --git a/src/Flypaper.Web/Components/Pages/Settings/HoldMethodModel.cs b/src/Flypaper.Web/Components/Pages/Settings/HoldMethodModel.cs index 7d84fef..fe4d99b 100644 --- a/src/Flypaper.Web/Components/Pages/Settings/HoldMethodModel.cs +++ b/src/Flypaper.Web/Components/Pages/Settings/HoldMethodModel.cs @@ -12,5 +12,7 @@ public sealed class HoldMethodModel public string? MaxChunkSize { get; set; } + public string? HeadStartSize { get; set; } + public string? MaxHoldDuration { get; set; } } diff --git a/src/Flypaper.Web/Components/Pages/Settings/HoldMethodSection.razor b/src/Flypaper.Web/Components/Pages/Settings/HoldMethodSection.razor index ed7acfc..7d5bf31 100644 --- a/src/Flypaper.Web/Components/Pages/Settings/HoldMethodSection.razor +++ b/src/Flypaper.Web/Components/Pages/Settings/HoldMethodSection.razor @@ -56,6 +56,9 @@ + + + @@ -125,6 +128,7 @@ Model.MaxDelayMs, Model.MinChunkSize, Model.MaxChunkSize, + Model.HeadStartSize, Model.MaxHoldDuration)); if (result.Settings is null) @@ -153,6 +157,7 @@ MaxDelayMs = ((int)drip.MaxDelay.TotalMilliseconds).ToString(CultureInfo.InvariantCulture), MinChunkSize = drip.MinChunkSize.ToString(CultureInfo.InvariantCulture), MaxChunkSize = drip.MaxChunkSize.ToString(CultureInfo.InvariantCulture), + HeadStartSize = drip.HeadStartSize.ToString(CultureInfo.InvariantCulture), MaxHoldDuration = Core.CurrentValue.MaxHoldDuration.ToString("c", CultureInfo.InvariantCulture) }; } diff --git a/test/Flypaper.Tests/Core/HoldMethodParserTests.cs b/test/Flypaper.Tests/Core/HoldMethodParserTests.cs index eb86a1f..7f7158a 100644 --- a/test/Flypaper.Tests/Core/HoldMethodParserTests.cs +++ b/test/Flypaper.Tests/Core/HoldMethodParserTests.cs @@ -6,7 +6,7 @@ namespace Flypaper.Tests.Core; public sealed class HoldMethodParserTests { - private static readonly HoldMethodInput Valid = new("Alternating", "50", "1000", "1", "64", "01:00:00"); + private static readonly HoldMethodInput Valid = new("Alternating", "50", "1000", "1", "64", "2048", "01:00:00"); [Fact] public void Parses_valid_input() @@ -18,6 +18,7 @@ public sealed class HoldMethodParserTests settings.MaxDelay.ShouldBe(TimeSpan.FromSeconds(1)); settings.MinChunkSize.ShouldBe(1); settings.MaxChunkSize.ShouldBe(64); + settings.HeadStartSize.ShouldBe(2048); settings.MaxHoldDuration.ShouldBe(TimeSpan.FromHours(1)); settings.ToConfiguration().Keys.ShouldBe(SettingKeys.HoldMethod, ignoreOrder: true); } @@ -56,6 +57,19 @@ public sealed class HoldMethodParserTests errors.Keys.ShouldBe([nameof(HoldMethodInput.MaxDelayMs), nameof(HoldMethodInput.MaxChunkSize)], ignoreOrder: true); } + [Theory] + [InlineData("0")] + [InlineData("65536")] + public void Accepts_head_start_bounds(string value) => + RuntimeSettingsParser.ParseHoldMethod(Valid with { HeadStartSize = value }).Settings.ShouldNotBeNull(); + + [Theory] + [InlineData("-1")] + [InlineData("65537")] + [InlineData("")] + public void Rejects_head_start_out_of_range(string value) => + RuntimeSettingsParser.ParseHoldMethod(Valid with { HeadStartSize = value }).Errors.Keys.ShouldBe([nameof(HoldMethodInput.HeadStartSize)]); + [Theory] [InlineData("10")] [InlineData("00:00:00")] diff --git a/test/Flypaper.Tests/Core/HtmlDripStrategyTests.cs b/test/Flypaper.Tests/Core/HtmlDripStrategyTests.cs index 028caad..c0edb7b 100644 --- a/test/Flypaper.Tests/Core/HtmlDripStrategyTests.cs +++ b/test/Flypaper.Tests/Core/HtmlDripStrategyTests.cs @@ -22,6 +22,46 @@ public sealed class HtmlDripStrategyTests Encoding.UTF8.GetString(channel.Writes.SelectMany(write => write).ToArray()).ShouldStartWith(""); } + [Fact] + public async Task Sends_head_start_without_delay_before_dripping() + { + var options = new DripOptions { MinDelay = TimeSpan.FromSeconds(30), MaxDelay = TimeSpan.FromSeconds(30), HeadStartSize = 1024 }; + var strategy = new HtmlDripStrategy(TestOptions.Monitor(options), new SeededRandomSource(21), TimeProvider.System); + var channel = new FakeTrapChannel(); + using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(200)); + + await Should.ThrowAsync(() => strategy.RunAsync(channel, cts.Token)); + + channel.Writes.Count.ShouldBe(2); + var head = channel.Writes.First(); + head.Length.ShouldBeGreaterThanOrEqualTo(1024); + Encoding.UTF8.GetString(head).ShouldStartWith(""); + } + + [Fact] + public async Task Skips_head_start_when_disabled() + { + var options = new DripOptions { MinDelay = TimeSpan.FromSeconds(30), MaxDelay = TimeSpan.FromSeconds(30), HeadStartSize = 0 }; + var strategy = new HtmlDripStrategy(TestOptions.Monitor(options), new SeededRandomSource(21), TimeProvider.System); + var channel = new FakeTrapChannel(); + using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(200)); + + await Should.ThrowAsync(() => strategy.RunAsync(channel, cts.Token)); + + channel.Writes.Count.ShouldBe(1); + channel.Writes.First().Length.ShouldBeLessThanOrEqualTo(64); + } + + [Theory] + [InlineData(1024, "html-drip 5-10ms 1-64B head 1024B")] + [InlineData(0, "html-drip 5-10ms 1-64B")] + public void Describe_marks_the_head_start_variant(int headStart, string expected) + { + var options = new DripOptions { MinDelay = TimeSpan.FromMilliseconds(5), MaxDelay = TimeSpan.FromMilliseconds(10), HeadStartSize = headStart }; + + new HtmlDripStrategy(TestOptions.Monitor(options), new SeededRandomSource(1), TimeProvider.System).Describe().ShouldBe(expected); + } + [Theory] [InlineData(TrapProtocol.Http, true)] [InlineData(TrapProtocol.Ssh, false)] @@ -29,7 +69,7 @@ public sealed class HtmlDripStrategyTests CreateStrategy().Supports(protocol).ShouldBe(expected); private static HtmlDripStrategy CreateStrategy() => new( - TestOptions.Monitor(new DripOptions { MinDelay = TimeSpan.FromMilliseconds(1), MaxDelay = TimeSpan.FromMilliseconds(5) }), + TestOptions.Monitor(new DripOptions { MinDelay = TimeSpan.FromMilliseconds(1), MaxDelay = TimeSpan.FromMilliseconds(5), HeadStartSize = 0 }), new SeededRandomSource(21), TimeProvider.System); } diff --git a/test/Flypaper.Tests/Core/StrategySelectionTests.cs b/test/Flypaper.Tests/Core/StrategySelectionTests.cs index 511e2aa..a1e6bcb 100644 --- a/test/Flypaper.Tests/Core/StrategySelectionTests.cs +++ b/test/Flypaper.Tests/Core/StrategySelectionTests.cs @@ -44,7 +44,7 @@ public sealed class StrategySelectionTests var options = TestOptions.Monitor(new DripOptions { MinDelay = TimeSpan.FromMilliseconds(100), MaxDelay = TimeSpan.FromMilliseconds(250), MinChunkSize = 2, MaxChunkSize = 8 }); var text = new TextDripStrategy(options, new SeededRandomSource(1), TimeProvider.System); - text.Describe().ShouldBe("text-drip 100-250ms 2-8B"); + text.Describe().ShouldBe("text-drip 100-250ms 2-8B head 1024B"); text.MediaType.ShouldStartWith("text/plain"); new HtmlDripStrategy(options, new SeededRandomSource(1), TimeProvider.System).MediaType.ShouldStartWith("text/html"); } diff --git a/test/Flypaper.Tests/Core/TrapRunnerTests.cs b/test/Flypaper.Tests/Core/TrapRunnerTests.cs index fe09370..fac7771 100644 --- a/test/Flypaper.Tests/Core/TrapRunnerTests.cs +++ b/test/Flypaper.Tests/Core/TrapRunnerTests.cs @@ -59,7 +59,7 @@ public sealed class TrapRunnerTests var outcome = await CreateRunner(TimeSpan.FromMinutes(1)).RunAsync(Context(), Open, cts.Token); outcome.ShouldBe(TrapOutcome.ClientAborted); - _sink.Received(1).Publish(Arg.Is(e => e.Outcome == TrapOutcome.ClientAborted && e.Strategy == "html-drip 5-20ms 1-64B")); + _sink.Received(1).Publish(Arg.Is(e => e.Outcome == TrapOutcome.ClientAborted && e.Strategy == "html-drip 5-20ms 1-64B head 1024B")); } [Theory] diff --git a/test/Flypaper.Tests/Http/ContentTypeTests.cs b/test/Flypaper.Tests/Http/ContentTypeTests.cs index 0431039..06d3774 100644 --- a/test/Flypaper.Tests/Http/ContentTypeTests.cs +++ b/test/Flypaper.Tests/Http/ContentTypeTests.cs @@ -45,5 +45,7 @@ public sealed class ContentTypeTests context.Response.StatusCode.ShouldBe(StatusCodes.Status200OK); context.Response.ContentType.ShouldBe(expected); + context.Response.Headers.Server.ToString().ShouldBe("nginx"); + context.Response.ContentLength.ShouldBeNull(); } } diff --git a/test/Flypaper.Tests/Storage/SettingsOverrideTests.cs b/test/Flypaper.Tests/Storage/SettingsOverrideTests.cs index 623dd3a..87e66e3 100644 --- a/test/Flypaper.Tests/Storage/SettingsOverrideTests.cs +++ b/test/Flypaper.Tests/Storage/SettingsOverrideTests.cs @@ -89,12 +89,13 @@ public sealed class SettingsOverrideTests : IDisposable var store = host.Services.GetRequiredService(); string[] holdKeys = [.. SettingKeys.HoldMethod]; - await store.SaveAsync(new HoldMethodSettings(DripContent.Text, TimeSpan.FromMilliseconds(200), TimeSpan.FromSeconds(2), 8, 16, TimeSpan.FromMinutes(5))); + await store.SaveAsync(new HoldMethodSettings(DripContent.Text, TimeSpan.FromMilliseconds(200), TimeSpan.FromSeconds(2), 8, 16, 256, TimeSpan.FromMinutes(5))); await store.SaveAsync(new RuntimeSettings(50, 4, 90, true)); drip.CurrentValue.Content.ShouldBe(DripContent.Text); drip.CurrentValue.MinDelay.ShouldBe(TimeSpan.FromMilliseconds(200)); drip.CurrentValue.MaxChunkSize.ShouldBe(16); + drip.CurrentValue.HeadStartSize.ShouldBe(256); core.CurrentValue.MaxHoldDuration.ShouldBe(TimeSpan.FromMinutes(5)); core.CurrentValue.MaxConnections.ShouldBe(50); (await store.HasOverridesAsync(holdKeys)).ShouldBeTrue();