From b530db4240e167e6875cfa1f2b77f8226f3ce10e Mon Sep 17 00:00:00 2001 From: xljiulang <366193849@qq.com> Date: Wed, 18 Aug 2021 23:07:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=90=AF=E5=8A=A8=E5=8F=82?= =?UTF-8?q?=E6=95=B0DnsPort=3D{port}=20=20#34?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FastGithubListenOptions.cs | 18 ++++++++++++++++++ .../ServiceCollectionExtensions.cs | 4 ++-- FastGithub.Dns/DnsHostedService.cs | 14 ++++++++++++-- .../KestrelServerOptionsExtensions.cs | 10 ++++++---- FastGithub/Startup.cs | 8 ++++++-- 5 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 FastGithub.Configuration/FastGithubListenOptions.cs diff --git a/FastGithub.Configuration/FastGithubListenOptions.cs b/FastGithub.Configuration/FastGithubListenOptions.cs new file mode 100644 index 0000000..3955490 --- /dev/null +++ b/FastGithub.Configuration/FastGithubListenOptions.cs @@ -0,0 +1,18 @@ +namespace FastGithub.Configuration +{ + /// + /// 监听选项 + /// + public class FastGithubListenOptions + { + /// + /// 监听的ssh端口 + /// + public int SshPort { get; set; } = 22; + + /// + /// 监听的dns端口 + /// + public int DnsPort { get; set; } = 53; + } +} diff --git a/FastGithub.Configuration/ServiceCollectionExtensions.cs b/FastGithub.Configuration/ServiceCollectionExtensions.cs index cc55ba9..7b44181 100644 --- a/FastGithub.Configuration/ServiceCollectionExtensions.cs +++ b/FastGithub.Configuration/ServiceCollectionExtensions.cs @@ -15,10 +15,10 @@ namespace FastGithub /// /// /// - public static OptionsBuilder AddConfiguration(this IServiceCollection services) + public static IServiceCollection AddConfiguration(this IServiceCollection services) { services.TryAddSingleton(); - return services.AddOptions(); + return services; } } } diff --git a/FastGithub.Dns/DnsHostedService.cs b/FastGithub.Dns/DnsHostedService.cs index 5c268c6..8b91f97 100644 --- a/FastGithub.Dns/DnsHostedService.cs +++ b/FastGithub.Dns/DnsHostedService.cs @@ -17,6 +17,7 @@ namespace FastGithub.Dns { private readonly DnsServer dnsServer; private readonly IEnumerable dnsValidators; + private readonly IOptions listenOptions; private readonly ILogger logger; /// @@ -25,15 +26,18 @@ namespace FastGithub.Dns /// /// /// + /// /// public DnsHostedService( DnsServer dnsServer, IEnumerable dnsValidators, IOptionsMonitor options, + IOptions listenOptions, ILogger logger) { this.dnsServer = dnsServer; this.dnsValidators = dnsValidators; + this.listenOptions = listenOptions; this.logger = logger; options.OnChange(opt => @@ -52,10 +56,16 @@ namespace FastGithub.Dns /// public override async Task StartAsync(CancellationToken cancellationToken) { - this.dnsServer.Bind(IPAddress.Any, 53); + var port = this.listenOptions.Value.DnsPort; + this.dnsServer.Bind(IPAddress.Any, port); this.logger.LogInformation("DNS服务启动成功"); - if (OperatingSystem.IsWindows()) + const int DNS_PORT = 53; + if (port != DNS_PORT) + { + this.logger.LogWarning($"由于使用了非标准DNS端口{port},你需要将{nameof(FastGithub)}设置为标准DNS的上游"); + } + else if (OperatingSystem.IsWindows()) { try { diff --git a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs index 4bd650e..61abc8b 100644 --- a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs +++ b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using System; using System.Net; @@ -71,16 +72,17 @@ namespace FastGithub /// public static void ListenGithubSshProxy(this KestrelServerOptions kestrel) { - const int SSH_PORT = 22; + var listenOptions = kestrel.ApplicationServices.GetRequiredService>(); + var sshPort = listenOptions.Value.SshPort; var logger = kestrel.GetLogger(); - if (LocalMachine.CanListenTcp(SSH_PORT) == false) + if (LocalMachine.CanListenTcp(sshPort) == false) { - logger.LogWarning($"由于tcp端口{SSH_PORT}已经被其它进程占用,github的ssh代理功能将受限"); + logger.LogWarning($"由于tcp端口{sshPort}已经被其它进程占用,github的ssh代理功能将受限"); } else { - kestrel.Listen(IPAddress.Any, SSH_PORT, listen => listen.UseConnectionHandler()); + kestrel.Listen(IPAddress.Any, sshPort, listen => listen.UseConnectionHandler()); logger.LogInformation("已监听github的ssh代理"); } } diff --git a/FastGithub/Startup.cs b/FastGithub/Startup.cs index 2238ee8..412298b 100644 --- a/FastGithub/Startup.cs +++ b/FastGithub/Startup.cs @@ -1,3 +1,4 @@ +using FastGithub.Configuration; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -26,12 +27,15 @@ namespace FastGithub /// public void ConfigureServices(IServiceCollection services) { - services.AddConfiguration().Bind(this.Configuration.GetSection(nameof(FastGithub))); + services.Configure(this.Configuration); + services.Configure(this.Configuration.GetSection(nameof(FastGithub))); + + services.AddConfiguration(); services.AddDnsServer(); services.AddDomainResolve(); services.AddHttpClient(); services.AddReverseProxy(); - + services.AddControllersWithViews(); }