1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-26 23:32:19 -05:00
bitwarden/src/Core/Services/Implementations/AzureQueueBlockIpService.cs
2019-02-14 14:16:18 -05:00

49 lines
1.5 KiB
C#

using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using System;
namespace Bit.Core.Services
{
public class AzureQueueBlockIpService : IBlockIpService
{
private readonly CloudQueue _blockIpQueue;
private readonly CloudQueue _unblockIpQueue;
private bool _didInit = false;
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 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;
}
}
}