mirror of
https://github.com/myawired/2025-RecRoom-Server-E12354.git
synced 2026-07-17 16:11:15 +10:00
30 lines
923 B
C#
30 lines
923 B
C#
public class IPBlockMiddleware
|
|
{
|
|
private readonly RequestDelegate _next;
|
|
private static readonly HashSet<string> _blockedIPs = new() // this can be ipv6 or ipv4
|
|
{
|
|
"37.203.37.86",
|
|
"173.2.187.39"
|
|
};
|
|
|
|
public IPBlockMiddleware(RequestDelegate next)
|
|
{
|
|
_next = next;
|
|
}
|
|
|
|
public async Task Invoke(HttpContext context)
|
|
{
|
|
string? remoteIp = context.Request.Headers["CF-Connecting-IP"].FirstOrDefault()
|
|
?? context.Request.Headers["X-Forwarded-For"].FirstOrDefault()
|
|
?? context.Connection.RemoteIpAddress?.ToString();
|
|
|
|
if (!string.IsNullOrEmpty(remoteIp) && _blockedIPs.Contains(remoteIp))
|
|
{
|
|
context.Response.StatusCode = StatusCodes.Status403Forbidden;// this do kick people //Status401Unauthorized;
|
|
await context.Response.WriteAsync("");
|
|
return;
|
|
}
|
|
|
|
await _next(context);
|
|
}
|
|
} |