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

50 lines
1.5 KiB
C#
Raw Normal View History

using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using System;
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;
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();
2019-03-10 23:11:28 -04:00
var blockMessage = new CloudQueueMessage(ipAddress);
await _blockIpQueue.AddMessageAsync(blockMessage);
if(!permanentBlock)
{
2019-03-10 23:11:28 -04:00
var unblockMessage = new CloudQueueMessage(ipAddress);
await _unblockIpQueue.AddMessageAsync(unblockMessage, null, new TimeSpan(0, 15, 0), null, null);
}
}
private async Task InitAsync()
{
if(_didInit)
{
return;
}
await _blockIpQueue.CreateIfNotExistsAsync();
await _unblockIpQueue.CreateIfNotExistsAsync();
_didInit = true;
}
}
}