增加启动参数DnsPort={port} #34

This commit is contained in:
xljiulang 2021-08-18 23:07:37 +08:00
parent a5714bc512
commit b530db4240
5 changed files with 44 additions and 10 deletions

View File

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

View File

@ -15,10 +15,10 @@ namespace FastGithub
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static OptionsBuilder<FastGithubOptions> AddConfiguration(this IServiceCollection services)
public static IServiceCollection AddConfiguration(this IServiceCollection services)
{
services.TryAddSingleton<FastGithubConfig>();
return services.AddOptions<FastGithubOptions>();
return services;
}
}
}

View File

@ -17,6 +17,7 @@ namespace FastGithub.Dns
{
private readonly DnsServer dnsServer;
private readonly IEnumerable<IDnsValidator> dnsValidators;
private readonly IOptions<FastGithubListenOptions> listenOptions;
private readonly ILogger<DnsHostedService> logger;
/// <summary>
@ -25,15 +26,18 @@ namespace FastGithub.Dns
/// <param name="dnsServer"></param>
/// <param name="dnsValidators"></param>
/// <param name="options"></param>
/// <param name="listenOptions"></param>
/// <param name="logger"></param>
public DnsHostedService(
DnsServer dnsServer,
IEnumerable<IDnsValidator> dnsValidators,
IOptionsMonitor<FastGithubOptions> options,
IOptions<FastGithubListenOptions> listenOptions,
ILogger<DnsHostedService> logger)
{
this.dnsServer = dnsServer;
this.dnsValidators = dnsValidators;
this.listenOptions = listenOptions;
this.logger = logger;
options.OnChange(opt =>
@ -52,10 +56,16 @@ namespace FastGithub.Dns
/// <returns></returns>
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
{

View File

@ -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
/// <param name="kestrel"></param>
public static void ListenGithubSshProxy(this KestrelServerOptions kestrel)
{
const int SSH_PORT = 22;
var listenOptions = kestrel.ApplicationServices.GetRequiredService<IOptions<FastGithubListenOptions>>();
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<GithubSshHandler>());
kestrel.Listen(IPAddress.Any, sshPort, listen => listen.UseConnectionHandler<GithubSshHandler>());
logger.LogInformation("已监听github的ssh代理");
}
}

View File

@ -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
/// <param name="services"></param>
public void ConfigureServices(IServiceCollection services)
{
services.AddConfiguration().Bind(this.Configuration.GetSection(nameof(FastGithub)));
services.Configure<FastGithubListenOptions>(this.Configuration);
services.Configure<FastGithubOptions>(this.Configuration.GetSection(nameof(FastGithub)));
services.AddConfiguration();
services.AddDnsServer();
services.AddDomainResolve();
services.AddHttpClient();
services.AddReverseProxy();
services.AddControllersWithViews();
}