From 4499c640f5be9faa36000ec8110594a570f513bd Mon Sep 17 00:00:00 2001
From: xljiulang <366193849@qq.com>
Date: Wed, 18 Aug 2021 23:25:10 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Listen=E7=9A=84=E9=85=8D?=
=?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6(#34)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FastGithub.Configuration/FastGithubOptions.cs | 5 +++++
.../{FastGithubListenOptions.cs => ListenConfig.cs} | 8 ++++----
FastGithub.Dns/DnsHostedService.cs | 10 ++++------
.../KestrelServerOptionsExtensions.cs | 4 ++--
FastGithub/Startup.cs | 1 -
FastGithub/appsettings.json | 4 ++++
6 files changed, 19 insertions(+), 13 deletions(-)
rename FastGithub.Configuration/{FastGithubListenOptions.cs => ListenConfig.cs} (61%)
diff --git a/FastGithub.Configuration/FastGithubOptions.cs b/FastGithub.Configuration/FastGithubOptions.cs
index 6813525..e50884a 100644
--- a/FastGithub.Configuration/FastGithubOptions.cs
+++ b/FastGithub.Configuration/FastGithubOptions.cs
@@ -7,6 +7,11 @@ namespace FastGithub.Configuration
///
public class FastGithubOptions
{
+ ///
+ /// 监听配置
+ ///
+ public ListenConfig Listen { get; set; } = new ListenConfig();
+
///
/// 未污染的dns
///
diff --git a/FastGithub.Configuration/FastGithubListenOptions.cs b/FastGithub.Configuration/ListenConfig.cs
similarity index 61%
rename from FastGithub.Configuration/FastGithubListenOptions.cs
rename to FastGithub.Configuration/ListenConfig.cs
index 3955490..c209ab9 100644
--- a/FastGithub.Configuration/FastGithubListenOptions.cs
+++ b/FastGithub.Configuration/ListenConfig.cs
@@ -1,18 +1,18 @@
namespace FastGithub.Configuration
{
///
- /// 监听选项
+ /// 监听配置
///
- public class FastGithubListenOptions
+ public record ListenConfig
{
///
/// 监听的ssh端口
///
- public int SshPort { get; set; } = 22;
+ public int SshPort { get; init; } = 22;
///
/// 监听的dns端口
///
- public int DnsPort { get; set; } = 53;
+ public int DnsPort { get; init; } = 53;
}
}
diff --git a/FastGithub.Dns/DnsHostedService.cs b/FastGithub.Dns/DnsHostedService.cs
index 8b91f97..dec1c70 100644
--- a/FastGithub.Dns/DnsHostedService.cs
+++ b/FastGithub.Dns/DnsHostedService.cs
@@ -17,7 +17,7 @@ namespace FastGithub.Dns
{
private readonly DnsServer dnsServer;
private readonly IEnumerable dnsValidators;
- private readonly IOptions listenOptions;
+ private readonly IOptionsMonitor options;
private readonly ILogger logger;
///
@@ -25,19 +25,17 @@ 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.options = options;
this.logger = logger;
options.OnChange(opt =>
@@ -56,7 +54,7 @@ namespace FastGithub.Dns
///
public override async Task StartAsync(CancellationToken cancellationToken)
{
- var port = this.listenOptions.Value.DnsPort;
+ var port = this.options.CurrentValue.Listen.DnsPort;
this.dnsServer.Bind(IPAddress.Any, port);
this.logger.LogInformation("DNS服务启动成功");
diff --git a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs
index 61abc8b..9ca702b 100644
--- a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs
+++ b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs
@@ -72,8 +72,8 @@ namespace FastGithub
///
public static void ListenGithubSshProxy(this KestrelServerOptions kestrel)
{
- var listenOptions = kestrel.ApplicationServices.GetRequiredService>();
- var sshPort = listenOptions.Value.SshPort;
+ var listenOptions = kestrel.ApplicationServices.GetRequiredService>();
+ var sshPort = listenOptions.Value.Listen.SshPort;
var logger = kestrel.GetLogger();
if (LocalMachine.CanListenTcp(sshPort) == false)
diff --git a/FastGithub/Startup.cs b/FastGithub/Startup.cs
index 412298b..2d14eb6 100644
--- a/FastGithub/Startup.cs
+++ b/FastGithub/Startup.cs
@@ -27,7 +27,6 @@ namespace FastGithub
///
public void ConfigureServices(IServiceCollection services)
{
- services.Configure(this.Configuration);
services.Configure(this.Configuration.GetSection(nameof(FastGithub)));
services.AddConfiguration();
diff --git a/FastGithub/appsettings.json b/FastGithub/appsettings.json
index 9c80f83..44858aa 100644
--- a/FastGithub/appsettings.json
+++ b/FastGithub/appsettings.json
@@ -1,6 +1,10 @@
{
// 新增的子配置文件appsettings.*.json,重启应用程序才生效
"FastGithub": {
+ "Listen": {
+ "SshPort": 22, // ssh监听的端口,修改后重启应用才生效
+ "DnsPort": 53 // dns监听的端口,修改后重启应用才生效
+ },
"PureDns": { // 用于解析DomainConfigs的域名
"IPAddress": "127.0.0.1",
"Port": 5533 // 5533指向dnscrypt-proxy