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,44 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using Flypaper.Core;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Flypaper.Http;
|
||||
|
||||
public static class FlypaperHttpExtensions
|
||||
{
|
||||
public static IServiceCollection AddFlypaperHttp(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.Configure<HttpTrapOptions>(configuration.GetSection(HttpTrapOptions.SectionName));
|
||||
services.TryAddSingleton<TrapMatcher>();
|
||||
|
||||
services.AddOptions<ForwardedHeadersOptions>()
|
||||
.Configure<IOptions<HttpTrapOptions>>((forwarded, trap) =>
|
||||
{
|
||||
forwarded.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
|
||||
forwarded.ForwardLimit = 1;
|
||||
foreach (var proxy in trap.Value.TrustedProxies)
|
||||
{
|
||||
forwarded.KnownIPNetworks.Add(System.Net.IPNetwork.Parse(proxy.Contains('/') ? proxy : $"{proxy}/{HostBits(proxy)}"));
|
||||
}
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IApplicationBuilder UseFlypaper(this IApplicationBuilder app)
|
||||
{
|
||||
app.UseForwardedHeaders();
|
||||
app.UseMiddleware<RealIpMiddleware>();
|
||||
app.UseMiddleware<FlypaperMiddleware>();
|
||||
return app;
|
||||
}
|
||||
|
||||
private static int HostBits(string address) =>
|
||||
IPAddress.Parse(address).AddressFamily == AddressFamily.InterNetworkV6 ? 128 : 32;
|
||||
}
|
||||
Reference in new issue
Block a user