From 7639066249c2d2b605ab19143675c91b33c91002 Mon Sep 17 00:00:00 2001 From: Stanley Dimant Date: Mon, 6 May 2024 14:05:24 +0200 Subject: [PATCH] Split AuthService/Server some refactoring fix some stuff add http context accessor configure metrics as well commit 713d054ccb965f7adb8eafa6e3fb52853a1e6dd2 (partial, Docker only) --- .../Dockerfile-MareSynchronosAuthService | 32 +++ .../Dockerfile-MareSynchronosAuthService-git | 30 +++ .../linux-git/docker-build-authservice.sh | 2 + Docker/build/linux-git/docker-build.sh | 1 + .../linux-local/docker-build-authservice.sh | 2 + Docker/build/linux-local/docker-build.sh | 1 + .../windows-git/docker-build-authservice.bat | 3 + Docker/build/windows-git/docker-build.bat | 1 + .../docker-build-authservice.bat | 4 + Docker/build/windows-local/docker-build.bat | 1 + Docker/run/compose/mare-standalone.yml | 15 ++ .../standalone/authservice-standalone.json | 42 ++++ .../config/standalone/files-standalone.json | 5 +- .../Authentication/SecretKeyAuthReply.cs | 2 +- .../SecretKeyFailedAuthorization.cs | 2 +- .../Controllers/JwtController.cs | 12 +- .../MareSynchronosAuthService.csproj | 26 ++ .../MareSynchronosAuthService/Program.cs | 40 ++++ .../Properties/launchSettings.json | 29 +++ .../Services}/AccountRegistrationService.cs | 17 +- .../Services/GeoIPService.cs | 23 +- .../SecretKeyAuthenticatorService.cs | 15 +- .../MareSynchronosAuthService/Startup.cs | 226 ++++++++++++++++++ .../appsettings.Development.json | 8 + .../appsettings.json | 9 + MareSynchronosServer/MareSynchronosServer.sln | 6 + .../MareSynchronosServer/Hubs/MareHub.cs | 2 +- .../MareSynchronosServer.csproj | 1 - .../MareSynchronosServer/Program.cs | 2 +- .../Services/SystemInfoService.cs | 2 +- .../Services/UserCleanupService.cs | 1 + .../MareSynchronosServer/Startup.cs | 22 +- .../Discord/DiscordBot.cs | 2 +- .../Discord/MareModule.cs | 1 + .../MareSynchronosServices/Program.cs | 2 +- .../MareSynchronosServices/Startup.cs | 10 +- .../Services/IConfigurationService.cs | 2 +- .../Services/MareConfigurationController.cs | 7 +- .../MareConfigurationServiceClient.cs | 6 +- .../MareConfigurationServiceServer.cs | 1 + .../AuthServiceConfiguration.cs} | 31 +-- .../CdnShardConfiguration.cs | 2 +- .../{ => Configuration}/IMareConfiguration.cs | 2 +- .../MareConfigurationBase.cs | 3 +- .../ServerConfiguration.cs | 9 +- .../ServicesConfiguration.cs | 2 +- .../StaticFilesServerConfiguration.cs | 8 +- .../Utils/ServerTokenGenerator.cs | 7 +- .../Controllers/DistributionController.cs | 1 + .../Controllers/ServerFilesController.cs | 3 +- .../Program.cs | 4 +- .../Services/CachedFileProvider.cs | 1 + .../Services/ColdTouchHashService.cs | 1 + .../Services/FileCleanupService.cs | 1 + .../Services/RequestQueueService.cs | 2 +- .../ShardClientReadyMessageService.cs | 1 + .../Services/ShardTouchMessageService.cs | 1 + .../Startup.cs | 13 +- .../Utils/FilePathUtil.cs | 4 +- .../Utils/RequestBlockFileListResult.cs | 1 + .../RequestBlockFileListResultFactory.cs | 1 + 61 files changed, 593 insertions(+), 120 deletions(-) create mode 100644 Docker/build/Dockerfile-MareSynchronosAuthService create mode 100644 Docker/build/Dockerfile-MareSynchronosAuthService-git create mode 100644 Docker/build/linux-git/docker-build-authservice.sh create mode 100644 Docker/build/linux-local/docker-build-authservice.sh create mode 100644 Docker/build/windows-git/docker-build-authservice.bat create mode 100644 Docker/build/windows-local/docker-build-authservice.bat create mode 100644 Docker/run/config/standalone/authservice-standalone.json rename MareSynchronosServer/{MareSynchronosServer => MareSynchronosAuthService}/Authentication/SecretKeyAuthReply.cs (65%) rename MareSynchronosServer/{MareSynchronosServer => MareSynchronosAuthService}/Authentication/SecretKeyFailedAuthorization.cs (83%) rename MareSynchronosServer/{MareSynchronosServer => MareSynchronosAuthService}/Controllers/JwtController.cs (95%) create mode 100644 MareSynchronosServer/MareSynchronosAuthService/MareSynchronosAuthService.csproj create mode 100644 MareSynchronosServer/MareSynchronosAuthService/Program.cs create mode 100644 MareSynchronosServer/MareSynchronosAuthService/Properties/launchSettings.json rename MareSynchronosServer/{MareSynchronosServer/Authentication => MareSynchronosAuthService/Services}/AccountRegistrationService.cs (87%) rename MareSynchronosServer/{MareSynchronosServer => MareSynchronosAuthService}/Services/GeoIPService.cs (84%) rename MareSynchronosServer/{MareSynchronosServer/Authentication => MareSynchronosAuthService/Services}/SecretKeyAuthenticatorService.cs (82%) create mode 100644 MareSynchronosServer/MareSynchronosAuthService/Startup.cs create mode 100644 MareSynchronosServer/MareSynchronosAuthService/appsettings.Development.json create mode 100644 MareSynchronosServer/MareSynchronosAuthService/appsettings.json rename MareSynchronosServer/MareSynchronosShared/Utils/{MareConfigurationAuthBase.cs => Configuration/AuthServiceConfiguration.cs} (56%) rename MareSynchronosServer/MareSynchronosShared/Utils/{ => Configuration}/CdnShardConfiguration.cs (84%) rename MareSynchronosServer/MareSynchronosShared/Utils/{ => Configuration}/IMareConfiguration.cs (77%) rename MareSynchronosServer/MareSynchronosShared/Utils/{ => Configuration}/MareConfigurationBase.cs (95%) rename MareSynchronosServer/MareSynchronosShared/Utils/{ => Configuration}/ServerConfiguration.cs (80%) rename MareSynchronosServer/MareSynchronosShared/Utils/{ => Configuration}/ServicesConfiguration.cs (93%) rename MareSynchronosServer/MareSynchronosShared/Utils/{ => Configuration}/StaticFilesServerConfiguration.cs (94%) diff --git a/Docker/build/Dockerfile-MareSynchronosAuthService b/Docker/build/Dockerfile-MareSynchronosAuthService new file mode 100644 index 0000000..9ede4d0 --- /dev/null +++ b/Docker/build/Dockerfile-MareSynchronosAuthService @@ -0,0 +1,32 @@ +FROM mcr.microsoft.com/dotnet/sdk:8.0 as BUILD + +COPY MareAPI /server/MareAPI +COPY MareSynchronosServer/MareSynchronosShared /server/MareSynchronosServer/MareSynchronosShared +COPY MareSynchronosServer/MareSynchronosAuthService /server/MareSynchronosServer/MareSynchronosAuthService + +WORKDIR /server/MareSynchronosServer/MareSynchronosAuthService/ + +RUN dotnet publish \ + --configuration=Debug \ + --os=linux \ + --output=/build \ + MareSynchronosAuthService.csproj + +FROM mcr.microsoft.com/dotnet/aspnet:8.0 + +RUN adduser \ + --disabled-password \ + --group \ + --no-create-home \ + --quiet \ + --system \ + mare + +COPY --from=BUILD /build /opt/MareSynchronosAuthService +RUN chown -R mare:mare /opt/MareSynchronosAuthService +RUN apt-get update; apt-get install curl -y + +USER mare:mare +WORKDIR /opt/MareSynchronosAuthService + +CMD ["./MareSynchronosAuthService"] diff --git a/Docker/build/Dockerfile-MareSynchronosAuthService-git b/Docker/build/Dockerfile-MareSynchronosAuthService-git new file mode 100644 index 0000000..de9b7c1 --- /dev/null +++ b/Docker/build/Dockerfile-MareSynchronosAuthService-git @@ -0,0 +1,30 @@ +FROM mcr.microsoft.com/dotnet/sdk:8.0 as BUILD + +RUN git clone --recurse-submodules https://github.com/Penumbra-Sync/server + +WORKDIR /server/MareSynchronosServer/MareSynchronosAuthService/ + +RUN dotnet publish \ + --configuration=Release \ + --os=linux \ + --output=/MareSynchronosAuthService \ + MareSynchronosAuthService.csproj + +FROM mcr.microsoft.com/dotnet/aspnet:8.0 + +RUN adduser \ + --disabled-password \ + --group \ + --no-create-home \ + --quiet \ + --system \ + mare + +COPY --from=BUILD /MareSynchronosAuthService /opt/MareSynchronosAuthService +RUN chown -R mare:mare /opt/MareSynchronosAuthService +RUN apt-get update; apt-get install curl -y + +USER mare:mare +WORKDIR /opt/MareSynchronosAuthService + +CMD ["./MareSynchronosAuthService"] diff --git a/Docker/build/linux-git/docker-build-authservice.sh b/Docker/build/linux-git/docker-build-authservice.sh new file mode 100644 index 0000000..fd7d396 --- /dev/null +++ b/Docker/build/linux-git/docker-build-authservice.sh @@ -0,0 +1,2 @@ +#!/bin/sh +docker build -t darkarchon/mare-synchronos-authservice:latest . -f ../Dockerfile-MareSynchronosAuthService-git --no-cache --pull --force-rm \ No newline at end of file diff --git a/Docker/build/linux-git/docker-build.sh b/Docker/build/linux-git/docker-build.sh index 57783bd..958ab02 100644 --- a/Docker/build/linux-git/docker-build.sh +++ b/Docker/build/linux-git/docker-build.sh @@ -1,4 +1,5 @@ #!/bin/sh ./docker-build-server.sh +./docker-build-authservice.sh ./docker-build-services.sh ./docker-build-staticfilesserver.sh \ No newline at end of file diff --git a/Docker/build/linux-local/docker-build-authservice.sh b/Docker/build/linux-local/docker-build-authservice.sh new file mode 100644 index 0000000..87859cd --- /dev/null +++ b/Docker/build/linux-local/docker-build-authservice.sh @@ -0,0 +1,2 @@ +#!/bin/sh +docker build -t darkarchon/mare-synchronos-authservice:latest . -f ../Dockerfile-MareSynchronosAuthService --no-cache --pull --force-rm \ No newline at end of file diff --git a/Docker/build/linux-local/docker-build.sh b/Docker/build/linux-local/docker-build.sh index 57783bd..958ab02 100644 --- a/Docker/build/linux-local/docker-build.sh +++ b/Docker/build/linux-local/docker-build.sh @@ -1,4 +1,5 @@ #!/bin/sh ./docker-build-server.sh +./docker-build-authservice.sh ./docker-build-services.sh ./docker-build-staticfilesserver.sh \ No newline at end of file diff --git a/Docker/build/windows-git/docker-build-authservice.bat b/Docker/build/windows-git/docker-build-authservice.bat new file mode 100644 index 0000000..e504c93 --- /dev/null +++ b/Docker/build/windows-git/docker-build-authservice.bat @@ -0,0 +1,3 @@ +@echo off + +docker build -t darkarchon/mare-synchronos-authservice:latest . -f ..\Dockerfile-MareSynchronosAuthService-git --no-cache --pull --force-rm \ No newline at end of file diff --git a/Docker/build/windows-git/docker-build.bat b/Docker/build/windows-git/docker-build.bat index 09a819c..2deba89 100644 --- a/Docker/build/windows-git/docker-build.bat +++ b/Docker/build/windows-git/docker-build.bat @@ -1,5 +1,6 @@ @echo off call docker-build-server.bat +call docker-build-authservice.bat call docker-build-services.bat call docker-build-staticfilesserver.bat \ No newline at end of file diff --git a/Docker/build/windows-local/docker-build-authservice.bat b/Docker/build/windows-local/docker-build-authservice.bat new file mode 100644 index 0000000..335a328 --- /dev/null +++ b/Docker/build/windows-local/docker-build-authservice.bat @@ -0,0 +1,4 @@ +@echo off +cd ..\..\..\ +docker build -t darkarchon/mare-synchronos-authservice:latest . -f Docker\build\Dockerfile-MareSynchronosAuthService --no-cache --pull --force-rm +cd Docker\build\windows-local \ No newline at end of file diff --git a/Docker/build/windows-local/docker-build.bat b/Docker/build/windows-local/docker-build.bat index 09a819c..2deba89 100644 --- a/Docker/build/windows-local/docker-build.bat +++ b/Docker/build/windows-local/docker-build.bat @@ -1,5 +1,6 @@ @echo off call docker-build-server.bat +call docker-build-authservice.bat call docker-build-services.bat call docker-build-staticfilesserver.bat \ No newline at end of file diff --git a/Docker/run/compose/mare-standalone.yml b/Docker/run/compose/mare-standalone.yml index a49e82f..bd114af 100644 --- a/Docker/run/compose/mare-standalone.yml +++ b/Docker/run/compose/mare-standalone.yml @@ -44,6 +44,21 @@ services: start_period: 10s timeout: 1s + mare-auth: + image: darkarchon/mare-synchronos-authservice:latest + restart: on-failure + environment: + DOTNET_USE_POLLING_FILE_WATCHER: 1 + volumes: + - ../config/standalone/authservice-standalone.json:/opt/MareSynchronosAuthService/appsettings.json + - ../log/authservice-standalone/:/opt/MareSynchronosAuthService/logs/:rw + - postgres_socket:/var/run/postgresql/:rw + depends_on: + mare-server: + condition: service_healthy + postgres: + condition: service_healthy + mare-services: image: darkarchon/mare-synchronos-services:latest restart: on-failure diff --git a/Docker/run/config/standalone/authservice-standalone.json b/Docker/run/config/standalone/authservice-standalone.json new file mode 100644 index 0000000..bc21fb2 --- /dev/null +++ b/Docker/run/config/standalone/authservice-standalone.json @@ -0,0 +1,42 @@ +{ + "ConnectionStrings": { + "DefaultConnection": "Host=/var/run/postgresql;Port=5432;Database=mare;Username=mare;Keepalive=15;Minimum Pool Size=10;Maximum Pool Size=50;No Reset On Close=true;Max Auto Prepare=50;Enlist=false" + }, + "Logging": { + "LogLevel": { + "Default": "Warning", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information", + "MareSynchronosServices": "Information", + "MareSynchronosShared": "Information", + "System.IO": "Information" + }, + "File": { + "BasePath": "logs", + "FileAccessMode": "KeepOpenAndAutoFlush", + "FileEncodingName": "utf-8", + "DateFormat": "yyyMMdd", + "MaxFileSize": 104857600, + "Files": [ + { + "Path": "///mare--.log" + } + ] + } + }, + "MareSynchronos": { + "DbContextPoolSize": 512, + "ShardName": "AuthServices", + "MetricsPort": 6150, + "Jwt": "teststringteststringteststringteststringteststringteststringteststringteststringteststringteststring", + "RedisConnectionString": "redis,password=secretredispassword", + "FailedAuthForTempBan": 5, + "UseGeoIP": false, + "GeoIPDbCityFile": "" + }, + "AllowedHosts": "*", + "Kestrel": { + }, + "IpRateLimiting": {}, + "IPRateLimitPolicies": {} +} \ No newline at end of file diff --git a/Docker/run/config/standalone/files-standalone.json b/Docker/run/config/standalone/files-standalone.json index 891358b..e87053c 100644 --- a/Docker/run/config/standalone/files-standalone.json +++ b/Docker/run/config/standalone/files-standalone.json @@ -28,7 +28,6 @@ "DbContextPoolSize": 512, "ShardName": "Files", "MetricsPort": 6250, - "FileServerGrpcAddress": "", "ForcedDeletionOfFilesAfterHours": -1, "CacheSizeHardLimitInGiB": -1, "UnusedFileRetentionPeriodInDays": 14, @@ -37,7 +36,9 @@ "MainServerAddress": "http://mare-server:6000/", "RedisConnectionString": "redis,password=secretredispassword", "MainFileServerAddress": "", - "Jwt": "teststringteststringteststringteststringteststringteststringteststringteststringteststringteststring" + "Jwt": "teststringteststringteststringteststringteststringteststringteststringteststringteststringteststring", + "UseColdStorage": false, + "IsDistributionNode": true }, "AllowedHosts": "*", "Kestrel": { diff --git a/MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthReply.cs b/MareSynchronosServer/MareSynchronosAuthService/Authentication/SecretKeyAuthReply.cs similarity index 65% rename from MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthReply.cs rename to MareSynchronosServer/MareSynchronosAuthService/Authentication/SecretKeyAuthReply.cs index 7e76ef4..f6ef7cf 100644 --- a/MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthReply.cs +++ b/MareSynchronosServer/MareSynchronosAuthService/Authentication/SecretKeyAuthReply.cs @@ -1,3 +1,3 @@ -namespace MareSynchronosServer.Authentication; +namespace MareSynchronosAuthService.Authentication; public record SecretKeyAuthReply(bool Success, string Uid, string Alias, bool TempBan, bool Permaban); diff --git a/MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyFailedAuthorization.cs b/MareSynchronosServer/MareSynchronosAuthService/Authentication/SecretKeyFailedAuthorization.cs similarity index 83% rename from MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyFailedAuthorization.cs rename to MareSynchronosServer/MareSynchronosAuthService/Authentication/SecretKeyFailedAuthorization.cs index a5bae15..fcf6581 100644 --- a/MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyFailedAuthorization.cs +++ b/MareSynchronosServer/MareSynchronosAuthService/Authentication/SecretKeyFailedAuthorization.cs @@ -1,4 +1,4 @@ -namespace MareSynchronosServer.Authentication; +namespace MareSynchronosAuthService.Authentication; internal record SecretKeyFailedAuthorization { diff --git a/MareSynchronosServer/MareSynchronosServer/Controllers/JwtController.cs b/MareSynchronosServer/MareSynchronosAuthService/Controllers/JwtController.cs similarity index 95% rename from MareSynchronosServer/MareSynchronosServer/Controllers/JwtController.cs rename to MareSynchronosServer/MareSynchronosAuthService/Controllers/JwtController.cs index 65d7b17..6087f78 100644 --- a/MareSynchronosServer/MareSynchronosServer/Controllers/JwtController.cs +++ b/MareSynchronosServer/MareSynchronosAuthService/Controllers/JwtController.cs @@ -1,11 +1,11 @@ using MareSynchronos.API.Routes; -using MareSynchronosServer.Authentication; -using MareSynchronosServer.Services; +using MareSynchronosAuthService.Services; using MareSynchronosShared; using MareSynchronosShared.Data; using MareSynchronosShared.Models; using MareSynchronosShared.Services; using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -15,7 +15,7 @@ using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Text; -namespace MareSynchronosServer.Controllers; +namespace MareSynchronosAuthService.Controllers; [AllowAnonymous] [Route(MareAuth.Auth)] @@ -27,12 +27,12 @@ public class JwtController : Controller private readonly GeoIPService _geoIPProvider; private readonly SecretKeyAuthenticatorService _secretKeyAuthenticatorService; private readonly AccountRegistrationService _accountRegistrationService; - private readonly IConfigurationService _configuration; + private readonly IConfigurationService _configuration; public JwtController(IHttpContextAccessor accessor, MareDbContext mareDbContext, SecretKeyAuthenticatorService secretKeyAuthenticatorService, AccountRegistrationService accountRegistrationService, - IConfigurationService configuration, + IConfigurationService configuration, IRedisDatabase redisDb, GeoIPService geoIPProvider) { _accessor = accessor; @@ -133,7 +133,7 @@ public class JwtController : Controller private JwtSecurityToken CreateToken(IEnumerable authClaims) { - var authSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(_configuration.GetValue(nameof(MareConfigurationAuthBase.Jwt)))); + var authSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(_configuration.GetValue(nameof(MareConfigurationBase.Jwt)))); var token = new SecurityTokenDescriptor() { diff --git a/MareSynchronosServer/MareSynchronosAuthService/MareSynchronosAuthService.csproj b/MareSynchronosServer/MareSynchronosAuthService/MareSynchronosAuthService.csproj new file mode 100644 index 0000000..9345eab --- /dev/null +++ b/MareSynchronosServer/MareSynchronosAuthService/MareSynchronosAuthService.csproj @@ -0,0 +1,26 @@ + + + + net8.0 + enable + enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + diff --git a/MareSynchronosServer/MareSynchronosAuthService/Program.cs b/MareSynchronosServer/MareSynchronosAuthService/Program.cs new file mode 100644 index 0000000..5bebfd8 --- /dev/null +++ b/MareSynchronosServer/MareSynchronosAuthService/Program.cs @@ -0,0 +1,40 @@ +namespace MareSynchronosAuthService; + +public class Program +{ + public static void Main(string[] args) + { + var hostBuilder = CreateHostBuilder(args); + using var host = hostBuilder.Build(); + try + { + host.Run(); + } + catch (Exception ex) + { + Console.WriteLine(ex); + } + } + + public static IHostBuilder CreateHostBuilder(string[] args) + { + using var loggerFactory = LoggerFactory.Create(builder => + { + builder.ClearProviders(); + builder.AddConsole(); + }); + var logger = loggerFactory.CreateLogger(); + return Host.CreateDefaultBuilder(args) + .UseSystemd() + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseContentRoot(AppContext.BaseDirectory); + webBuilder.ConfigureLogging((ctx, builder) => + { + builder.AddConfiguration(ctx.Configuration.GetSection("Logging")); + builder.AddFile(o => o.RootPath = AppContext.BaseDirectory); + }); + webBuilder.UseStartup(ctx => new Startup(ctx.Configuration, logger)); + }); + } +} diff --git a/MareSynchronosServer/MareSynchronosAuthService/Properties/launchSettings.json b/MareSynchronosServer/MareSynchronosAuthService/Properties/launchSettings.json new file mode 100644 index 0000000..db41dc1 --- /dev/null +++ b/MareSynchronosServer/MareSynchronosAuthService/Properties/launchSettings.json @@ -0,0 +1,29 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:37726", + "sslPort": 0 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5056", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/MareSynchronosServer/MareSynchronosServer/Authentication/AccountRegistrationService.cs b/MareSynchronosServer/MareSynchronosAuthService/Services/AccountRegistrationService.cs similarity index 87% rename from MareSynchronosServer/MareSynchronosServer/Authentication/AccountRegistrationService.cs rename to MareSynchronosServer/MareSynchronosAuthService/Services/AccountRegistrationService.cs index fd1576c..88bd462 100644 --- a/MareSynchronosServer/MareSynchronosServer/Authentication/AccountRegistrationService.cs +++ b/MareSynchronosServer/MareSynchronosAuthService/Services/AccountRegistrationService.cs @@ -4,11 +4,12 @@ using MareSynchronosShared.Data; using MareSynchronosShared.Metrics; using MareSynchronosShared.Services; using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; using Microsoft.EntityFrameworkCore; using System.Text.RegularExpressions; using MareSynchronosShared.Models; -namespace MareSynchronosServer.Authentication; +namespace MareSynchronosAuthService.Services; internal record IpRegistrationCount { @@ -27,14 +28,14 @@ public class AccountRegistrationService private readonly MareMetrics _metrics; private readonly MareDbContext _mareDbContext; private readonly IServiceScopeFactory _serviceScopeFactory; - private readonly IConfigurationService _configurationService; + private readonly IConfigurationService _configurationService; private readonly ILogger _logger; private readonly ConcurrentDictionary _registrationsPerIp = new(StringComparer.Ordinal); private Regex _registrationUserAgentRegex = new Regex(@"^MareSynchronos/", RegexOptions.Compiled); public AccountRegistrationService(MareMetrics metrics, MareDbContext mareDbContext, - IServiceScopeFactory serviceScopeFactory, IConfigurationService configuration, + IServiceScopeFactory serviceScopeFactory, IConfigurationService configuration, ILogger logger) { _mareDbContext = mareDbContext; @@ -55,7 +56,7 @@ public class AccountRegistrationService } if (_registrationsPerIp.TryGetValue(ip, out var registrationCount) - && registrationCount.Count >= _configurationService.GetValueOrDefault(nameof(MareConfigurationAuthBase.RegisterIpLimit), 3)) + && registrationCount.Count >= _configurationService.GetValueOrDefault(nameof(AuthServiceConfiguration.RegisterIpLimit), 3)) { _logger.LogWarning("Rejecting {ip} for registration spam", ip); @@ -68,7 +69,7 @@ public class AccountRegistrationService registrationCount.ResetTask = Task.Run(async () => { - await Task.Delay(TimeSpan.FromMinutes(_configurationService.GetValueOrDefault(nameof(MareConfigurationAuthBase.RegisterIpDurationInMinutes), 10))).ConfigureAwait(false); + await Task.Delay(TimeSpan.FromMinutes(_configurationService.GetValueOrDefault(nameof(AuthServiceConfiguration.RegisterIpDurationInMinutes), 10))).ConfigureAwait(false); }).ContinueWith((t) => { @@ -110,7 +111,7 @@ public class AccountRegistrationService await _mareDbContext.SaveChangesAsync().ConfigureAwait(false); _logger.LogInformation("User registered: {userUID} from IP {ip}", user.UID, ip); - _metrics.IncCounter(MetricsAPI.CounterAuthenticationRequests); + _metrics.IncCounter(MetricsAPI.CounterAccountsCreated); reply.Success = true; reply.UID = user.UID; @@ -123,7 +124,7 @@ public class AccountRegistrationService private void RecordIpRegistration(string ip) { - var whitelisted = _configurationService.GetValueOrDefault(nameof(MareConfigurationAuthBase.WhitelistedIps), new List()); + var whitelisted = _configurationService.GetValueOrDefault(nameof(AuthServiceConfiguration.WhitelistedIps), new List()); if (!whitelisted.Any(w => ip.Contains(w, StringComparison.OrdinalIgnoreCase))) { if (_registrationsPerIp.TryGetValue(ip, out var count)) @@ -141,7 +142,7 @@ public class AccountRegistrationService count.ResetTask = Task.Run(async () => { - await Task.Delay(TimeSpan.FromMinutes(_configurationService.GetValueOrDefault(nameof(MareConfigurationAuthBase.RegisterIpDurationInMinutes), 10))).ConfigureAwait(false); + await Task.Delay(TimeSpan.FromMinutes(_configurationService.GetValueOrDefault(nameof(AuthServiceConfiguration.RegisterIpDurationInMinutes), 10))).ConfigureAwait(false); }).ContinueWith((t) => { diff --git a/MareSynchronosServer/MareSynchronosServer/Services/GeoIPService.cs b/MareSynchronosServer/MareSynchronosAuthService/Services/GeoIPService.cs similarity index 84% rename from MareSynchronosServer/MareSynchronosServer/Services/GeoIPService.cs rename to MareSynchronosServer/MareSynchronosAuthService/Services/GeoIPService.cs index dbbbf97..dbebf35 100644 --- a/MareSynchronosServer/MareSynchronosServer/Services/GeoIPService.cs +++ b/MareSynchronosServer/MareSynchronosAuthService/Services/GeoIPService.cs @@ -1,14 +1,14 @@ using MareSynchronosShared; using MareSynchronosShared.Services; -using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; using MaxMind.GeoIP2; -namespace MareSynchronosServer.Services; +namespace MareSynchronosAuthService.Services; public class GeoIPService : IHostedService { private readonly ILogger _logger; - private readonly IConfigurationService _mareConfiguration; + private readonly IConfigurationService _mareConfiguration; private bool _useGeoIP = false; private string _cityFile = string.Empty; private DatabaseReader? _dbReader; @@ -17,7 +17,7 @@ public class GeoIPService : IHostedService private bool _processingReload = false; public GeoIPService(ILogger logger, - IConfigurationService mareConfiguration) + IConfigurationService mareConfiguration) { _logger = logger; _mareConfiguration = mareConfiguration; @@ -38,11 +38,12 @@ public class GeoIPService : IHostedService waitCts.CancelAfter(TimeSpan.FromSeconds(5)); while (_processingReload) await Task.Delay(100, waitCts.Token).ConfigureAwait(false); - if (_dbReader.TryCity(ip, out var response)) + if (_dbReader!.TryCity(ip, out var response)) { - var continent = response.Continent.Code; - if (string.Equals(continent, "NA", StringComparison.Ordinal) - && response.Location.Longitude != null) + string? continent = response?.Continent.Code; + if (!string.IsNullOrEmpty(continent) && + string.Equals(continent, "NA", StringComparison.Ordinal) + && response?.Location.Longitude != null) { if (response.Location.Longitude < -102) { @@ -84,8 +85,8 @@ public class GeoIPService : IHostedService { _processingReload = true; - var useGeoIP = _mareConfiguration.GetValueOrDefault(nameof(ServerConfiguration.UseGeoIP), false); - var cityFile = _mareConfiguration.GetValueOrDefault(nameof(ServerConfiguration.GeoIPDbCityFile), string.Empty); + var useGeoIP = _mareConfiguration.GetValueOrDefault(nameof(AuthServiceConfiguration.UseGeoIP), false); + var cityFile = _mareConfiguration.GetValueOrDefault(nameof(AuthServiceConfiguration.GeoIPDbCityFile), string.Empty); var lastWriteTime = new FileInfo(cityFile).LastWriteTimeUtc; if (useGeoIP && (!string.Equals(cityFile, _cityFile, StringComparison.OrdinalIgnoreCase) || lastWriteTime != _dbLastWriteTime)) { @@ -131,7 +132,7 @@ public class GeoIPService : IHostedService { _fileWriteTimeCheckCts.Cancel(); _fileWriteTimeCheckCts.Dispose(); - _dbReader.Dispose(); + _dbReader?.Dispose(); return Task.CompletedTask; } } diff --git a/MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthenticatorService.cs b/MareSynchronosServer/MareSynchronosAuthService/Services/SecretKeyAuthenticatorService.cs similarity index 82% rename from MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthenticatorService.cs rename to MareSynchronosServer/MareSynchronosAuthService/Services/SecretKeyAuthenticatorService.cs index 830c78a..bf246f2 100644 --- a/MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthenticatorService.cs +++ b/MareSynchronosServer/MareSynchronosAuthService/Services/SecretKeyAuthenticatorService.cs @@ -1,22 +1,23 @@ using System.Collections.Concurrent; +using MareSynchronosAuthService.Authentication; using MareSynchronosShared.Data; using MareSynchronosShared.Metrics; using MareSynchronosShared.Services; -using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; using Microsoft.EntityFrameworkCore; -namespace MareSynchronosServer.Authentication; +namespace MareSynchronosAuthService.Services; public class SecretKeyAuthenticatorService { private readonly MareMetrics _metrics; private readonly MareDbContext _mareDbContext; - private readonly IConfigurationService _configurationService; + private readonly IConfigurationService _configurationService; private readonly ILogger _logger; private readonly ConcurrentDictionary _failedAuthorizations = new(StringComparer.Ordinal); public SecretKeyAuthenticatorService(MareMetrics metrics, MareDbContext mareDbContext, - IConfigurationService configuration, ILogger logger) + IConfigurationService configuration, ILogger logger) { _logger = logger; _configurationService = configuration; @@ -29,7 +30,7 @@ public class SecretKeyAuthenticatorService _metrics.IncCounter(MetricsAPI.CounterAuthenticationRequests); if (_failedAuthorizations.TryGetValue(ip, out var existingFailedAuthorization) - && existingFailedAuthorization.FailedAttempts > _configurationService.GetValueOrDefault(nameof(MareConfigurationAuthBase.FailedAuthForTempBan), 5)) + && existingFailedAuthorization.FailedAttempts > _configurationService.GetValueOrDefault(nameof(AuthServiceConfiguration.FailedAuthForTempBan), 5)) { if (existingFailedAuthorization.ResetTask == null) { @@ -37,7 +38,7 @@ public class SecretKeyAuthenticatorService existingFailedAuthorization.ResetTask = Task.Run(async () => { - await Task.Delay(TimeSpan.FromMinutes(_configurationService.GetValueOrDefault(nameof(MareConfigurationAuthBase.TempBanDurationInMinutes), 5))).ConfigureAwait(false); + await Task.Delay(TimeSpan.FromMinutes(_configurationService.GetValueOrDefault(nameof(AuthServiceConfiguration.TempBanDurationInMinutes), 5))).ConfigureAwait(false); }).ContinueWith((t) => { @@ -69,7 +70,7 @@ public class SecretKeyAuthenticatorService _metrics.IncCounter(MetricsAPI.CounterAuthenticationFailures); _logger.LogWarning("Failed authorization from {ip}", ip); - var whitelisted = _configurationService.GetValueOrDefault(nameof(MareConfigurationAuthBase.WhitelistedIps), new List()); + var whitelisted = _configurationService.GetValueOrDefault(nameof(AuthServiceConfiguration.WhitelistedIps), new List()); if (!whitelisted.Any(w => ip.Contains(w, StringComparison.OrdinalIgnoreCase))) { if (_failedAuthorizations.TryGetValue(ip, out var auth)) diff --git a/MareSynchronosServer/MareSynchronosAuthService/Startup.cs b/MareSynchronosServer/MareSynchronosAuthService/Startup.cs new file mode 100644 index 0000000..bd06ef5 --- /dev/null +++ b/MareSynchronosServer/MareSynchronosAuthService/Startup.cs @@ -0,0 +1,226 @@ +using MareSynchronosAuthService.Controllers; +using MareSynchronosShared.Metrics; +using MareSynchronosShared.Services; +using MareSynchronosShared.Utils; +using Microsoft.AspNetCore.Mvc.Controllers; +using StackExchange.Redis.Extensions.Core.Configuration; +using StackExchange.Redis.Extensions.System.Text.Json; +using StackExchange.Redis; +using System.Net; +using MareSynchronosAuthService.Services; +using MareSynchronosShared.RequirementHandlers; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.IdentityModel.Tokens; +using System.Text; +using MareSynchronosShared.Data; +using Microsoft.EntityFrameworkCore; +using Prometheus; +using MareSynchronosShared.Utils.Configuration; + +namespace MareSynchronosAuthService; + +public class Startup +{ + private readonly IConfiguration _configuration; + private ILogger _logger; + + public Startup(IConfiguration configuration, ILogger logger) + { + _configuration = configuration; + _logger = logger; + } + + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger logger) + { + var config = app.ApplicationServices.GetRequiredService>(); + + app.UseRouting(); + + app.UseHttpMetrics(); + + app.UseAuthentication(); + app.UseAuthorization(); + + KestrelMetricServer metricServer = new KestrelMetricServer(config.GetValueOrDefault(nameof(MareConfigurationBase.MetricsPort), 4985)); + metricServer.Start(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + + foreach (var source in endpoints.DataSources.SelectMany(e => e.Endpoints).Cast()) + { + if (source == null) continue; + _logger.LogInformation("Endpoint: {url} ", source.RoutePattern.RawText); + } + }); + } + + public void ConfigureServices(IServiceCollection services) + { + var mareConfig = _configuration.GetRequiredSection("MareSynchronos"); + + services.AddHttpContextAccessor(); + + ConfigureRedis(services, mareConfig); + + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + + services.AddHostedService(provider => provider.GetRequiredService()); + + services.Configure(_configuration.GetRequiredSection("MareSynchronos")); + services.Configure(_configuration.GetRequiredSection("MareSynchronos")); + + services.AddSingleton(); + + ConfigureAuthorization(services); + + ConfigureDatabase(services, mareConfig); + + ConfigureConfigServices(services); + + ConfigureMetrics(services); + + services.AddHealthChecks(); + services.AddControllers().ConfigureApplicationPartManager(a => + { + a.FeatureProviders.Remove(a.FeatureProviders.OfType().First()); + a.FeatureProviders.Add(new AllowedControllersFeatureProvider(typeof(JwtController))); + }); + } + + private static void ConfigureAuthorization(IServiceCollection services) + { + services.AddTransient(); + + services.AddOptions(JwtBearerDefaults.AuthenticationScheme) + .Configure>((options, config) => + { + options.TokenValidationParameters = new() + { + ValidateIssuer = false, + ValidateLifetime = true, + ValidateAudience = false, + ValidateIssuerSigningKey = true, + IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(config.GetValue(nameof(MareConfigurationBase.Jwt)))), + }; + }); + + services.AddAuthentication(o => + { + o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; + o.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; + }).AddJwtBearer(); + + services.AddAuthorization(options => + { + options.DefaultPolicy = new AuthorizationPolicyBuilder() + .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) + .RequireAuthenticatedUser().Build(); + options.AddPolicy("Authenticated", policy => + { + policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme); + policy.RequireAuthenticatedUser(); + }); + options.AddPolicy("Identified", policy => + { + policy.AddRequirements(new UserRequirement(UserRequirements.Identified)); + + }); + options.AddPolicy("Admin", policy => + { + policy.AddRequirements(new UserRequirement(UserRequirements.Identified | UserRequirements.Administrator)); + + }); + options.AddPolicy("Moderator", policy => + { + policy.AddRequirements(new UserRequirement(UserRequirements.Identified | UserRequirements.Moderator | UserRequirements.Administrator)); + }); + options.AddPolicy("Internal", new AuthorizationPolicyBuilder().RequireClaim(MareClaimTypes.Internal, "true").Build()); + }); + } + + private static void ConfigureMetrics(IServiceCollection services) + { + services.AddSingleton(m => new MareMetrics(m.GetService>(), new List + { + MetricsAPI.CounterAuthenticationCacheHits, + MetricsAPI.CounterAuthenticationFailures, + MetricsAPI.CounterAuthenticationRequests, + MetricsAPI.CounterAuthenticationSuccesses, + MetricsAPI.CounterAccountsCreated, + }, new List + { + })); + } + + private static void ConfigureRedis(IServiceCollection services, IConfigurationSection mareConfig) + { + // configure redis for SignalR + var redisConnection = mareConfig.GetValue(nameof(ServerConfiguration.RedisConnectionString), string.Empty); + + var options = ConfigurationOptions.Parse(redisConnection); + + var endpoint = options.EndPoints[0]; + string address = ""; + int port = 0; + if (endpoint is DnsEndPoint dnsEndPoint) { address = dnsEndPoint.Host; port = dnsEndPoint.Port; } + if (endpoint is IPEndPoint ipEndPoint) { address = ipEndPoint.Address.ToString(); port = ipEndPoint.Port; } + var redisConfiguration = new RedisConfiguration() + { + AbortOnConnectFail = true, + KeyPrefix = "", + Hosts = new RedisHost[] + { + new RedisHost(){ Host = address, Port = port }, + }, + AllowAdmin = true, + ConnectTimeout = options.ConnectTimeout, + Database = 0, + Ssl = false, + Password = options.Password, + ServerEnumerationStrategy = new ServerEnumerationStrategy() + { + Mode = ServerEnumerationStrategy.ModeOptions.All, + TargetRole = ServerEnumerationStrategy.TargetRoleOptions.Any, + UnreachableServerAction = ServerEnumerationStrategy.UnreachableServerActionOptions.Throw, + }, + MaxValueLength = 1024, + PoolSize = mareConfig.GetValue(nameof(ServerConfiguration.RedisPool), 50), + SyncTimeout = options.SyncTimeout, + }; + + services.AddStackExchangeRedisExtensions(redisConfiguration); + } + private void ConfigureConfigServices(IServiceCollection services) + { + services.AddSingleton, MareConfigurationServiceServer>(); + services.AddSingleton, MareConfigurationServiceServer>(); + } + + private void ConfigureDatabase(IServiceCollection services, IConfigurationSection mareConfig) + { + services.AddDbContextPool(options => + { + options.UseNpgsql(_configuration.GetConnectionString("DefaultConnection"), builder => + { + builder.MigrationsHistoryTable("_efmigrationshistory", "public"); + builder.MigrationsAssembly("MareSynchronosShared"); + }).UseSnakeCaseNamingConvention(); + options.EnableThreadSafetyChecks(false); + }, mareConfig.GetValue(nameof(MareConfigurationBase.DbContextPoolSize), 1024)); + services.AddDbContextFactory(options => + { + options.UseNpgsql(_configuration.GetConnectionString("DefaultConnection"), builder => + { + builder.MigrationsHistoryTable("_efmigrationshistory", "public"); + builder.MigrationsAssembly("MareSynchronosShared"); + }).UseSnakeCaseNamingConvention(); + options.EnableThreadSafetyChecks(false); + }); + } +} diff --git a/MareSynchronosServer/MareSynchronosAuthService/appsettings.Development.json b/MareSynchronosServer/MareSynchronosAuthService/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/MareSynchronosServer/MareSynchronosAuthService/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/MareSynchronosServer/MareSynchronosAuthService/appsettings.json b/MareSynchronosServer/MareSynchronosAuthService/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/MareSynchronosServer/MareSynchronosAuthService/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/MareSynchronosServer/MareSynchronosServer.sln b/MareSynchronosServer/MareSynchronosServer.sln index 8f8c992..84f8d6f 100644 --- a/MareSynchronosServer/MareSynchronosServer.sln +++ b/MareSynchronosServer/MareSynchronosServer.sln @@ -18,6 +18,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution .editorconfig = .editorconfig EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MareSynchronosAuthService", "MareSynchronosAuthService\MareSynchronosAuthService.csproj", "{D7D4041C-DCD9-4B7A-B423-0F458DFFF3D6}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -44,6 +46,10 @@ Global {E29C8677-AB44-4950-9EB1-D8E70B710A56}.Debug|Any CPU.Build.0 = Debug|Any CPU {E29C8677-AB44-4950-9EB1-D8E70B710A56}.Release|Any CPU.ActiveCfg = Release|Any CPU {E29C8677-AB44-4950-9EB1-D8E70B710A56}.Release|Any CPU.Build.0 = Release|Any CPU + {D7D4041C-DCD9-4B7A-B423-0F458DFFF3D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D7D4041C-DCD9-4B7A-B423-0F458DFFF3D6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D7D4041C-DCD9-4B7A-B423-0F458DFFF3D6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D7D4041C-DCD9-4B7A-B423-0F458DFFF3D6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/MareSynchronosServer/MareSynchronosServer/Hubs/MareHub.cs b/MareSynchronosServer/MareSynchronosServer/Hubs/MareHub.cs index 6b5b005..5fbfdb6 100644 --- a/MareSynchronosServer/MareSynchronosServer/Hubs/MareHub.cs +++ b/MareSynchronosServer/MareSynchronosServer/Hubs/MareHub.cs @@ -8,7 +8,7 @@ using MareSynchronosShared; using MareSynchronosShared.Data; using MareSynchronosShared.Metrics; using MareSynchronosShared.Services; -using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.SignalR; using StackExchange.Redis.Extensions.Core.Abstractions; diff --git a/MareSynchronosServer/MareSynchronosServer/MareSynchronosServer.csproj b/MareSynchronosServer/MareSynchronosServer/MareSynchronosServer.csproj index 160d820..b662f74 100644 --- a/MareSynchronosServer/MareSynchronosServer/MareSynchronosServer.csproj +++ b/MareSynchronosServer/MareSynchronosServer/MareSynchronosServer.csproj @@ -29,7 +29,6 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - diff --git a/MareSynchronosServer/MareSynchronosServer/Program.cs b/MareSynchronosServer/MareSynchronosServer/Program.cs index 8beb97f..e05f94f 100644 --- a/MareSynchronosServer/MareSynchronosServer/Program.cs +++ b/MareSynchronosServer/MareSynchronosServer/Program.cs @@ -2,7 +2,7 @@ using Microsoft.EntityFrameworkCore; using MareSynchronosShared.Data; using MareSynchronosShared.Metrics; using MareSynchronosShared.Services; -using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; namespace MareSynchronosServer; diff --git a/MareSynchronosServer/MareSynchronosServer/Services/SystemInfoService.cs b/MareSynchronosServer/MareSynchronosServer/Services/SystemInfoService.cs index 5b8d5a6..35db7ac 100644 --- a/MareSynchronosServer/MareSynchronosServer/Services/SystemInfoService.cs +++ b/MareSynchronosServer/MareSynchronosServer/Services/SystemInfoService.cs @@ -4,7 +4,7 @@ using MareSynchronosServer.Hubs; using MareSynchronosShared.Data; using MareSynchronosShared.Metrics; using MareSynchronosShared.Services; -using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; using Microsoft.AspNetCore.SignalR; using Microsoft.EntityFrameworkCore; using StackExchange.Redis.Extensions.Core.Abstractions; diff --git a/MareSynchronosServer/MareSynchronosServer/Services/UserCleanupService.cs b/MareSynchronosServer/MareSynchronosServer/Services/UserCleanupService.cs index b33c4bd..32bc3c6 100644 --- a/MareSynchronosServer/MareSynchronosServer/Services/UserCleanupService.cs +++ b/MareSynchronosServer/MareSynchronosServer/Services/UserCleanupService.cs @@ -3,6 +3,7 @@ using MareSynchronosShared.Metrics; using MareSynchronosShared.Models; using MareSynchronosShared.Services; using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; using Microsoft.EntityFrameworkCore; namespace MareSynchronosServer.Services; diff --git a/MareSynchronosServer/MareSynchronosServer/Startup.cs b/MareSynchronosServer/MareSynchronosServer/Startup.cs index 7b57ffe..eebe181 100644 --- a/MareSynchronosServer/MareSynchronosServer/Startup.cs +++ b/MareSynchronosServer/MareSynchronosServer/Startup.cs @@ -13,7 +13,6 @@ using Prometheus; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.IdentityModel.Tokens; using System.Text; -using MareSynchronosServer.Authentication; using StackExchange.Redis; using StackExchange.Redis.Extensions.Core.Configuration; using System.Net; @@ -24,6 +23,7 @@ using MessagePack.Resolvers; using Microsoft.AspNetCore.Mvc.Controllers; using MareSynchronosServer.Controllers; using MareSynchronosShared.RequirementHandlers; +using MareSynchronosShared.Utils.Configuration; namespace MareSynchronosServer; @@ -71,7 +71,7 @@ public class Startup a.FeatureProviders.Remove(a.FeatureProviders.OfType().First()); if (mareConfig.GetValue(nameof(ServerConfiguration.MainServerAddress), defaultValue: null) == null) { - a.FeatureProviders.Add(new AllowedControllersFeatureProvider(typeof(MareServerConfigurationController), typeof(MareAuthBaseConfigurationController), typeof(JwtController), typeof(ClientMessageController), typeof(MainController))); + a.FeatureProviders.Add(new AllowedControllersFeatureProvider(typeof(MareServerConfigurationController), typeof(MareBaseConfigurationController), typeof(ClientMessageController), typeof(MainController))); } else { @@ -86,7 +86,6 @@ public class Startup services.Configure(Configuration.GetRequiredSection("MareSynchronos")); services.Configure(Configuration.GetRequiredSection("MareSynchronos")); - services.Configure(Configuration.GetRequiredSection("MareSynchronos")); services.AddSingleton(); services.AddSingleton(); @@ -96,10 +95,8 @@ public class Startup if (isMainServer) { - services.AddSingleton(); services.AddSingleton(); services.AddHostedService(provider => provider.GetService()); - services.AddHostedService(provider => provider.GetService()); } } @@ -183,12 +180,10 @@ public class Startup private static void ConfigureAuthorization(IServiceCollection services) { - services.AddSingleton(); - services.AddSingleton(); services.AddTransient(); services.AddOptions(JwtBearerDefaults.AuthenticationScheme) - .Configure>((options, config) => + .Configure>((options, config) => { options.TokenValidationParameters = new() { @@ -196,7 +191,7 @@ public class Startup ValidateLifetime = false, ValidateAudience = false, ValidateIssuerSigningKey = true, - IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(config.GetValue(nameof(MareConfigurationAuthBase.Jwt)))), + IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(config.GetValue(nameof(MareConfigurationBase.Jwt)))), }; }); @@ -258,7 +253,6 @@ public class Startup MetricsAPI.CounterAuthenticationFailures, MetricsAPI.CounterAuthenticationRequests, MetricsAPI.CounterAuthenticationSuccesses, - MetricsAPI.CounterAccountsCreated, }, new List { MetricsAPI.GaugeAuthorizedConnections, @@ -279,15 +273,15 @@ public class Startup if (!isMainServer) { services.AddSingleton, MareConfigurationServiceClient>(); - services.AddSingleton, MareConfigurationServiceClient>(); + services.AddSingleton, MareConfigurationServiceClient>(); services.AddHostedService(p => (MareConfigurationServiceClient)p.GetService>()); - services.AddHostedService(p => (MareConfigurationServiceClient)p.GetService>()); + services.AddHostedService(p => (MareConfigurationServiceClient)p.GetService>()); } else { services.AddSingleton, MareConfigurationServiceServer>(); - services.AddSingleton, MareConfigurationServiceServer>(); + services.AddSingleton, MareConfigurationServiceServer>(); } } @@ -295,7 +289,7 @@ public class Startup { logger.LogInformation("Running Configure"); - var config = app.ApplicationServices.GetRequiredService>(); + var config = app.ApplicationServices.GetRequiredService>(); app.UseIpRateLimiting(); diff --git a/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBot.cs b/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBot.cs index 628b6a7..bf17215 100644 --- a/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBot.cs +++ b/MareSynchronosServer/MareSynchronosServices/Discord/DiscordBot.cs @@ -8,7 +8,7 @@ using MareSynchronos.API.SignalR; using MareSynchronosServer.Hubs; using MareSynchronosShared.Data; using MareSynchronosShared.Services; -using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; using Microsoft.AspNetCore.SignalR; using Microsoft.EntityFrameworkCore; using StackExchange.Redis; diff --git a/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs b/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs index 3d474a1..bd10600 100644 --- a/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs +++ b/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs @@ -11,6 +11,7 @@ using MareSynchronosShared.Services; using StackExchange.Redis; using MareSynchronos.API.Data.Enum; using System.Net.Http.Headers; +using MareSynchronosShared.Utils.Configuration; namespace MareSynchronosServices.Discord; diff --git a/MareSynchronosServer/MareSynchronosServices/Program.cs b/MareSynchronosServer/MareSynchronosServices/Program.cs index aca06f6..4a3f930 100644 --- a/MareSynchronosServer/MareSynchronosServices/Program.cs +++ b/MareSynchronosServer/MareSynchronosServices/Program.cs @@ -1,7 +1,7 @@ using MareSynchronosServices; using MareSynchronosShared.Data; using MareSynchronosShared.Services; -using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; public class Program { diff --git a/MareSynchronosServer/MareSynchronosServices/Startup.cs b/MareSynchronosServer/MareSynchronosServices/Startup.cs index cc6cf68..e8b7437 100644 --- a/MareSynchronosServer/MareSynchronosServices/Startup.cs +++ b/MareSynchronosServer/MareSynchronosServices/Startup.cs @@ -8,7 +8,7 @@ using MareSynchronosShared.Services; using StackExchange.Redis; using MessagePack.Resolvers; using MessagePack; -using Microsoft.AspNetCore.Authorization; +using MareSynchronosShared.Utils.Configuration; namespace MareSynchronosServices; @@ -23,7 +23,7 @@ public class Startup public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { - var config = app.ApplicationServices.GetRequiredService>(); + var config = app.ApplicationServices.GetRequiredService>(); var metricServer = new KestrelMetricServer(config.GetValueOrDefault(nameof(MareConfigurationBase.MetricsPort), 4982)); metricServer.Start(); @@ -89,16 +89,16 @@ public class Startup services.Configure(Configuration.GetRequiredSection("MareSynchronos")); services.Configure(Configuration.GetRequiredSection("MareSynchronos")); - services.Configure(Configuration.GetRequiredSection("MareSynchronos")); + services.Configure(Configuration.GetRequiredSection("MareSynchronos")); services.AddSingleton(Configuration); services.AddSingleton(); services.AddSingleton(); services.AddHostedService(); services.AddSingleton, MareConfigurationServiceServer>(); services.AddSingleton, MareConfigurationServiceClient>(); - services.AddSingleton, MareConfigurationServiceClient>(); + services.AddSingleton, MareConfigurationServiceClient>(); - services.AddHostedService(p => (MareConfigurationServiceClient)p.GetService>()); + services.AddHostedService(p => (MareConfigurationServiceClient)p.GetService>()); services.AddHostedService(p => (MareConfigurationServiceClient)p.GetService>()); } } \ No newline at end of file diff --git a/MareSynchronosServer/MareSynchronosShared/Services/IConfigurationService.cs b/MareSynchronosServer/MareSynchronosShared/Services/IConfigurationService.cs index e5da0ca..85c21e6 100644 --- a/MareSynchronosServer/MareSynchronosShared/Services/IConfigurationService.cs +++ b/MareSynchronosServer/MareSynchronosShared/Services/IConfigurationService.cs @@ -1,4 +1,4 @@ -using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; namespace MareSynchronosShared.Services; diff --git a/MareSynchronosServer/MareSynchronosShared/Services/MareConfigurationController.cs b/MareSynchronosServer/MareSynchronosShared/Services/MareConfigurationController.cs index 8f66f27..4b9a01d 100644 --- a/MareSynchronosServer/MareSynchronosShared/Services/MareConfigurationController.cs +++ b/MareSynchronosServer/MareSynchronosShared/Services/MareConfigurationController.cs @@ -1,5 +1,4 @@ -using MareSynchronosShared.Utils; -using MareSynchronosStaticFilesServer; +using MareSynchronosShared.Utils.Configuration; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; @@ -38,9 +37,9 @@ public class MareStaticFilesServerConfigurationController : MareConfigurationCon } } -public class MareAuthBaseConfigurationController : MareConfigurationController +public class MareBaseConfigurationController : MareConfigurationController { - public MareAuthBaseConfigurationController(IOptionsMonitor config, ILogger logger) : base(config, logger) + public MareBaseConfigurationController(IOptionsMonitor config, ILogger logger) : base(config, logger) { } } diff --git a/MareSynchronosServer/MareSynchronosShared/Services/MareConfigurationServiceClient.cs b/MareSynchronosServer/MareSynchronosShared/Services/MareConfigurationServiceClient.cs index 4929342..6eaff9f 100644 --- a/MareSynchronosServer/MareSynchronosShared/Services/MareConfigurationServiceClient.cs +++ b/MareSynchronosServer/MareSynchronosShared/Services/MareConfigurationServiceClient.cs @@ -1,5 +1,5 @@ using MareSynchronosShared.Utils; -using MareSynchronosStaticFilesServer; +using MareSynchronosShared.Utils.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -27,8 +27,8 @@ public class MareConfigurationServiceClient : IHostedService, IConfigurationS { if (_config.CurrentValue.GetType() == typeof(ServerConfiguration)) return new Uri((_config.CurrentValue as ServerConfiguration).MainServerAddress, $"configuration/MareServerConfiguration/{nameof(MareServerConfigurationController.GetConfigurationEntry)}?key={key}&defaultValue={value}"); - if (_config.CurrentValue.GetType() == typeof(MareConfigurationAuthBase)) - return new Uri((_config.CurrentValue as MareConfigurationAuthBase).MainServerAddress, $"configuration/MareAuthBaseConfiguration/{nameof(MareAuthBaseConfigurationController.GetConfigurationEntry)}?key={key}&defaultValue={value}"); + if (_config.CurrentValue.GetType() == typeof(MareConfigurationBase)) + return new Uri((_config.CurrentValue as MareConfigurationBase).MainServerAddress, $"configuration/MareBaseConfiguration/{nameof(MareBaseConfigurationController.GetConfigurationEntry)}?key={key}&defaultValue={value}"); if (_config.CurrentValue.GetType() == typeof(ServicesConfiguration)) return new Uri((_config.CurrentValue as ServicesConfiguration).MainServerAddress, $"configuration/MareServicesConfiguration/{nameof(MareServicesConfigurationController.GetConfigurationEntry)}?key={key}&defaultValue={value}"); if (_config.CurrentValue.GetType() == typeof(StaticFilesServerConfiguration)) diff --git a/MareSynchronosServer/MareSynchronosShared/Services/MareConfigurationServiceServer.cs b/MareSynchronosServer/MareSynchronosShared/Services/MareConfigurationServiceServer.cs index 579c4fe..657f5a4 100644 --- a/MareSynchronosServer/MareSynchronosShared/Services/MareConfigurationServiceServer.cs +++ b/MareSynchronosServer/MareSynchronosShared/Services/MareConfigurationServiceServer.cs @@ -1,4 +1,5 @@ using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; using Microsoft.Extensions.Options; using System.Collections; using System.Text; diff --git a/MareSynchronosServer/MareSynchronosShared/Utils/MareConfigurationAuthBase.cs b/MareSynchronosServer/MareSynchronosShared/Utils/Configuration/AuthServiceConfiguration.cs similarity index 56% rename from MareSynchronosServer/MareSynchronosShared/Utils/MareConfigurationAuthBase.cs rename to MareSynchronosServer/MareSynchronosShared/Utils/Configuration/AuthServiceConfiguration.cs index a91c24e..63d4e7a 100644 --- a/MareSynchronosServer/MareSynchronosShared/Utils/MareConfigurationAuthBase.cs +++ b/MareSynchronosServer/MareSynchronosShared/Utils/Configuration/AuthServiceConfiguration.cs @@ -1,33 +1,26 @@ using System.Text; -namespace MareSynchronosShared.Utils; +namespace MareSynchronosShared.Utils.Configuration; -public class MareConfigurationAuthBase : MareConfigurationBase +public class AuthServiceConfiguration : MareConfigurationBase { - [RemoteConfiguration] - public int FailedAuthForTempBan { get; set; } = 5; - [RemoteConfiguration] - public int TempBanDurationInMinutes { get; set; } = 5; - [RemoteConfiguration] - public int RegisterIpLimit { get; set; } = 3; - [RemoteConfiguration] - public int RegisterIpDurationInMinutes { get; set; } = 10; - [RemoteConfiguration] - public List WhitelistedIps { get; set; } = new(); - [RemoteConfiguration] + public string GeoIPDbCityFile { get; set; } = string.Empty; public bool UseGeoIP { get; set; } = false; + public int FailedAuthForTempBan { get; set; } = 5; + public int TempBanDurationInMinutes { get; set; } = 5; + public List WhitelistedIps { get; set; } = new(); + + public int RegisterIpLimit { get; set; } = 3; + public int RegisterIpDurationInMinutes { get; set; } = 10; public override string ToString() { StringBuilder sb = new(); sb.AppendLine(base.ToString()); - sb.AppendLine($"{nameof(FailedAuthForTempBan)} => {FailedAuthForTempBan}"); - sb.AppendLine($"{nameof(TempBanDurationInMinutes)} => {TempBanDurationInMinutes}"); + sb.AppendLine($"{nameof(GeoIPDbCityFile)} => {GeoIPDbCityFile}"); + sb.AppendLine($"{nameof(UseGeoIP)} => {UseGeoIP}"); sb.AppendLine($"{nameof(RegisterIpLimit)} => {RegisterIpLimit}"); sb.AppendLine($"{nameof(RegisterIpDurationInMinutes)} => {RegisterIpDurationInMinutes}"); - sb.AppendLine($"{nameof(Jwt)} => {Jwt}"); - sb.AppendLine($"{nameof(WhitelistedIps)} => {string.Join(", ", WhitelistedIps)}"); - sb.AppendLine($"{nameof(UseGeoIP)} => {UseGeoIP}"); return sb.ToString(); } -} \ No newline at end of file +} diff --git a/MareSynchronosServer/MareSynchronosShared/Utils/CdnShardConfiguration.cs b/MareSynchronosServer/MareSynchronosShared/Utils/Configuration/CdnShardConfiguration.cs similarity index 84% rename from MareSynchronosServer/MareSynchronosShared/Utils/CdnShardConfiguration.cs rename to MareSynchronosServer/MareSynchronosShared/Utils/Configuration/CdnShardConfiguration.cs index 4ffe680..32ab367 100644 --- a/MareSynchronosServer/MareSynchronosShared/Utils/CdnShardConfiguration.cs +++ b/MareSynchronosServer/MareSynchronosShared/Utils/Configuration/CdnShardConfiguration.cs @@ -1,4 +1,4 @@ -namespace MareSynchronosShared.Utils; +namespace MareSynchronosShared.Utils.Configuration; public class CdnShardConfiguration { diff --git a/MareSynchronosServer/MareSynchronosShared/Utils/IMareConfiguration.cs b/MareSynchronosServer/MareSynchronosShared/Utils/Configuration/IMareConfiguration.cs similarity index 77% rename from MareSynchronosServer/MareSynchronosShared/Utils/IMareConfiguration.cs rename to MareSynchronosServer/MareSynchronosShared/Utils/Configuration/IMareConfiguration.cs index cc71b51..e18a839 100644 --- a/MareSynchronosServer/MareSynchronosShared/Utils/IMareConfiguration.cs +++ b/MareSynchronosServer/MareSynchronosShared/Utils/Configuration/IMareConfiguration.cs @@ -1,4 +1,4 @@ -namespace MareSynchronosShared.Utils; +namespace MareSynchronosShared.Utils.Configuration; public interface IMareConfiguration { diff --git a/MareSynchronosServer/MareSynchronosShared/Utils/MareConfigurationBase.cs b/MareSynchronosServer/MareSynchronosShared/Utils/Configuration/MareConfigurationBase.cs similarity index 95% rename from MareSynchronosServer/MareSynchronosShared/Utils/MareConfigurationBase.cs rename to MareSynchronosServer/MareSynchronosShared/Utils/Configuration/MareConfigurationBase.cs index 844a396..0bd2c62 100644 --- a/MareSynchronosServer/MareSynchronosShared/Utils/MareConfigurationBase.cs +++ b/MareSynchronosServer/MareSynchronosShared/Utils/Configuration/MareConfigurationBase.cs @@ -2,13 +2,14 @@ using System.Text; using System.Text.Json; -namespace MareSynchronosShared.Utils; +namespace MareSynchronosShared.Utils.Configuration; public class MareConfigurationBase : IMareConfiguration { public int DbContextPoolSize { get; set; } = 100; public string Jwt { get; set; } = string.Empty; public Uri MainServerAddress { get; set; } + public int RedisPool { get; set; } = 50; public int MetricsPort { get; set; } public string RedisConnectionString { get; set; } = string.Empty; public string ShardName { get; set; } = string.Empty; diff --git a/MareSynchronosServer/MareSynchronosShared/Utils/ServerConfiguration.cs b/MareSynchronosServer/MareSynchronosShared/Utils/Configuration/ServerConfiguration.cs similarity index 80% rename from MareSynchronosServer/MareSynchronosShared/Utils/ServerConfiguration.cs rename to MareSynchronosServer/MareSynchronosShared/Utils/Configuration/ServerConfiguration.cs index abb648a..c76bf74 100644 --- a/MareSynchronosServer/MareSynchronosShared/Utils/ServerConfiguration.cs +++ b/MareSynchronosServer/MareSynchronosShared/Utils/Configuration/ServerConfiguration.cs @@ -1,8 +1,8 @@ using System.Text; -namespace MareSynchronosShared.Utils; +namespace MareSynchronosShared.Utils.Configuration; -public class ServerConfiguration : MareConfigurationAuthBase +public class ServerConfiguration : MareConfigurationBase { [RemoteConfiguration] public Uri CdnFullUrl { get; set; } = null; @@ -24,9 +24,6 @@ public class ServerConfiguration : MareConfigurationAuthBase [RemoteConfiguration] public int PurgeUnusedAccountsPeriodInDays { get; set; } = 14; - public string GeoIPDbCityFile { get; set; } = string.Empty; - - public int RedisPool { get; set; } = 50; public override string ToString() { @@ -34,14 +31,12 @@ public class ServerConfiguration : MareConfigurationAuthBase sb.AppendLine(base.ToString()); sb.AppendLine($"{nameof(CdnFullUrl)} => {CdnFullUrl}"); sb.AppendLine($"{nameof(RedisConnectionString)} => {RedisConnectionString}"); - sb.AppendLine($"{nameof(RedisPool)} => {RedisPool}"); sb.AppendLine($"{nameof(ExpectedClientVersion)} => {ExpectedClientVersion}"); sb.AppendLine($"{nameof(MaxExistingGroupsByUser)} => {MaxExistingGroupsByUser}"); sb.AppendLine($"{nameof(MaxJoinedGroupsByUser)} => {MaxJoinedGroupsByUser}"); sb.AppendLine($"{nameof(MaxGroupUserCount)} => {MaxGroupUserCount}"); sb.AppendLine($"{nameof(PurgeUnusedAccounts)} => {PurgeUnusedAccounts}"); sb.AppendLine($"{nameof(PurgeUnusedAccountsPeriodInDays)} => {PurgeUnusedAccountsPeriodInDays}"); - sb.AppendLine($"{nameof(GeoIPDbCityFile)} => {GeoIPDbCityFile}"); return sb.ToString(); } } \ No newline at end of file diff --git a/MareSynchronosServer/MareSynchronosShared/Utils/ServicesConfiguration.cs b/MareSynchronosServer/MareSynchronosShared/Utils/Configuration/ServicesConfiguration.cs similarity index 93% rename from MareSynchronosServer/MareSynchronosShared/Utils/ServicesConfiguration.cs rename to MareSynchronosServer/MareSynchronosShared/Utils/Configuration/ServicesConfiguration.cs index 6e1768f..70e0b23 100644 --- a/MareSynchronosServer/MareSynchronosShared/Utils/ServicesConfiguration.cs +++ b/MareSynchronosServer/MareSynchronosShared/Utils/Configuration/ServicesConfiguration.cs @@ -1,6 +1,6 @@ using System.Text; -namespace MareSynchronosShared.Utils; +namespace MareSynchronosShared.Utils.Configuration; public class ServicesConfiguration : MareConfigurationBase { diff --git a/MareSynchronosServer/MareSynchronosShared/Utils/StaticFilesServerConfiguration.cs b/MareSynchronosServer/MareSynchronosShared/Utils/Configuration/StaticFilesServerConfiguration.cs similarity index 94% rename from MareSynchronosServer/MareSynchronosShared/Utils/StaticFilesServerConfiguration.cs rename to MareSynchronosServer/MareSynchronosShared/Utils/Configuration/StaticFilesServerConfiguration.cs index 996a503..cd13538 100644 --- a/MareSynchronosServer/MareSynchronosShared/Utils/StaticFilesServerConfiguration.cs +++ b/MareSynchronosServer/MareSynchronosShared/Utils/Configuration/StaticFilesServerConfiguration.cs @@ -1,14 +1,14 @@ using MareSynchronosShared.Utils; using System.Text; -namespace MareSynchronosStaticFilesServer; +namespace MareSynchronosShared.Utils.Configuration; public class StaticFilesServerConfiguration : MareConfigurationBase { public bool IsDistributionNode { get; set; } = false; public bool NotifyMainServerDirectly { get; set; } = false; - public Uri? MainFileServerAddress { get; set; } = null; - public Uri? DistributionFileServerAddress { get; set; } = null; + public Uri MainFileServerAddress { get; set; } = null; + public Uri DistributionFileServerAddress { get; set; } = null; public bool DistributionFileServerForceHTTP2 { get; set; } = false; public int ForcedDeletionOfFilesAfterHours { get; set; } = -1; public double CacheSizeHardLimitInGiB { get; set; } = -1; @@ -21,7 +21,7 @@ public class StaticFilesServerConfiguration : MareConfigurationBase public int DownloadQueueClearLimit { get; set; } = 15000; public int CleanupCheckInMinutes { get; set; } = 15; public bool UseColdStorage { get; set; } = false; - public string? ColdStorageDirectory { get; set; } = null; + public string ColdStorageDirectory { get; set; } = null; public double ColdStorageSizeHardLimitInGiB { get; set; } = -1; public int ColdStorageMinimumFileRetentionPeriodInDays { get; set; } = 30; public int ColdStorageUnusedFileRetentionPeriodInDays { get; set; } = 30; diff --git a/MareSynchronosServer/MareSynchronosShared/Utils/ServerTokenGenerator.cs b/MareSynchronosServer/MareSynchronosShared/Utils/ServerTokenGenerator.cs index 60df4c9..d7fffff 100644 --- a/MareSynchronosServer/MareSynchronosShared/Utils/ServerTokenGenerator.cs +++ b/MareSynchronosServer/MareSynchronosShared/Utils/ServerTokenGenerator.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.Logging; +using MareSynchronosShared.Utils.Configuration; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; using System.IdentityModel.Tokens.Jwt; @@ -9,7 +10,7 @@ namespace MareSynchronosShared.Utils; public class ServerTokenGenerator { - private readonly IOptionsMonitor _configuration; + private readonly IOptionsMonitor _configuration; private readonly ILogger _logger; private Dictionary _tokenDictionary { get; set; } = new(StringComparer.Ordinal); @@ -27,7 +28,7 @@ public class ServerTokenGenerator } } - public ServerTokenGenerator(IOptionsMonitor configuration, ILogger logger) + public ServerTokenGenerator(IOptionsMonitor configuration, ILogger logger) { _configuration = configuration; _logger = logger; diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Controllers/DistributionController.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Controllers/DistributionController.cs index 88cef12..f18dd10 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Controllers/DistributionController.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Controllers/DistributionController.cs @@ -1,5 +1,6 @@ using MareSynchronos.API.Routes; using MareSynchronosShared.Services; +using MareSynchronosShared.Utils.Configuration; using MareSynchronosStaticFilesServer.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Controllers/ServerFilesController.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Controllers/ServerFilesController.cs index 0fa1201..7f95358 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Controllers/ServerFilesController.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Controllers/ServerFilesController.cs @@ -7,13 +7,12 @@ using MareSynchronosShared.Data; using MareSynchronosShared.Metrics; using MareSynchronosShared.Models; using MareSynchronosShared.Services; -using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; using MareSynchronosStaticFilesServer.Services; using MareSynchronosStaticFilesServer.Utils; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Configuration; using System.Collections.Concurrent; using System.Security.Cryptography; using System.Security.Policy; diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Program.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Program.cs index 22bb2c1..42da70d 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Program.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Program.cs @@ -1,5 +1,5 @@ using MareSynchronosShared.Services; -using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; namespace MareSynchronosStaticFilesServer; @@ -13,7 +13,7 @@ public class Program using (var scope = host.Services.CreateScope()) { var options = host.Services.GetService>(); - var optionsServer = host.Services.GetService>(); + var optionsServer = host.Services.GetService>(); var logger = host.Services.GetService>(); logger.LogInformation("Loaded MareSynchronos Static Files Server Configuration (IsMain: {isMain})", options.IsMain); logger.LogInformation(options.ToString()); diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/CachedFileProvider.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/CachedFileProvider.cs index 2a8536b..bc0c987 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/CachedFileProvider.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/CachedFileProvider.cs @@ -5,6 +5,7 @@ using System.Collections.Concurrent; using System.Net.Http.Headers; using MareSynchronosShared.Utils; using MareSynchronos.API.Routes; +using MareSynchronosShared.Utils.Configuration; namespace MareSynchronosStaticFilesServer.Services; diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ColdTouchHashService.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ColdTouchHashService.cs index d9118b6..f63d5ce 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ColdTouchHashService.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ColdTouchHashService.cs @@ -1,4 +1,5 @@ using MareSynchronosShared.Services; +using MareSynchronosShared.Utils.Configuration; using MareSynchronosStaticFilesServer.Utils; using System.Collections.Concurrent; diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/FileCleanupService.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/FileCleanupService.cs index 5ede841..ff18cfe 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/FileCleanupService.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/FileCleanupService.cs @@ -4,6 +4,7 @@ using MareSynchronosShared.Data; using MareSynchronosShared.Metrics; using MareSynchronosShared.Models; using MareSynchronosShared.Services; +using MareSynchronosShared.Utils.Configuration; using MareSynchronosStaticFilesServer.Utils; using MessagePack.Formatters; using Microsoft.EntityFrameworkCore; diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/RequestQueueService.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/RequestQueueService.cs index 0458e57..7170226 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/RequestQueueService.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/RequestQueueService.cs @@ -1,8 +1,8 @@ using MareSynchronosShared.Metrics; using MareSynchronosShared.Services; +using MareSynchronosShared.Utils.Configuration; using MareSynchronosStaticFilesServer.Utils; using System.Collections.Concurrent; -using System.Linq; using System.Timers; namespace MareSynchronosStaticFilesServer.Services; diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ShardClientReadyMessageService.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ShardClientReadyMessageService.cs index 3e4a1c8..b8584f2 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ShardClientReadyMessageService.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ShardClientReadyMessageService.cs @@ -1,6 +1,7 @@ using MareSynchronos.API.Routes; using MareSynchronosShared.Services; using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; using System.Net.Http.Headers; namespace MareSynchronosStaticFilesServer.Services; diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ShardTouchMessageService.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ShardTouchMessageService.cs index 78a59df..5a02881 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ShardTouchMessageService.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ShardTouchMessageService.cs @@ -1,6 +1,7 @@ using MareSynchronos.API.Routes; using MareSynchronosShared.Services; using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; using System.Net.Http.Headers; namespace MareSynchronosStaticFilesServer.Services; diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Startup.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Startup.cs index a49e525..9c01a36 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Startup.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Startup.cs @@ -20,6 +20,7 @@ using StackExchange.Redis.Extensions.System.Text.Json; using StackExchange.Redis; using System.Net; using System.Text; +using MareSynchronosShared.Utils.Configuration; namespace MareSynchronosStaticFilesServer; @@ -49,7 +50,7 @@ public class Startup services.AddLogging(); services.Configure(Configuration.GetRequiredSection("MareSynchronos")); - services.Configure(Configuration.GetRequiredSection("MareSynchronos")); + services.Configure(Configuration.GetRequiredSection("MareSynchronos")); services.Configure(Configuration.GetSection("Kestrel")); services.AddSingleton(Configuration); @@ -93,8 +94,8 @@ public class Startup services.AddSingleton(); services.AddHostedService(p => p.GetService()); services.AddHostedService(m => m.GetService()); - services.AddSingleton, MareConfigurationServiceClient>(); - services.AddHostedService(p => (MareConfigurationServiceClient)p.GetService>()); + services.AddSingleton, MareConfigurationServiceClient>(); + services.AddHostedService(p => (MareConfigurationServiceClient)p.GetService>()); // specific services if (_isMain) @@ -212,7 +213,7 @@ public class Startup // authentication and authorization services.AddOptions(JwtBearerDefaults.AuthenticationScheme) - .Configure>((o, s) => + .Configure>((o, s) => { o.TokenValidationParameters = new() { @@ -220,7 +221,7 @@ public class Startup ValidateLifetime = false, ValidateAudience = false, ValidateIssuerSigningKey = true, - IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(s.GetValue(nameof(MareConfigurationAuthBase.Jwt)))) + IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(s.GetValue(nameof(MareConfigurationBase.Jwt)))) }; }); services.AddAuthentication(o => @@ -246,7 +247,7 @@ public class Startup app.UseRouting(); - var config = app.ApplicationServices.GetRequiredService>(); + var config = app.ApplicationServices.GetRequiredService>(); var metricServer = new KestrelMetricServer(config.GetValueOrDefault(nameof(MareConfigurationBase.MetricsPort), 4981)); metricServer.Start(); diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Utils/FilePathUtil.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Utils/FilePathUtil.cs index 2dcc40a..cd0d04d 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Utils/FilePathUtil.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Utils/FilePathUtil.cs @@ -1,6 +1,4 @@ -using System.Text.RegularExpressions; - -namespace MareSynchronosStaticFilesServer.Utils; +namespace MareSynchronosStaticFilesServer.Utils; public static partial class FilePathUtil { diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Utils/RequestBlockFileListResult.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Utils/RequestBlockFileListResult.cs index 54420cf..303e287 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Utils/RequestBlockFileListResult.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Utils/RequestBlockFileListResult.cs @@ -1,5 +1,6 @@ using MareSynchronosShared.Metrics; using MareSynchronosShared.Services; +using MareSynchronosShared.Utils.Configuration; using MareSynchronosStaticFilesServer.Services; using Microsoft.AspNetCore.Mvc; using System.Globalization; diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Utils/RequestBlockFileListResultFactory.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Utils/RequestBlockFileListResultFactory.cs index 3cd35d2..b9103a6 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Utils/RequestBlockFileListResultFactory.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Utils/RequestBlockFileListResultFactory.cs @@ -1,5 +1,6 @@ using MareSynchronosShared.Metrics; using MareSynchronosShared.Services; +using MareSynchronosShared.Utils.Configuration; using MareSynchronosStaticFilesServer.Services; namespace MareSynchronosStaticFilesServer.Utils;