diff --git a/MareSynchronosServer/MareSynchronosServer/Throttling/SignalRLimitFilter.cs b/MareSynchronosServer/MareSynchronosServer/Throttling/SignalRLimitFilter.cs index 59cb7b6..1567555 100644 --- a/MareSynchronosServer/MareSynchronosServer/Throttling/SignalRLimitFilter.cs +++ b/MareSynchronosServer/MareSynchronosServer/Throttling/SignalRLimitFilter.cs @@ -1,6 +1,7 @@ using AspNetCoreRateLimit; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using System; using System.Threading.Tasks; @@ -10,12 +11,14 @@ public class SignalRLimitFilter : IHubFilter { private readonly IRateLimitProcessor _processor; private readonly IHttpContextAccessor accessor; + private readonly ILogger logger; public SignalRLimitFilter( - IOptions options, IProcessingStrategy processing, IIpPolicyStore policyStore, IHttpContextAccessor accessor) + IOptions options, IProcessingStrategy processing, IIpPolicyStore policyStore, IHttpContextAccessor accessor, ILogger logger) { _processor = new IpRateLimitProcessor(options?.Value, policyStore, processing); this.accessor = accessor; + this.logger = logger; } public async ValueTask InvokeMethodAsync( @@ -32,15 +35,15 @@ public class SignalRLimitFilter : IHubFilter foreach (var rule in await _processor.GetMatchingRulesAsync(client)) { var counter = await _processor.ProcessRequestAsync(client, rule); - Console.WriteLine("time: {0}, count: {1}", counter.Timestamp, counter.Count); if (counter.Count > rule.Limit) { var retry = counter.Timestamp.RetryAfterFrom(rule); + logger.LogWarning($"Method rate limit triggered from {ip}: {invocationContext.HubMethodName}"); throw new HubException($"call limit {retry}"); } } - Console.WriteLine($"Calling hub method '{invocationContext.HubMethodName}'"); + //Console.WriteLine($"Calling hub method '{invocationContext.HubMethodName}'"); return await next(invocationContext); } @@ -57,11 +60,11 @@ public class SignalRLimitFilter : IHubFilter foreach (var rule in await _processor.GetMatchingRulesAsync(client)) { var counter = await _processor.ProcessRequestAsync(client, rule); - Console.WriteLine("time: {0}, count: {1}", counter.Timestamp, counter.Count); if (counter.Count > rule.Limit) { var retry = counter.Timestamp.RetryAfterFrom(rule); - throw new HubException($"rate limit {retry}"); + logger.LogWarning($"Connection rate limit triggered from {ip}"); + throw new HubException($"Connection rate limit {retry}"); } }