浏览代码

ipv6监听

陈国伟 3 年之前
父节点
当前提交
6b7450b94f

+ 3 - 8
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
         /// <summary>
         /// 获取可用的随机Tcp端口
         /// </summary>
-        /// <param name="minValue"></param>
-        /// <param name="addressFamily"></param>
+        /// <param name="minValue"></param> 
         /// <returns></returns>
-        private static int GetAvailableTcpPort(int minValue, AddressFamily addressFamily = AddressFamily.InterNetwork)
+        private static int GetAvailableTcpPort(int minValue)
         {
             var hashSet = new HashSet<int>();
             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++)

+ 11 - 2
FastGithub.HttpServer/HttpProxyMiddleware.cs

@@ -20,7 +20,6 @@ namespace FastGithub.HttpServer
     /// </summary>
     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
         /// <returns></returns>
         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);
         }
 
         /// <summary>

+ 11 - 14
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代理服务启动完成");
         }
 
         /// <summary>
@@ -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<SshReverseProxyHandler>();
@@ -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
         /// <summary>
         /// 是否可以监听指定tcp端口
         /// </summary>
-        /// <param name="port"></param>
-        /// <param name="addressFamily"></param>
+        /// <param name="port"></param> 
         /// <returns></returns>
-        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;
         }
     }
 }

+ 1 - 1
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();

+ 1 - 2
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;