whitelist fixes, grpc stream fixes

This commit is contained in:
Stanley Dimant
2022-10-20 23:08:46 +02:00
parent e3e07fe117
commit 8164d737bf
5 changed files with 29 additions and 12 deletions

View File

@@ -42,13 +42,13 @@ public class Startup
services.Configure<IpRateLimitOptions>(Configuration.GetSection("IpRateLimiting")); services.Configure<IpRateLimitOptions>(Configuration.GetSection("IpRateLimiting"));
services.Configure<IpRateLimitPolicies>(Configuration.GetSection("IpRateLimitPolicies")); services.Configure<IpRateLimitPolicies>(Configuration.GetSection("IpRateLimitPolicies"));
services.AddTransient(_ => Configuration);
services.AddMemoryCache(); services.AddMemoryCache();
services.AddInMemoryRateLimiting(); services.AddInMemoryRateLimiting();
services.AddSingleton<SystemInfoService, SystemInfoService>(); services.AddSingleton<SystemInfoService, SystemInfoService>();
services.AddSingleton<IUserIdProvider, IdBasedUserIdProvider>(); services.AddSingleton<IUserIdProvider, IdBasedUserIdProvider>();
services.AddTransient(_ => Configuration);
var mareConfig = Configuration.GetRequiredSection("MareSynchronos"); 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<string> services.AddSingleton(new MareMetrics(new List<string>
{ {
MetricsAPI.CounterInitializedConnections, MetricsAPI.CounterInitializedConnections,
@@ -89,7 +95,7 @@ public class Startup
c.Address = new Uri(mareConfig.GetValue<string>("ServiceAddress")); c.Address = new Uri(mareConfig.GetValue<string>("ServiceAddress"));
}).ConfigureChannel(c => }).ConfigureChannel(c =>
{ {
c.ServiceConfig = new ServiceConfig { MethodConfigs = { defaultMethodConfig } }; c.ServiceConfig = new ServiceConfig { MethodConfigs = { noRetryConfig } };
c.HttpHandler = new SocketsHttpHandler() c.HttpHandler = new SocketsHttpHandler()
{ {
EnableMultipleHttp2Connections = true EnableMultipleHttp2Connections = true
@@ -107,7 +113,7 @@ public class Startup
c.Address = new Uri(mareConfig.GetValue<string>("ServiceAddress")); c.Address = new Uri(mareConfig.GetValue<string>("ServiceAddress"));
}).ConfigureChannel(c => }).ConfigureChannel(c =>
{ {
c.ServiceConfig = new ServiceConfig { MethodConfigs = { defaultMethodConfig } }; c.ServiceConfig = new ServiceConfig { MethodConfigs = { noRetryConfig } };
c.HttpHandler = new SocketsHttpHandler() c.HttpHandler = new SocketsHttpHandler()
{ {
EnableMultipleHttp2Connections = true EnableMultipleHttp2Connections = true

View File

@@ -176,7 +176,13 @@ public class SecretKeyAuthenticationHandler
this.metrics = metrics; this.metrics = metrics;
var config = configuration.GetRequiredSection("MareSynchronos"); var config = configuration.GetRequiredSection("MareSynchronos");
_failedAttemptsForTempBan = config.GetValue<int>("FailedAuthForTempBan", 5); _failedAttemptsForTempBan = config.GetValue<int>("FailedAuthForTempBan", 5);
logger.LogInformation("FailedAuthForTempBan: {num}", _failedAttemptsForTempBan);
_tempBanMinutes = config.GetValue<int>("TempBanDurationInMinutes", 30); _tempBanMinutes = config.GetValue<int>("TempBanDurationInMinutes", 30);
_whitelistedIps = config.GetValue<List<string>>("WhitelistedIps", new List<string>()); logger.LogInformation("TempBanMinutes: {num}", _tempBanMinutes);
_whitelistedIps = config.GetSection("WhitelistedIps").Get<List<string>>();
foreach (var ip in _whitelistedIps)
{
logger.LogInformation("Whitelisted IP: " + ip);
}
} }
} }

View File

@@ -72,7 +72,7 @@ internal class IdentityService : IdentificationService.IdentificationServiceBase
_handler.RegisterServerForQueue(server.ServerId); _handler.RegisterServerForQueue(server.ServerId);
_logger.LogInformation("Registered Server " + server.ServerId + " input stream"); _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; var cur = requestStream.Current.IdentChange;
if (cur == null) throw new System.Exception("Expected client ident change"); if (cur == null) throw new System.Exception("Expected client ident change");

View File

@@ -44,11 +44,11 @@ public class Startup
MetricsAPI.GaugeUsersRegistered MetricsAPI.GaugeUsersRegistered
})); }));
services.AddTransient(_ => Configuration);
services.AddSingleton<SecretKeyAuthenticationHandler>(); services.AddSingleton<SecretKeyAuthenticationHandler>();
services.AddSingleton<DiscordBotServices>(); services.AddSingleton<DiscordBotServices>();
services.AddSingleton<IdentityHandler>(); services.AddSingleton<IdentityHandler>();
services.AddSingleton<CleanupService>(); services.AddSingleton<CleanupService>();
services.AddTransient(_ => Configuration);
services.AddHostedService(provider => provider.GetService<CleanupService>()); services.AddHostedService(provider => provider.GetService<CleanupService>());
services.AddHostedService<DiscordBot>(); services.AddHostedService<DiscordBot>();
services.AddGrpc(); services.AddGrpc();

View File

@@ -39,15 +39,16 @@ public abstract class GrpcBaseService : IHostedService, IDisposable
private async Task RestartStreams() private async Task RestartStreams()
{ {
_streamCts?.Cancel();
_streamCts?.Dispose();
_streamCts = new();
if (!GrpcIsFaulty) if (!GrpcIsFaulty)
{ {
_streamCts?.Cancel();
_streamCts?.Dispose();
_streamCts = new();
var token = _streamCts.Token;
try try
{ {
await PreStartStream().ConfigureAwait(false); await PreStartStream().ConfigureAwait(false);
await StartStream(_streamCts.Token).ConfigureAwait(false); await StartStream(token).ConfigureAwait(false);
await PostStartStream().ConfigureAwait(false); await PostStartStream().ConfigureAwait(false);
} }
catch catch
@@ -74,7 +75,11 @@ public abstract class GrpcBaseService : IHostedService, IDisposable
{ {
await CheckFaultStateAndRestore().ConfigureAwait(false); await CheckFaultStateAndRestore().ConfigureAwait(false);
} }
catch { SetGrpcFaulty(); } catch
{
SetGrpcFaulty();
await Task.Delay(5000, ct).ConfigureAwait(false);
}
await Task.Delay(250).ConfigureAwait(false); await Task.Delay(250).ConfigureAwait(false);
} }
} }
@@ -83,10 +88,10 @@ public abstract class GrpcBaseService : IHostedService, IDisposable
{ {
if (GrpcIsFaulty) if (GrpcIsFaulty)
{ {
GrpcIsFaulty = false;
await RestartStreams().ConfigureAwait(false); await RestartStreams().ConfigureAwait(false);
await OnGrpcRestore().ConfigureAwait(false); await OnGrpcRestore().ConfigureAwait(false);
_logger.LogInformation("GRPC connection is restored"); _logger.LogInformation("GRPC connection is restored");
GrpcIsFaulty = false;
} }
} }