From 8164d737bf06fa200080022448d32119fda161eb Mon Sep 17 00:00:00 2001 From: Stanley Dimant Date: Thu, 20 Oct 2022 23:08:46 +0200 Subject: [PATCH] whitelist fixes, grpc stream fixes --- .../MareSynchronosServer/Startup.cs | 12 +++++++++--- .../SecretKeyAuthenticationHandler.cs | 8 +++++++- .../Identity/IdentityService.cs | 2 +- .../MareSynchronosServices/Startup.cs | 2 +- .../Services/GrpcBaseService.cs | 17 +++++++++++------ 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/MareSynchronosServer/MareSynchronosServer/Startup.cs b/MareSynchronosServer/MareSynchronosServer/Startup.cs index fa23961..56d810f 100644 --- a/MareSynchronosServer/MareSynchronosServer/Startup.cs +++ b/MareSynchronosServer/MareSynchronosServer/Startup.cs @@ -42,13 +42,13 @@ public class Startup services.Configure(Configuration.GetSection("IpRateLimiting")); services.Configure(Configuration.GetSection("IpRateLimitPolicies")); + services.AddTransient(_ => Configuration); services.AddMemoryCache(); services.AddInMemoryRateLimiting(); services.AddSingleton(); services.AddSingleton(); - services.AddTransient(_ => Configuration); var mareConfig = Configuration.GetRequiredSection("MareSynchronos"); @@ -65,6 +65,12 @@ public class Startup } }; + var noRetryConfig = new MethodConfig + { + Names = { MethodName.Default }, + RetryPolicy = null + }; + services.AddSingleton(new MareMetrics(new List { MetricsAPI.CounterInitializedConnections, @@ -89,7 +95,7 @@ public class Startup c.Address = new Uri(mareConfig.GetValue("ServiceAddress")); }).ConfigureChannel(c => { - c.ServiceConfig = new ServiceConfig { MethodConfigs = { defaultMethodConfig } }; + c.ServiceConfig = new ServiceConfig { MethodConfigs = { noRetryConfig } }; c.HttpHandler = new SocketsHttpHandler() { EnableMultipleHttp2Connections = true @@ -107,7 +113,7 @@ public class Startup c.Address = new Uri(mareConfig.GetValue("ServiceAddress")); }).ConfigureChannel(c => { - c.ServiceConfig = new ServiceConfig { MethodConfigs = { defaultMethodConfig } }; + c.ServiceConfig = new ServiceConfig { MethodConfigs = { noRetryConfig } }; c.HttpHandler = new SocketsHttpHandler() { EnableMultipleHttp2Connections = true diff --git a/MareSynchronosServer/MareSynchronosServices/Authentication/SecretKeyAuthenticationHandler.cs b/MareSynchronosServer/MareSynchronosServices/Authentication/SecretKeyAuthenticationHandler.cs index 37fbba5..6caa66c 100644 --- a/MareSynchronosServer/MareSynchronosServices/Authentication/SecretKeyAuthenticationHandler.cs +++ b/MareSynchronosServer/MareSynchronosServices/Authentication/SecretKeyAuthenticationHandler.cs @@ -176,7 +176,13 @@ public class SecretKeyAuthenticationHandler this.metrics = metrics; var config = configuration.GetRequiredSection("MareSynchronos"); _failedAttemptsForTempBan = config.GetValue("FailedAuthForTempBan", 5); + logger.LogInformation("FailedAuthForTempBan: {num}", _failedAttemptsForTempBan); _tempBanMinutes = config.GetValue("TempBanDurationInMinutes", 30); - _whitelistedIps = config.GetValue>("WhitelistedIps", new List()); + logger.LogInformation("TempBanMinutes: {num}", _tempBanMinutes); + _whitelistedIps = config.GetSection("WhitelistedIps").Get>(); + foreach (var ip in _whitelistedIps) + { + logger.LogInformation("Whitelisted IP: " + ip); + } } } \ No newline at end of file diff --git a/MareSynchronosServer/MareSynchronosServices/Identity/IdentityService.cs b/MareSynchronosServer/MareSynchronosServices/Identity/IdentityService.cs index 08a0d07..1cf1f93 100644 --- a/MareSynchronosServer/MareSynchronosServices/Identity/IdentityService.cs +++ b/MareSynchronosServer/MareSynchronosServices/Identity/IdentityService.cs @@ -72,7 +72,7 @@ internal class IdentityService : IdentificationService.IdentificationServiceBase _handler.RegisterServerForQueue(server.ServerId); _logger.LogInformation("Registered Server " + server.ServerId + " input stream"); - while (await requestStream.MoveNext().ConfigureAwait(false)) + while (await requestStream.MoveNext(context.CancellationToken).ConfigureAwait(false)) { var cur = requestStream.Current.IdentChange; if (cur == null) throw new System.Exception("Expected client ident change"); diff --git a/MareSynchronosServer/MareSynchronosServices/Startup.cs b/MareSynchronosServer/MareSynchronosServices/Startup.cs index 524abcc..328703a 100644 --- a/MareSynchronosServer/MareSynchronosServices/Startup.cs +++ b/MareSynchronosServer/MareSynchronosServices/Startup.cs @@ -44,11 +44,11 @@ public class Startup MetricsAPI.GaugeUsersRegistered })); + services.AddTransient(_ => Configuration); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - services.AddTransient(_ => Configuration); services.AddHostedService(provider => provider.GetService()); services.AddHostedService(); services.AddGrpc(); diff --git a/MareSynchronosServer/MareSynchronosShared/Services/GrpcBaseService.cs b/MareSynchronosServer/MareSynchronosShared/Services/GrpcBaseService.cs index 2c95ef0..9038eb7 100644 --- a/MareSynchronosServer/MareSynchronosShared/Services/GrpcBaseService.cs +++ b/MareSynchronosServer/MareSynchronosShared/Services/GrpcBaseService.cs @@ -39,15 +39,16 @@ public abstract class GrpcBaseService : IHostedService, IDisposable private async Task RestartStreams() { - _streamCts?.Cancel(); - _streamCts?.Dispose(); - _streamCts = new(); if (!GrpcIsFaulty) { + _streamCts?.Cancel(); + _streamCts?.Dispose(); + _streamCts = new(); + var token = _streamCts.Token; try { await PreStartStream().ConfigureAwait(false); - await StartStream(_streamCts.Token).ConfigureAwait(false); + await StartStream(token).ConfigureAwait(false); await PostStartStream().ConfigureAwait(false); } catch @@ -74,7 +75,11 @@ public abstract class GrpcBaseService : IHostedService, IDisposable { await CheckFaultStateAndRestore().ConfigureAwait(false); } - catch { SetGrpcFaulty(); } + catch + { + SetGrpcFaulty(); + await Task.Delay(5000, ct).ConfigureAwait(false); + } await Task.Delay(250).ConfigureAwait(false); } } @@ -83,10 +88,10 @@ public abstract class GrpcBaseService : IHostedService, IDisposable { if (GrpcIsFaulty) { + GrpcIsFaulty = false; await RestartStreams().ConfigureAwait(false); await OnGrpcRestore().ConfigureAwait(false); _logger.LogInformation("GRPC connection is restored"); - GrpcIsFaulty = false; } }