Path, headers and body of every catch are checked; command and target server are stored, shown defanged in the dashboard and via MCP list_commands. Changelog prepared for 0.11.0. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
69 lines
3.4 KiB
C#
69 lines
3.4 KiB
C#
using Flypaper.Core.Analysis;
|
|
using Flypaper.Storage.Entities;
|
|
using Flypaper.Storage.Explanations;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Flypaper.Storage;
|
|
|
|
public sealed class FlypaperDbContext(DbContextOptions<FlypaperDbContext> options) : DbContext(options)
|
|
{
|
|
internal const int MaxTextLength = 2048;
|
|
internal const int MaxNameLength = 256;
|
|
|
|
public DbSet<TrapEventEntity> TrapEvents => Set<TrapEventEntity>();
|
|
|
|
public DbSet<TrapRequestEntity> TrapRequests => Set<TrapRequestEntity>();
|
|
|
|
public DbSet<SettingEntity> Settings => Set<SettingEntity>();
|
|
|
|
public DbSet<McpTokenEntity> McpTokens => Set<McpTokenEntity>();
|
|
|
|
public DbSet<ExplanationRuleEntity> ExplanationRules => Set<ExplanationRuleEntity>();
|
|
|
|
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder) =>
|
|
configurationBuilder.Properties<DateTime>().HaveConversion<UtcDateTimeConverter>();
|
|
|
|
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.Property(e => e.CountryCode).HasMaxLength(2);
|
|
trapEvent.Property(e => e.Country).HasMaxLength(MaxNameLength);
|
|
trapEvent.Property(e => e.City).HasMaxLength(MaxNameLength);
|
|
trapEvent.Property(e => e.Provider).HasMaxLength(MaxNameLength);
|
|
trapEvent.HasIndex(e => e.RemoteIp);
|
|
trapEvent.HasIndex(e => e.CountryCode);
|
|
trapEvent.HasIndex(e => e.Asn);
|
|
trapEvent.Property(e => e.Command).HasMaxLength(CommandDetector.MaxCommandLength);
|
|
trapEvent.Property(e => e.CommandServer).HasMaxLength(CommandDetector.MaxServerLength);
|
|
trapEvent.HasIndex(e => e.CommandScan);
|
|
trapEvent.HasIndex(e => e.StartedAt, "IX_TrapEvents_Commands").HasFilter("\"Command\" IS NOT NULL");
|
|
trapEvent.HasOne(e => e.Request)
|
|
.WithOne()
|
|
.HasForeignKey<TrapRequestEntity>(e => e.TrapEventId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
var setting = modelBuilder.Entity<SettingEntity>();
|
|
setting.HasKey(e => e.Key);
|
|
setting.Property(e => e.Key).HasMaxLength(128);
|
|
|
|
var mcpToken = modelBuilder.Entity<McpTokenEntity>();
|
|
mcpToken.Property(e => e.Name).HasMaxLength(100);
|
|
mcpToken.Property(e => e.TokenHash).HasMaxLength(64);
|
|
mcpToken.HasIndex(e => e.TokenHash).IsUnique();
|
|
|
|
var explanationRule = modelBuilder.Entity<ExplanationRuleEntity>();
|
|
explanationRule.Property(e => e.Kind).HasConversion<string>().HasMaxLength(16);
|
|
explanationRule.Property(e => e.Pattern).HasMaxLength(ExplanationLimits.MaxPatternLength);
|
|
explanationRule.Property(e => e.Text).HasMaxLength(ExplanationLimits.MaxTextLength);
|
|
explanationRule.Property(e => e.Note).HasMaxLength(ExplanationLimits.MaxNoteLength);
|
|
explanationRule.Property(e => e.CreatedBy).HasMaxLength(ExplanationLimits.MaxCreatedByLength);
|
|
}
|
|
}
|