cancel and dispose cts in ban auth
This commit is contained in:
@@ -18,12 +18,23 @@ using Microsoft.Extensions.Options;
|
|||||||
|
|
||||||
namespace MareSynchronosServer.Authentication
|
namespace MareSynchronosServer.Authentication
|
||||||
{
|
{
|
||||||
public class FailedAuthorization
|
public class FailedAuthorization : IDisposable
|
||||||
{
|
{
|
||||||
private int failedAttempts = 1;
|
private int failedAttempts = 1;
|
||||||
public int FailedAttempts => failedAttempts;
|
public int FailedAttempts => failedAttempts;
|
||||||
public Task ResetTask { get; set; }
|
public Task ResetTask { get; set; }
|
||||||
public CancellationTokenSource ResetCts { get; set; } = new();
|
public CancellationTokenSource ResetCts { get; set; } = new();
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ResetCts?.Cancel();
|
||||||
|
ResetCts?.Dispose();
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
|
|
||||||
public void IncreaseFailedAttempts()
|
public void IncreaseFailedAttempts()
|
||||||
{
|
{
|
||||||
Interlocked.Increment(ref failedAttempts);
|
Interlocked.Increment(ref failedAttempts);
|
||||||
@@ -82,13 +93,15 @@ namespace MareSynchronosServer.Authentication
|
|||||||
if (failedAuth.FailedAttempts > failedAttemptsForTempBan)
|
if (failedAuth.FailedAttempts > failedAttemptsForTempBan)
|
||||||
{
|
{
|
||||||
failedAuth.ResetCts.Cancel();
|
failedAuth.ResetCts.Cancel();
|
||||||
|
failedAuth.ResetCts.Dispose();
|
||||||
failedAuth.ResetCts = new CancellationTokenSource();
|
failedAuth.ResetCts = new CancellationTokenSource();
|
||||||
var token = failedAuth.ResetCts.Token;
|
var token = failedAuth.ResetCts.Token;
|
||||||
failedAuth.ResetTask = Task.Run(async () =>
|
failedAuth.ResetTask = Task.Run(async () =>
|
||||||
{
|
{
|
||||||
await Task.Delay(TimeSpan.FromMinutes(tempBanMinutes), token);
|
await Task.Delay(TimeSpan.FromMinutes(tempBanMinutes), token);
|
||||||
if (token.IsCancellationRequested) return;
|
if (token.IsCancellationRequested) return;
|
||||||
FailedAuthorizations.Remove(ip, out _);
|
FailedAuthorizations.Remove(ip, out var fauth);
|
||||||
|
fauth.Dispose();
|
||||||
}, token);
|
}, token);
|
||||||
Logger.LogWarning("TempBan " + ip + " for authorization spam");
|
Logger.LogWarning("TempBan " + ip + " for authorization spam");
|
||||||
return AuthenticateResult.Fail("Failed Authorization");
|
return AuthenticateResult.Fail("Failed Authorization");
|
||||||
|
|||||||
Reference in New Issue
Block a user