From 6ceea8187d39d4ed4e2c801120b2e1b008e93d0d Mon Sep 17 00:00:00 2001 From: nd Date: Sat, 6 Apr 2024 17:03:42 +0200 Subject: [PATCH] Update --- README.md | 18 +++++----- .../Models/DiscoveryConfig.cs | 7 ++++ src/wan24-AutoDiscover/Program.cs | 34 +++++++++++++++++-- .../Properties/launchSettings.json | 1 - src/wan24-AutoDiscover/appsettings.json | 6 ++-- 5 files changed, 51 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index ebc096c..f761044 100644 --- a/README.md +++ b/README.md @@ -26,13 +26,16 @@ example: "Kestrel": { "Endpoints": { "AutoDiscover": { - "Url": "http://localhost:5000" + "Url": "http://127.0.0.1:5000" } } }, "AllowedHosts": "*", "DiscoveryConfig": { "PreForkResponses": 10, + "KnownProxies": [ + "127.0.0.1" + ], "Discovery": { "localhost": { "AcceptedDomains": [ @@ -98,14 +101,11 @@ Create the file `/etc/apache2/sites-available/autodiscover.conf`: ServerName [DOMAIN] SSLEngine on - SSLCertificateFile /etc/letsencrypt/live/[DOMAIN]/fullchain.pem - SSLCertificateKeyFile /etc/letsencrypt/[DOMAIN]/privkey.pem + SSLCertificateFile /path/to/fullchain.pem + SSLCertificateKeyFile /path/to/privkey.pem ProxyPreserveHost On - ProxyPass / http://localhost:5000/ - ProxyPassReverse / http://localhost:5000/ - RewriteEngine on - RewriteCond %{SERVER_NAME} =[DOMAIN] - RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] + ProxyPass / http://127.0.0.1:5000/ + ProxyPassReverse / http://127.0.0.1:5000/ ``` @@ -115,7 +115,7 @@ domain name which you'd like to use for serving autodiscover. Then activate the proxy: ```bash -a2enmod rewrite proxy +a2enmod proxy a2ensite autodiscover systemctl restart apache2 ``` diff --git a/src/wan24-AutoDiscover Shared/Models/DiscoveryConfig.cs b/src/wan24-AutoDiscover Shared/Models/DiscoveryConfig.cs index d98a9a3..eed1362 100644 --- a/src/wan24-AutoDiscover Shared/Models/DiscoveryConfig.cs +++ b/src/wan24-AutoDiscover Shared/Models/DiscoveryConfig.cs @@ -2,8 +2,10 @@ using System.Collections; using System.Collections.Frozen; using System.ComponentModel.DataAnnotations; +using System.Net; using System.Text.Json.Serialization; using wan24.Core; +using wan24.ObjectValidation; namespace wan24.AutoDiscover.Models { @@ -44,6 +46,11 @@ public DiscoveryConfig() { } : TypeHelper.Instance.GetType(DiscoveryTypeName) ?? throw new InvalidDataException($"Discovery type {DiscoveryTypeName.ToQuotedLiteral()} not found"); + /// + /// Known http proxies + /// + public HashSet KnownProxies { get; init; } = []; + /// /// Get the discovery configuration /// diff --git a/src/wan24-AutoDiscover/Program.cs b/src/wan24-AutoDiscover/Program.cs index b13489f..e638315 100644 --- a/src/wan24-AutoDiscover/Program.cs +++ b/src/wan24-AutoDiscover/Program.cs @@ -1,3 +1,5 @@ +using Microsoft.AspNetCore.HttpLogging; +using Microsoft.AspNetCore.HttpOverrides; using wan24.AutoDiscover.Models; using wan24.AutoDiscover.Services; using wan24.CLI; @@ -36,7 +38,7 @@ Settings.ProcessId = "webservice"; Settings.LogLevel = config.GetValue("Logging:LogLevel:Default"); Logging.Logger = discovery.LogFile is string logFile && !string.IsNullOrWhiteSpace(logFile) - ? await FileLogger.CreateAsync(logFile).DynamicContext() + ? await FileLogger.CreateAsync(logFile, next: new VividConsoleLogger()).DynamicContext() : new VividConsoleLogger(); ErrorHandling.ErrorHandler = (e) => Logging.WriteError($"{e.Info}: {e.Exception}"); Logging.WriteInfo($"Using configuration \"{configFile}\""); @@ -101,16 +103,42 @@ void ReloadConfig(object sender, FileSystemEventArgs e) // Build and run the app Logging.WriteInfo("Autodiscovery service app startup"); WebApplicationBuilder builder = WebApplication.CreateBuilder(args); +builder.Logging.ClearProviders() + .AddConsole(); +if (ENV.IsLinux) + builder.Logging.AddSystemdConsole(); builder.Services.AddControllers(); builder.Services.AddSingleton(typeof(XmlDocumentInstances), services => new XmlDocumentInstances(capacity: discovery.PreForkResponses)) .AddHostedService(services => services.GetRequiredService()) - .AddExceptionHandler(); + .AddExceptionHandler() + .AddHttpLogging(options => options.LoggingFields = HttpLoggingFields.RequestPropertiesAndHeaders); +builder.Services.Configure(options => +{ + options.ForwardLimit = 2; + options.KnownProxies.AddRange(discovery.KnownProxies); + options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; +}); WebApplication app = builder.Build(); try { await using (app.DynamicContext()) { - app.UseExceptionHandler(b => { });// .NET 8 bugfix :( + app.UseForwardedHeaders(); + if (app.Environment.IsDevelopment()) + { + if (Logging.Trace) + Logging.WriteTrace("Using development environment"); + app.UseHttpLogging(); + } + app.UseExceptionHandler(builder => { });// .NET 8 bugfix :( + if (!app.Environment.IsDevelopment()) + { + if (Logging.Trace) + Logging.WriteTrace("Using production environment"); + app.UseHsts(); + app.UseHttpsRedirection(); + app.UseAuthorization(); + } app.MapControllers(); Logging.WriteInfo("Autodiscovery service app starting"); await app.RunAsync().DynamicContext(); diff --git a/src/wan24-AutoDiscover/Properties/launchSettings.json b/src/wan24-AutoDiscover/Properties/launchSettings.json index 29278b5..ae79731 100644 --- a/src/wan24-AutoDiscover/Properties/launchSettings.json +++ b/src/wan24-AutoDiscover/Properties/launchSettings.json @@ -2,7 +2,6 @@ "profiles": { "http": { "commandName": "Project", - "commandLineArgs": "autodiscover systemd", "launchBrowser": true, "launchUrl": "autodiscover/autodiscover.xml", "environmentVariables": { diff --git a/src/wan24-AutoDiscover/appsettings.json b/src/wan24-AutoDiscover/appsettings.json index c3b0868..ab662e5 100644 --- a/src/wan24-AutoDiscover/appsettings.json +++ b/src/wan24-AutoDiscover/appsettings.json @@ -2,13 +2,14 @@ "Logging": { "LogLevel": { "Default": "Information", - "Microsoft.AspNetCore": "Warning" + "Microsoft.AspNetCore": "Warning", + "Microsoft.AspNetCore.HttpLogging": "Information" } }, "Kestrel": { "Endpoints": { "AutoDiscover": { - "Url": "http://localhost:5000" + "Url": "http://127.0.0.1:5000" } } }, @@ -17,6 +18,7 @@ "LogFile": null, "PreForkResponses": 10, "DiscoveryType": null, + "KnownProxies": [], "Discovery": { "localhost": { "AcceptedDomains": [