From 2cfc2e65ea06c4c9267f80014c52e214725a963e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E9=9B=A8=E8=90=BD?= Date: Tue, 12 Sep 2023 11:40:07 +0800 Subject: [PATCH] :sparkles: Use kestrel. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 秋雨落 --- CrystalConnector/CrystalConnector.csproj | 32 ++++++------- CrystalConnector/Program.cs | 57 +++++++++++++++++++----- CrystalConnector/Startup.cs | 5 ++- CrystalConnector/config.json | 28 ++++++------ 4 files changed, 81 insertions(+), 41 deletions(-) diff --git a/CrystalConnector/CrystalConnector.csproj b/CrystalConnector/CrystalConnector.csproj index 75fdd48..b29c029 100644 --- a/CrystalConnector/CrystalConnector.csproj +++ b/CrystalConnector/CrystalConnector.csproj @@ -8,28 +8,28 @@ - - - - - - - - - + + + + + + + + + - + - - PreserveNewest - - - PreserveNewest - + + PreserveNewest + + + PreserveNewest + diff --git a/CrystalConnector/Program.cs b/CrystalConnector/Program.cs index 785f193..44fcd66 100644 --- a/CrystalConnector/Program.cs +++ b/CrystalConnector/Program.cs @@ -1,4 +1,6 @@ -using CrystalConnector; +using System.Net; +using System.Security.Cryptography.X509Certificates; +using CrystalConnector; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -25,15 +27,6 @@ builder.ConfigureAppConfiguration((context, config) => builder.UseContentRoot(Environment.CurrentDirectory); -builder.ConfigureWebHost(webHost => -{ - webHost.UseStartup(); - - webHost.UseKestrel(kestrel => - { - }); -}); - builder.ConfigureLogging(logging => { logging.ClearProviders() @@ -41,6 +34,50 @@ builder.ConfigureLogging(logging => .AddNLog(); }); +builder.ConfigureWebHost(webHost => +{ + webHost.UseStartup(); + + webHost.UseKestrel(); + + webHost.ConfigureKestrel((context, kestrel) => + { + var config = context.Configuration; + foreach (var listener in config.GetSection("Server:Listener").GetChildren()) + { + if (listener.GetValue("Enabled")) + { + var ipStr = listener.GetValue("IP"); + if (!IPAddress.TryParse(ipStr, out var ip)) + { + continue; + } + + var port = listener.GetValue("Port"); + + var https = listener.GetSection("Https"); + if (!https.GetValue("Enabled") || https.GetValue("Cert") == null) + { + kestrel.Listen(ip, port); + } + else + { + var cert = https.GetValue("Cert"); + var password = https.GetValue("Password"); + + kestrel.Listen(ip, port, listenOptions => + { + listenOptions.UseHttps(httpsOptions => + { + httpsOptions.ServerCertificate = X509Certificate2.CreateFromEncryptedPemFile(cert!, password); + }); + }); + } + } + } + }); +}); + using var host = builder.Build(); await host.RunAsync(); diff --git a/CrystalConnector/Startup.cs b/CrystalConnector/Startup.cs index 4796ca8..2ea873d 100644 --- a/CrystalConnector/Startup.cs +++ b/CrystalConnector/Startup.cs @@ -1,12 +1,15 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using NLog; namespace CrystalConnector; public class Startup { - public IConfiguration Config { get; } + private IConfiguration Config { get; } + + private Logger Logger { get; } = LogManager.GetCurrentClassLogger(); public Startup(IConfiguration config) { diff --git a/CrystalConnector/config.json b/CrystalConnector/config.json index bdc18c6..af58a73 100644 --- a/CrystalConnector/config.json +++ b/CrystalConnector/config.json @@ -1,16 +1,16 @@ { - "server": { - "listen": [ - { - "enabled": true, - "ip": "0.0.0.0", - "port": 33927, - "https": { - "enabled": false, - "cert": "cert.pfx", - "password": "" - } - } - ] - } + "Server": { + "Listener": [ + { + "Enabled": true, + "IP": "0.0.0.0", + "Port": 33927, + "Https": { + "Enabled": false, + "Cert": "cert.pfx", + "Password": "" + } + } + ] + } } \ No newline at end of file