mirror of
https://github.com/Caraxi/mare.server.git
synced 2025-12-18 15:04:16 +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
|
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");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue