Compare commits

..

2 Commits

Author SHA1 Message Date
f15bf4dfe6 Добавил управление RateLimiter из ENV
All checks were successful
Create and publish a Docker image / Publish image (push) Successful in 4m14s
2025-09-07 10:13:39 +03:00
e3bcdfa4e6 Исправил CRON 2025-09-07 10:13:21 +03:00

View File

@@ -13,7 +13,10 @@ var configuration = builder.Configuration;
string? preinstalledJwtToken = configuration["TOKEN"]; string? preinstalledJwtToken = configuration["TOKEN"];
string? tgChatId = configuration["TG_CHAT_ID"]; string? tgChatId = configuration["TG_CHAT_ID"];
string? tgToken = configuration["TG_TOKEN"]; string? tgToken = configuration["TG_TOKEN"];
string updateJwtCron = configuration["UPDATE_JWT_CRON"] ?? "0 4 * ? * *"; string updateJwtCron = configuration["UPDATE_JWT_CRON"] ?? "0 0 4 ? * *";
int permitLimit = int.TryParse(configuration["PERMIT_LIMIT"], out var parsedPermitLimit) ? parsedPermitLimit : 40;
int timeLimit = int.TryParse(configuration["TIME_LIMIT"], out var parsedTimeLimit) ? parsedTimeLimit : 10;
// создать папку data если не существует // создать папку data если не существует
var dataDirectory = Path.Combine(AppContext.BaseDirectory, "data"); var dataDirectory = Path.Combine(AppContext.BaseDirectory, "data");
@@ -82,14 +85,14 @@ builder.Services.AddRateLimiter(options =>
: (httpContext.Connection.RemoteIpAddress?.ToString() ?? "unknown"), : (httpContext.Connection.RemoteIpAddress?.ToString() ?? "unknown"),
factory: _ => new FixedWindowRateLimiterOptions factory: _ => new FixedWindowRateLimiterOptions
{ {
PermitLimit = 40, PermitLimit = permitLimit,
Window = TimeSpan.FromSeconds(10) Window = TimeSpan.FromSeconds(timeLimit)
})); }));
options.OnRejected = async (context, cancellationToken) => options.OnRejected = async (context, cancellationToken) =>
{ {
context.HttpContext.Response.StatusCode = StatusCodes.Status429TooManyRequests; context.HttpContext.Response.StatusCode = StatusCodes.Status429TooManyRequests;
context.HttpContext.Response.Headers["Retry-After"] = "60"; context.HttpContext.Response.Headers["Retry-After"] = timeLimit.ToString();
await context.HttpContext.Response.WriteAsync("Rate limit exceeded. Please try again later.", await context.HttpContext.Response.WriteAsync("Rate limit exceeded. Please try again later.",
cancellationToken); cancellationToken);