diff --git a/MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthReply.cs b/MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthReply.cs index 5ca2a01..7e76ef4 100644 --- a/MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthReply.cs +++ b/MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthReply.cs @@ -1,3 +1,3 @@ namespace MareSynchronosServer.Authentication; -public record SecretKeyAuthReply(bool Success, string Uid, bool TempBan, bool Permaban); +public record SecretKeyAuthReply(bool Success, string Uid, string Alias, bool TempBan, bool Permaban); diff --git a/MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthenticatorService.cs b/MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthenticatorService.cs index b9a0144..fda0b45 100644 --- a/MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthenticatorService.cs +++ b/MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthenticatorService.cs @@ -10,30 +10,24 @@ namespace MareSynchronosServer.Authentication; public class SecretKeyAuthenticatorService { private readonly MareMetrics _metrics; - private readonly IServiceScopeFactory _serviceScopeFactory; + private readonly IDbContextFactory _dbContextFactory; private readonly IConfigurationService _configurationService; private readonly ILogger _logger; - private readonly ConcurrentDictionary _cachedPositiveResponses = new(StringComparer.Ordinal); private readonly ConcurrentDictionary _failedAuthorizations = new(StringComparer.Ordinal); - public SecretKeyAuthenticatorService(MareMetrics metrics, IServiceScopeFactory serviceScopeFactory, IConfigurationService configuration, ILogger logger) + public SecretKeyAuthenticatorService(MareMetrics metrics, IDbContextFactory dbContextFactory, + IConfigurationService configuration, ILogger logger) { _logger = logger; _configurationService = configuration; _metrics = metrics; - _serviceScopeFactory = serviceScopeFactory; + _dbContextFactory = dbContextFactory; } public async Task AuthorizeAsync(string ip, string hashedSecretKey) { _metrics.IncCounter(MetricsAPI.CounterAuthenticationRequests); - if (_cachedPositiveResponses.TryGetValue(hashedSecretKey, out var cachedPositiveResponse)) - { - _metrics.IncCounter(MetricsAPI.CounterAuthenticationCacheHits); - return cachedPositiveResponse; - } - if (_failedAuthorizations.TryGetValue(ip, out var existingFailedAuthorization) && existingFailedAuthorization.FailedAttempts > _configurationService.GetValueOrDefault(nameof(MareConfigurationAuthBase.FailedAuthForTempBan), 5)) { @@ -50,26 +44,18 @@ public class SecretKeyAuthenticatorService _failedAuthorizations.Remove(ip, out _); }); } - return new(Success: false, Uid: null, TempBan: true, Permaban: false); + return new(Success: false, Uid: null, TempBan: true, Alias: null, Permaban: false); } - using var scope = _serviceScopeFactory.CreateScope(); - using var context = scope.ServiceProvider.GetService(); - var authReply = await context.Auth.AsNoTracking().SingleOrDefaultAsync(u => u.HashedKey == hashedSecretKey).ConfigureAwait(false); + using var context = await _dbContextFactory.CreateDbContextAsync().ConfigureAwait(false); + var authReply = await context.Auth.Include(a => a.User).AsNoTracking() + .SingleOrDefaultAsync(u => u.HashedKey == hashedSecretKey).ConfigureAwait(false); - SecretKeyAuthReply reply = new(authReply != null, authReply?.UserUID, false, authReply?.IsBanned ?? false); + SecretKeyAuthReply reply = new(authReply != null, authReply?.UserUID, authReply?.User?.Alias ?? string.Empty, TempBan: false, authReply?.IsBanned ?? false); if (reply.Success) { _metrics.IncCounter(MetricsAPI.CounterAuthenticationSuccesses); - - _cachedPositiveResponses[hashedSecretKey] = reply; - _ = Task.Run(async () => - { - await Task.Delay(TimeSpan.FromMinutes(5)).ConfigureAwait(false); - _cachedPositiveResponses.TryRemove(hashedSecretKey, out _); - }); - } else { @@ -97,6 +83,6 @@ public class SecretKeyAuthenticatorService } } - return new(Success: false, Uid: null, TempBan: false, Permaban: false); + return new(Success: false, Uid: null, Alias: null, TempBan: false, Permaban: false); } } diff --git a/MareSynchronosServer/MareSynchronosServer/Controllers/JwtController.cs b/MareSynchronosServer/MareSynchronosServer/Controllers/JwtController.cs index c0412ab..65d7b17 100644 --- a/MareSynchronosServer/MareSynchronosServer/Controllers/JwtController.cs +++ b/MareSynchronosServer/MareSynchronosServer/Controllers/JwtController.cs @@ -69,7 +69,7 @@ public class JwtController : Controller } if (!authResult.Success && !authResult.TempBan) return Unauthorized("The provided secret key is invalid. Verify your accounts existence and/or recover the secret key."); - if (!authResult.Success && authResult.TempBan) return Unauthorized("You are temporarily banned. Try connecting again in 5 minutes."); + if (!authResult.Success && authResult.TempBan) return Unauthorized("Due to an excessive amount of failed authentication attempts you are temporarily banned. Check your Secret Key configuration and try connecting again in 5 minutes."); if (authResult.Permaban) { if (!_mareDbContext.BannedUsers.Any(c => c.CharacterIdentification == charaIdent)) @@ -115,6 +115,7 @@ public class JwtController : Controller { new Claim(MareClaimTypes.Uid, authResult.Uid), new Claim(MareClaimTypes.CharaIdent, charaIdent), + new Claim(MareClaimTypes.Alias, authResult.Alias), new Claim(MareClaimTypes.Continent, await _geoIPProvider.GetCountryFromIP(_accessor)), }); diff --git a/MareSynchronosServer/MareSynchronosShared/Utils/MareClaimTypes.cs b/MareSynchronosServer/MareSynchronosShared/Utils/MareClaimTypes.cs index dd2e747..7641e9e 100644 --- a/MareSynchronosServer/MareSynchronosShared/Utils/MareClaimTypes.cs +++ b/MareSynchronosServer/MareSynchronosShared/Utils/MareClaimTypes.cs @@ -3,6 +3,7 @@ public static class MareClaimTypes { public const string Uid = "uid"; + public const string Alias = "alias"; public const string CharaIdent = "character_identification"; public const string Internal = "internal"; public const string Continent = "continent"; diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Controllers/ControllerBase.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Controllers/ControllerBase.cs index 630a741..14a12a1 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Controllers/ControllerBase.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Controllers/ControllerBase.cs @@ -14,4 +14,5 @@ public class ControllerBase : Controller protected string MareUser => HttpContext.User.Claims.First(f => string.Equals(f.Type, MareClaimTypes.Uid, StringComparison.Ordinal)).Value; protected string Continent => HttpContext.User.Claims.FirstOrDefault(f => string.Equals(f.Type, MareClaimTypes.Continent, StringComparison.Ordinal))?.Value ?? "*"; + protected bool IsPriority => !string.IsNullOrEmpty(HttpContext.User.Claims.FirstOrDefault(f => string.Equals(f.Type, MareClaimTypes.Alias, StringComparison.Ordinal))?.Value ?? string.Empty); } diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Controllers/RequestController.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Controllers/RequestController.cs index e22e08c..0fab291 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Controllers/RequestController.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Controllers/RequestController.cs @@ -1,5 +1,4 @@ using MareSynchronos.API.Routes; -using MareSynchronosShared.Data; using MareSynchronosStaticFilesServer.Services; using Microsoft.AspNetCore.Mvc; @@ -10,14 +9,12 @@ public class RequestController : ControllerBase { private readonly CachedFileProvider _cachedFileProvider; private readonly RequestQueueService _requestQueue; - private readonly MareDbContext _mareDbContext; private static readonly SemaphoreSlim _parallelRequestSemaphore = new(500); - public RequestController(ILogger logger, CachedFileProvider cachedFileProvider, RequestQueueService requestQueue, MareDbContext mareDbContext) : base(logger) + public RequestController(ILogger logger, CachedFileProvider cachedFileProvider, RequestQueueService requestQueue) : base(logger) { _cachedFileProvider = cachedFileProvider; _requestQueue = requestQueue; - _mareDbContext = mareDbContext; } [HttpGet] @@ -27,7 +24,7 @@ public class RequestController : ControllerBase try { await _parallelRequestSemaphore.WaitAsync(HttpContext.RequestAborted); - _requestQueue.RemoveFromQueue(requestId, MareUser); + _requestQueue.RemoveFromQueue(requestId, MareUser, IsPriority); return Ok(); } catch (OperationCanceledException) { return BadRequest(); } @@ -51,7 +48,7 @@ public class RequestController : ControllerBase } Guid g = Guid.NewGuid(); - await _requestQueue.EnqueueUser(new(g, MareUser, files.ToList()), _mareDbContext); + _requestQueue.EnqueueUser(new(g, MareUser, files.ToList()), IsPriority); return Ok(g); } @@ -68,8 +65,8 @@ public class RequestController : ControllerBase { try { - if (!await _requestQueue.StillEnqueued(requestId, MareUser, _mareDbContext)) - await _requestQueue.EnqueueUser(new(requestId, MareUser, files.ToList()), _mareDbContext); + if (!_requestQueue.StillEnqueued(requestId, MareUser, IsPriority)) + _requestQueue.EnqueueUser(new(requestId, MareUser, files.ToList()), IsPriority); return Ok(); } catch (OperationCanceledException) { return BadRequest(); } diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/RequestQueueService.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/RequestQueueService.cs index f271591..b1a2ad6 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/RequestQueueService.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/RequestQueueService.cs @@ -5,9 +5,6 @@ using Microsoft.AspNetCore.SignalR; using System.Collections.Concurrent; using System.Timers; using MareSynchronos.API.SignalR; -using MareSynchronosShared.Data; -using Microsoft.EntityFrameworkCore; -using System.Linq; namespace MareSynchronosStaticFilesServer.Services; @@ -25,7 +22,6 @@ public class RequestQueueService : IHostedService private readonly ConcurrentDictionary _queueRemoval = new(); private readonly SemaphoreSlim _queueSemaphore = new(1); private readonly UserQueueEntry[] _userQueueRequests; - private readonly ConcurrentDictionary _priorityCache = new(StringComparer.Ordinal); private int _queueLimitForReset; private readonly int _queueReleaseSeconds; private System.Timers.Timer _queueTimer; @@ -49,50 +45,11 @@ public class RequestQueueService : IHostedService req.MarkActive(); } - private async Task IsHighPriority(string uid, MareDbContext mareDbContext) - { - return false; - if (!_priorityCache.TryGetValue(uid, out PriorityEntry entry) || entry.LastChecked.Add(TimeSpan.FromHours(6)) < DateTime.UtcNow) - { - var user = await mareDbContext.Users.FirstOrDefaultAsync(u => u.UID == uid).ConfigureAwait(false); - entry = new(user != null && !string.IsNullOrEmpty(user.Alias), DateTime.UtcNow); - _priorityCache[uid] = entry; - } - - return entry.IsHighPriority; - } - - public async Task EnqueueUser(UserRequest request, MareDbContext mareDbContext) + public void EnqueueUser(UserRequest request, bool isPriority) { _logger.LogDebug("Enqueueing req {guid} from {user} for {file}", request.RequestId, request.User, string.Join(", ", request.FileIds)); - bool isPriorityQueue = await IsHighPriority(request.User, mareDbContext).ConfigureAwait(false); - - if (_queueProcessingSemaphore.CurrentCount == 0) - { - if (isPriorityQueue) _priorityQueue.Enqueue(request); - else _queue.Enqueue(request); - return; - } - - try - { - await _queueSemaphore.WaitAsync().ConfigureAwait(false); - if (isPriorityQueue) _priorityQueue.Enqueue(request); - else _queue.Enqueue(request); - - return; - } - catch (Exception ex) - { - _logger.LogError(ex, "Error during EnqueueUser"); - } - finally - { - _queueSemaphore.Release(); - } - - throw new Exception("Error during EnqueueUser"); + GetQueue(isPriority).Enqueue(request); } public void FinishRequest(Guid request) @@ -117,9 +74,10 @@ public class RequestQueueService : IHostedService return userQueueRequest != null && userRequest != null && userQueueRequest.ExpirationDate > DateTime.UtcNow; } - public void RemoveFromQueue(Guid requestId, string user) + public void RemoveFromQueue(Guid requestId, string user, bool isPriority) { - if (!_queue.Any(f => f.RequestId == requestId && string.Equals(f.User, user, StringComparison.Ordinal))) + var existingRequest = GetQueue(isPriority).FirstOrDefault(f => f.RequestId == requestId && string.Equals(f.User, user, StringComparison.Ordinal)); + if (existingRequest == null) { var activeSlot = _userQueueRequests.FirstOrDefault(r => r != null && string.Equals(r.UserRequest.User, user, StringComparison.Ordinal) && r.UserRequest.RequestId == requestId); if (activeSlot != null) @@ -145,14 +103,11 @@ public class RequestQueueService : IHostedService return Task.CompletedTask; } - public async Task StillEnqueued(Guid request, string user, MareDbContext mareDbContext) + private ConcurrentQueue GetQueue(bool isPriority) => isPriority ? _priorityQueue : _queue; + + public bool StillEnqueued(Guid request, string user, bool isPriority) { - bool isPriorityQueue = await IsHighPriority(user, mareDbContext).ConfigureAwait(false); - if (isPriorityQueue) - { - return _priorityQueue.Any(c => c.RequestId == request && string.Equals(c.User, user, StringComparison.Ordinal)); - } - return _queue.Any(c => c.RequestId == request && string.Equals(c.User, user, StringComparison.Ordinal)); + return GetQueue(isPriority).Any(c => c.RequestId == request && string.Equals(c.User, user, StringComparison.Ordinal)); } public Task StopAsync(CancellationToken cancellationToken) @@ -182,11 +137,7 @@ public class RequestQueueService : IHostedService return; } - Parallel.For(0, _userQueueRequests.Length, new ParallelOptions() - { - MaxDegreeOfParallelism = 10, - }, - async (i) => + for (int i = 0; i < _userQueueRequests.Length; i++) { try { @@ -246,7 +197,7 @@ public class RequestQueueService : IHostedService { _logger.LogWarning(ex, "Error during inside queue processing"); } - }); + } } catch (Exception ex) {