From 45576311533e99def043f81cf3e0748eb3e76f1e Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 20 Jul 2025 16:51:47 +0800 Subject: [PATCH] :sparkles: Load certificate to use HTTPs --- DysonNetwork.Drive/Program.cs | 2 +- DysonNetwork.Pass/Program.cs | 2 +- DysonNetwork.Pusher/Program.cs | 3 +- .../Startup/KestrelConfiguration.cs | 17 ---------- .../Http/KestrelConfiguration.cs | 31 +++++++++++++++---- DysonNetwork.Sphere/Program.cs | 3 +- .../Startup/KestrelConfiguration.cs | 17 ---------- 7 files changed, 31 insertions(+), 44 deletions(-) delete mode 100644 DysonNetwork.Pusher/Startup/KestrelConfiguration.cs delete mode 100644 DysonNetwork.Sphere/Startup/KestrelConfiguration.cs diff --git a/DysonNetwork.Drive/Program.cs b/DysonNetwork.Drive/Program.cs index a759e6f..6fd29a5 100644 --- a/DysonNetwork.Drive/Program.cs +++ b/DysonNetwork.Drive/Program.cs @@ -9,7 +9,7 @@ using tusdotnet.Stores; var builder = WebApplication.CreateBuilder(args); // Configure Kestrel and server options -builder.ConfigureAppKestrel(); +builder.ConfigureAppKestrel(builder.Configuration); // Add application services builder.Services.AddRegistryService(builder.Configuration); diff --git a/DysonNetwork.Pass/Program.cs b/DysonNetwork.Pass/Program.cs index bb6ce4b..a466b8d 100644 --- a/DysonNetwork.Pass/Program.cs +++ b/DysonNetwork.Pass/Program.cs @@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); // Configure Kestrel and server options -builder.ConfigureAppKestrel(); +builder.ConfigureAppKestrel(builder.Configuration); // Add metrics and telemetry builder.Services.AddAppMetrics(); diff --git a/DysonNetwork.Pusher/Program.cs b/DysonNetwork.Pusher/Program.cs index 57c5174..3c327df 100644 --- a/DysonNetwork.Pusher/Program.cs +++ b/DysonNetwork.Pusher/Program.cs @@ -1,13 +1,14 @@ using DysonNetwork.Pusher; using DysonNetwork.Pusher.Startup; using DysonNetwork.Shared.Auth; +using DysonNetwork.Shared.Http; using DysonNetwork.Shared.Registry; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); // Configure Kestrel and server options -builder.ConfigureAppKestrel(); +builder.ConfigureAppKestrel(builder.Configuration); // Add application services builder.Services.AddRegistryService(builder.Configuration); diff --git a/DysonNetwork.Pusher/Startup/KestrelConfiguration.cs b/DysonNetwork.Pusher/Startup/KestrelConfiguration.cs deleted file mode 100644 index f35e4dd..0000000 --- a/DysonNetwork.Pusher/Startup/KestrelConfiguration.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace DysonNetwork.Pusher.Startup; - -public static class KestrelConfiguration -{ - public static WebApplicationBuilder ConfigureAppKestrel(this WebApplicationBuilder builder) - { - builder.Host.UseContentRoot(Directory.GetCurrentDirectory()); - builder.WebHost.ConfigureKestrel(options => - { - options.Limits.MaxRequestBodySize = 50 * 1024 * 1024; - options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2); - options.Limits.RequestHeadersTimeout = TimeSpan.FromSeconds(30); - }); - - return builder; - } -} diff --git a/DysonNetwork.Shared/Http/KestrelConfiguration.cs b/DysonNetwork.Shared/Http/KestrelConfiguration.cs index a023a75..97bfb66 100644 --- a/DysonNetwork.Shared/Http/KestrelConfiguration.cs +++ b/DysonNetwork.Shared/Http/KestrelConfiguration.cs @@ -1,13 +1,16 @@ +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Server.Kestrel.Core; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; namespace DysonNetwork.Shared.Http; public static class KestrelConfiguration { - public static WebApplicationBuilder ConfigureAppKestrel(this WebApplicationBuilder builder) + public static WebApplicationBuilder ConfigureAppKestrel(this WebApplicationBuilder builder, IConfiguration configuration) { builder.Host.UseContentRoot(Directory.GetCurrentDirectory()); builder.WebHost.ConfigureKestrel(options => @@ -15,12 +18,28 @@ public static class KestrelConfiguration options.Limits.MaxRequestBodySize = 50 * 1024 * 1024; options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2); options.Limits.RequestHeadersTimeout = TimeSpan.FromSeconds(30); - options.ConfigureEndpointDefaults(endpoints => - { - endpoints.Protocols = HttpProtocols.Http1AndHttp2; - }); - }); + + var certPath = configuration["Service:ClientCert"]!; + var keyPath = configuration["Service:ClientKey"]!; + // Load PEM cert and key manually + var certificate = X509Certificate2.CreateFromPemFile(certPath, keyPath); + + // You MUST call this to make sure the key is usable + certificate = certificate.CopyWithPrivateKey( + RSA.Create() + ); + + // Now pass the full cert + options.ListenAnyIP(5001, listenOptions => + { + listenOptions.UseHttps(certificate); + }); + + // Optional: HTTP fallback + options.ListenAnyIP(8080); + }); + return builder; } } \ No newline at end of file diff --git a/DysonNetwork.Sphere/Program.cs b/DysonNetwork.Sphere/Program.cs index 56d4590..f8b9c9d 100644 --- a/DysonNetwork.Sphere/Program.cs +++ b/DysonNetwork.Sphere/Program.cs @@ -1,4 +1,5 @@ using DysonNetwork.Shared.Auth; +using DysonNetwork.Shared.Http; using DysonNetwork.Shared.Registry; using DysonNetwork.Sphere; using DysonNetwork.Sphere.Startup; @@ -7,7 +8,7 @@ using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); // Configure Kestrel and server options -builder.ConfigureAppKestrel(); +builder.ConfigureAppKestrel(builder.Configuration); // Add metrics and telemetry builder.Services.AddAppMetrics(); diff --git a/DysonNetwork.Sphere/Startup/KestrelConfiguration.cs b/DysonNetwork.Sphere/Startup/KestrelConfiguration.cs deleted file mode 100644 index a73babd..0000000 --- a/DysonNetwork.Sphere/Startup/KestrelConfiguration.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace DysonNetwork.Sphere.Startup; - -public static class KestrelConfiguration -{ - public static WebApplicationBuilder ConfigureAppKestrel(this WebApplicationBuilder builder) - { - builder.Host.UseContentRoot(Directory.GetCurrentDirectory()); - builder.WebHost.ConfigureKestrel(options => - { - options.Limits.MaxRequestBodySize = 50 * 1024 * 1024; - options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2); - options.Limits.RequestHeadersTimeout = TimeSpan.FromSeconds(30); - }); - - return builder; - } -}