1
0
mirror of https://github.com/bitwarden/server synced 2025-12-19 17:53:44 +00:00

amazon sqs block ip queuing

This commit is contained in:
Kyle Spearrin
2019-03-18 16:23:37 -04:00
parent 7d65f8fa76
commit 8427c23b5e
7 changed files with 262 additions and 67 deletions

View File

@@ -0,0 +1,74 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Bit.Core;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
namespace Bit.Admin.HostedServices
{
public class AzureQueueBlockIpHostedService : BlockIpHostedService
{
private CloudQueue _blockQueue;
private CloudQueue _unblockQueue;
public AzureQueueBlockIpHostedService(
ILogger<AzureQueueBlockIpHostedService> logger,
IOptions<AdminSettings> adminSettings,
GlobalSettings globalSettings)
: base(logger, adminSettings, globalSettings)
{ }
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
var storageAccount = CloudStorageAccount.Parse(_globalSettings.Storage.ConnectionString);
var queueClient = storageAccount.CreateCloudQueueClient();
_blockQueue = queueClient.GetQueueReference("blockip");
_unblockQueue = queueClient.GetQueueReference("unblockip");
while(!cancellationToken.IsCancellationRequested)
{
var blockMessages = await _blockQueue.GetMessagesAsync(32, TimeSpan.FromSeconds(15),
null, null, cancellationToken);
if(blockMessages.Any())
{
foreach(var message in blockMessages)
{
try
{
await BlockIpAsync(message.AsString, cancellationToken);
}
catch(Exception e)
{
_logger.LogError(e, "Failed to block IP.");
}
await _blockQueue.DeleteMessageAsync(message);
}
}
var unblockMessages = await _unblockQueue.GetMessagesAsync(32, TimeSpan.FromSeconds(15),
null, null, cancellationToken);
if(unblockMessages.Any())
{
foreach(var message in unblockMessages)
{
try
{
await UnblockIpAsync(message.AsString, cancellationToken);
}
catch(Exception e)
{
_logger.LogError(e, "Failed to unblock IP.");
}
await _unblockQueue.DeleteMessageAsync(message);
}
}
await Task.Delay(TimeSpan.FromSeconds(15));
}
}
}
}