Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
nd1012 committed Apr 6, 2024
1 parent f2e87ab commit 6ceea81
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 15 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -98,14 +101,11 @@ Create the file `/etc/apache2/sites-available/autodiscover.conf`:
<VirtualHost [IP]:443>
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/
</VirtualHost>
```

Expand All @@ -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
```
Expand Down
7 changes: 7 additions & 0 deletions src/wan24-AutoDiscover Shared/Models/DiscoveryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -44,6 +46,11 @@ public DiscoveryConfig() { }
: TypeHelper.Instance.GetType(DiscoveryTypeName)
?? throw new InvalidDataException($"Discovery type {DiscoveryTypeName.ToQuotedLiteral()} not found");

/// <summary>
/// Known http proxies
/// </summary>
public HashSet<IPAddress> KnownProxies { get; init; } = [];

/// <summary>
/// Get the discovery configuration
/// </summary>
Expand Down
34 changes: 31 additions & 3 deletions src/wan24-AutoDiscover/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Microsoft.AspNetCore.HttpLogging;
using Microsoft.AspNetCore.HttpOverrides;
using wan24.AutoDiscover.Models;
using wan24.AutoDiscover.Services;
using wan24.CLI;
Expand Down Expand Up @@ -36,7 +38,7 @@
Settings.ProcessId = "webservice";
Settings.LogLevel = config.GetValue<LogLevel>("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}\"");
Expand Down Expand Up @@ -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<XmlDocumentInstances>())
.AddExceptionHandler<ExceptionHandler>();
.AddExceptionHandler<ExceptionHandler>()
.AddHttpLogging(options => options.LoggingFields = HttpLoggingFields.RequestPropertiesAndHeaders);
builder.Services.Configure<ForwardedHeadersOptions>(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();
Expand Down
1 change: 0 additions & 1 deletion src/wan24-AutoDiscover/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"profiles": {
"http": {
"commandName": "Project",
"commandLineArgs": "autodiscover systemd",
"launchBrowser": true,
"launchUrl": "autodiscover/autodiscover.xml",
"environmentVariables": {
Expand Down
6 changes: 4 additions & 2 deletions src/wan24-AutoDiscover/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
},
Expand All @@ -17,6 +18,7 @@
"LogFile": null,
"PreForkResponses": 10,
"DiscoveryType": null,
"KnownProxies": [],
"Discovery": {
"localhost": {
"AcceptedDomains": [
Expand Down

0 comments on commit 6ceea81

Please sign in to comment.