2022-06-29 19:46:41 -04:00
|
|
|
|
using Amazon;
|
2019-03-18 16:23:37 -04:00
|
|
|
|
using Amazon.SQS;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
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
|
|
|
|
{
|
2020-06-18 10:13:22 -07:00
|
|
|
|
if (string.IsNullOrWhiteSpace(globalSettings.Amazon?.AccessKeyId))
|
2019-03-18 16:23:37 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
throw new ArgumentNullException(nameof(globalSettings.Amazon.AccessKeyId));
|
2019-03-18 16:23:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(globalSettings.Amazon?.AccessKeySecret))
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
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;
|
2022-08-29 15:53:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
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))
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
// 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 15:53:48 -04:00
|
|
|
|
}
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|