Restructure solution into folders and fix persistence issues

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
TomandClaude Opus 5.5 committed 2026-11-14 14:10:24 +01:00
1 parent 37987a394a
commit 7c39cb5562
62 files changed
+219 -151

No files matched your search

+102
View File
@@ -0,0 +1,102 @@
using System.Net;
using Flypaper.Core.Abstractions;
using Flypaper.Core.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Flypaper.Core.Runtime;
public sealed partial class TrapRunner(
IConnectionGovernor governor,
IEnumerable<ITarpitStrategy> strategies,
ITrapEventSink sink,
IOptionsMonitor<FlypaperOptions> options,
TimeProvider timeProvider,
ILogger<TrapRunner> logger)
{
public async Task<TrapOutcome> RunAsync(
TrapContext context,
Func<CancellationToken, ValueTask<ITrapChannel>> openChannel,
CancellationToken ct)
{
var session = context.Session;
var strategy = strategies.FirstOrDefault(s => s.Supports(session.Protocol));
if (strategy is null || !governor.TryAcquire(session, out var lease))
{
Publish(context, strategy?.Name ?? "none", 0, TrapOutcome.Rejected);
return TrapOutcome.Rejected;
}
using var _ = lease;
using var hold = new CancellationTokenSource(options.CurrentValue.MaxHoldDuration, timeProvider);
using var linked = CancellationTokenSource.CreateLinkedTokenSource(ct, hold.Token);
CountingTrapChannel? channel = null;
var outcome = TrapOutcome.Completed;
try
{
channel = new CountingTrapChannel(await openChannel(linked.Token));
await strategy.RunAsync(channel, linked.Token);
}
catch (OperationCanceledException) when (linked.IsCancellationRequested)
{
outcome = hold.IsCancellationRequested && !ct.IsCancellationRequested
? TrapOutcome.Timeout
: TrapOutcome.ClientAborted;
}
catch (IOException)
{
outcome = TrapOutcome.ClientAborted;
}
Publish(context, strategy.Name, channel?.BytesSent ?? 0, outcome);
return outcome;
}
private void Publish(TrapContext context, string strategy, long bytesSent, TrapOutcome outcome)
{
var session = context.Session;
var trapEvent = new TrapEvent(
session.Id,
session.Protocol,
session.RemoteIp,
strategy,
session.StartedAt,
timeProvider.GetUtcNow(),
bytesSent,
outcome,
context.Target,
context.ClientInfo);
if (outcome == TrapOutcome.Rejected)
{
LogRejected(trapEvent.RemoteIp, trapEvent.Target, trapEvent.ClientInfo);
}
else
{
LogTrapped(
trapEvent.RemoteIp,
trapEvent.Target,
trapEvent.Duration.TotalSeconds,
trapEvent.BytesSent,
trapEvent.Outcome,
trapEvent.ClientInfo);
}
sink.Publish(trapEvent);
}
[LoggerMessage(Level = LogLevel.Information,
Message = "Trapped {RemoteIp} {Target} {DurationSeconds:F1}s {BytesSent}B {Outcome} \"{UserAgent}\"")]
private partial void LogTrapped(
IPAddress remoteIp,
string? target,
double durationSeconds,
long bytesSent,
TrapOutcome outcome,
string? userAgent);
[LoggerMessage(Level = LogLevel.Information, Message = "Rejected {RemoteIp} {Target} \"{UserAgent}\"")]
private partial void LogRejected(IPAddress remoteIp, string? target, string? userAgent);
}