diff --git a/MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthReply.cs b/MareSynchronosServer/MareSynchronosAuthService/Authentication/SecretKeyAuthReply.cs similarity index 69% rename from MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthReply.cs rename to MareSynchronosServer/MareSynchronosAuthService/Authentication/SecretKeyAuthReply.cs index a8679f8..00f294e 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 PrimaryUid, 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 96% rename from MareSynchronosServer/MareSynchronosServer/Controllers/JwtController.cs rename to MareSynchronosServer/MareSynchronosAuthService/Controllers/JwtController.cs index de0e0ba..ab76182 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; @@ -16,7 +16,7 @@ using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Text; -namespace MareSynchronosServer.Controllers; +namespace MareSynchronosAuthService.Controllers; [AllowAnonymous] [Route(MareAuth.Auth)] @@ -24,7 +24,7 @@ public class JwtController : Controller { private readonly ILogger _logger; private readonly IHttpContextAccessor _accessor; - private readonly IConfigurationService _configuration; + private readonly IConfigurationService _configuration; private readonly MareDbContext _mareDbContext; private readonly IRedisDatabase _redis; private readonly GeoIPService _geoIPProvider; @@ -33,7 +33,7 @@ public class JwtController : Controller public JwtController(ILogger logger, IHttpContextAccessor accessor, MareDbContext mareDbContext, SecretKeyAuthenticatorService secretKeyAuthenticatorService, - IConfigurationService configuration, + IConfigurationService configuration, IRedisDatabase redisDb, GeoIPService geoIPProvider) { _logger = logger; @@ -138,7 +138,7 @@ public class JwtController : Controller private JwtSecurityToken CreateJwt(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..c46484a --- /dev/null +++ b/MareSynchronosServer/MareSynchronosAuthService/MareSynchronosAuthService.csproj @@ -0,0 +1,18 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + diff --git a/MareSynchronosServer/MareSynchronosAuthService/Program.cs b/MareSynchronosServer/MareSynchronosAuthService/Program.cs new file mode 100644 index 0000000..983f773 --- /dev/null +++ b/MareSynchronosServer/MareSynchronosAuthService/Program.cs @@ -0,0 +1,41 @@ +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() + .UseConsoleLifetime() + .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/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 85% rename from MareSynchronosServer/MareSynchronosServer/Authentication/SecretKeyAuthenticatorService.cs rename to MareSynchronosServer/MareSynchronosAuthService/Services/SecretKeyAuthenticatorService.cs index f090294..9c806f6 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 IDbContextFactory _dbContextFactory; - private readonly IConfigurationService _configurationService; + private readonly IConfigurationService _configurationService; private readonly ILogger _logger; private readonly ConcurrentDictionary _failedAuthorizations = new(StringComparer.Ordinal); public SecretKeyAuthenticatorService(MareMetrics metrics, IDbContextFactory dbContextFactory, - 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) => { @@ -80,7 +81,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.Exists(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..ab96079 --- /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"); + + ConfigureRedis(services, mareConfig); + + 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); + + 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.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(); + policy.AddRequirements(new ValidTokenRequirement()); + }); + options.AddPolicy("Identified", policy => + { + policy.AddRequirements(new UserRequirement(UserRequirements.Identified)); + policy.AddRequirements(new ValidTokenRequirement()); + + }); + options.AddPolicy("Admin", policy => + { + policy.AddRequirements(new UserRequirement(UserRequirements.Identified | UserRequirements.Administrator)); + policy.AddRequirements(new ValidTokenRequirement()); + + }); + options.AddPolicy("Moderator", policy => + { + policy.AddRequirements(new UserRequirement(UserRequirements.Identified | UserRequirements.Moderator | UserRequirements.Administrator)); + policy.AddRequirements(new ValidTokenRequirement()); + }); + 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, + }, new List + { + MetricsAPI.GaugeAuthenticationCacheEntries, + })); + } + + 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 6edf0f4..9df8f0d 100644 --- a/MareSynchronosServer/MareSynchronosServer.sln +++ b/MareSynchronosServer/MareSynchronosServer.sln @@ -20,6 +20,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 @@ -50,6 +52,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 330ff81..7bd90f8 100644 --- a/MareSynchronosServer/MareSynchronosServer/Hubs/MareHub.cs +++ b/MareSynchronosServer/MareSynchronosServer/Hubs/MareHub.cs @@ -9,7 +9,7 @@ using MareSynchronosShared.Data; using MareSynchronosShared.Metrics; using MareSynchronosShared.Models; using MareSynchronosShared.Services; -using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.SignalR; using Microsoft.EntityFrameworkCore; diff --git a/MareSynchronosServer/MareSynchronosServer/MareSynchronosServer.csproj b/MareSynchronosServer/MareSynchronosServer/MareSynchronosServer.csproj index 11ec481..59dd8a7 100644 --- a/MareSynchronosServer/MareSynchronosServer/MareSynchronosServer.csproj +++ b/MareSynchronosServer/MareSynchronosServer/MareSynchronosServer.csproj @@ -26,7 +26,6 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/MareSynchronosServer/MareSynchronosServer/Program.cs b/MareSynchronosServer/MareSynchronosServer/Program.cs index e6dcfc9..86d3841 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 437170b..d90474e 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 7516f12..e906420 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 bfc0d9f..10c263a 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))); + a.FeatureProviders.Add(new AllowedControllersFeatureProvider(typeof(MareServerConfigurationController), typeof(MareAuthBaseConfigurationController), typeof(ClientMessageController))); } 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(); @@ -100,10 +99,8 @@ public class Startup if (isMainServer) { - services.AddSingleton(); services.AddSingleton(); services.AddHostedService(provider => provider.GetService()); - services.AddHostedService(provider => provider.GetService()); } } @@ -188,13 +185,12 @@ public class Startup private static void ConfigureAuthorization(IServiceCollection services) { - services.AddSingleton(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddOptions(JwtBearerDefaults.AuthenticationScheme) - .Configure>((options, config) => + .Configure>((options, config) => { options.TokenValidationParameters = new() { @@ -202,7 +198,7 @@ public class Startup ValidateLifetime = true, 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)))), }; }); @@ -305,15 +301,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>(); } } @@ -321,7 +317,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 4c7c289..564ecc5 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 Microsoft.Extensions.Logging; diff --git a/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs b/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs index d0a46c0..75e067f 100644 --- a/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs +++ b/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs @@ -8,6 +8,7 @@ using MareSynchronosShared.Utils; using MareSynchronosShared.Services; using StackExchange.Redis; using MareSynchronos.API.Data.Enum; +using MareSynchronosShared.Utils.Configuration; namespace MareSynchronosServices.Discord; diff --git a/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.AprilFools2024.cs b/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.AprilFools2024.cs index 787b4b5..6b44712 100644 --- a/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.AprilFools2024.cs +++ b/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.AprilFools2024.cs @@ -1,6 +1,6 @@ using Discord; using Discord.Interactions; -using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; using System.Text.Json; namespace MareSynchronosServices.Discord; diff --git a/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.Delete.cs b/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.Delete.cs index b5aef5b..8f09cd4 100644 --- a/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.Delete.cs +++ b/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.Delete.cs @@ -2,6 +2,7 @@ using Discord; using MareSynchronosShared.Data; using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; namespace MareSynchronosServices.Discord; diff --git a/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.cs b/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.cs index a133127..4865d48 100644 --- a/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.cs +++ b/MareSynchronosServer/MareSynchronosServices/Discord/MareWizardModule.cs @@ -5,6 +5,7 @@ using MareSynchronosShared.Data; using MareSynchronosShared.Models; using MareSynchronosShared.Services; using MareSynchronosShared.Utils; +using MareSynchronosShared.Utils.Configuration; using Microsoft.EntityFrameworkCore; using StackExchange.Redis; using System.Text.RegularExpressions; diff --git a/MareSynchronosServer/MareSynchronosServices/Program.cs b/MareSynchronosServer/MareSynchronosServices/Program.cs index ed66bdc..aabdc3a 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 3128203..e8b7437 100644 --- a/MareSynchronosServer/MareSynchronosServices/Startup.cs +++ b/MareSynchronosServer/MareSynchronosServices/Startup.cs @@ -8,6 +8,7 @@ using MareSynchronosShared.Services; using StackExchange.Redis; using MessagePack.Resolvers; using MessagePack; +using MareSynchronosShared.Utils.Configuration; namespace MareSynchronosServices; @@ -22,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(); @@ -88,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..3543b10 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; diff --git a/MareSynchronosServer/MareSynchronosShared/Services/MareConfigurationServiceClient.cs b/MareSynchronosServer/MareSynchronosShared/Services/MareConfigurationServiceClient.cs index 4929342..61d2be8 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/MareAuthBaseConfiguration/{nameof(MareAuthBaseConfigurationController.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/Configuration/AuthServiceConfiguration.cs b/MareSynchronosServer/MareSynchronosShared/Utils/Configuration/AuthServiceConfiguration.cs new file mode 100644 index 0000000..e85f6b7 --- /dev/null +++ b/MareSynchronosServer/MareSynchronosShared/Utils/Configuration/AuthServiceConfiguration.cs @@ -0,0 +1,22 @@ +using System.Text; + +namespace MareSynchronosShared.Utils.Configuration; + +public class AuthServiceConfiguration : MareConfigurationBase +{ + 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 override string ToString() + { + StringBuilder sb = new(); + sb.AppendLine(base.ToString()); + sb.AppendLine($"{nameof(RedisPool)} => {RedisPool}"); + sb.AppendLine($"{nameof(GeoIPDbCityFile)} => {GeoIPDbCityFile}"); + sb.AppendLine($"{nameof(UseGeoIP)} => {UseGeoIP}"); + return sb.ToString(); + } +} diff --git a/MareSynchronosServer/MareSynchronosShared/Utils/CdnShardConfiguration.cs b/MareSynchronosServer/MareSynchronosShared/Utils/Configuration/CdnShardConfiguration.cs similarity index 85% 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 88% rename from MareSynchronosServer/MareSynchronosShared/Utils/ServerConfiguration.cs rename to MareSynchronosServer/MareSynchronosShared/Utils/Configuration/ServerConfiguration.cs index abb648a..0c0b466 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; @@ -26,15 +26,12 @@ public class ServerConfiguration : MareConfigurationAuthBase public int PurgeUnusedAccountsPeriodInDays { get; set; } = 14; public string GeoIPDbCityFile { get; set; } = string.Empty; - public int RedisPool { get; set; } = 50; - public override string ToString() { StringBuilder sb = new(); 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}"); diff --git a/MareSynchronosServer/MareSynchronosShared/Utils/ServicesConfiguration.cs b/MareSynchronosServer/MareSynchronosShared/Utils/Configuration/ServicesConfiguration.cs similarity index 96% rename from MareSynchronosServer/MareSynchronosShared/Utils/ServicesConfiguration.cs rename to MareSynchronosServer/MareSynchronosShared/Utils/Configuration/ServicesConfiguration.cs index df09f4c..be7347e 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 90% rename from MareSynchronosServer/MareSynchronosShared/Utils/StaticFilesServerConfiguration.cs rename to MareSynchronosServer/MareSynchronosShared/Utils/Configuration/StaticFilesServerConfiguration.cs index 8882b62..7da2d80 100644 --- a/MareSynchronosServer/MareSynchronosShared/Utils/StaticFilesServerConfiguration.cs +++ b/MareSynchronosServer/MareSynchronosShared/Utils/Configuration/StaticFilesServerConfiguration.cs @@ -1,13 +1,13 @@ using MareSynchronosShared.Utils; using System.Text; -namespace MareSynchronosStaticFilesServer; +namespace MareSynchronosShared.Utils.Configuration; public class StaticFilesServerConfiguration : MareConfigurationBase { public bool IsDistributionNode { 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 int ForcedDeletionOfFilesAfterHours { get; set; } = -1; public double CacheSizeHardLimitInGiB { get; set; } = -1; public int UnusedFileRetentionPeriodInDays { get; set; } = 14; @@ -18,7 +18,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 ColdStorageUnusedFileRetentionPeriodInDays { get; set; } = 30; [RemoteConfiguration] diff --git a/MareSynchronosServer/MareSynchronosShared/Utils/MareConfigurationAuthBase.cs b/MareSynchronosServer/MareSynchronosShared/Utils/MareConfigurationAuthBase.cs deleted file mode 100644 index 749fbd6..0000000 --- a/MareSynchronosServer/MareSynchronosShared/Utils/MareConfigurationAuthBase.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System.Text; - -namespace MareSynchronosShared.Utils; - -public class MareConfigurationAuthBase : MareConfigurationBase -{ - [RemoteConfiguration] - public int FailedAuthForTempBan { get; set; } = 5; - [RemoteConfiguration] - public int TempBanDurationInMinutes { get; set; } = 5; - [RemoteConfiguration] - public List WhitelistedIps { get; set; } = new(); - [RemoteConfiguration] - public bool UseGeoIP { get; set; } = false; - - public override string ToString() - { - StringBuilder sb = new(); - sb.AppendLine(base.ToString()); - sb.AppendLine($"{nameof(FailedAuthForTempBan)} => {FailedAuthForTempBan}"); - sb.AppendLine($"{nameof(TempBanDurationInMinutes)} => {TempBanDurationInMinutes}"); - 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/ServerTokenGenerator.cs b/MareSynchronosServer/MareSynchronosShared/Utils/ServerTokenGenerator.cs index d70b6c3..3ee1df9 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.Globalization; @@ -10,7 +11,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); @@ -28,7 +29,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/ServerFilesController.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Controllers/ServerFilesController.cs index 6141418..9f4e7ed 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.Text.Json; diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Program.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Program.cs index 9b58f03..ce5d80c 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 d3508f1..f687dcd 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/MainFileCleanupService.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/MainFileCleanupService.cs index a766d6c..835352e 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/MainFileCleanupService.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/MainFileCleanupService.cs @@ -3,6 +3,7 @@ using MareSynchronosShared.Data; using MareSynchronosShared.Metrics; using MareSynchronosShared.Models; using MareSynchronosShared.Services; +using MareSynchronosShared.Utils.Configuration; using MareSynchronosStaticFilesServer.Utils; using Microsoft.EntityFrameworkCore; diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/RequestQueueService.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/RequestQueueService.cs index 275120f..22a75e3 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 eb52190..655928d 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/ShardFileCleanupService.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ShardFileCleanupService.cs index 3bbbd61..ade54da 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ShardFileCleanupService.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ShardFileCleanupService.cs @@ -1,6 +1,7 @@ using ByteSizeLib; using MareSynchronosShared.Metrics; using MareSynchronosShared.Services; +using MareSynchronosShared.Utils.Configuration; using Microsoft.EntityFrameworkCore; namespace MareSynchronosStaticFilesServer.Services; diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Startup.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Startup.cs index 98337bb..9358fc0 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; @@ -47,7 +48,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); @@ -88,8 +89,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) @@ -198,7 +199,7 @@ public class Startup // authentication and authorization services.AddOptions(JwtBearerDefaults.AuthenticationScheme) - .Configure>((o, s) => + .Configure>((o, s) => { o.TokenValidationParameters = new() { @@ -206,7 +207,7 @@ public class Startup ValidateLifetime = true, 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 => @@ -232,7 +233,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/RequestFileStreamResultFactory.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Utils/RequestFileStreamResultFactory.cs index 43a952d..fd8fdac 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Utils/RequestFileStreamResultFactory.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Utils/RequestFileStreamResultFactory.cs @@ -1,5 +1,6 @@ using MareSynchronosShared.Metrics; using MareSynchronosShared.Services; +using MareSynchronosShared.Utils.Configuration; using MareSynchronosStaticFilesServer.Services; namespace MareSynchronosStaticFilesServer.Utils;