Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7c11bf211e |
No files matched your search
+11
-1
@@ -6,6 +6,15 @@ All notable changes to Flypaper are documented in this file. The format follows
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [0.8.0] - 2026-11-22
|
||||
|
||||
### Added
|
||||
|
||||
- Dashboard: the overview lists the bots held in the trap right now („Gerade in der Falle“) with IP (links to its
|
||||
catches), origin, target, strategy, user agent, time held and bytes sent. The list is kept in memory only and
|
||||
refreshes with the page (every 30 s).
|
||||
- MCP: read-only tool `list_active` returns the same list (with provider, city and ASN); empty after a restart.
|
||||
|
||||
## [0.7.0] - 2026-11-21
|
||||
|
||||
### Added
|
||||
@@ -242,7 +251,8 @@ First release.
|
||||
- Unraid Docker template; when started as root, the container hands `/data` to its app user
|
||||
and drops privileges.
|
||||
|
||||
[Unreleased]: https://gitea.anticarnist.de/Tom/Flypaper/compare/v0.7.0...main
|
||||
[Unreleased]: https://gitea.anticarnist.de/Tom/Flypaper/compare/v0.8.0...main
|
||||
[0.8.0]: https://gitea.anticarnist.de/Tom/Flypaper/releases/tag/v0.8.0
|
||||
[0.7.0]: https://gitea.anticarnist.de/Tom/Flypaper/releases/tag/v0.7.0
|
||||
[0.6.0]: https://gitea.anticarnist.de/Tom/Flypaper/releases/tag/v0.6.0
|
||||
[0.5.2]: https://gitea.anticarnist.de/Tom/Flypaper/releases/tag/v0.5.2
|
||||
|
||||
@@ -61,6 +61,7 @@ seconds. A *period* is a number with `m`, `h`, `d` or `w` (e.g. `1h`, `24h`, `7d
|
||||
|
||||
| Tool | Parameters | Returns |
|
||||
|---|---|---|
|
||||
| `list_active` | – | Bots held in the trap right now with origin, URL, user agent, strategy, time held and bytes sent (memory only) |
|
||||
| `get_stats` | `period` (default `24h`) | Catches, rejected connections, unique IPs, total and average hold time, traps active now, longest catch |
|
||||
| `list_catches` | `since`, `until` (ISO 8601), `country` (ISO code), `provider` (`AS4134` or part of the name), `ip`, `target_contains`, `limit` (default 50, max 500) | Events, newest first, including rejected ones |
|
||||
| `top` | `dimension` (`ips`, `countries`, `providers`, `targets`, `user_agents`), `period` (default `24h`), `limit` (default 10) | Most frequent values by number of catches, with total hold time |
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
using Flypaper.Core.Models;
|
||||
|
||||
namespace Flypaper.Core.Abstractions;
|
||||
|
||||
public interface IActiveTraps
|
||||
{
|
||||
IActiveTrapHandle Begin(TrapContext context, string strategy);
|
||||
|
||||
IReadOnlyList<ActiveTrap> Snapshot();
|
||||
}
|
||||
|
||||
public interface IActiveTrapHandle : IDisposable
|
||||
{
|
||||
void Attach(Func<long> bytesSent);
|
||||
}
|
||||
@@ -18,6 +18,7 @@ public static class FlypaperCoreExtensions
|
||||
|
||||
services.TryAddSingleton(TimeProvider.System);
|
||||
services.TryAddSingleton<IConnectionGovernor, ConnectionGovernor>();
|
||||
services.TryAddSingleton<IActiveTraps, ActiveTrapRegistry>();
|
||||
services.TryAddSingleton<IRandomSource, SystemRandomSource>();
|
||||
services.TryAddEnumerable(ServiceDescriptor.Singleton<ITarpitStrategy, HtmlDripStrategy>());
|
||||
services.TryAddEnumerable(ServiceDescriptor.Singleton<ITarpitStrategy, RobotsStaticStrategy>());
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
using System.Net;
|
||||
|
||||
namespace Flypaper.Core.Models;
|
||||
|
||||
public sealed record ActiveTrap(Guid Id, IPAddress Ip, string? Target, string? ClientInfo, string Strategy, DateTimeOffset StartedAt, long BytesSent);
|
||||
@@ -0,0 +1,38 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Flypaper.Core.Abstractions;
|
||||
using Flypaper.Core.Models;
|
||||
|
||||
namespace Flypaper.Core.Runtime;
|
||||
|
||||
public sealed class ActiveTrapRegistry(TimeProvider timeProvider) : IActiveTraps
|
||||
{
|
||||
private readonly ConcurrentDictionary<Guid, Entry> _entries = new();
|
||||
|
||||
public IActiveTrapHandle Begin(TrapContext context, string strategy)
|
||||
{
|
||||
var entry = new Entry(this, context, strategy, timeProvider.GetUtcNow());
|
||||
_entries[context.Session.Id] = entry;
|
||||
return entry;
|
||||
}
|
||||
|
||||
public IReadOnlyList<ActiveTrap> Snapshot() =>
|
||||
_entries.Values.Select(entry => entry.ToActiveTrap()).OrderBy(trap => trap.StartedAt).ToList();
|
||||
|
||||
private sealed class Entry(ActiveTrapRegistry owner, TrapContext context, string strategy, DateTimeOffset startedAt) : IActiveTrapHandle
|
||||
{
|
||||
private Func<long>? _bytesSent;
|
||||
|
||||
public void Attach(Func<long> bytesSent) => _bytesSent = bytesSent;
|
||||
|
||||
public ActiveTrap ToActiveTrap() => new(
|
||||
context.Session.Id,
|
||||
context.Session.RemoteIp,
|
||||
context.Target,
|
||||
context.ClientInfo,
|
||||
strategy,
|
||||
startedAt,
|
||||
_bytesSent?.Invoke() ?? 0);
|
||||
|
||||
public void Dispose() => owner._entries.TryRemove(context.Session.Id, out _);
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ public sealed partial class TrapRunner(
|
||||
IConnectionGovernor governor,
|
||||
ITrapStrategySelector strategies,
|
||||
ITrapEventSink sink,
|
||||
IActiveTraps activeTraps,
|
||||
IRepeatOffenderLookup repeatOffenders,
|
||||
IOptionsMonitor<FlypaperOptions> options,
|
||||
TimeProvider timeProvider,
|
||||
@@ -46,6 +47,7 @@ public sealed partial class TrapRunner(
|
||||
description += " repeat";
|
||||
}
|
||||
|
||||
using var active = activeTraps.Begin(context, description);
|
||||
using var hold = new CancellationTokenSource(holdDuration, timeProvider);
|
||||
using var linked = CancellationTokenSource.CreateLinkedTokenSource(ct, hold.Token);
|
||||
|
||||
@@ -54,6 +56,8 @@ public sealed partial class TrapRunner(
|
||||
try
|
||||
{
|
||||
channel = new CountingTrapChannel(await openChannel(strategy, linked.Token));
|
||||
var counting = channel;
|
||||
active.Attach(() => counting.BytesSent);
|
||||
await strategy.RunAsync(channel, linked.Token);
|
||||
}
|
||||
catch (OperationCanceledException) when (linked.IsCancellationRequested)
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
@inject DashboardQueries Queries
|
||||
@inject ExplanationRuleStore Explanations
|
||||
@inject IConnectionGovernor Governor
|
||||
@inject IActiveTraps ActiveTraps
|
||||
@inject IIpEnricher Enricher
|
||||
@inject IOptions<PortOptions> Ports
|
||||
|
||||
<PageTitle>Flypaper</PageTitle>
|
||||
@@ -53,6 +55,58 @@
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@if (_active.Count > 0)
|
||||
{
|
||||
<section class="card recent-card active-card">
|
||||
<div class="card-head">
|
||||
<h2>Gerade in der Falle <span class="count">· @Formatting.Number(_active.Count)</span></h2>
|
||||
<span class="hint">Seit dem Laden der Seite weiter festgehalten</span>
|
||||
</div>
|
||||
<div class="scroll">
|
||||
<table class="recent-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="c-ip">IP</th>
|
||||
<th class="c-origin">Herkunft</th>
|
||||
<th class="c-target">Ziel</th>
|
||||
<th class="c-strategy">Strategie</th>
|
||||
<th class="c-agent">User-Agent</th>
|
||||
<th class="c-duration num">Seit</th>
|
||||
<th class="c-bytes num">Bytes</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var trap in _active)
|
||||
{
|
||||
var origin = Enricher.Lookup(trap.Ip);
|
||||
var target = Formatting.Target(trap.Target);
|
||||
var strategy = StrategyNames.Describe(trap.Strategy);
|
||||
var ip = trap.Ip.ToString();
|
||||
<tr>
|
||||
<td class="c-ip mono"><a href="@(new DashboardFilter { Ip = ip }.Href(DashboardFilter.Anchor))" title="Fänge dieser IP anzeigen">@ip</a></td>
|
||||
<td class="c-origin" title="@origin?.Provider">
|
||||
@if (origin?.CountryCode is { } countryCode)
|
||||
{
|
||||
@($"{Formatting.Flag(countryCode)} {countryCode}")
|
||||
}
|
||||
else
|
||||
{
|
||||
<span class="muted">–</span>
|
||||
}
|
||||
</td>
|
||||
<td class="c-target mono" title="@target">@target</td>
|
||||
<td class="c-strategy" title="@strategy.Tooltip">@strategy.DisplayName</td>
|
||||
<td class="c-agent" title="@trap.ClientInfo">@trap.ClientInfo</td>
|
||||
<td class="c-duration num">@Formatting.Duration(_loadedAtUtc - trap.StartedAt)</td>
|
||||
<td class="c-bytes num">@Formatting.Number(trap.BytesSent)</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@if (data.TotalCatches == 0)
|
||||
{
|
||||
<section class="card onboarding">
|
||||
@@ -269,6 +323,8 @@
|
||||
private DashboardFilter _filter = new();
|
||||
private RuleSet _rules = RuleSet.Empty;
|
||||
private DateTime _loadedAt;
|
||||
private DateTimeOffset _loadedAtUtc;
|
||||
private IReadOnlyList<ActiveTrap> _active = [];
|
||||
|
||||
[SupplyParameterFromQuery(Name = "zeit")] public string? Zeit { get; set; }
|
||||
|
||||
@@ -296,6 +352,8 @@
|
||||
_rules = await Explanations.GetRuleSetAsync();
|
||||
_data = await Queries.GetAsync();
|
||||
_recent = await Queries.GetRecentAsync(_filter.ToRecentFilter() with { SearchStrategies = StrategyNames.Matching(_filter.Q) });
|
||||
_active = ActiveTraps.Snapshot();
|
||||
_loadedAtUtc = DateTimeOffset.UtcNow;
|
||||
_loadedAt = DateTime.Now;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ using ModelContextProtocol.Server;
|
||||
namespace Flypaper.Web.Mcp;
|
||||
|
||||
[McpServerToolType]
|
||||
public sealed class FlypaperTools(CatchQueries queries, IConnectionGovernor governor, TimeProvider timeProvider)
|
||||
public sealed class FlypaperTools(CatchQueries queries, IConnectionGovernor governor, IActiveTraps activeTraps, IIpEnricher enricher, TimeProvider timeProvider)
|
||||
{
|
||||
private const string PeriodDescription = "Time window ending now: a number with m (minutes), h (hours), d (days) or w (weeks), e.g. 1h, 24h, 7d; or all.";
|
||||
|
||||
@@ -35,6 +35,31 @@ public sealed class FlypaperTools(CatchQueries queries, IConnectionGovernor gove
|
||||
stats.Longest is null ? null : ToItem(stats.Longest));
|
||||
}
|
||||
|
||||
[McpServerTool(Name = "list_active", Title = "Bots in the trap right now", ReadOnly = true, Destructive = false, Idempotent = true, OpenWorld = false, UseStructuredContent = true)]
|
||||
[Description("Bots held in the trap right now, longest running first: IP, origin, requested URL, user agent, strategy, how long they have been held and bytes sent so far. Held in memory only, so it is empty after a restart.")]
|
||||
public ActiveTrapListResult ListActive()
|
||||
{
|
||||
var now = timeProvider.GetUtcNow();
|
||||
var traps = activeTraps.Snapshot().Select(trap =>
|
||||
{
|
||||
var origin = enricher.Lookup(trap.Ip);
|
||||
return new ActiveTrapItem(
|
||||
trap.StartedAt.UtcDateTime,
|
||||
Round((now - trap.StartedAt).TotalSeconds),
|
||||
trap.BytesSent,
|
||||
trap.Strategy,
|
||||
trap.Ip.ToString(),
|
||||
origin?.CountryCode,
|
||||
CountryNames.Display(origin?.CountryCode, origin?.Country),
|
||||
origin?.City,
|
||||
Asn(origin?.Asn),
|
||||
origin?.Provider,
|
||||
trap.Target,
|
||||
trap.ClientInfo);
|
||||
}).ToList();
|
||||
return new ActiveTrapListResult(traps.Count, traps);
|
||||
}
|
||||
|
||||
[McpServerTool(Name = "list_catches", Title = "List catches", ReadOnly = true, Destructive = false, Idempotent = true, OpenWorld = false, UseStructuredContent = true)]
|
||||
[Description("Trap events, newest first, optionally filtered. Includes rejected connections (outcome Rejected).")]
|
||||
public async Task<CatchListResult> ListCatches(
|
||||
|
||||
@@ -20,7 +20,7 @@ public static class McpExtensions
|
||||
"answered with an endless page a few bytes at a time and held as long as possible. Every such catch is " +
|
||||
"stored with its time, client IP, country, network provider (ASN), requested URL, user agent, duration " +
|
||||
"and bytes sent. Times are ISO 8601 UTC, durations in seconds. The tools list_catches, top, longest_catches, " +
|
||||
"lookup_ip and get_stats only read that data. Explanation rules turn catches into plain German sentences " +
|
||||
"lookup_ip, list_active (bots held right now) and get_stats only read that data. Explanation rules turn catches into plain German sentences " +
|
||||
"shown in the dashboard (what a bot wanted, with which tool, how it ended); they are matched when a catch is " +
|
||||
"displayed, so a new rule also explains old catches. Use list_unexplained to find catches without an " +
|
||||
"explanation, test_explanation_rule to try a pattern, and list_explanation_rules to see what exists. The tools " +
|
||||
|
||||
@@ -28,6 +28,22 @@ public sealed record StatsResult(
|
||||
int ActiveTraps,
|
||||
CatchItem? LongestCatch);
|
||||
|
||||
public sealed record ActiveTrapItem(
|
||||
DateTime StartedAt,
|
||||
double DurationSeconds,
|
||||
long BytesSent,
|
||||
string Strategy,
|
||||
string Ip,
|
||||
string? CountryCode,
|
||||
string? Country,
|
||||
string? City,
|
||||
string? Asn,
|
||||
string? Provider,
|
||||
string? Target,
|
||||
string? UserAgent);
|
||||
|
||||
public sealed record ActiveTrapListResult(int Count, IReadOnlyList<ActiveTrapItem> Traps);
|
||||
|
||||
public sealed record CatchListResult(int Count, IReadOnlyList<CatchItem> Catches);
|
||||
|
||||
public sealed record TopResultItem(string Key, string? Label, int Catches, double TotalHoldSeconds);
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
using System.Net;
|
||||
using Flypaper.Core.Models;
|
||||
using Flypaper.Core.Runtime;
|
||||
using Flypaper.Tests.Fakes;
|
||||
using Shouldly;
|
||||
|
||||
namespace Flypaper.Tests.Core;
|
||||
|
||||
public sealed class ActiveTrapRegistryTests
|
||||
{
|
||||
private static readonly DateTimeOffset Start = new(2026, 11, 22, 10, 0, 0, TimeSpan.Zero);
|
||||
|
||||
[Fact]
|
||||
public void Lists_running_traps_with_bytes_and_forgets_finished_ones()
|
||||
{
|
||||
var registry = new ActiveTrapRegistry(new FixedTimeProvider(Start));
|
||||
var first = Context("203.0.113.1", "GET /.env");
|
||||
var second = Context("203.0.113.2", "GET /");
|
||||
|
||||
var handle = registry.Begin(first, "env-drip");
|
||||
registry.Begin(second, "maze-drip");
|
||||
var bytes = 0L;
|
||||
handle.Attach(() => bytes);
|
||||
bytes = 4096;
|
||||
|
||||
var snapshot = registry.Snapshot();
|
||||
|
||||
snapshot.Select(t => t.Ip.ToString()).ShouldBe(["203.0.113.1", "203.0.113.2"], ignoreOrder: true);
|
||||
snapshot.Single(t => t.Ip.ToString() == "203.0.113.1").BytesSent.ShouldBe(4096);
|
||||
snapshot.Single(t => t.Ip.ToString() == "203.0.113.2").BytesSent.ShouldBe(0);
|
||||
snapshot.ShouldAllBe(t => t.StartedAt == Start);
|
||||
|
||||
handle.Dispose();
|
||||
|
||||
registry.Snapshot().Select(t => t.Ip.ToString()).ShouldBe(["203.0.113.2"]);
|
||||
}
|
||||
|
||||
private static TrapContext Context(string ip, string target) =>
|
||||
new(new TrapSession(Guid.NewGuid(), TrapProtocol.Http, IPAddress.Parse(ip), Start), target);
|
||||
}
|
||||
@@ -179,6 +179,7 @@ public sealed class TrapRunnerTests
|
||||
_governor,
|
||||
new ConfiguredStrategySelector([drip], dripOptions, random),
|
||||
_sink,
|
||||
new ActiveTrapRegistry(TimeProvider.System),
|
||||
repeatOffenders ?? new NullRepeatLookup(),
|
||||
TestOptions.Monitor(new FlypaperOptions
|
||||
{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Flypaper.Core.Abstractions;
|
||||
using Flypaper.Core.Runtime;
|
||||
using Flypaper.Storage.Queries;
|
||||
using Flypaper.Tests.Fakes;
|
||||
using Flypaper.Web.Mcp;
|
||||
@@ -12,14 +13,42 @@ public sealed class FlypaperToolsTests : IDisposable
|
||||
{
|
||||
private readonly McpTestData _data = new();
|
||||
private readonly FlypaperTools _tools;
|
||||
private readonly ActiveTrapRegistry _active = new(new FixedTimeProvider(McpTestData.Now));
|
||||
private readonly IIpEnricher _enricher = Substitute.For<IIpEnricher>();
|
||||
|
||||
public FlypaperToolsTests()
|
||||
{
|
||||
var governor = Substitute.For<IConnectionGovernor>();
|
||||
governor.ActiveConnections.Returns(2);
|
||||
_tools = new FlypaperTools(new CatchQueries(_data.Factory), governor, new FixedTimeProvider(McpTestData.Now));
|
||||
_tools = new FlypaperTools(new CatchQueries(_data.Factory), governor, _active, _enricher, new FixedTimeProvider(McpTestData.Now));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void List_active_shows_running_traps_with_origin_and_bytes()
|
||||
{
|
||||
var address = System.Net.IPAddress.Parse("203.0.113.7");
|
||||
_enricher.Lookup(address).Returns(new Flypaper.Core.Models.IpOrigin("DE", "Germany", "Berlin", 3320, "Telekom"));
|
||||
var context = new Flypaper.Core.Models.TrapContext(
|
||||
new Flypaper.Core.Models.TrapSession(Guid.NewGuid(), Flypaper.Core.Models.TrapProtocol.Http, address, McpTestData.Now.AddMinutes(-5)),
|
||||
"GET /.env",
|
||||
"curl/8.7.1");
|
||||
_active.Begin(context, "env-drip").Attach(() => 2048);
|
||||
|
||||
var result = _tools.ListActive();
|
||||
|
||||
var trap = result.Traps.ShouldHaveSingleItem();
|
||||
result.Count.ShouldBe(1);
|
||||
trap.Ip.ShouldBe("203.0.113.7");
|
||||
trap.CountryCode.ShouldBe("DE");
|
||||
trap.Asn.ShouldBe("AS3320");
|
||||
trap.Target.ShouldBe("GET /.env");
|
||||
trap.BytesSent.ShouldBe(2048);
|
||||
trap.DurationSeconds.ShouldBe(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void List_active_is_empty_without_traps() => _tools.ListActive().Count.ShouldBe(0);
|
||||
|
||||
[Fact]
|
||||
public async Task Get_stats_summarizes_the_period()
|
||||
{
|
||||
|
||||
@@ -24,7 +24,7 @@ public sealed class McpEndpointTests : IDisposable
|
||||
}
|
||||
|
||||
private static readonly string[] ReadTools =
|
||||
["get_stats", "list_catches", "longest_catches", "lookup_ip", "top", "list_explanation_rules", "list_unexplained", "test_explanation_rule"];
|
||||
["get_stats", "list_active", "list_catches", "longest_catches", "lookup_ip", "top", "list_explanation_rules", "list_unexplained", "test_explanation_rule"];
|
||||
|
||||
[Fact]
|
||||
public async Task Rejects_requests_without_token()
|
||||
@@ -80,6 +80,7 @@ public sealed class McpEndpointTests : IDisposable
|
||||
{
|
||||
{ "get_stats", """{"period":"all"}""" },
|
||||
{ "get_stats", """{"period":"24h"}""" },
|
||||
{ "list_active", """{}""" },
|
||||
{ "list_catches", """{}""" },
|
||||
{ "list_catches", """{"country":"CN","limit":1}""" },
|
||||
{ "top", """{"dimension":"ips","period":"all"}""" },
|
||||
|
||||
Reference in new issue
Block a user