增加Listen的配置文件(#34)

This commit is contained in:
xljiulang 2021-08-18 23:25:10 +08:00
parent b530db4240
commit 4499c640f5
6 changed files with 19 additions and 13 deletions

View File

@ -7,6 +7,11 @@ namespace FastGithub.Configuration
/// </summary> /// </summary>
public class FastGithubOptions public class FastGithubOptions
{ {
/// <summary>
/// 监听配置
/// </summary>
public ListenConfig Listen { get; set; } = new ListenConfig();
/// <summary> /// <summary>
/// 未污染的dns /// 未污染的dns
/// </summary> /// </summary>

View File

@ -1,18 +1,18 @@
namespace FastGithub.Configuration namespace FastGithub.Configuration
{ {
/// <summary> /// <summary>
/// 监听选项 /// 监听配置
/// </summary> /// </summary>
public class FastGithubListenOptions public record ListenConfig
{ {
/// <summary> /// <summary>
/// 监听的ssh端口 /// 监听的ssh端口
/// </summary> /// </summary>
public int SshPort { get; set; } = 22; public int SshPort { get; init; } = 22;
/// <summary> /// <summary>
/// 监听的dns端口 /// 监听的dns端口
/// </summary> /// </summary>
public int DnsPort { get; set; } = 53; public int DnsPort { get; init; } = 53;
} }
} }

View File

@ -17,7 +17,7 @@ namespace FastGithub.Dns
{ {
private readonly DnsServer dnsServer; private readonly DnsServer dnsServer;
private readonly IEnumerable<IDnsValidator> dnsValidators; private readonly IEnumerable<IDnsValidator> dnsValidators;
private readonly IOptions<FastGithubListenOptions> listenOptions; private readonly IOptionsMonitor<FastGithubOptions> options;
private readonly ILogger<DnsHostedService> logger; private readonly ILogger<DnsHostedService> logger;
/// <summary> /// <summary>
@ -25,19 +25,17 @@ namespace FastGithub.Dns
/// </summary> /// </summary>
/// <param name="dnsServer"></param> /// <param name="dnsServer"></param>
/// <param name="dnsValidators"></param> /// <param name="dnsValidators"></param>
/// <param name="options"></param> /// <param name="options"></param>
/// <param name="listenOptions"></param>
/// <param name="logger"></param> /// <param name="logger"></param>
public DnsHostedService( public DnsHostedService(
DnsServer dnsServer, DnsServer dnsServer,
IEnumerable<IDnsValidator> dnsValidators, IEnumerable<IDnsValidator> dnsValidators,
IOptionsMonitor<FastGithubOptions> options, IOptionsMonitor<FastGithubOptions> options,
IOptions<FastGithubListenOptions> listenOptions,
ILogger<DnsHostedService> logger) ILogger<DnsHostedService> logger)
{ {
this.dnsServer = dnsServer; this.dnsServer = dnsServer;
this.dnsValidators = dnsValidators; this.dnsValidators = dnsValidators;
this.listenOptions = listenOptions; this.options = options;
this.logger = logger; this.logger = logger;
options.OnChange(opt => options.OnChange(opt =>
@ -56,7 +54,7 @@ namespace FastGithub.Dns
/// <returns></returns> /// <returns></returns>
public override async Task StartAsync(CancellationToken cancellationToken) 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.dnsServer.Bind(IPAddress.Any, port);
this.logger.LogInformation("DNS服务启动成功"); this.logger.LogInformation("DNS服务启动成功");

View File

@ -72,8 +72,8 @@ namespace FastGithub
/// <param name="kestrel"></param> /// <param name="kestrel"></param>
public static void ListenGithubSshProxy(this KestrelServerOptions kestrel) public static void ListenGithubSshProxy(this KestrelServerOptions kestrel)
{ {
var listenOptions = kestrel.ApplicationServices.GetRequiredService<IOptions<FastGithubListenOptions>>(); var listenOptions = kestrel.ApplicationServices.GetRequiredService<IOptions<FastGithubOptions>>();
var sshPort = listenOptions.Value.SshPort; var sshPort = listenOptions.Value.Listen.SshPort;
var logger = kestrel.GetLogger(); var logger = kestrel.GetLogger();
if (LocalMachine.CanListenTcp(sshPort) == false) if (LocalMachine.CanListenTcp(sshPort) == false)

View File

@ -27,7 +27,6 @@ namespace FastGithub
/// <param name="services"></param> /// <param name="services"></param>
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.Configure<FastGithubListenOptions>(this.Configuration);
services.Configure<FastGithubOptions>(this.Configuration.GetSection(nameof(FastGithub))); services.Configure<FastGithubOptions>(this.Configuration.GetSection(nameof(FastGithub)));
services.AddConfiguration(); services.AddConfiguration();

View File

@ -1,6 +1,10 @@
{ {
// appsettings.*.json // appsettings.*.json
"FastGithub": { "FastGithub": {
"Listen": {
"SshPort": 22, // ssh
"DnsPort": 53 // dns
},
"PureDns": { // DomainConfigs "PureDns": { // DomainConfigs
"IPAddress": "127.0.0.1", "IPAddress": "127.0.0.1",
"Port": 5533 // 5533dnscrypt-proxy "Port": 5533 // 5533dnscrypt-proxy