Add endpoints to manage IPC bans (#2715)

* Add endpoints to manage IPC bans

* Remove debug code

* Misc.

* Simplify unban logic

* Add explanatory comment to new string resource
This commit is contained in:
Sebastian Göls
2022-10-10 19:28:07 +02:00
committed by GitHub
parent 406a5f1fd1
commit 321e02c0ff
4 changed files with 105 additions and 1 deletions

View File

@@ -24,6 +24,7 @@ using MvcNewtonsoftJsonOptions = Microsoft.AspNetCore.Mvc.MvcJsonOptions;
#endif
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
@@ -88,7 +89,19 @@ internal sealed class ApiAuthenticationMiddleware {
await context.Response.WriteJsonAsync(new GenericResponse<StatusCodeResponse>(false, statusCodeResponse), jsonOptions.Value.SerializerSettings).ConfigureAwait(false);
}
private static void ClearFailedAuthorizations(object? state = null) => FailedAuthorizations.Clear();
internal static void ClearFailedAuthorizations(object? state = null) => FailedAuthorizations.Clear();
internal static HashSet<IPAddress> GetCurrentlyBannedIPs() => FailedAuthorizations.Where(static kv => kv.Value >= MaxFailedAuthorizationAttempts).Select(static kv => kv.Key).ToHashSet();
internal static bool UnbanIP(IPAddress ipAddress) {
ArgumentNullException.ThrowIfNull(ipAddress);
if (!FailedAuthorizations.TryGetValue(ipAddress, out byte attempts) || (attempts < MaxFailedAuthorizationAttempts)) {
return false;
}
return FailedAuthorizations.TryRemove(ipAddress, out _);
}
private async Task<(HttpStatusCode StatusCode, bool Permanent)> GetAuthenticationStatus(HttpContext context) {
ArgumentNullException.ThrowIfNull(context);