Files
server/src/Core/Services/Implementations/AmazonSqsBlockIpService.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

82 lines
2.4 KiB
C#
Raw Normal View History

using Amazon;
2019-03-18 16:23:37 -04:00
using Amazon.SQS;
using Bit.Core.Settings;
2019-03-18 16:23:37 -04:00
namespace Bit.Core.Services;
2022-08-29 16:06:55 -04:00
2019-03-18 16:23:37 -04:00
public class AmazonSqsBlockIpService : IBlockIpService, IDisposable
{
private readonly IAmazonSQS _client;
private string _blockIpQueueUrl;
private string _unblockIpQueueUrl;
private bool _didInit = false;
private Tuple<string, bool, DateTime> _lastBlock;
2022-08-29 16:06:55 -04:00
2019-03-18 16:23:37 -04:00
public AmazonSqsBlockIpService(
GlobalSettings globalSettings)
: this(globalSettings, new AmazonSQSClient(
globalSettings.Amazon.AccessKeyId,
globalSettings.Amazon.AccessKeySecret,
RegionEndpoint.GetBySystemName(globalSettings.Amazon.Region))
2022-08-29 16:06:55 -04:00
)
2019-03-18 16:23:37 -04:00
{
2022-08-29 16:06:55 -04:00
}
2019-03-18 16:23:37 -04:00
public AmazonSqsBlockIpService(
GlobalSettings globalSettings,
IAmazonSQS amazonSqs)
2022-08-29 16:06:55 -04:00
{
if (string.IsNullOrWhiteSpace(globalSettings.Amazon?.AccessKeyId))
2019-03-18 16:23:37 -04:00
{
throw new ArgumentNullException(nameof(globalSettings.Amazon.AccessKeyId));
2019-03-18 16:23:37 -04:00
}
if (string.IsNullOrWhiteSpace(globalSettings.Amazon?.AccessKeySecret))
{
2019-03-18 16:23:37 -04:00
throw new ArgumentNullException(nameof(globalSettings.Amazon.AccessKeySecret));
2022-08-29 14:53:16 -04:00
}
2019-03-18 16:23:37 -04:00
if (string.IsNullOrWhiteSpace(globalSettings.Amazon?.Region))
{
throw new ArgumentNullException(nameof(globalSettings.Amazon.Region));
}
_client = amazonSqs;
}
2019-03-18 16:23:37 -04:00
public void Dispose()
2022-08-29 16:06:55 -04:00
{
2019-03-18 16:23:37 -04:00
_client?.Dispose();
2022-08-29 16:06:55 -04:00
}
2019-03-18 16:23:37 -04:00
public async Task BlockIpAsync(string ipAddress, bool permanentBlock)
2022-08-29 16:06:55 -04:00
{
2019-03-18 16:23:37 -04:00
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;
2022-08-29 14:53:16 -04:00
}
2019-03-18 16:23:37 -04:00
_lastBlock = new Tuple<string, bool, DateTime>(ipAddress, permanentBlock, now);
await _client.SendMessageAsync(_blockIpQueueUrl, ipAddress);
if (!permanentBlock)
2022-08-29 14:53:16 -04:00
{
2019-03-18 16:23:37 -04:00
await _client.SendMessageAsync(_unblockIpQueueUrl, ipAddress);
2022-08-29 16:06:55 -04:00
}
2019-03-18 16:23:37 -04:00
}
2022-08-29 14:53:16 -04:00
2019-03-18 16:23:37 -04:00
private async Task InitAsync()
2022-08-29 16:06:55 -04:00
{
2019-03-18 16:23:37 -04:00
if (_didInit)
2022-08-29 16:06:55 -04:00
{
2019-03-18 16:23:37 -04:00
return;
}
2022-08-29 16:06:55 -04:00
2019-03-18 16:23:37 -04:00
var blockIpQueue = await _client.GetQueueUrlAsync("block-ip");
_blockIpQueueUrl = blockIpQueue.QueueUrl;
var unblockIpQueue = await _client.GetQueueUrlAsync("unblock-ip");
_unblockIpQueueUrl = unblockIpQueue.QueueUrl;
_didInit = true;
}
}