From cbd983014389684ce1dfcf283ada0d783feb1683 Mon Sep 17 00:00:00 2001 From: Tom Date: Tue, 17 Nov 2026 19:29:53 +0100 Subject: [PATCH] Group strategies by base type and rank them by median hold time Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 11 +++ .../Dashboard/DashboardData.cs | 6 +- .../Dashboard/DashboardQueries.cs | 16 +++- .../Dashboard/RecentFilter.cs | 4 +- .../Pages/Dashboard/DashboardFilter.cs | 5 +- .../Pages/Dashboard/DashboardPage.razor | 16 +++- .../Components/Pages/Dashboard/TopTable.razor | 53 +++++++++-- .../Components/Shared/StrategyNames.cs | 94 +++++++++++++++++-- src/Flypaper.Web/wwwroot/app.css | 39 ++++++++ .../Storage/RecentCatchesTests.cs | 34 ++++++- .../Web/DashboardFilterTests.cs | 17 +++- test/Flypaper.Tests/Web/StrategyNamesTests.cs | 41 ++++++++ 12 files changed, 301 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9513493..b89882c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,17 @@ All notable changes to Flypaper are documented in this file. The format follows ## [Unreleased] +### Changed + +- Dashboard, Top 10 „Strategien“: strategies are grouped by base type, so `html-drip` with any drip speed + is one row „Endlose Webseite“; the old name `drip` counts as the same. Only `head`, `link` and `repeat` stay + separate rows and show small badges („Schnellstart“, „Links“, „Wiederholer“). The drip parameters moved to the + tooltip and the expanded row. +- Columns are now catches, **median hold time per catch** and total. The bar and the default sort use the median + (rows with fewer than 5 catches are greyed out, marked „wenige Daten“ and listed last). +- Clicking a row filters the catch list on all variants of the group. The catch list, filter chip and + expanded row show name plus badges. Old `?strategie=` links still work. + ## [0.5.1] - 2026-11-17 ### Changed diff --git a/src/Flypaper.Storage/Dashboard/DashboardData.cs b/src/Flypaper.Storage/Dashboard/DashboardData.cs index ad46a30..eb50da6 100644 --- a/src/Flypaper.Storage/Dashboard/DashboardData.cs +++ b/src/Flypaper.Storage/Dashboard/DashboardData.cs @@ -10,9 +10,11 @@ public sealed record DashboardData( TimeSpan TotalHoldTime, IReadOnlyList TopIps, IReadOnlyList TopTargets, - IReadOnlyList TopStrategies, + IReadOnlyList StrategyCatches, IReadOnlyList TopCountries, IReadOnlyList TopProviders, IReadOnlyDictionary CatchesByCountry); -public sealed record TopEntry(string Key, int Count, TimeSpan HoldTime, string? Label = null); +public sealed record TopEntry(string Key, int Count, TimeSpan HoldTime, string? Label = null, TimeSpan? MedianHold = null); + +public sealed record StrategySample(string Strategy, long DurationMs); diff --git a/src/Flypaper.Storage/Dashboard/DashboardQueries.cs b/src/Flypaper.Storage/Dashboard/DashboardQueries.cs index efebdc5..3567f01 100644 --- a/src/Flypaper.Storage/Dashboard/DashboardQueries.cs +++ b/src/Flypaper.Storage/Dashboard/DashboardQueries.cs @@ -42,7 +42,7 @@ public sealed class DashboardQueries(IDbContextFactory dbCont TimeSpan.FromMilliseconds(await catches.SumAsync(e => e.DurationMs, ct)), await TopAsync(catches.GroupBy(e => e.RemoteIp), ct), await TopAsync(catches.Where(e => e.Target != null).GroupBy(e => e.Target!), ct), - await TopAsync(catches.GroupBy(e => e.Strategy), ct), + await catches.Select(e => new StrategySample(e.Strategy, e.DurationMs)).ToListAsync(ct), countries .Take(TopCount) .Select(x => new TopEntry(x.Key, x.Count, TimeSpan.FromMilliseconds(x.DurationMs), x.Name)) @@ -107,7 +107,19 @@ public sealed class DashboardQueries(IDbContextFactory dbCont if (filter.Strategy is { } strategy) { - events = events.Where(e => e.Strategy == strategy); + var bases = strategy.Bases; + events = events.Where(e => bases.Any(name => e.Strategy == name || EF.Functions.Like(e.Strategy, name + " %", "\\"))); + foreach (var flag in strategy.With) + { + var pattern = $"% {flag} %"; + events = events.Where(e => EF.Functions.Like(" " + e.Strategy + " ", pattern, "\\")); + } + + foreach (var flag in strategy.Without) + { + var pattern = $"% {flag} %"; + events = events.Where(e => !EF.Functions.Like(" " + e.Strategy + " ", pattern, "\\")); + } } if (!string.IsNullOrWhiteSpace(filter.Search)) diff --git a/src/Flypaper.Storage/Dashboard/RecentFilter.cs b/src/Flypaper.Storage/Dashboard/RecentFilter.cs index f226eb0..1489da6 100644 --- a/src/Flypaper.Storage/Dashboard/RecentFilter.cs +++ b/src/Flypaper.Storage/Dashboard/RecentFilter.cs @@ -10,7 +10,7 @@ public sealed record RecentFilter( long? Asn = null, string? Ip = null, string? Target = null, - string? Strategy = null, + StrategyMatch? Strategy = null, string? Search = null, int Limit = RecentFilter.DefaultLimit, IReadOnlyList? SearchStrategies = null) @@ -22,3 +22,5 @@ public sealed record RecentFilter( public sealed record RecentCatches(IReadOnlyList Items, int Total, IReadOnlyList Countries); public sealed record CountryOption(string Code, string? Name); + +public sealed record StrategyMatch(IReadOnlyList Bases, IReadOnlyList With, IReadOnlyList Without); diff --git a/src/Flypaper.Web/Components/Pages/Dashboard/DashboardFilter.cs b/src/Flypaper.Web/Components/Pages/Dashboard/DashboardFilter.cs index 85505a6..8b0ae64 100644 --- a/src/Flypaper.Web/Components/Pages/Dashboard/DashboardFilter.cs +++ b/src/Flypaper.Web/Components/Pages/Dashboard/DashboardFilter.cs @@ -2,6 +2,7 @@ using System.Globalization; using System.Net; using Flypaper.Core.Models; using Flypaper.Storage.Dashboard; +using Flypaper.Web.Components.Shared; namespace Flypaper.Web.Components.Pages.Dashboard; @@ -47,7 +48,7 @@ public sealed record DashboardFilter( Asn(provider) is { } asn ? $"AS{asn.ToString(CultureInfo.InvariantCulture)}" : null, IPAddress.TryParse(Clean(ip), out var address) ? address.ToString() : null, Clean(ziel), - Clean(strategie), + Clean(strategie) is { } strategy ? StrategyNames.GroupKey(strategy) : null, Clean(q), parsedLimit); } @@ -70,7 +71,7 @@ public sealed record DashboardFilter( Asn(Provider), Ip, Ziel, - Strategie, + StrategyNames.Group(Strategie), Q, Limit); diff --git a/src/Flypaper.Web/Components/Pages/Dashboard/DashboardPage.razor b/src/Flypaper.Web/Components/Pages/Dashboard/DashboardPage.razor index 73f5b0a..a893ebf 100644 --- a/src/Flypaper.Web/Components/Pages/Dashboard/DashboardPage.razor +++ b/src/Flypaper.Web/Components/Pages/Dashboard/DashboardPage.razor @@ -81,7 +81,7 @@ @foreach (var tab in tabs) { } @@ -199,6 +199,10 @@
User-Agent: @(trapEvent.ClientInfo ?? "–")
@strategy.DisplayName + @foreach (var badge in strategy.Badges) + { + @badge + } @if (strategy.Explanation is not null) {
@strategy.Explanation
@@ -213,7 +217,7 @@
Dauer: @duration · Bytes: @bytes
Datum: @started.ToString("dd.MM.yyyy") · Ausgang: @OutcomeLabel(trapEvent.Outcome)
- @strategy.DisplayName + @strategy.DisplayName@foreach (var badge in strategy.Badges){@badge} @trapEvent.ClientInfo @duration @bytes @@ -282,7 +286,7 @@ new("providers", "Provider", "Provider", data.TopProviders, f => f.Provider, (f, key) => f with { Provider = key }), new("ips", "IPs", "IP", data.TopIps, f => f.Ip, (f, key) => f with { Ip = key }), new("targets", "Ziele", "Ziel", data.TopTargets, f => f.Ziel, (f, key) => f with { Ziel = key }, KeyFormatter: Formatting.Target), - new("strategies", "Strategien", "Strategie", data.TopStrategies, f => f.Strategie, (f, key) => f with { Strategie = key }, KeyFormatter: StrategyNames.Summary, LabelFormatter: (key, _) => StrategyNames.Display(key), TitleFormatter: key => StrategyNames.Describe(key).Tooltip) + new("strategies", "Strategien", "Strategie", StrategyNames.Rank(data.StrategyCatches, 10), f => f.Strategie, (f, key) => f with { Strategie = key }, KeyFormatter: _ => "", LabelFormatter: (key, _) => StrategyNames.Display(key), TitleFormatter: key => StrategyNames.Describe(key).Tooltip, BadgesFormatter: key => StrategyNames.Describe(key).Badges) ]; private DashboardFilter TopFilter(TopTab tab, TopEntry entry) => @@ -326,7 +330,8 @@ if (_filter.Strategie is { } strategie) { - yield return ($"Strategie: {StrategyNames.Display(strategie)}", reset with { Strategie = null }); + var badges = StrategyNames.Describe(strategie).Badges; + yield return ($"Strategie: {StrategyNames.Display(strategie)}{(badges.Count == 0 ? "" : $" ({string.Join(", ", badges)})")}", reset with { Strategie = null }); } if (_filter.Q is { } q) @@ -357,5 +362,6 @@ bool ShowFlags = false, Func? KeyFormatter = null, Func? LabelFormatter = null, - Func? TitleFormatter = null); + Func? TitleFormatter = null, + Func>? BadgesFormatter = null); } diff --git a/src/Flypaper.Web/Components/Pages/Dashboard/TopTable.razor b/src/Flypaper.Web/Components/Pages/Dashboard/TopTable.razor index 99622e1..3285eb5 100644 --- a/src/Flypaper.Web/Components/Pages/Dashboard/TopTable.razor +++ b/src/Flypaper.Web/Components/Pages/Dashboard/TopTable.razor @@ -1,25 +1,40 @@ @using System.Globalization @using Flypaper.Storage.Dashboard +@using Flypaper.Web.Components.Shared +@{ + var byMedian = Entries.Any(entry => entry.MedianHold is not null); + var barMax = byMedian + ? Entries.Where(entry => entry.Count >= StrategyNames.MinCatches).Select(entry => entry.MedianHold!.Value.TotalMilliseconds).DefaultIfEmpty(0).Max() + : Entries.Count == 0 ? 0 : Entries[0].Count; +} @if (Entries.Count == 0) {

Noch keine Einträge.

} else { - +
- + @if (byMedian) + { + + } + - + @if (byMedian) + { + + } + @@ -27,10 +42,18 @@ else { var key = KeyFormatter?.Invoke(entry.Key) ?? entry.Key; var label = LabelFormatter is null ? entry.Label : LabelFormatter(entry.Key, entry.Label); - var share = (entry.Count * 100.0 / Entries[0].Count).ToString("0.#", CultureInfo.InvariantCulture); + var value = byMedian ? entry.MedianHold!.Value.TotalMilliseconds : entry.Count; + var share = (barMax == 0 ? 0 : Math.Min(100, value * 100.0 / barMax)).ToString("0.#", CultureInfo.InvariantCulture); + var sparse = byMedian && entry.Count < StrategyNames.MinCatches; + var badges = BadgesFormatter?.Invoke(entry.Key) ?? []; var active = IsActive(entry); var title = TitleFormatter?.Invoke(entry.Key) ?? (label is null ? key : $"{label} · {key}"); - + if (sparse) + { + title += $"\nNur {entry.Count} {(entry.Count == 1 ? "Fang" : "Fänge")}: wenig aussagekräftig"; + } + + - + @if (byMedian) + { + + } + }
@KeyLabel Anteil FängeHaltezeitMedian@(byMedian ? "Summe" : "Haltezeit")
@if (ShowFlags) @@ -40,6 +63,14 @@ else @if (label is not null) { @label + @foreach (var badge in badges) + { + @badge + } + @if (sparse) + { + wenige Daten + } @if (key.Length > 0) { @key @@ -53,11 +84,19 @@ else @Formatting.Number(entry.Count)@Formatting.Duration(entry.HoldTime)@Formatting.Duration(entry.MedianHold!.Value)@Formatting.Duration(entry.HoldTime)
+ @if (byMedian) + { +

Balken = Median Haltezeit pro Fang. Unter @StrategyNames.MinCatches Fängen wenig aussagekräftig, diese Zeilen stehen am Ende.

+ } } @code { @@ -73,6 +112,8 @@ else [Parameter] public Func? TitleFormatter { get; set; } + [Parameter] public Func>? BadgesFormatter { get; set; } + [Parameter, EditorRequired] public required Func Href { get; set; } [Parameter, EditorRequired] public required Func IsActive { get; set; } diff --git a/src/Flypaper.Web/Components/Shared/StrategyNames.cs b/src/Flypaper.Web/Components/Shared/StrategyNames.cs index 6ce9e0b..0fe65e2 100644 --- a/src/Flypaper.Web/Components/Shared/StrategyNames.cs +++ b/src/Flypaper.Web/Components/Shared/StrategyNames.cs @@ -1,10 +1,15 @@ using System.Globalization; using System.Text.RegularExpressions; +using Flypaper.Storage.Dashboard; namespace Flypaper.Web.Components.Shared; -public sealed record StrategyInfo(string Raw, string BaseName, string DisplayName, string? Explanation, IReadOnlyList Details) +public sealed record StrategyInfo(string Raw, string BaseName, string DisplayName, string? Explanation, IReadOnlyList Details, IReadOnlyList Flags) { + public string GroupKey => string.Join(' ', [BaseName, .. Flags]); + + public IReadOnlyList Badges => Flags.Select(StrategyNames.BadgeLabel).ToList(); + public string Tooltip { get @@ -44,16 +49,45 @@ public static partial class StrategyNames ["robots-bait"] = ("Köder aus der robots.txt", "Ein Pfad, der in der robots.txt als verboten steht, aber ins Link-Labyrinth führt. Wer dort landet, hat das Verbot gelesen und ignoriert. Er wird mindestens 24 Stunden festgehalten.") }; + private static readonly Dictionary Aliases = new(StringComparer.Ordinal) + { + ["drip"] = "html-drip" + }; + + private static readonly string[] VariantFlags = ["head", "link", "repeat"]; + + private static readonly HashSet LinkedByDesign = new(StringComparer.Ordinal) { "maze-drip", "index-drip", "robots-bait" }; + + public const int MinCatches = 5; + + public static string BadgeLabel(string flag) => flag switch + { + "head" => "Schnellstart", + "link" => "Links", + "repeat" => "Wiederholer", + _ => flag + }; + public static StrategyInfo Describe(string? strategy) { var raw = strategy?.Trim() ?? ""; var tokens = raw.Split(' ', StringSplitOptions.RemoveEmptyEntries); if (tokens.Length == 0) { - return new StrategyInfo(raw, raw, raw, null, []); + return new StrategyInfo(raw, raw, raw, null, [], []); } + var baseName = Aliases.GetValueOrDefault(tokens[0], tokens[0]); + var flags = VariantFlags + .Where(flag => tokens.Skip(1).Contains(flag) && !(flag == "link" && LinkedByDesign.Contains(baseName))) + .ToList(); + var details = new List(); + if (baseName != tokens[0]) + { + details.Add($"früherer Name von {Known[baseName].Name}"); + } + for (var i = 1; i < tokens.Length; i++) { var token = tokens[i]; @@ -71,6 +105,10 @@ public static partial class StrategyNames : $"alle {bytes} Byte ein Link"); i++; } + else if (token is "head" or "link") + { + details.Add(token == "head" ? "schneller Start: erste Bytes sofort" : "enthält Links"); + } else if (token == "repeat") { details.Add("Wiederholer: darf länger festgehalten werden"); @@ -81,22 +119,58 @@ public static partial class StrategyNames } } - return Known.TryGetValue(tokens[0], out var known) - ? new StrategyInfo(raw, tokens[0], known.Name, known.Explanation, details) - : new StrategyInfo(raw, tokens[0], tokens[0], null, details); + return Known.TryGetValue(baseName, out var known) + ? new StrategyInfo(raw, baseName, known.Name, known.Explanation, details, flags) + : new StrategyInfo(raw, baseName, baseName, null, details, flags); } public static string Display(string? strategy) => Describe(strategy).DisplayName; - public static string Summary(string? strategy) => - string.Join(" · ", Describe(strategy).Details.Where(detail => detail.StartsWith("tröpfelt", StringComparison.Ordinal) || detail.StartsWith("Wiederholer", StringComparison.Ordinal)).Select(detail => detail.Split(':')[0])); + public static string GroupKey(string? strategy) => Describe(strategy).GroupKey; + + public static StrategyMatch? Group(string? strategy) + { + var info = Describe(strategy); + if (info.BaseName.Length == 0) + { + return null; + } + + var bases = Aliases.Where(pair => pair.Value == info.BaseName).Select(pair => pair.Key).Prepend(info.BaseName).ToList(); + var variableFlags = VariantFlags.Where(flag => !(flag == "link" && LinkedByDesign.Contains(info.BaseName))); + return new StrategyMatch(bases, info.Flags, variableFlags.Except(info.Flags).ToList()); + } + + public static IReadOnlyList Rank(IEnumerable samples, int take) + { + var groups = samples + .GroupBy(sample => GroupKey(sample.Strategy)) + .Select(group => + { + var durations = group.Select(sample => sample.DurationMs).Order().ToList(); + var middle = durations.Count / 2; + var median = durations.Count % 2 == 1 ? durations[middle] : (durations[middle - 1] + durations[middle]) / 2d; + return new TopEntry(group.Key, durations.Count, TimeSpan.FromMilliseconds(durations.Sum()), MedianHold: TimeSpan.FromMilliseconds(median)); + }); + + return groups + .OrderBy(entry => entry.Count < MinCatches) + .ThenByDescending(entry => entry.MedianHold) + .ThenByDescending(entry => entry.Count) + .Take(take) + .ToList(); + } public static IReadOnlyList Matching(string? search) { var term = search?.Trim(); - return string.IsNullOrEmpty(term) - ? [] - : Known.Where(pair => pair.Value.Name.Contains(term, StringComparison.CurrentCultureIgnoreCase)).Select(pair => pair.Key).ToList(); + if (string.IsNullOrEmpty(term)) + { + return []; + } + + var keys = Known.Where(pair => pair.Value.Name.Contains(term, StringComparison.CurrentCultureIgnoreCase)).Select(pair => pair.Key).ToList(); + return [.. keys, .. Aliases.Where(pair => keys.Contains(pair.Value)).Select(pair => pair.Key)]; } private static string Range(string min, string max) => min == max ? Number(min) : $"{Number(min)}–{Number(max)}"; diff --git a/src/Flypaper.Web/wwwroot/app.css b/src/Flypaper.Web/wwwroot/app.css index 9b9a8b7..0ac7a7c 100644 --- a/src/Flypaper.Web/wwwroot/app.css +++ b/src/Flypaper.Web/wwwroot/app.css @@ -266,6 +266,33 @@ code { font-size: 12px; } +.badge { + display: inline-block; + margin-left: 6px; + padding: 0 6px; + border-radius: 999px; + background: color-mix(in srgb, var(--text) 8%, transparent); + color: var(--muted); + font-size: 11px; + line-height: 17px; + white-space: nowrap; + vertical-align: 1px; +} + +.top-table tr.sparse td { + opacity: 0.55; +} + +.top-note { + margin: 8px 0 0; + color: var(--muted); + font-size: 12px; +} + +.top-table.by-median .count { + width: 52px; +} + .top-table tr[data-href] { cursor: pointer; } @@ -1017,6 +1044,18 @@ button.danger { width: 84px; } + .top-table.by-median .sum { + display: none; + } + + .top-table.by-median .count { + width: 48px; + } + + .top-table.by-median .clip { + white-space: normal; + } + .nav .brand { margin-right: 4px; font-size: 0; diff --git a/test/Flypaper.Tests/Storage/RecentCatchesTests.cs b/test/Flypaper.Tests/Storage/RecentCatchesTests.cs index cfb8022..804efc0 100644 --- a/test/Flypaper.Tests/Storage/RecentCatchesTests.cs +++ b/test/Flypaper.Tests/Storage/RecentCatchesTests.cs @@ -1,4 +1,7 @@ +using System.Net; using Flypaper.Core.Models; +using Flypaper.Storage.Entities; +using Flypaper.Web.Components.Shared; using Flypaper.Storage.Dashboard; using Flypaper.Tests.Fakes; using Flypaper.Tests.Mcp; @@ -47,19 +50,42 @@ public sealed class RecentCatchesTests : IDisposable [Fact] public async Task Matches_strategy_exactly() { - var recent = await _queries.GetRecentAsync(new RecentFilter(Strategy: "env-drip")); + var recent = await _queries.GetRecentAsync(new RecentFilter(Strategy: StrategyNames.Group("env-drip"))); recent.Total.ShouldBe(2); recent.Items.ShouldAllBe(e => e.Strategy == "env-drip"); } [Fact] - public async Task Dashboard_ranks_strategies_among_catches() + public async Task Dashboard_lists_strategy_durations_among_catches() { var data = await _queries.GetAsync(); - data.TopStrategies.Select(s => (s.Key, s.Count)).ShouldBe([("html-drip", 4), ("env-drip", 1)]); - data.TopStrategies[1].HoldTime.ShouldBe(TimeSpan.FromHours(1)); + data.StrategyCatches.Count.ShouldBe(5); + data.StrategyCatches.Count(s => s.Strategy == "env-drip").ShouldBe(1); + data.StrategyCatches.Single(s => s.Strategy == "env-drip").DurationMs.ShouldBe(3_600_000); + } + + [Fact] + public async Task Strategy_group_matches_all_variants_of_the_base_type_and_its_old_name() + { + await using (var db = _data.Factory.CreateDbContext()) + { + foreach (var strategy in new[] { "drip", "html-drip 50-500ms 1-640B", "html-drip 5-20ms 1-64B head 1024B", "html-drip 50-500ms 1-1024B repeat", "html-drip 50-500ms 1-64B head 1024B repeat" }) + { + db.TrapEvents.Add(TrapEventEntity.From(new TrapEvent(Guid.NewGuid(), TrapProtocol.Http, IPAddress.Parse("192.0.2.1"), strategy, McpTestData.Now, McpTestData.Now, 0, TrapOutcome.ClientAborted, null, null))); + } + + await db.SaveChangesAsync(); + } + + async Task> Find(string group) => + (await _queries.GetRecentAsync(new RecentFilter(Strategy: StrategyNames.Group(group)))).Items.Select(e => e.Strategy).OrderBy(s => s).ToList(); + + (await Find("html-drip")).Where(s => s != "html-drip").ShouldBe(["drip", "html-drip 50-500ms 1-640B"]); + (await Find("html-drip head")).ShouldBe(["html-drip 5-20ms 1-64B head 1024B"]); + (await Find("html-drip repeat")).ShouldBe(["html-drip 50-500ms 1-1024B repeat"]); + (await Find("html-drip head repeat")).ShouldBe(["html-drip 50-500ms 1-64B head 1024B repeat"]); } [Theory] diff --git a/test/Flypaper.Tests/Web/DashboardFilterTests.cs b/test/Flypaper.Tests/Web/DashboardFilterTests.cs index 7d8b9c6..985b2a3 100644 --- a/test/Flypaper.Tests/Web/DashboardFilterTests.cs +++ b/test/Flypaper.Tests/Web/DashboardFilterTests.cs @@ -13,7 +13,11 @@ public sealed class DashboardFilterTests var filter = DashboardFilter.Parse("24h", "Abgelehnt", "hk", "as4134", " 203.0.113.9 ", "GET /.env", " env-drip ", " wp ", "100"); filter.ShouldBe(new DashboardFilter("24h", "abgelehnt", "HK", "AS4134", "203.0.113.9", "GET /.env", "env-drip", "wp", 100)); - filter.ToRecentFilter().ShouldBe(new RecentFilter(TimeSpan.FromHours(24), TrapOutcome.Rejected, "HK", 4134, "203.0.113.9", "GET /.env", "env-drip", "wp", 100)); + var recent = filter.ToRecentFilter(); + recent.ShouldBe(new RecentFilter(TimeSpan.FromHours(24), TrapOutcome.Rejected, "HK", 4134, "203.0.113.9", "GET /.env", recent.Strategy, "wp", 100)); + recent.Strategy!.Bases.ShouldBe(["env-drip"]); + recent.Strategy.With.ShouldBeEmpty(); + recent.Strategy.Without.ShouldBe(["head", "link", "repeat"]); } [Theory] @@ -44,8 +48,8 @@ public sealed class DashboardFilterTests public void Builds_links_without_defaults_and_with_escaping() { new DashboardFilter().Href().ShouldBe("/#faenge"); - new DashboardFilter(Land: "CN", Ziel: "GET example.com/a&b", Strategie: "html-drip 50-500ms 1-1024B", Limit: 100).Href("f50") - .ShouldBe("/?land=CN&ziel=GET%20example.com%2Fa%26b&strategie=html-drip%2050-500ms%201-1024B&limit=100#f50"); + new DashboardFilter(Land: "CN", Ziel: "GET example.com/a&b", Strategie: "html-drip head", Limit: 100).Href("f50") + .ShouldBe("/?land=CN&ziel=GET%20example.com%2Fa%26b&strategie=html-drip%20head&limit=100#f50"); } [Fact] @@ -58,6 +62,13 @@ public sealed class DashboardFilterTests .ShouldBe(filter); } + [Theory] + [InlineData("html-drip 50-500ms 1-1024B", "html-drip")] + [InlineData("drip", "html-drip")] + [InlineData("html-drip 50-500ms 1-64B head 1024B", "html-drip head")] + public void Normalizes_strategy_links_to_the_group(string strategy, string expected) => + DashboardFilter.Parse(null, null, null, null, null, null, strategy, null, null).Strategie.ShouldBe(expected); + [Fact] public void Doubles_the_limit_up_to_the_maximum() { diff --git a/test/Flypaper.Tests/Web/StrategyNamesTests.cs b/test/Flypaper.Tests/Web/StrategyNamesTests.cs index 83a26f6..67f1d81 100644 --- a/test/Flypaper.Tests/Web/StrategyNamesTests.cs +++ b/test/Flypaper.Tests/Web/StrategyNamesTests.cs @@ -1,4 +1,5 @@ using Flypaper.Core.Strategies; +using Flypaper.Storage.Dashboard; using Flypaper.Web.Components.Shared; using Shouldly; @@ -63,4 +64,44 @@ public sealed class StrategyNamesTests StrategyNames.Matching("").ShouldBeEmpty(); StrategyNames.Matching("nichts").ShouldBeEmpty(); } + + [Fact] + public void Groups_variants_by_base_type_and_shows_only_content_differences_as_badges() + { + StrategyNames.Describe("html-drip 50-500ms 1-1024B").GroupKey.ShouldBe("html-drip"); + StrategyNames.Describe("html-drip 50-500ms 1-640B").GroupKey.ShouldBe("html-drip"); + var variant = StrategyNames.Describe("html-drip 5-20ms 1-64B head 1024B repeat"); + variant.GroupKey.ShouldBe("html-drip head repeat"); + variant.Badges.ShouldBe(["Schnellstart", "Wiederholer"]); + StrategyNames.Describe("maze-drip 50-500ms 1-1024B head 1024B link 160B").Badges.ShouldBe(["Schnellstart"]); + } + + [Fact] + public void Treats_drip_as_the_old_name_of_the_endless_page() + { + var info = StrategyNames.Describe("drip"); + + info.DisplayName.ShouldBe("Endlose Webseite"); + info.GroupKey.ShouldBe("html-drip"); + StrategyNames.Matching("endlose webseite").ShouldBe(["html-drip", "drip"]); + } + + [Fact] + public void Ranks_groups_by_median_and_sinks_groups_with_few_catches() + { + var samples = new List(); + samples.AddRange(Enumerable.Repeat(new StrategySample("html-drip 50-500ms 1-1024B", 10_000), 3)); + samples.AddRange(Enumerable.Repeat(new StrategySample("html-drip 50-500ms 1-640B", 20_000), 2)); + samples.Add(new StrategySample("drip", 1_000_000)); + samples.AddRange(Enumerable.Repeat(new StrategySample("env-drip 50-500ms 1-1024B", 30_000), 5)); + samples.Add(new StrategySample("text-drip", 500_000_000)); + + var ranked = StrategyNames.Rank(samples, 10); + + ranked.Select(r => r.Key).ShouldBe(["env-drip", "html-drip", "text-drip"]); + ranked[0].MedianHold.ShouldBe(TimeSpan.FromSeconds(30)); + ranked[1].Count.ShouldBe(6); + ranked[1].MedianHold.ShouldBe(TimeSpan.FromSeconds(15)); + ranked[1].HoldTime.ShouldBe(TimeSpan.FromMilliseconds(1_070_000)); + } }