移除hostedService

This commit is contained in:
陈国伟 2021-08-17 11:56:04 +08:00
parent 8e6edd57e8
commit 1f2c8c82ff
3 changed files with 25 additions and 61 deletions

View File

@ -7,7 +7,6 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Net; using System.Net;
using System.Security.Authentication;
namespace FastGithub namespace FastGithub
{ {
@ -23,15 +22,16 @@ namespace FastGithub
public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel) public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel)
{ {
const int HTTP_PORT = 80; const int HTTP_PORT = 80;
var logger = kestrel.GetLogger();
if (LocalMachine.CanListenTcp(HTTP_PORT) == false) if (LocalMachine.CanListenTcp(HTTP_PORT) == false)
{ {
var loggerFactory = kestrel.ApplicationServices.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(ReverseProxy)}");
logger.LogWarning($"由于tcp端口{HTTP_PORT}已经被其它进程占用http反向代理功能将受限"); logger.LogWarning($"由于tcp端口{HTTP_PORT}已经被其它进程占用http反向代理功能将受限");
} }
else else
{ {
kestrel.Listen(IPAddress.Any, HTTP_PORT); 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.CreateCaCertIfNotExists();
certService.InstallAndTrustCaCert(); certService.InstallAndTrustCaCert();
kestrel.Listen(IPAddress.Any, HTTPS_PORT, listen => listen.UseHttps(https => kestrel.Listen(IPAddress.Any, HTTPS_PORT,
{ listen => listen.UseHttps(https =>
if (OperatingSystem.IsWindows() && Environment.OSVersion.Version < new Version(6, 2)) https.ServerCertificateSelector = (ctx, domain) =>
{ certService.GetOrCreateServerCert(domain)));
https.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13;
} var logger = kestrel.GetLogger();
https.ServerCertificateSelector = (ctx, domain) => certService.GetOrCreateServerCert(domain); logger.LogInformation($"已监听https反向代理访问 https://127.0.0.1 或本机其它任意ip可进入Dashboard");
}));
} }
/// <summary> /// <summary>
@ -73,16 +72,28 @@ namespace FastGithub
public static void ListenGithubSshProxy(this KestrelServerOptions kestrel) public static void ListenGithubSshProxy(this KestrelServerOptions kestrel)
{ {
const int SSH_PORT = 22; const int SSH_PORT = 22;
var logger = kestrel.GetLogger();
if (LocalMachine.CanListenTcp(SSH_PORT) == false) if (LocalMachine.CanListenTcp(SSH_PORT) == false)
{ {
var loggerFactory = kestrel.ApplicationServices.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(ReverseProxy)}");
logger.LogWarning($"由于tcp端口{SSH_PORT}已经被其它进程占用github的ssh代理功能将受限"); logger.LogWarning($"由于tcp端口{SSH_PORT}已经被其它进程占用github的ssh代理功能将受限");
} }
else else
{ {
kestrel.Listen(IPAddress.Any, SSH_PORT, listen => listen.UseConnectionHandler<GithubSshHandler>()); kestrel.Listen(IPAddress.Any, SSH_PORT, listen => listen.UseConnectionHandler<GithubSshHandler>());
logger.LogInformation("已监听github的ssh代理");
} }
} }
/// <summary>
/// 获取日志
/// </summary>
/// <param name="kestrel"></param>
/// <returns></returns>
private static ILogger GetLogger(this KestrelServerOptions kestrel)
{
var loggerFactory = kestrel.ApplicationServices.GetRequiredService<ILoggerFactory>();
return loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(ReverseProxy)}");
}
} }
} }

View File

@ -1,46 +0,0 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub
{
/// <summary>
/// 后台服务
/// </summary>
sealed class HostedService : IHostedService
{
private readonly ILogger<HostedService> logger;
/// <summary>
/// 后台服务
/// </summary>
/// <param name="upgradeService"></param>
/// <param name="logger"></param>
public HostedService(ILogger<HostedService> logger)
{
this.logger = logger;
}
/// <summary>
/// 服务启动时
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 服务停止时
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
}

View File

@ -31,8 +31,7 @@ namespace FastGithub
services.AddDomainResolve(); services.AddDomainResolve();
services.AddHttpClient(); services.AddHttpClient();
services.AddReverseProxy(); services.AddReverseProxy();
services.AddHostedService<HostedService>();
services.AddControllersWithViews(); services.AddControllersWithViews();
} }