diff --git a/FastGithub.Configuration/ReverseProxyPort.cs b/FastGithub.Configuration/ReverseProxyPort.cs index 4f66816..aa6e7fc 100644 --- a/FastGithub.Configuration/ReverseProxyPort.cs +++ b/FastGithub.Configuration/ReverseProxyPort.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Net; using System.Net.NetworkInformation; -using System.Net.Sockets; namespace FastGithub.Configuration { @@ -29,20 +28,16 @@ namespace FastGithub.Configuration /// /// 获取可用的随机Tcp端口 /// - /// - /// + /// /// - private static int GetAvailableTcpPort(int minValue, AddressFamily addressFamily = AddressFamily.InterNetwork) + private static int GetAvailableTcpPort(int minValue) { var hashSet = new HashSet(); var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners(); foreach (var endpoint in tcpListeners) { - if (endpoint.AddressFamily == addressFamily) - { - hashSet.Add(endpoint.Port); - } + hashSet.Add(endpoint.Port); } for (var port = minValue; port < IPEndPoint.MaxPort; port++) diff --git a/FastGithub.HttpServer/HttpProxyMiddleware.cs b/FastGithub.HttpServer/HttpProxyMiddleware.cs index 95b4bdb..1efebcc 100644 --- a/FastGithub.HttpServer/HttpProxyMiddleware.cs +++ b/FastGithub.HttpServer/HttpProxyMiddleware.cs @@ -20,7 +20,6 @@ namespace FastGithub.HttpServer /// sealed class HttpProxyMiddleware { - private const string LOOPBACK = "127.0.0.1"; private const string LOCALHOST = "localhost"; private const int HTTP_PORT = 80; private const int HTTPS_PORT = 443; @@ -120,7 +119,17 @@ namespace FastGithub.HttpServer /// private bool IsFastGithubServer(HostString host) { - return host.Port == this.fastGithubConfig.HttpProxyPort && (host.Host == LOOPBACK || host.Host == LOCALHOST); + if (host.Port != this.fastGithubConfig.HttpProxyPort) + { + return false; + } + + if (host.Host == LOCALHOST) + { + return true; + } + + return IPAddress.TryParse(host.Host, out var address) && IPAddress.IsLoopback(address); } /// diff --git a/FastGithub.HttpServer/KestrelServerOptionsExtensions.cs b/FastGithub.HttpServer/KestrelServerOptionsExtensions.cs index 731f4bc..38e7fe2 100644 --- a/FastGithub.HttpServer/KestrelServerOptionsExtensions.cs +++ b/FastGithub.HttpServer/KestrelServerOptionsExtensions.cs @@ -8,9 +8,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using System; using System.Linq; -using System.Net; using System.Net.NetworkInformation; -using System.Net.Sockets; namespace FastGithub { @@ -45,8 +43,8 @@ namespace FastGithub } var logger = kestrel.GetLogger(); - kestrel.Listen(IPAddress.Loopback, httpProxyPort); - logger.LogInformation($"已监听http://{IPAddress.Loopback}:{httpProxyPort},http代理服务启动完成"); + kestrel.ListenLocalhost(httpProxyPort); + logger.LogInformation($"已监听http://localhost:{httpProxyPort},http代理服务启动完成"); } /// @@ -56,7 +54,7 @@ namespace FastGithub public static void ListenSshReverseProxy(this KestrelServerOptions kestrel) { var sshPort = ReverseProxyPort.Ssh; - kestrel.Listen(IPAddress.Loopback, sshPort, listen => + kestrel.ListenLocalhost(sshPort, listen => { listen.UseFlowAnalyze(); listen.UseConnectionHandler(); @@ -64,7 +62,7 @@ namespace FastGithub if (OperatingSystem.IsWindows()) { - kestrel.GetLogger().LogInformation($"已监听ssh://{IPAddress.Loopback}:{sshPort},github的ssh反向代理服务启动完成"); + kestrel.GetLogger().LogInformation($"已监听ssh://localhost:{sshPort},github的ssh反向代理服务启动完成"); } } @@ -75,11 +73,11 @@ namespace FastGithub public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel) { var httpPort = ReverseProxyPort.Http; - kestrel.Listen(IPAddress.Loopback, httpPort); + kestrel.ListenLocalhost(httpPort); if (OperatingSystem.IsWindows()) { - kestrel.GetLogger().LogInformation($"已监听http://{IPAddress.Loopback}:{httpPort},http反向代理服务启动完成"); + kestrel.GetLogger().LogInformation($"已监听http://localhost:{httpPort},http反向代理服务启动完成"); } } @@ -95,7 +93,7 @@ namespace FastGithub certService.InstallAndTrustCaCert(); var httpsPort = ReverseProxyPort.Https; - kestrel.Listen(IPAddress.Loopback, httpsPort, listen => + kestrel.ListenLocalhost(httpsPort, listen => { if (OperatingSystem.IsWindows()) { @@ -110,7 +108,7 @@ namespace FastGithub if (OperatingSystem.IsWindows()) { var logger = kestrel.GetLogger(); - logger.LogInformation($"已监听https://{IPAddress.Loopback}:{httpsPort},https反向代理服务启动完成"); + logger.LogInformation($"已监听https://localhost:{httpsPort},https反向代理服务启动完成"); } } @@ -128,13 +126,12 @@ namespace FastGithub /// /// 是否可以监听指定tcp端口 /// - /// - /// + /// /// - private static bool CanListenTcp(int port, AddressFamily addressFamily = AddressFamily.InterNetwork) + private static bool CanListenTcp(int port) { var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners(); - return tcpListeners.Any(item => item.AddressFamily == addressFamily && item.Port == port) == false; + return tcpListeners.Any(item => item.Port == port) == false; } } } diff --git a/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs b/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs index b7d23d6..6a5d7d4 100644 --- a/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs +++ b/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs @@ -142,7 +142,7 @@ namespace FastGithub.PacketIntercept.Dns // dns响应数据 var response = Response.FromRequest(request); - var loopback = question.Type == RecordType.A ? IPAddress.Loopback : IPAddress.Loopback.MapToIPv6(); + var loopback = question.Type == RecordType.A ? IPAddress.Loopback : IPAddress.IPv6Loopback; var record = new IPAddressResourceRecord(domain, loopback, this.ttl); response.AnswerRecords.Add(record); var responsePayload = response.ToArray(); diff --git a/FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs b/FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs index 1cccfcc..729480b 100644 --- a/FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs +++ b/FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs @@ -2,7 +2,6 @@ using Microsoft.Extensions.Logging; using System; using System.ComponentModel; -using System.Net; using System.Runtime.Versioning; using System.Threading; using System.Threading.Tasks; @@ -54,7 +53,7 @@ namespace FastGithub.PacketIntercept.Tcp throw new Win32Exception(); } - this.logger.LogInformation($"tcp://{IPAddress.Loopback}:{this.oldServerPort} => tcp://{IPAddress.Loopback}:{this.newServerPort}"); + this.logger.LogInformation($"tcp://localhost:{this.oldServerPort} => tcp://localhost:{this.newServerPort}"); cancellationToken.Register(hwnd => WinDivert.WinDivertClose((IntPtr)hwnd!), handle); var packetLength = 0U;