Add Flypaper solution skeleton with HTTP tarpit, SQLite event log and tests
This commit is contained in:
1 parent
20314268c6
commit
f9161f48e7
55 files changed
+1331
No files matched your search
@@ -0,0 +1,20 @@
|
||||
using System.Threading.Channels;
|
||||
using Flypaper.Core;
|
||||
|
||||
namespace Flypaper.Storage;
|
||||
|
||||
public sealed class ChannelTrapEventSink : ITrapEventSink
|
||||
{
|
||||
private const int Capacity = 10_000;
|
||||
|
||||
private readonly Channel<TrapEvent> _channel = Channel.CreateBounded<TrapEvent>(
|
||||
new BoundedChannelOptions(Capacity)
|
||||
{
|
||||
FullMode = BoundedChannelFullMode.DropOldest,
|
||||
SingleReader = true
|
||||
});
|
||||
|
||||
public ChannelReader<TrapEvent> Reader => _channel.Reader;
|
||||
|
||||
public void Publish(TrapEvent trapEvent) => _channel.Writer.TryWrite(trapEvent);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Flypaper.Core\Flypaper.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,23 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Flypaper.Storage;
|
||||
|
||||
public sealed class FlypaperDbContext(DbContextOptions<FlypaperDbContext> options) : DbContext(options)
|
||||
{
|
||||
internal const int MaxTextLength = 2048;
|
||||
|
||||
public DbSet<TrapEventEntity> TrapEvents => Set<TrapEventEntity>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
var trapEvent = modelBuilder.Entity<TrapEventEntity>();
|
||||
trapEvent.Property(e => e.Protocol).HasConversion<string>().HasMaxLength(16);
|
||||
trapEvent.Property(e => e.Outcome).HasConversion<string>().HasMaxLength(16);
|
||||
trapEvent.Property(e => e.RemoteIp).HasMaxLength(64);
|
||||
trapEvent.Property(e => e.Strategy).HasMaxLength(64);
|
||||
trapEvent.Property(e => e.Target).HasMaxLength(MaxTextLength);
|
||||
trapEvent.Property(e => e.ClientInfo).HasMaxLength(MaxTextLength);
|
||||
trapEvent.HasIndex(e => e.StartedAt);
|
||||
trapEvent.HasIndex(e => e.RemoteIp);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using Flypaper.Core;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Flypaper.Storage;
|
||||
|
||||
public static class FlypaperStorageExtensions
|
||||
{
|
||||
public static IServiceCollection AddFlypaperStorage(this IServiceCollection services, string connectionString)
|
||||
{
|
||||
services.AddDbContextFactory<FlypaperDbContext>(options => options.UseSqlite(connectionString));
|
||||
services.AddSingleton<ChannelTrapEventSink>();
|
||||
services.AddSingleton<ITrapEventSink>(sp => sp.GetRequiredService<ChannelTrapEventSink>());
|
||||
services.AddHostedService<TrapEventWriter>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using Flypaper.Core;
|
||||
|
||||
namespace Flypaper.Storage;
|
||||
|
||||
public sealed class TrapEventEntity
|
||||
{
|
||||
public long Id { get; set; }
|
||||
|
||||
public Guid SessionId { get; set; }
|
||||
|
||||
public TrapProtocol Protocol { get; set; }
|
||||
|
||||
public required string RemoteIp { get; set; }
|
||||
|
||||
public required string Strategy { get; set; }
|
||||
|
||||
public DateTime StartedAt { get; set; }
|
||||
|
||||
public DateTime EndedAt { get; set; }
|
||||
|
||||
public long DurationMs { get; set; }
|
||||
|
||||
public long BytesSent { get; set; }
|
||||
|
||||
public TrapOutcome Outcome { get; set; }
|
||||
|
||||
public string? Target { get; set; }
|
||||
|
||||
public string? ClientInfo { get; set; }
|
||||
|
||||
public static TrapEventEntity From(TrapEvent trapEvent) => new()
|
||||
{
|
||||
SessionId = trapEvent.SessionId,
|
||||
Protocol = trapEvent.Protocol,
|
||||
RemoteIp = trapEvent.RemoteIp.ToString(),
|
||||
Strategy = trapEvent.Strategy,
|
||||
StartedAt = trapEvent.StartedAt.UtcDateTime,
|
||||
EndedAt = trapEvent.EndedAt.UtcDateTime,
|
||||
DurationMs = (long)trapEvent.Duration.TotalMilliseconds,
|
||||
BytesSent = trapEvent.BytesSent,
|
||||
Outcome = trapEvent.Outcome,
|
||||
Target = Truncate(trapEvent.Target, FlypaperDbContext.MaxTextLength),
|
||||
ClientInfo = Truncate(trapEvent.ClientInfo, FlypaperDbContext.MaxTextLength)
|
||||
};
|
||||
|
||||
private static string? Truncate(string? value, int maxLength) =>
|
||||
value is { Length: > 0 } && value.Length > maxLength ? value[..maxLength] : value;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Flypaper.Core;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Flypaper.Storage;
|
||||
|
||||
internal sealed class TrapEventWriter(
|
||||
ChannelTrapEventSink sink,
|
||||
IDbContextFactory<FlypaperDbContext> dbContextFactory,
|
||||
ILogger<TrapEventWriter> logger) : BackgroundService
|
||||
{
|
||||
private const int MaxBatchSize = 100;
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
await using (var db = await dbContextFactory.CreateDbContextAsync(stoppingToken))
|
||||
{
|
||||
await db.Database.EnsureCreatedAsync(stoppingToken);
|
||||
}
|
||||
|
||||
var batch = new List<TrapEvent>(MaxBatchSize);
|
||||
while (await sink.Reader.WaitToReadAsync(stoppingToken))
|
||||
{
|
||||
while (batch.Count < MaxBatchSize && sink.Reader.TryRead(out var trapEvent))
|
||||
{
|
||||
batch.Add(trapEvent);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await using var db = await dbContextFactory.CreateDbContextAsync(stoppingToken);
|
||||
db.TrapEvents.AddRange(batch.Select(TrapEventEntity.From));
|
||||
await db.SaveChangesAsync(stoppingToken);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
logger.LogError(ex, "Failed to persist {Count} trap events", batch.Count);
|
||||
}
|
||||
|
||||
batch.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user