From 50ff75c7ab78e14aa1eeced5afb9bb061225fdd3 Mon Sep 17 00:00:00 2001 From: Stanley Dimant Date: Fri, 12 Aug 2022 10:57:03 +0200 Subject: [PATCH] cancel and dispose cts in ban auth --- .../SecretKeyAuthenticationHandler.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthenticationHandler.cs b/MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthenticationHandler.cs index 7a9ec8e..d5a046d 100644 --- a/MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthenticationHandler.cs +++ b/MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthenticationHandler.cs @@ -18,12 +18,23 @@ using Microsoft.Extensions.Options; namespace MareSynchronosServer.Authentication { - public class FailedAuthorization + public class FailedAuthorization : IDisposable { private int failedAttempts = 1; public int FailedAttempts => failedAttempts; public Task ResetTask { get; set; } public CancellationTokenSource ResetCts { get; set; } = new(); + + public void Dispose() + { + try + { + ResetCts?.Cancel(); + ResetCts?.Dispose(); + } + catch { } + } + public void IncreaseFailedAttempts() { Interlocked.Increment(ref failedAttempts); @@ -82,13 +93,15 @@ namespace MareSynchronosServer.Authentication if (failedAuth.FailedAttempts > failedAttemptsForTempBan) { failedAuth.ResetCts.Cancel(); + failedAuth.ResetCts.Dispose(); failedAuth.ResetCts = new CancellationTokenSource(); var token = failedAuth.ResetCts.Token; failedAuth.ResetTask = Task.Run(async () => { await Task.Delay(TimeSpan.FromMinutes(tempBanMinutes), token); if (token.IsCancellationRequested) return; - FailedAuthorizations.Remove(ip, out _); + FailedAuthorizations.Remove(ip, out var fauth); + fauth.Dispose(); }, token); Logger.LogWarning("TempBan " + ip + " for authorization spam"); return AuthenticateResult.Fail("Failed Authorization");