|
| 1 | +using Microsoft.AspNetCore.Http; |
| 2 | +using Microsoft.Extensions.Logging; |
| 3 | +using Microsoft.Extensions.Options; |
| 4 | +using System.Collections.Concurrent; |
| 5 | +using System.Threading.RateLimiting; |
| 6 | + |
| 7 | +namespace OrchardCoreContrib.HealthChecks; |
| 8 | + |
| 9 | +public class HealthChecksBlockingRateLimitingMiddleware |
| 10 | +{ |
| 11 | + private static readonly ConcurrentDictionary<string, DateTime> _blockedIPs = new(); |
| 12 | + |
| 13 | + private readonly RequestDelegate _next; |
| 14 | + private readonly HealthChecksOptions _healthChecksOptions; |
| 15 | + private readonly HealthChecksRateLimitingOptions _healthChecksRateLimitingOptions; |
| 16 | + private readonly HealthChecksBlockingRateLimitingOptions _healthChecksBlockingRateLimitingOptions; |
| 17 | + private readonly SlidingWindowRateLimiter _rateLimiter; |
| 18 | + private readonly ILogger _logger; |
| 19 | + |
| 20 | + public HealthChecksBlockingRateLimitingMiddleware( |
| 21 | + RequestDelegate next, |
| 22 | + IOptions<HealthChecksOptions> healthChecksOptions, |
| 23 | + IOptions<HealthChecksRateLimitingOptions> healthChecksRateLimitingOptions, |
| 24 | + IOptions<HealthChecksBlockingRateLimitingOptions> healthChecksBlockingRateLimitingOptions, |
| 25 | + ILogger<HealthChecksRateLimitingMiddleware> logger) |
| 26 | + { |
| 27 | + _next = next; |
| 28 | + _healthChecksOptions = healthChecksOptions.Value; |
| 29 | + _healthChecksRateLimitingOptions = healthChecksRateLimitingOptions.Value; |
| 30 | + _healthChecksBlockingRateLimitingOptions = healthChecksBlockingRateLimitingOptions.Value; |
| 31 | + _logger = logger; |
| 32 | + _rateLimiter = new(new SlidingWindowRateLimiterOptions |
| 33 | + { |
| 34 | + PermitLimit = _healthChecksRateLimitingOptions.PermitLimit, |
| 35 | + Window = _healthChecksRateLimitingOptions.Window, |
| 36 | + SegmentsPerWindow = _healthChecksRateLimitingOptions.SegmentsPerWindow, |
| 37 | + QueueLimit = _healthChecksRateLimitingOptions.QueueLimit, |
| 38 | + QueueProcessingOrder = QueueProcessingOrder.OldestFirst |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + public async Task InvokeAsync(HttpContext context) |
| 43 | + { |
| 44 | + if (context.Request.Path.Equals(_healthChecksOptions.Url)) |
| 45 | + { |
| 46 | + var ip = context.Connection.RemoteIpAddress?.ToString() ?? "Unknown"; |
| 47 | + |
| 48 | + if (_blockedIPs.TryGetValue(ip, out var blockedUntil)) |
| 49 | + { |
| 50 | + if (DateTime.UtcNow < blockedUntil) |
| 51 | + { |
| 52 | + context.Response.StatusCode = StatusCodes.Status403Forbidden; |
| 53 | + |
| 54 | + await context.Response.WriteAsync("Blocked due to excessive requests"); |
| 55 | + |
| 56 | + return; |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + _blockedIPs.TryRemove(ip, out _); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + var rateLimitLease = _rateLimiter.AttemptAcquire(1); |
| 65 | + |
| 66 | + if (!rateLimitLease.IsAcquired) |
| 67 | + { |
| 68 | + _blockedIPs[ip] = DateTime.UtcNow.Add(_healthChecksBlockingRateLimitingOptions.BlockDuration); |
| 69 | + |
| 70 | + _logger.LogWarning("Rate limit exceeded for IP Address {RemoteIP}.", context.Connection.RemoteIpAddress); |
| 71 | + |
| 72 | + context.Response.StatusCode = StatusCodes.Status429TooManyRequests; |
| 73 | + |
| 74 | + await context.Response.WriteAsync("Too Many Requests."); |
| 75 | + |
| 76 | + return; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + await _next(context); |
| 81 | + } |
| 82 | +} |
| 83 | + |
0 commit comments