| | 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 => |
| 626 | 16 | | RateLimitPartition.GetFixedWindowLimiter( |
| 626 | 17 | | partitionKey: httpContext.Connection.RemoteIpAddress?.ToString(), |
| 628 | 18 | | factory: _ => new FixedWindowRateLimiterOptions |
| 628 | 19 | | { |
| 628 | 20 | | PermitLimit = settings.SuperVerySmall, |
| 628 | 21 | | Window = TimeSpan.FromHours(1) |
| 628 | 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 => |
| 2308 | 34 | | RateLimitPartition.GetFixedWindowLimiter( |
| 2308 | 35 | | partitionKey: httpContext.Connection.RemoteIpAddress?.ToString(), |
| 2310 | 36 | | factory: _ => new FixedWindowRateLimiterOptions |
| 2310 | 37 | | { |
| 2310 | 38 | | PermitLimit = settings.Small, |
| 2310 | 39 | | Window = TimeSpan.FromHours(1) |
| 2310 | 40 | | })); |
| 2 | 41 | |
|
| 2 | 42 | | options.AddPolicy(nameof(settings.Medium), httpContext => |
| 4940 | 43 | | RateLimitPartition.GetFixedWindowLimiter( |
| 4940 | 44 | | partitionKey: httpContext.Connection.RemoteIpAddress?.ToString(), |
| 4942 | 45 | | factory: _ => new FixedWindowRateLimiterOptions |
| 4942 | 46 | | { |
| 4942 | 47 | | PermitLimit = settings.Medium, |
| 4942 | 48 | | Window = TimeSpan.FromHours(1) |
| 4942 | 49 | | })); |
| 4 | 50 | | }); |
| 2 | 51 | | } |
| | 52 | | } |