Headers and the first 8 KB of the body are stored unmasked for 30 days, read alongside the response so the trap answers as before. MCP get_request and the expanded catch row show them. Changelog prepared for 0.10.0. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
112 lines
3.7 KiB
C#
112 lines
3.7 KiB
C#
using System.Net;
|
|
using Flypaper.Core;
|
|
using Flypaper.Core.Abstractions;
|
|
using Flypaper.Core.Models;
|
|
using Flypaper.Core.Networking;
|
|
using Flypaper.Core.Runtime;
|
|
using Flypaper.Core.Strategies;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Http.Features;
|
|
using Microsoft.AspNetCore.Server.Kestrel.Core.Features;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace Flypaper.Http.Middleware;
|
|
|
|
internal sealed class FlypaperMiddleware(
|
|
RequestDelegate next,
|
|
TrapRunner runner,
|
|
IOptions<PortOptions> ports,
|
|
IKnownIpLookup knownIps,
|
|
IOptionsMonitor<DripOptions> drip,
|
|
IOptionsMonitor<FlypaperOptions> options,
|
|
TimeProvider timeProvider)
|
|
{
|
|
public async Task InvokeAsync(HttpContext context)
|
|
{
|
|
if (context.Connection.LocalPort == ports.Value.Dashboard || IsInternalHealthProbe(context))
|
|
{
|
|
await next(context);
|
|
return;
|
|
}
|
|
|
|
var request = context.Request;
|
|
|
|
var session = new TrapSession(
|
|
Guid.NewGuid(),
|
|
TrapProtocol.Http,
|
|
context.Connection.RemoteIpAddress ?? IPAddress.None,
|
|
timeProvider.GetUtcNow());
|
|
|
|
var isNewIp = drip.CurrentValue.Lure
|
|
&& HttpMethods.IsGet(request.Method)
|
|
&& LureContent.Handles(request.Path.Value)
|
|
&& !await knownIps.HasSeenAsync(session.RemoteIp, context.RequestAborted);
|
|
|
|
var capture = options.CurrentValue.CaptureRequests ? new RequestCapture(context) : null;
|
|
|
|
var trapContext = new TrapContext(
|
|
session,
|
|
$"{request.Method} {StoredHost(request.Host)}{request.Path}{request.QueryString}",
|
|
request.Headers.UserAgent.ToString(),
|
|
request.Path.Value,
|
|
isNewIp,
|
|
capture);
|
|
|
|
var outcome = await runner.RunAsync(
|
|
trapContext,
|
|
async (strategy, ct) =>
|
|
{
|
|
var channel = await OpenChannelAsync(context, session, strategy, ct);
|
|
capture?.Start();
|
|
return channel;
|
|
},
|
|
context.RequestAborted);
|
|
|
|
if (outcome == TrapOutcome.Rejected && !context.Response.HasStarted)
|
|
{
|
|
context.Response.StatusCode = StatusCodes.Status404NotFound;
|
|
}
|
|
}
|
|
|
|
private static string StoredHost(HostString host) =>
|
|
string.IsNullOrEmpty(host.Host) || host.Host == "_" ? "" : host.Value!;
|
|
|
|
private static bool IsInternalHealthProbe(HttpContext context) =>
|
|
context.Request.Path.Equals(HttpTrapOptions.HealthPath, StringComparison.OrdinalIgnoreCase)
|
|
&& context.Connection.RemoteIpAddress is { } remoteIp
|
|
&& PrivateNetworks.Contains(remoteIp);
|
|
|
|
private static async ValueTask<ITrapChannel> OpenChannelAsync(
|
|
HttpContext context,
|
|
TrapSession session,
|
|
ITarpitStrategy strategy,
|
|
CancellationToken ct)
|
|
{
|
|
var minDataRate = context.Features.Get<IHttpMinResponseDataRateFeature>();
|
|
if (minDataRate is not null)
|
|
{
|
|
minDataRate.MinDataRate = null;
|
|
}
|
|
|
|
context.Features.Get<IHttpResponseBodyFeature>()?.DisableBuffering();
|
|
|
|
var response = context.Response;
|
|
response.StatusCode = StatusCodes.Status200OK;
|
|
response.ContentType = strategy is IStrategyDetails details ? details.MediaType : "text/html; charset=utf-8";
|
|
response.Headers.Server = "nginx";
|
|
response.Headers.CacheControl = "no-store";
|
|
response.Headers["X-Accel-Buffering"] = "no";
|
|
if (strategy is IResponseHeaders extra)
|
|
{
|
|
foreach (var (name, value) in extra.Headers)
|
|
{
|
|
response.Headers[name] = value;
|
|
}
|
|
}
|
|
|
|
await response.StartAsync(ct);
|
|
|
|
return new HttpTrapChannel(session, response);
|
|
}
|
|
}
|