| | | 1 | | using System.Threading.RateLimiting; |
| | | 2 | | |
| | | 3 | | namespace Syki.Back.Configs; |
| | | 4 | | |
| | | 5 | | public static class RateLimiterConfigs |
| | | 6 | | { |
| | | 7 | | public static void AddRateLimiterConfigs(this WebApplicationBuilder builder) |
| | | 8 | | { |
| | 2 | 9 | | var settings = new RateLimiterSettings(builder.Configuration); |
| | | 10 | | |
| | 2 | 11 | | builder.Services.AddRateLimiter(options => |
| | 2 | 12 | | { |
| | 2 | 13 | | options.RejectionStatusCode = StatusCodes.Status429TooManyRequests; |
| | 2 | 14 | | |
| | 2 | 15 | | options.AddPolicy(nameof(settings.SuperVerySmall), httpContext => |
| | 690 | 16 | | RateLimitPartition.GetFixedWindowLimiter( |
| | 690 | 17 | | partitionKey: httpContext.Connection.RemoteIpAddress?.ToString(), |
| | 692 | 18 | | factory: _ => new FixedWindowRateLimiterOptions |
| | 692 | 19 | | { |
| | 692 | 20 | | PermitLimit = settings.SuperVerySmall, |
| | 692 | 21 | | Window = TimeSpan.FromHours(1) |
| | 692 | 22 | | })); |
| | 2 | 23 | | |
| | 2 | 24 | | options.AddPolicy(nameof(settings.VerySmall), httpContext => |
| | 2 | 25 | | RateLimitPartition.GetFixedWindowLimiter( |
| | 2 | 26 | | partitionKey: httpContext.Connection.RemoteIpAddress?.ToString(), |
| | 2 | 27 | | factory: _ => new FixedWindowRateLimiterOptions |
| | 2 | 28 | | { |
| | 2 | 29 | | PermitLimit = settings.VerySmall, |
| | 2 | 30 | | Window = TimeSpan.FromHours(1) |
| | 2 | 31 | | })); |
| | 2 | 32 | | |
| | 2 | 33 | | options.AddPolicy(nameof(settings.Small), httpContext => |
| | 2436 | 34 | | RateLimitPartition.GetFixedWindowLimiter( |
| | 2436 | 35 | | partitionKey: httpContext.Connection.RemoteIpAddress?.ToString(), |
| | 2438 | 36 | | factory: _ => new FixedWindowRateLimiterOptions |
| | 2438 | 37 | | { |
| | 2438 | 38 | | PermitLimit = settings.Small, |
| | 2438 | 39 | | Window = TimeSpan.FromHours(1) |
| | 2438 | 40 | | })); |
| | 2 | 41 | | |
| | 2 | 42 | | options.AddPolicy(nameof(settings.Medium), httpContext => |
| | 5562 | 43 | | RateLimitPartition.GetFixedWindowLimiter( |
| | 5562 | 44 | | partitionKey: httpContext.Connection.RemoteIpAddress?.ToString(), |
| | 5564 | 45 | | factory: _ => new FixedWindowRateLimiterOptions |
| | 5564 | 46 | | { |
| | 5564 | 47 | | PermitLimit = settings.Medium, |
| | 5564 | 48 | | Window = TimeSpan.FromHours(1) |
| | 5564 | 49 | | })); |
| | 4 | 50 | | }); |
| | 2 | 51 | | } |
| | | 52 | | } |