mirror of
https://github.com/bitwarden/server.git
synced 2025-07-12 05:13:58 -05:00
Revert filescoped (#2227)
* Revert "Add git blame entry (#2226)" This reverts commit239286737d
. * Revert "Turn on file scoped namespaces (#2225)" This reverts commit34fb4cca2a
.
This commit is contained in:
@ -6,85 +6,86 @@ using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Bit.Core.Utilities;
|
||||
|
||||
public class CustomIpRateLimitMiddleware : IpRateLimitMiddleware
|
||||
namespace Bit.Core.Utilities
|
||||
{
|
||||
private readonly IBlockIpService _blockIpService;
|
||||
private readonly ILogger<CustomIpRateLimitMiddleware> _logger;
|
||||
private readonly IDistributedCache _distributedCache;
|
||||
private readonly IpRateLimitOptions _options;
|
||||
|
||||
public CustomIpRateLimitMiddleware(
|
||||
IDistributedCache distributedCache,
|
||||
IBlockIpService blockIpService,
|
||||
RequestDelegate next,
|
||||
IProcessingStrategy processingStrategy,
|
||||
IRateLimitConfiguration rateLimitConfiguration,
|
||||
IOptions<IpRateLimitOptions> options,
|
||||
IIpPolicyStore policyStore,
|
||||
ILogger<CustomIpRateLimitMiddleware> logger)
|
||||
: base(next, processingStrategy, options, policyStore, rateLimitConfiguration, logger)
|
||||
public class CustomIpRateLimitMiddleware : IpRateLimitMiddleware
|
||||
{
|
||||
_distributedCache = distributedCache;
|
||||
_blockIpService = blockIpService;
|
||||
_options = options.Value;
|
||||
_logger = logger;
|
||||
}
|
||||
private readonly IBlockIpService _blockIpService;
|
||||
private readonly ILogger<CustomIpRateLimitMiddleware> _logger;
|
||||
private readonly IDistributedCache _distributedCache;
|
||||
private readonly IpRateLimitOptions _options;
|
||||
|
||||
public override Task ReturnQuotaExceededResponse(HttpContext httpContext, RateLimitRule rule, string retryAfter)
|
||||
{
|
||||
var message = string.IsNullOrWhiteSpace(_options.QuotaExceededMessage)
|
||||
? $"Slow down! Too many requests. Try again in {rule.Period}."
|
||||
: _options.QuotaExceededMessage;
|
||||
httpContext.Response.Headers["Retry-After"] = retryAfter;
|
||||
httpContext.Response.StatusCode = _options.HttpStatusCode;
|
||||
var errorModel = new ErrorResponseModel { Message = message };
|
||||
return httpContext.Response.WriteAsJsonAsync(errorModel, httpContext.RequestAborted);
|
||||
}
|
||||
|
||||
protected override void LogBlockedRequest(HttpContext httpContext, ClientRequestIdentity identity,
|
||||
RateLimitCounter counter, RateLimitRule rule)
|
||||
{
|
||||
base.LogBlockedRequest(httpContext, identity, counter, rule);
|
||||
var key = $"blockedIp_{identity.ClientIp}";
|
||||
|
||||
_distributedCache.TryGetValue(key, out int blockedCount);
|
||||
|
||||
blockedCount++;
|
||||
if (blockedCount > 10)
|
||||
public CustomIpRateLimitMiddleware(
|
||||
IDistributedCache distributedCache,
|
||||
IBlockIpService blockIpService,
|
||||
RequestDelegate next,
|
||||
IProcessingStrategy processingStrategy,
|
||||
IRateLimitConfiguration rateLimitConfiguration,
|
||||
IOptions<IpRateLimitOptions> options,
|
||||
IIpPolicyStore policyStore,
|
||||
ILogger<CustomIpRateLimitMiddleware> logger)
|
||||
: base(next, processingStrategy, options, policyStore, rateLimitConfiguration, logger)
|
||||
{
|
||||
_blockIpService.BlockIpAsync(identity.ClientIp, false);
|
||||
_logger.LogInformation(Constants.BypassFiltersEventId, null,
|
||||
"Banned {0}. \nInfo: \n{1}", identity.ClientIp, GetRequestInfo(httpContext));
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation(Constants.BypassFiltersEventId, null,
|
||||
"Request blocked {0}. \nInfo: \n{1}", identity.ClientIp, GetRequestInfo(httpContext));
|
||||
_distributedCache.Set(key, blockedCount,
|
||||
new DistributedCacheEntryOptions().SetSlidingExpiration(new TimeSpan(0, 5, 0)));
|
||||
}
|
||||
}
|
||||
|
||||
private string GetRequestInfo(HttpContext httpContext)
|
||||
{
|
||||
if (httpContext == null || httpContext.Request == null)
|
||||
{
|
||||
return null;
|
||||
_distributedCache = distributedCache;
|
||||
_blockIpService = blockIpService;
|
||||
_options = options.Value;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
var s = string.Empty;
|
||||
foreach (var header in httpContext.Request.Headers)
|
||||
public override Task ReturnQuotaExceededResponse(HttpContext httpContext, RateLimitRule rule, string retryAfter)
|
||||
{
|
||||
s += $"Header \"{header.Key}\": {header.Value} \n";
|
||||
var message = string.IsNullOrWhiteSpace(_options.QuotaExceededMessage)
|
||||
? $"Slow down! Too many requests. Try again in {rule.Period}."
|
||||
: _options.QuotaExceededMessage;
|
||||
httpContext.Response.Headers["Retry-After"] = retryAfter;
|
||||
httpContext.Response.StatusCode = _options.HttpStatusCode;
|
||||
var errorModel = new ErrorResponseModel { Message = message };
|
||||
return httpContext.Response.WriteAsJsonAsync(errorModel, httpContext.RequestAborted);
|
||||
}
|
||||
|
||||
foreach (var query in httpContext.Request.Query)
|
||||
protected override void LogBlockedRequest(HttpContext httpContext, ClientRequestIdentity identity,
|
||||
RateLimitCounter counter, RateLimitRule rule)
|
||||
{
|
||||
s += $"Query \"{query.Key}\": {query.Value} \n";
|
||||
base.LogBlockedRequest(httpContext, identity, counter, rule);
|
||||
var key = $"blockedIp_{identity.ClientIp}";
|
||||
|
||||
_distributedCache.TryGetValue(key, out int blockedCount);
|
||||
|
||||
blockedCount++;
|
||||
if (blockedCount > 10)
|
||||
{
|
||||
_blockIpService.BlockIpAsync(identity.ClientIp, false);
|
||||
_logger.LogInformation(Constants.BypassFiltersEventId, null,
|
||||
"Banned {0}. \nInfo: \n{1}", identity.ClientIp, GetRequestInfo(httpContext));
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation(Constants.BypassFiltersEventId, null,
|
||||
"Request blocked {0}. \nInfo: \n{1}", identity.ClientIp, GetRequestInfo(httpContext));
|
||||
_distributedCache.Set(key, blockedCount,
|
||||
new DistributedCacheEntryOptions().SetSlidingExpiration(new TimeSpan(0, 5, 0)));
|
||||
}
|
||||
}
|
||||
|
||||
return s;
|
||||
private string GetRequestInfo(HttpContext httpContext)
|
||||
{
|
||||
if (httpContext == null || httpContext.Request == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var s = string.Empty;
|
||||
foreach (var header in httpContext.Request.Headers)
|
||||
{
|
||||
s += $"Header \"{header.Key}\": {header.Value} \n";
|
||||
}
|
||||
|
||||
foreach (var query in httpContext.Request.Query)
|
||||
{
|
||||
s += $"Query \"{query.Key}\": {query.Value} \n";
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user