From 7e44b56ec4112f5e131c7fb96838b18550a62c85 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 21 Nov 2022 11:08:48 +0800 Subject: Fix #1354 #1355 . --- .../Timeline/Configs/ApplicationConfiguration.cs | 39 +++++++++++++++++++++- .../DatabaseManagementService.cs | 11 ++++-- BackEnd/Timeline/Startup.cs | 34 ++++++++++++++++++- 3 files changed, 80 insertions(+), 4 deletions(-) (limited to 'BackEnd/Timeline') diff --git a/BackEnd/Timeline/Configs/ApplicationConfiguration.cs b/BackEnd/Timeline/Configs/ApplicationConfiguration.cs index c7c679d1..c808e946 100644 --- a/BackEnd/Timeline/Configs/ApplicationConfiguration.cs +++ b/BackEnd/Timeline/Configs/ApplicationConfiguration.cs @@ -1,4 +1,8 @@ -namespace Timeline.Configs +using System; +using System.Collections.Generic; +using Microsoft.Extensions.Configuration; + +namespace Timeline.Configs { public static class ApplicationConfiguration { @@ -7,5 +11,38 @@ public const string DatabaseFileName = "timeline.db"; public const string DatabaseBackupDirectoryName = "backup"; public const string FrontEndKey = "FrontEnd"; + public const string DisableAutoBackupKey = "DisableAutoBackup"; + public const string EnableForwardedHeadersKey = "EnableForwardedHeaders"; + public const string ForwardedHeadersAllowedProxyHostsKey = "ForwardedHeadersAllowedProxyHosts"; + + public static bool CheckIsValidBoolString(string? value, string configPath, Boolean defaultValue) + { + if (value is null) + { + return defaultValue; + } + + var true_strings = new List { "true", "1", "y", "yes", "on" }; + var false_strings = new List { "false", "0", "n", "no", "off" }; + + if (true_strings.Contains(value.ToLowerInvariant())) + { + return true; + } + else if (false_strings.Contains(value.ToLowerInvariant())) + { + return false; + } + else + { + throw new Exception($"Invalid boolean value {value} in config {configPath}."); + } + } + + public static bool GetBoolConfig(IConfiguration configuration, string configPath, bool defaultValue) + { + var value = configuration.GetValue(configPath); + return CheckIsValidBoolString(value, configPath, defaultValue); + } } } diff --git a/BackEnd/Timeline/Services/DatabaseManagement/DatabaseManagementService.cs b/BackEnd/Timeline/Services/DatabaseManagement/DatabaseManagementService.cs index c3d9ac4e..a4814069 100644 --- a/BackEnd/Timeline/Services/DatabaseManagement/DatabaseManagementService.cs +++ b/BackEnd/Timeline/Services/DatabaseManagement/DatabaseManagementService.cs @@ -1,9 +1,11 @@ using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; using System.Threading; using System.Threading.Tasks; +using Timeline.Configs; using Timeline.Entities; namespace Timeline.Services.DatabaseManagement @@ -11,10 +13,12 @@ namespace Timeline.Services.DatabaseManagement public class DatabaseManagementService : IHostedService { private readonly IServiceProvider _serviceProvider; + private readonly bool _disableAutoBackup; - public DatabaseManagementService(IServiceProvider serviceProvider) + public DatabaseManagementService(IServiceProvider serviceProvider, IConfiguration configuration) { _serviceProvider = serviceProvider; + _disableAutoBackup = ApplicationConfiguration.GetBoolConfig(configuration, ApplicationConfiguration.DisableAutoBackupKey, false); } public async Task StartAsync(CancellationToken cancellationToken = default) @@ -27,7 +31,10 @@ namespace Timeline.Services.DatabaseManagement var customMigrator = provider.GetRequiredService(); - await backupService.BackupAsync(cancellationToken); + if (!_disableAutoBackup) + { + await backupService.BackupAsync(cancellationToken); + } await database.Database.MigrateAsync(cancellationToken); await customMigrator.MigrateAsync(cancellationToken); } diff --git a/BackEnd/Timeline/Startup.cs b/BackEnd/Timeline/Startup.cs index 3f94f8a9..fa4968d7 100644 --- a/BackEnd/Timeline/Startup.cs +++ b/BackEnd/Timeline/Startup.cs @@ -33,6 +33,8 @@ namespace Timeline { public class Startup { + private readonly bool _enableForwardedHeaders; + private readonly string? _forwardedHeadersAllowedProxyHosts; private readonly FrontEndMode _frontEndMode; public Startup(IConfiguration configuration, IWebHostEnvironment environment) @@ -51,9 +53,12 @@ namespace Timeline if (!Enum.TryParse(frontEndModeString, true, out _frontEndMode)) { _frontEndMode = FrontEndMode.Normal; - Console.WriteLine("Unknown FrontEnd configuaration value '{0}', fallback to normal.", frontEndModeString); + Console.WriteLine("Unknown FrontEnd configuration value '{0}', fallback to normal.", frontEndModeString); } } + + _enableForwardedHeaders = ApplicationConfiguration.GetBoolConfig(configuration, ApplicationConfiguration.EnableForwardedHeadersKey, false); + _forwardedHeadersAllowedProxyHosts = Configuration.GetValue(ApplicationConfiguration.ForwardedHeadersAllowedProxyHostsKey); } public IWebHostEnvironment Environment { get; } @@ -135,6 +140,28 @@ namespace Timeline config.RootPath = "ClientApp"; }); } + + if (_enableForwardedHeaders) + { + services.Configure(options => + { + options.ForwardedHeaders = Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedFor | Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedProto; + if (_forwardedHeadersAllowedProxyHosts is not null) + { + options.KnownNetworks.Clear(); + options.KnownProxies.Clear(); + foreach (var host in _forwardedHeadersAllowedProxyHosts.Split(new char[] { ';', ',' })) + { + // Resolve host to ip + var ips = System.Net.Dns.GetHostAddresses(host); + foreach (var ip in ips) + { + options.KnownProxies.Add(ip); + } + } + } + }); + } } @@ -151,6 +178,11 @@ namespace Timeline }); } + if (_enableForwardedHeaders) + { + app.UseForwardedHeaders(); + } + app.UseOpenApi(); app.UseReDoc(); -- cgit v1.2.3