using System.Net; using Flypaper.Core.Models; using Flypaper.Storage; using Flypaper.Storage.Entities; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; namespace Flypaper.Tests.Mcp; internal sealed class McpTestData : IDisposable { public static readonly DateTimeOffset Now = new(2026, 11, 15, 12, 0, 0, TimeSpan.Zero); private readonly string _dbPath = Path.Combine(Path.GetTempPath(), $"flypaper-mcp-{Guid.NewGuid():N}.db"); public McpTestData() { Factory = new TestDbContextFactory(ConnectionString); using var db = Factory.CreateDbContext(); db.Database.Migrate(); Add(db, "203.0.113.9", hoursAgo: 1, seconds: 3600, "CN", "Volksrepublik China", "Beijing", 4134, "Chinanet", "GET /.env", "zgrab/0.x", strategy: "env-drip"); Add(db, "203.0.113.9", hoursAgo: 2, seconds: 120, "CN", "Volksrepublik China", "Beijing", 4134, "Chinanet", "GET example.com/wp-login.php", "zgrab/0.x"); Add(db, "198.51.100.7", hoursAgo: 3, seconds: 30, "TW", "Republik China", "Taipeh", 3462, "Data Communication Business Group", "GET /.git/config", "curl/8.5.0"); Add(db, "2001:db8::1", hoursAgo: 30, seconds: 900, "US", "Vereinigte Staaten von Amerika", "Mountain View", 15169, "Google LLC", "POST /xmlrpc.php", "python-requests/2.31"); Add(db, "198.51.100.7", hoursAgo: 4, seconds: 0, "TW", "Republik China", "Taipeh", 3462, "Data Communication Business Group", "GET /.env", "curl/8.5.0", TrapOutcome.Rejected, "env-drip"); Add(db, "192.0.2.50", hoursAgo: 200, seconds: 5, null, null, null, null, null, "GET /", null); db.SaveChanges(); } public IDbContextFactory Factory { get; } public string ConnectionString => $"Data Source={_dbPath}"; private static void Add( FlypaperDbContext db, string ip, int hoursAgo, int seconds, string? countryCode, string? country, string? city, long? asn, string? provider, string target, string? userAgent, TrapOutcome outcome = TrapOutcome.ClientAborted, string strategy = "html-drip") { var started = Now.AddHours(-hoursAgo); var entity = TrapEventEntity.From(new TrapEvent( Guid.NewGuid(), TrapProtocol.Http, IPAddress.Parse(ip), strategy, started, started.AddSeconds(seconds), seconds * 10, outcome, target, userAgent)); entity.ApplyOrigin(countryCode is null && asn is null ? null : new IpOrigin(countryCode, country, city, asn, provider)); db.TrapEvents.Add(entity); } public void Dispose() { SqliteConnection.ClearAllPools(); foreach (var suffix in new[] { "", "-wal", "-shm" }) { File.Delete(_dbPath + suffix); } } private sealed class TestDbContextFactory(string connectionString) : IDbContextFactory { public FlypaperDbContext CreateDbContext() => new(new DbContextOptionsBuilder().UseSqlite(connectionString).Options); } }