mirror of
https://github.com/bitwarden/server.git
synced 2025-07-02 16:42:50 -05:00
amazon sqs block ip queuing
This commit is contained in:
@ -22,6 +22,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AWSSDK.SimpleEmail" Version="3.3.7.38" />
|
||||
<PackageReference Include="AWSSDK.SQS" Version="3.3.3.62" />
|
||||
<PackageReference Include="Handlebars.Net" Version="1.9.5" />
|
||||
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="2.7.0" />
|
||||
<PackageReference Include="MailKit" Version="2.1.3" />
|
||||
|
72
src/Core/Services/Implementations/AmazonSqsBlockIpService.cs
Normal file
72
src/Core/Services/Implementations/AmazonSqsBlockIpService.cs
Normal file
@ -0,0 +1,72 @@
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using Amazon.SQS;
|
||||
using Amazon;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
public class AmazonSqsBlockIpService : IBlockIpService, IDisposable
|
||||
{
|
||||
private readonly AmazonSQSClient _client;
|
||||
private string _blockIpQueueUrl;
|
||||
private string _unblockIpQueueUrl;
|
||||
private bool _didInit = false;
|
||||
private Tuple<string, bool, DateTime> _lastBlock;
|
||||
|
||||
public AmazonSqsBlockIpService(
|
||||
GlobalSettings globalSettings)
|
||||
{
|
||||
if(string.IsNullOrWhiteSpace(globalSettings.Amazon?.AccessKeyId))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(globalSettings.Amazon.AccessKeyId));
|
||||
}
|
||||
if(string.IsNullOrWhiteSpace(globalSettings.Amazon?.AccessKeySecret))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(globalSettings.Amazon.AccessKeySecret));
|
||||
}
|
||||
if(string.IsNullOrWhiteSpace(globalSettings.Amazon?.Region))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(globalSettings.Amazon.Region));
|
||||
}
|
||||
_client = new AmazonSQSClient(globalSettings.Amazon.AccessKeyId,
|
||||
globalSettings.Amazon.AccessKeySecret, RegionEndpoint.GetBySystemName(globalSettings.Amazon.Region));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_client?.Dispose();
|
||||
}
|
||||
|
||||
public async Task BlockIpAsync(string ipAddress, bool permanentBlock)
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
if(_lastBlock != null && _lastBlock.Item1 == ipAddress && _lastBlock.Item2 == permanentBlock &&
|
||||
(now - _lastBlock.Item3) < TimeSpan.FromMinutes(1))
|
||||
{
|
||||
// Already blocked this IP recently.
|
||||
return;
|
||||
}
|
||||
|
||||
_lastBlock = new Tuple<string, bool, DateTime>(ipAddress, permanentBlock, now);
|
||||
await _client.SendMessageAsync(_blockIpQueueUrl, ipAddress);
|
||||
if(!permanentBlock)
|
||||
{
|
||||
await _client.SendMessageAsync(_unblockIpQueueUrl, ipAddress);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task InitAsync()
|
||||
{
|
||||
if(_didInit)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var blockIpQueue = await _client.GetQueueUrlAsync("block-ip");
|
||||
_blockIpQueueUrl = blockIpQueue.QueueUrl;
|
||||
var unblockIpQueue = await _client.GetQueueUrlAsync("unblock-ip");
|
||||
_unblockIpQueueUrl = unblockIpQueue.QueueUrl;
|
||||
_didInit = true;
|
||||
}
|
||||
}
|
||||
}
|
@ -121,6 +121,10 @@ namespace Bit.Core.Utilities
|
||||
{
|
||||
services.AddSingleton<IBlockIpService, AzureQueueBlockIpService>();
|
||||
}
|
||||
else if(!globalSettings.SelfHosted && CoreHelpers.SettingHasValue(globalSettings.Amazon?.AccessKeySecret))
|
||||
{
|
||||
services.AddSingleton<IBlockIpService, AmazonSqsBlockIpService>();
|
||||
}
|
||||
else
|
||||
{
|
||||
services.AddSingleton<IBlockIpService, NoopBlockIpService>();
|
||||
|
Reference in New Issue
Block a user