From 1f2c8c82ff053fd487ef57bd205e56e003f161ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Tue, 17 Aug 2021 11:56:04 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4hostedService?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../KestrelServerOptionsExtensions.cs | 37 +++++++++------ FastGithub/HostedService.cs | 46 ------------------- FastGithub/Startup.cs | 3 +- 3 files changed, 25 insertions(+), 61 deletions(-) delete mode 100644 FastGithub/HostedService.cs diff --git a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs index e8a5c7e..4bd650e 100644 --- a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs +++ b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs @@ -7,7 +7,6 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Net; -using System.Security.Authentication; namespace FastGithub { @@ -23,15 +22,16 @@ namespace FastGithub public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel) { const int HTTP_PORT = 80; + var logger = kestrel.GetLogger(); + if (LocalMachine.CanListenTcp(HTTP_PORT) == false) { - var loggerFactory = kestrel.ApplicationServices.GetRequiredService(); - var logger = loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(ReverseProxy)}"); logger.LogWarning($"由于tcp端口{HTTP_PORT}已经被其它进程占用,http反向代理功能将受限"); } else { kestrel.Listen(IPAddress.Any, HTTP_PORT); + logger.LogInformation($"已监听http反向代理,访问 http://127.0.0.1 或本机其它任意ip可进入Dashboard"); } } @@ -56,14 +56,13 @@ namespace FastGithub certService.CreateCaCertIfNotExists(); certService.InstallAndTrustCaCert(); - kestrel.Listen(IPAddress.Any, HTTPS_PORT, listen => listen.UseHttps(https => - { - if (OperatingSystem.IsWindows() && Environment.OSVersion.Version < new Version(6, 2)) - { - https.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13; - } - https.ServerCertificateSelector = (ctx, domain) => certService.GetOrCreateServerCert(domain); - })); + kestrel.Listen(IPAddress.Any, HTTPS_PORT, + listen => listen.UseHttps(https => + https.ServerCertificateSelector = (ctx, domain) => + certService.GetOrCreateServerCert(domain))); + + var logger = kestrel.GetLogger(); + logger.LogInformation($"已监听https反向代理,访问 https://127.0.0.1 或本机其它任意ip可进入Dashboard"); } /// @@ -73,16 +72,28 @@ namespace FastGithub public static void ListenGithubSshProxy(this KestrelServerOptions kestrel) { const int SSH_PORT = 22; + var logger = kestrel.GetLogger(); + if (LocalMachine.CanListenTcp(SSH_PORT) == false) { - var loggerFactory = kestrel.ApplicationServices.GetRequiredService(); - var logger = loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(ReverseProxy)}"); logger.LogWarning($"由于tcp端口{SSH_PORT}已经被其它进程占用,github的ssh代理功能将受限"); } else { kestrel.Listen(IPAddress.Any, SSH_PORT, listen => listen.UseConnectionHandler()); + logger.LogInformation("已监听github的ssh代理"); } } + + /// + /// 获取日志 + /// + /// + /// + private static ILogger GetLogger(this KestrelServerOptions kestrel) + { + var loggerFactory = kestrel.ApplicationServices.GetRequiredService(); + return loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(ReverseProxy)}"); + } } } diff --git a/FastGithub/HostedService.cs b/FastGithub/HostedService.cs deleted file mode 100644 index 431e182..0000000 --- a/FastGithub/HostedService.cs +++ /dev/null @@ -1,46 +0,0 @@ -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using System.Threading; -using System.Threading.Tasks; - -namespace FastGithub -{ - /// - /// 后台服务 - /// - sealed class HostedService : IHostedService - { - private readonly ILogger logger; - - /// - /// 后台服务 - /// - /// - /// - public HostedService(ILogger logger) - { - this.logger = logger; - } - - /// - /// 服务启动时 - /// - /// - /// - public Task StartAsync(CancellationToken cancellationToken) - { - this.logger.LogInformation($"{nameof(FastGithub)}启动完成,访问 http://127.0.0.1 或 https://127.0.0.1 或本机其它任意ip可进入Dashboard"); - return Task.CompletedTask; - } - - /// - /// 服务停止时 - /// - /// - /// - public Task StopAsync(CancellationToken cancellationToken) - { - return Task.CompletedTask; - } - } -} diff --git a/FastGithub/Startup.cs b/FastGithub/Startup.cs index fc80690..2238ee8 100644 --- a/FastGithub/Startup.cs +++ b/FastGithub/Startup.cs @@ -31,8 +31,7 @@ namespace FastGithub services.AddDomainResolve(); services.AddHttpClient(); services.AddReverseProxy(); - - services.AddHostedService(); + services.AddControllersWithViews(); }