1
0
mirror of https://github.com/bitwarden/server.git synced 2025-02-16 01:51:21 +01:00

throttle block messages and base64 encode them

This commit is contained in:
Kyle Spearrin 2019-03-11 23:31:45 -04:00
parent 2bdcff56b6
commit 14fd7e2801
2 changed files with 33 additions and 4 deletions

View File

@ -2,6 +2,7 @@
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using System;
using Bit.Core.Utilities;
namespace Bit.Core.Services
{
@ -10,6 +11,7 @@ namespace Bit.Core.Services
private readonly CloudQueue _blockIpQueue;
private readonly CloudQueue _unblockIpQueue;
private bool _didInit = false;
private Tuple<string, bool, DateTime> _lastBlock;
public AzureQueueBlockIpService(
GlobalSettings globalSettings)
@ -24,13 +26,20 @@ namespace Bit.Core.Services
public async Task BlockIpAsync(string ipAddress, bool permanentBlock)
{
await InitAsync();
var blockMessage = new CloudQueueMessage(ipAddress);
await _blockIpQueue.AddMessageAsync(blockMessage);
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);
var message = new CloudQueueMessage(CoreHelpers.Base64UrlEncodeString(ipAddress));
await _blockIpQueue.AddMessageAsync(message);
if(!permanentBlock)
{
var unblockMessage = new CloudQueueMessage(ipAddress);
await _unblockIpQueue.AddMessageAsync(unblockMessage, null, new TimeSpan(0, 15, 0), null, null);
await _unblockIpQueue.AddMessageAsync(message, null, new TimeSpan(0, 15, 0), null, null);
}
}

View File

@ -317,6 +317,26 @@ namespace Bit.Core.Utilities
!normalizedSetting.Equals("replace");
}
public static string Base64EncodeString(string input)
{
return Convert.ToBase64String(Encoding.UTF8.GetBytes(input));
}
public static string Base64DecodeString(string input)
{
return Encoding.UTF8.GetString(Convert.FromBase64String(input));
}
public static string Base64UrlEncodeString(string input)
{
return Base64UrlEncode(Encoding.UTF8.GetBytes(input));
}
public static string Base64UrlDecodeString(string input)
{
return Encoding.UTF8.GetString(Base64UrlDecode(input));
}
public static string Base64UrlEncode(byte[] input)
{
var output = Convert.ToBase64String(input)