mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-05-24 00:52:23 -04:00
38 lines
1.3 KiB
C#
38 lines
1.3 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Threading;
|
|
using System.Threading.RateLimiting;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.RateLimiting;
|
|
|
|
namespace API.Middleware.RateLimit;
|
|
#nullable enable
|
|
|
|
public class AuthenticationRateLimiterPolicy : IRateLimiterPolicy<string>
|
|
{
|
|
public RateLimitPartition<string> GetPartition(HttpContext httpContext)
|
|
{
|
|
return RateLimitPartition.GetFixedWindowLimiter(httpContext.Request.Headers.Host.ToString(),
|
|
partition => new FixedWindowRateLimiterOptions
|
|
{
|
|
AutoReplenishment = true,
|
|
PermitLimit = 1,
|
|
Window = TimeSpan.FromMinutes(10),
|
|
});
|
|
}
|
|
|
|
public Func<OnRejectedContext, CancellationToken, ValueTask>? OnRejected { get; } =
|
|
(context, _) =>
|
|
{
|
|
if (context.Lease.TryGetMetadata(MetadataName.RetryAfter, out var retryAfter))
|
|
{
|
|
context.HttpContext.Response.Headers.RetryAfter =
|
|
((int) retryAfter.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo);
|
|
}
|
|
|
|
context.HttpContext.Response.StatusCode = StatusCodes.Status429TooManyRequests;
|
|
return new ValueTask();
|
|
};
|
|
}
|