Restructure solution into folders and fix persistence issues
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
37987a394a
commit
7c39cb5562
62 files changed
+219
-151
No files matched your search
@@ -0,0 +1,72 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using Flypaper.Core.Abstractions;
|
||||
using Flypaper.Core.Models;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Flypaper.Core.Runtime;
|
||||
|
||||
public sealed class ConnectionGovernor(IOptionsMonitor<FlypaperOptions> options) : IConnectionGovernor
|
||||
{
|
||||
private readonly Lock _gate = new();
|
||||
private readonly Dictionary<IPAddress, int> _perIp = [];
|
||||
private int _total;
|
||||
|
||||
public int ActiveConnections
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
return _total;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryAcquire(TrapSession session, [NotNullWhen(true)] out IDisposable? lease)
|
||||
{
|
||||
var ip = session.RemoteIp;
|
||||
var limits = options.CurrentValue;
|
||||
|
||||
lock (_gate)
|
||||
{
|
||||
_perIp.TryGetValue(ip, out var count);
|
||||
if (_total >= limits.MaxConnections || count >= limits.MaxConnectionsPerIp)
|
||||
{
|
||||
lease = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
_total++;
|
||||
_perIp[ip] = count + 1;
|
||||
}
|
||||
|
||||
lease = new Lease(this, ip);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void Release(IPAddress ip)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_total--;
|
||||
if (--_perIp[ip] == 0)
|
||||
{
|
||||
_perIp.Remove(ip);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class Lease(ConnectionGovernor owner, IPAddress ip) : IDisposable
|
||||
{
|
||||
private int _disposed;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Interlocked.Exchange(ref _disposed, 1) == 0)
|
||||
{
|
||||
owner.Release(ip);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using Flypaper.Core.Abstractions;
|
||||
using Flypaper.Core.Models;
|
||||
|
||||
namespace Flypaper.Core.Runtime;
|
||||
|
||||
internal sealed class CountingTrapChannel(ITrapChannel inner) : ITrapChannel
|
||||
{
|
||||
private long _bytesSent;
|
||||
|
||||
public TrapSession Session => inner.Session;
|
||||
|
||||
public long BytesSent => Interlocked.Read(ref _bytesSent);
|
||||
|
||||
public async ValueTask WriteAsync(ReadOnlyMemory<byte> data, CancellationToken ct)
|
||||
{
|
||||
await inner.WriteAsync(data, ct);
|
||||
Interlocked.Add(ref _bytesSent, data.Length);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
Reference in new issue
Block a user