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

59 lines
1.9 KiB
C#
Raw Normal View History

using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using System;
using Bit.Core.Utilities;
namespace Bit.Core.Services
{
2016-12-02 23:37:08 -05:00
public class AzureQueueBlockIpService : IBlockIpService
{
2016-11-30 21:54:04 -05:00
private readonly CloudQueue _blockIpQueue;
private readonly CloudQueue _unblockIpQueue;
private bool _didInit = false;
private Tuple<string, bool, DateTime> _lastBlock;
2016-12-02 23:37:08 -05:00
public AzureQueueBlockIpService(
GlobalSettings globalSettings)
{
var storageAccount = CloudStorageAccount.Parse(globalSettings.Storage.ConnectionString);
var queueClient = storageAccount.CreateCloudQueueClient();
_blockIpQueue = queueClient.GetQueueReference("blockip");
_unblockIpQueue = queueClient.GetQueueReference("unblockip");
}
public async Task BlockIpAsync(string ipAddress, bool permanentBlock)
{
await InitAsync();
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);
2019-03-13 09:42:04 -04:00
var message = new CloudQueueMessage(ipAddress);
await _blockIpQueue.AddMessageAsync(message);
if(!permanentBlock)
{
await _unblockIpQueue.AddMessageAsync(message, null, new TimeSpan(0, 15, 0), null, null);
}
}
private async Task InitAsync()
{
if(_didInit)
{
return;
}
await _blockIpQueue.CreateIfNotExistsAsync();
await _unblockIpQueue.CreateIfNotExistsAsync();
_didInit = true;
}
}
}