mirror of
https://github.com/Caraxi/mare.server.git
synced 2025-12-13 15:24:14 +01:00
cancel and dispose cts in ban auth
This commit is contained in:
parent
c3fc83e819
commit
50ff75c7ab
1 changed files with 15 additions and 2 deletions
|
|
@ -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");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue