|
@@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
+using Microsoft.Extensions.Options;
|
|
using System;
|
|
using System;
|
|
using System.Net;
|
|
using System.Net;
|
|
|
|
|
|
@@ -30,18 +31,18 @@ namespace FastGithub
|
|
/// <param name="kestrel"></param>
|
|
/// <param name="kestrel"></param>
|
|
public static void ListenHttpProxy(this KestrelServerOptions kestrel)
|
|
public static void ListenHttpProxy(this KestrelServerOptions kestrel)
|
|
{
|
|
{
|
|
- var httpPort = kestrel.ApplicationServices.GetRequiredService<PortService>().HttpProxyPort;
|
|
|
|
- if (LocalMachine.CanListenTcp(httpPort) == false)
|
|
|
|
|
|
+ var httpProxyPort = kestrel.ApplicationServices.GetRequiredService<IOptions<FastGithubOptions>>().Value.HttpProxyPort;
|
|
|
|
+ if (LocalMachine.CanListenTcp(httpProxyPort) == false)
|
|
{
|
|
{
|
|
- throw new FastGithubException("tcp端口{httpsPort}已经被其它进程占用,请在配置文件更换一个端口");
|
|
|
|
|
|
+ throw new FastGithubException($"tcp端口{httpProxyPort}已经被其它进程占用,请在配置文件更换{nameof(FastGithubOptions.HttpProxyPort)}为其它端口");
|
|
}
|
|
}
|
|
|
|
|
|
- kestrel.Listen(IPAddress.Any, httpPort);
|
|
|
|
- kestrel.GetLogger().LogInformation($"已监听tcp端口{httpPort},http代理启动完成");
|
|
|
|
|
|
+ kestrel.Listen(IPAddress.Loopback, httpProxyPort);
|
|
|
|
+ kestrel.GetLogger().LogInformation($"已监听http://127.0.0.1:{httpProxyPort},http代理启动完成");
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
- /// 监听ssh反向代理
|
|
|
|
|
|
+ /// 尝试监听ssh反向代理
|
|
/// </summary>
|
|
/// </summary>
|
|
/// <param name="kestrel"></param>
|
|
/// <param name="kestrel"></param>
|
|
public static void ListenSshReverseProxy(this KestrelServerOptions kestrel)
|
|
public static void ListenSshReverseProxy(this KestrelServerOptions kestrel)
|
|
@@ -49,13 +50,13 @@ namespace FastGithub
|
|
const int SSH_PORT = 22;
|
|
const int SSH_PORT = 22;
|
|
if (LocalMachine.CanListenTcp(SSH_PORT) == true)
|
|
if (LocalMachine.CanListenTcp(SSH_PORT) == true)
|
|
{
|
|
{
|
|
- kestrel.Listen(IPAddress.Any, SSH_PORT, listen => listen.UseConnectionHandler<SshReverseProxyHandler>());
|
|
|
|
- kestrel.GetLogger().LogInformation($"已监听tcp端口{SSH_PORT},github的ssh代理启动完成");
|
|
|
|
|
|
+ kestrel.Listen(IPAddress.Loopback, SSH_PORT, listen => listen.UseConnectionHandler<SshReverseProxyHandler>());
|
|
|
|
+ kestrel.GetLogger().LogInformation($"已监听ssh://127.0.0.1:{SSH_PORT},ssh反向代理到github启动完成");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
- /// 监听http反向代理
|
|
|
|
|
|
+ /// 尝试监听http反向代理
|
|
/// </summary>
|
|
/// </summary>
|
|
/// <param name="kestrel"></param>
|
|
/// <param name="kestrel"></param>
|
|
public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel)
|
|
public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel)
|
|
@@ -63,8 +64,8 @@ namespace FastGithub
|
|
const int HTTP_PORT = 80;
|
|
const int HTTP_PORT = 80;
|
|
if (LocalMachine.CanListenTcp(HTTP_PORT) == true)
|
|
if (LocalMachine.CanListenTcp(HTTP_PORT) == true)
|
|
{
|
|
{
|
|
- kestrel.Listen(IPAddress.Any, HTTP_PORT);
|
|
|
|
- kestrel.GetLogger().LogInformation($"已监听tcp端口{HTTP_PORT},http反向代理启动完成");
|
|
|
|
|
|
+ kestrel.Listen(IPAddress.Loopback, HTTP_PORT);
|
|
|
|
+ kestrel.GetLogger().LogInformation($"已监听http://127.0.0.1:{HTTP_PORT},http反向代理启动完成");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -73,9 +74,10 @@ namespace FastGithub
|
|
/// </summary>
|
|
/// </summary>
|
|
/// <param name="kestrel"></param>
|
|
/// <param name="kestrel"></param>
|
|
/// <exception cref="FastGithubException"></exception>
|
|
/// <exception cref="FastGithubException"></exception>
|
|
- public static void ListenHttpsReverseProxy(this KestrelServerOptions kestrel)
|
|
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static int ListenHttpsReverseProxy(this KestrelServerOptions kestrel)
|
|
{
|
|
{
|
|
- var httpsPort = kestrel.ApplicationServices.GetRequiredService<PortService>().HttpsReverseProxyPort;
|
|
|
|
|
|
+ var httpsPort = HttpsReverseProxyPort.Value;
|
|
if (OperatingSystem.IsWindows())
|
|
if (OperatingSystem.IsWindows())
|
|
{
|
|
{
|
|
TcpTable.KillPortOwner(httpsPort);
|
|
TcpTable.KillPortOwner(httpsPort);
|
|
@@ -90,15 +92,19 @@ namespace FastGithub
|
|
certService.CreateCaCertIfNotExists();
|
|
certService.CreateCaCertIfNotExists();
|
|
certService.InstallAndTrustCaCert();
|
|
certService.InstallAndTrustCaCert();
|
|
|
|
|
|
- kestrel.Listen(IPAddress.Any, httpsPort,
|
|
|
|
|
|
+ kestrel.Listen(IPAddress.Loopback, httpsPort,
|
|
listen => listen.UseHttps(https =>
|
|
listen => listen.UseHttps(https =>
|
|
https.ServerCertificateSelector = (ctx, domain) =>
|
|
https.ServerCertificateSelector = (ctx, domain) =>
|
|
certService.GetOrCreateServerCert(domain)));
|
|
certService.GetOrCreateServerCert(domain)));
|
|
|
|
|
|
- var logger = kestrel.GetLogger();
|
|
|
|
- logger.LogInformation($"已监听tcp端口{httpsPort},https反向代理启动完成");
|
|
|
|
- }
|
|
|
|
|
|
+ if (httpsPort == 443)
|
|
|
|
+ {
|
|
|
|
+ var logger = kestrel.GetLogger();
|
|
|
|
+ logger.LogInformation($"已监听https://127.0.0.1:{httpsPort},https反向代理启动完成");
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ return httpsPort;
|
|
|
|
+ }
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 获取日志
|
|
/// 获取日志
|