Send a head start of the response immediately before dripping
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
13a78f4268
commit
e48b6bb832
17 files changed
+120
-8
No files matched your search
@@ -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 <n>B` suffix (none when off).
|
||||
|
||||
## [0.4.0] - 2026-11-15
|
||||
|
||||
### Fixed
|
||||
|
||||
@@ -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` |
|
||||
|
||||
@@ -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<string, string> Errors);
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<string> General =
|
||||
[MaxConnections, MaxConnectionsPerIp, IgnorePrivateNetworks, RetentionDays];
|
||||
|
||||
public static readonly IReadOnlyList<string> HoldMethod =
|
||||
[DripContent, DripMinDelay, DripMaxDelay, DripMinChunkSize, DripMaxChunkSize, MaxHoldDuration];
|
||||
[DripContent, DripMinDelay, DripMaxDelay, DripMinChunkSize, DripMaxChunkSize, DripHeadStartSize, MaxHoldDuration];
|
||||
|
||||
public static readonly IReadOnlySet<string> All =
|
||||
new HashSet<string>(General.Concat(HoldMethod), StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -5,6 +5,25 @@ namespace Flypaper.Core.Strategies;
|
||||
|
||||
public static class DripSchedule
|
||||
{
|
||||
public static ReadOnlyMemory<byte> TakeHead(IEnumerator<string> fragments, int minBytes)
|
||||
{
|
||||
var head = new List<byte>();
|
||||
while (head.Count < minBytes && fragments.MoveNext())
|
||||
{
|
||||
head.AddRange(Encoding.UTF8.GetBytes(fragments.Current));
|
||||
}
|
||||
|
||||
return head.ToArray();
|
||||
}
|
||||
|
||||
public static IEnumerable<string> Remaining(IEnumerator<string> fragments)
|
||||
{
|
||||
while (fragments.MoveNext())
|
||||
{
|
||||
yield return fragments.Current;
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<DripStep> Create(IEnumerable<string> content, IRandomSource random, DripOptions options)
|
||||
{
|
||||
var minChunk = Math.Max(1, options.MinChunkSize);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -12,5 +12,7 @@ public sealed class HoldMethodModel
|
||||
|
||||
public string? MaxChunkSize { get; set; }
|
||||
|
||||
public string? HeadStartSize { get; set; }
|
||||
|
||||
public string? MaxHoldDuration { get; set; }
|
||||
}
|
||||
@@ -56,6 +56,9 @@
|
||||
<SettingsField Label="Paket max." For="drip-max-chunk" Unit="Bytes" Hint="Zufällig zwischen min. und max." Errors="_errors" Key="@nameof(HoldMethodModel.MaxChunkSize)">
|
||||
<InputText id="drip-max-chunk" @bind-Value="Model!.MaxChunkSize" inputmode="numeric" data-bandwidth-input />
|
||||
</SettingsField>
|
||||
<SettingsField Label="Schneller Start" For="drip-head-start" Unit="Bytes" Hint="0 bis 65536, sofort ohne Pause gesendet" Errors="_errors" Key="@nameof(HoldMethodModel.HeadStartSize)">
|
||||
<InputText id="drip-head-start" @bind-Value="Model!.HeadStartSize" inputmode="numeric" />
|
||||
</SettingsField>
|
||||
<SettingsField Label="Maximale Haltedauer" For="hold" Hint="hh:mm:ss, höchstens 7 Tage" Errors="_errors" Key="@nameof(HoldMethodModel.MaxHoldDuration)">
|
||||
<InputText id="hold" @bind-Value="Model!.MaxHoldDuration" />
|
||||
</SettingsField>
|
||||
@@ -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)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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")]
|
||||
|
||||
@@ -22,6 +22,46 @@ public sealed class HtmlDripStrategyTests
|
||||
Encoding.UTF8.GetString(channel.Writes.SelectMany(write => write).ToArray()).ShouldStartWith("<!doctype html>");
|
||||
}
|
||||
|
||||
[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<OperationCanceledException>(() => strategy.RunAsync(channel, cts.Token));
|
||||
|
||||
channel.Writes.Count.ShouldBe(2);
|
||||
var head = channel.Writes.First();
|
||||
head.Length.ShouldBeGreaterThanOrEqualTo(1024);
|
||||
Encoding.UTF8.GetString(head).ShouldStartWith("<!doctype html>");
|
||||
}
|
||||
|
||||
[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<OperationCanceledException>(() => 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);
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
@@ -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<TrapEvent>(e => e.Outcome == TrapOutcome.ClientAborted && e.Strategy == "html-drip 5-20ms 1-64B"));
|
||||
_sink.Received(1).Publish(Arg.Is<TrapEvent>(e => e.Outcome == TrapOutcome.ClientAborted && e.Strategy == "html-drip 5-20ms 1-64B head 1024B"));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -89,12 +89,13 @@ public sealed class SettingsOverrideTests : IDisposable
|
||||
var store = host.Services.GetRequiredService<RuntimeSettingsStore>();
|
||||
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();
|
||||
|
||||
Reference in new issue
Block a user