Files
server/src/Core/Utilities/CustomIpRateLimitMiddleware.cs

96 lines
3.6 KiB
C#
Raw Normal View History

2016-11-12 18:43:32 -05:00
using AspNetCoreRateLimit;
2017-03-08 21:55:08 -05:00
using Bit.Core.Models.Api;
using Bit.Core.Services;
2016-11-12 18:43:32 -05:00
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Memory;
2016-11-12 18:43:32 -05:00
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System.Threading.Tasks;
2017-11-22 09:09:46 -05:00
using System;
2016-11-12 18:43:32 -05:00
2017-09-28 15:01:43 -04:00
namespace Bit.Core.Utilities
2016-11-12 18:43:32 -05:00
{
public class CustomIpRateLimitMiddleware : IpRateLimitMiddleware
{
private readonly IpRateLimitOptions _options;
private readonly IMemoryCache _memoryCache;
private readonly IBlockIpService _blockIpService;
private readonly ILogger<CustomIpRateLimitMiddleware> _logger;
2016-11-12 18:43:32 -05:00
public CustomIpRateLimitMiddleware(
IMemoryCache memoryCache,
IBlockIpService blockIpService,
2016-11-12 18:43:32 -05:00
RequestDelegate next,
IOptions<IpRateLimitOptions> options,
IRateLimitCounterStore counterStore,
IIpPolicyStore policyStore,
ILogger<CustomIpRateLimitMiddleware> logger,
IIpAddressParser ipParser = null)
: base(next, options, counterStore, policyStore, logger, ipParser)
2016-11-12 18:43:32 -05:00
{
_memoryCache = memoryCache;
_blockIpService = blockIpService;
2016-11-12 18:43:32 -05:00
_options = options.Value;
_logger = logger;
2016-11-12 18:43:32 -05:00
}
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;
httpContext.Response.ContentType = "application/json";
var errorModel = new ErrorResponseModel { Message = message };
return httpContext.Response.WriteAsync(JsonConvert.SerializeObject(errorModel));
}
public override void LogBlockedRequest(HttpContext httpContext, ClientRequestIdentity identity,
RateLimitCounter counter, RateLimitRule rule)
{
base.LogBlockedRequest(httpContext, identity, counter, rule);
var key = $"blockedIp_{identity.ClientIp}";
2017-08-31 11:23:16 -04:00
_memoryCache.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));
_memoryCache.Set(key, blockedCount,
2017-11-22 09:09:46 -05:00
new MemoryCacheEntryOptions().SetSlidingExpiration(new TimeSpan(0, 5, 0)));
}
}
2017-11-22 09:09:46 -05:00
2017-11-29 08:42:29 -05:00
private string GetRequestInfo(HttpContext httpContext)
2017-11-22 09:09:46 -05:00
{
if (httpContext == null || httpContext.Request == null)
2017-11-22 09:09:46 -05:00
{
return null;
}
2017-11-29 08:42:29 -05:00
var s = string.Empty;
foreach (var header in httpContext.Request.Headers)
2017-11-22 09:09:46 -05:00
{
2018-03-30 08:40:58 -04:00
s += $"Header \"{header.Key}\": {header.Value} \n";
2017-11-22 09:09:46 -05:00
}
foreach (var query in httpContext.Request.Query)
2017-11-29 08:42:29 -05:00
{
2018-03-30 08:40:58 -04:00
s += $"Query \"{query.Key}\": {query.Value} \n";
2017-11-29 08:42:29 -05:00
}
return s;
2017-11-22 09:09:46 -05:00
}
2016-11-12 18:43:32 -05:00
}
}