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

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

40 lines
1.4 KiB
C#
Raw Normal View History

using System;
using System.Threading.Tasks;
2020-01-10 08:33:13 -05:00
using Azure.Storage.Queues;
using Bit.Core.Settings;
namespace Bit.Core.Services
{
2016-12-02 23:37:08 -05:00
public class AzureQueueBlockIpService : IBlockIpService
{
2020-01-10 08:33:13 -05:00
private readonly QueueClient _blockIpQueueClient;
private readonly QueueClient _unblockIpQueueClient;
private Tuple<string, bool, DateTime> _lastBlock;
2016-12-02 23:37:08 -05:00
public AzureQueueBlockIpService(
GlobalSettings globalSettings)
{
2020-01-10 08:33:13 -05:00
_blockIpQueueClient = new QueueClient(globalSettings.Storage.ConnectionString, "blockip");
_unblockIpQueueClient = new QueueClient(globalSettings.Storage.ConnectionString, "unblockip");
}
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);
2020-01-10 08:33:13 -05:00
await _blockIpQueueClient.SendMessageAsync(ipAddress);
if (!permanentBlock)
{
2020-01-10 08:33:13 -05:00
await _unblockIpQueueClient.SendMessageAsync(ipAddress, new TimeSpan(0, 15, 0));
}
}
}
}