✨ Use kestrel.

Signed-off-by: 秋雨落 <i@rain.cx>
This commit is contained in:
秋雨落
2023-09-12 11:40:07 +08:00
parent 52adef63cd
commit 2cfc2e65ea
4 changed files with 81 additions and 41 deletions
+47 -10
View File
@@ -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<Startup>();
webHost.UseKestrel(kestrel =>
{
});
});
builder.ConfigureLogging(logging =>
{
logging.ClearProviders()
@@ -41,6 +34,50 @@ builder.ConfigureLogging(logging =>
.AddNLog();
});
builder.ConfigureWebHost(webHost =>
{
webHost.UseStartup<Startup>();
webHost.UseKestrel();
webHost.ConfigureKestrel((context, kestrel) =>
{
var config = context.Configuration;
foreach (var listener in config.GetSection("Server:Listener").GetChildren())
{
if (listener.GetValue<bool>("Enabled"))
{
var ipStr = listener.GetValue<string>("IP");
if (!IPAddress.TryParse(ipStr, out var ip))
{
continue;
}
var port = listener.GetValue<int>("Port");
var https = listener.GetSection("Https");
if (!https.GetValue<bool>("Enabled") || https.GetValue<string>("Cert") == null)
{
kestrel.Listen(ip, port);
}
else
{
var cert = https.GetValue<string>("Cert");
var password = https.GetValue<string>("Password");
kestrel.Listen(ip, port, listenOptions =>
{
listenOptions.UseHttps(httpsOptions =>
{
httpsOptions.ServerCertificate = X509Certificate2.CreateFromEncryptedPemFile(cert!, password);
});
});
}
}
}
});
});
using var host = builder.Build();
await host.RunAsync();
+4 -1
View File
@@ -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)
{
+9 -9
View File
@@ -1,14 +1,14 @@
{
"server": {
"listen": [
"Server": {
"Listener": [
{
"enabled": true,
"ip": "0.0.0.0",
"port": 33927,
"https": {
"enabled": false,
"cert": "cert.pfx",
"password": ""
"Enabled": true,
"IP": "0.0.0.0",
"Port": 33927,
"Https": {
"Enabled": false,
"Cert": "cert.pfx",
"Password": ""
}
}
]