mirror of
https://github.com/bitwarden/server.git
synced 2025-07-07 02:52:50 -05:00
throttle block messages and base64 encode them
This commit is contained in:
@ -2,6 +2,7 @@
|
|||||||
using Microsoft.WindowsAzure.Storage;
|
using Microsoft.WindowsAzure.Storage;
|
||||||
using Microsoft.WindowsAzure.Storage.Queue;
|
using Microsoft.WindowsAzure.Storage.Queue;
|
||||||
using System;
|
using System;
|
||||||
|
using Bit.Core.Utilities;
|
||||||
|
|
||||||
namespace Bit.Core.Services
|
namespace Bit.Core.Services
|
||||||
{
|
{
|
||||||
@ -10,6 +11,7 @@ namespace Bit.Core.Services
|
|||||||
private readonly CloudQueue _blockIpQueue;
|
private readonly CloudQueue _blockIpQueue;
|
||||||
private readonly CloudQueue _unblockIpQueue;
|
private readonly CloudQueue _unblockIpQueue;
|
||||||
private bool _didInit = false;
|
private bool _didInit = false;
|
||||||
|
private Tuple<string, bool, DateTime> _lastBlock;
|
||||||
|
|
||||||
public AzureQueueBlockIpService(
|
public AzureQueueBlockIpService(
|
||||||
GlobalSettings globalSettings)
|
GlobalSettings globalSettings)
|
||||||
@ -24,13 +26,20 @@ namespace Bit.Core.Services
|
|||||||
public async Task BlockIpAsync(string ipAddress, bool permanentBlock)
|
public async Task BlockIpAsync(string ipAddress, bool permanentBlock)
|
||||||
{
|
{
|
||||||
await InitAsync();
|
await InitAsync();
|
||||||
var blockMessage = new CloudQueueMessage(ipAddress);
|
var now = DateTime.UtcNow;
|
||||||
await _blockIpQueue.AddMessageAsync(blockMessage);
|
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)
|
if(!permanentBlock)
|
||||||
{
|
{
|
||||||
var unblockMessage = new CloudQueueMessage(ipAddress);
|
await _unblockIpQueue.AddMessageAsync(message, null, new TimeSpan(0, 15, 0), null, null);
|
||||||
await _unblockIpQueue.AddMessageAsync(unblockMessage, null, new TimeSpan(0, 15, 0), null, null);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -317,6 +317,26 @@ namespace Bit.Core.Utilities
|
|||||||
!normalizedSetting.Equals("replace");
|
!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)
|
public static string Base64UrlEncode(byte[] input)
|
||||||
{
|
{
|
||||||
var output = Convert.ToBase64String(input)
|
var output = Convert.ToBase64String(input)
|
||||||
|
Reference in New Issue
Block a user