2022-06-29 19:46:41 -04:00
|
|
|
|
using Azure.Storage.Queues;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
2016-11-30 21:51:43 -05:00
|
|
|
|
|
2022-08-29 14:53:16 -04:00
|
|
|
|
namespace Bit.Core.Services;
|
|
|
|
|
|
|
|
|
|
|
|
public class AzureQueueBlockIpService : IBlockIpService
|
2016-11-30 21:51:43 -05:00
|
|
|
|
{
|
2022-08-29 14:53:16 -04:00
|
|
|
|
private readonly QueueClient _blockIpQueueClient;
|
|
|
|
|
|
private readonly QueueClient _unblockIpQueueClient;
|
|
|
|
|
|
private Tuple<string, bool, DateTime> _lastBlock;
|
|
|
|
|
|
|
|
|
|
|
|
public AzureQueueBlockIpService(
|
|
|
|
|
|
GlobalSettings globalSettings)
|
2016-11-30 21:51:43 -05:00
|
|
|
|
{
|
2022-08-29 14:53:16 -04:00
|
|
|
|
_blockIpQueueClient = new QueueClient(globalSettings.Storage.ConnectionString, "blockip");
|
|
|
|
|
|
_unblockIpQueueClient = new QueueClient(globalSettings.Storage.ConnectionString, "unblockip");
|
|
|
|
|
|
}
|
2016-11-30 21:51:43 -05:00
|
|
|
|
|
2022-08-29 14:53:16 -04:00
|
|
|
|
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))
|
2016-11-30 21:51:43 -05:00
|
|
|
|
{
|
2022-08-29 14:53:16 -04:00
|
|
|
|
// Already blocked this IP recently.
|
|
|
|
|
|
return;
|
2016-11-30 21:51:43 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-29 14:53:16 -04:00
|
|
|
|
_lastBlock = new Tuple<string, bool, DateTime>(ipAddress, permanentBlock, now);
|
|
|
|
|
|
await _blockIpQueueClient.SendMessageAsync(ipAddress);
|
|
|
|
|
|
if (!permanentBlock)
|
2016-11-30 21:51:43 -05:00
|
|
|
|
{
|
2022-08-29 14:53:16 -04:00
|
|
|
|
await _unblockIpQueueClient.SendMessageAsync(ipAddress, new TimeSpan(0, 15, 0));
|
2017-07-31 16:58:27 -04:00
|
|
|
|
}
|
2016-11-30 21:51:43 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|