Files
Flypaper/test/Flypaper.Tests/Mcp/McpTestData.cs
T
TomandClaude Opus 5.5 49a8fa1154
Build and deploy / build (push) Successful in 1m31s
Build and deploy / deploy (push) Successful in 20s
Add read-only MCP server with token access
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:34:16 +01:00

78 lines
3.0 KiB
C#

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($"Data Source={_dbPath}");
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");
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);
Add(db, "192.0.2.50", hoursAgo: 200, seconds: 5, null, null, null, null, null, "GET /", null);
db.SaveChanges();
}
public IDbContextFactory<FlypaperDbContext> Factory { get; }
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)
{
var started = Now.AddHours(-hoursAgo);
var entity = TrapEventEntity.From(new TrapEvent(
Guid.NewGuid(),
TrapProtocol.Http,
IPAddress.Parse(ip),
"html-drip",
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<FlypaperDbContext>
{
public FlypaperDbContext CreateDbContext() =>
new(new DbContextOptionsBuilder<FlypaperDbContext>().UseSqlite(connectionString).Options);
}
}