Explorar el Código

移除LocalMachine类

老九 hace 3 años
padre
commit
351be8c207

+ 0 - 94
FastGithub.Configuration/LocalMachine.cs

@@ -1,94 +0,0 @@
-using System.Collections.Generic;
-using System.Linq;
-using System.Net.NetworkInformation;
-using System.Net.Sockets;
-
-namespace FastGithub.Configuration
-{
-    /// <summary>
-    /// 提供本机设备信息
-    /// </summary>
-    public static class LocalMachine
-    { 
-        /// <summary>
-        /// 获取可用的随机Tcp端口
-        /// </summary>
-        /// <param name="addressFamily"></param>
-        /// <param name="min">最小值</param>
-        /// <returns></returns>
-        public static int GetAvailableTcpPort(AddressFamily addressFamily, int min = 1025)
-        {
-            var hashSet = new HashSet<int>();
-            var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
-
-            foreach (var item in tcpListeners)
-            {
-                if (item.AddressFamily == addressFamily)
-                {
-                    hashSet.Add(item.Port);
-                }
-            }
-
-            for (var port = min; port < ushort.MaxValue; port++)
-            {
-                if (hashSet.Contains(port) == false)
-                {
-                    return port;
-                }
-            }
-
-            throw new FastGithubException("当前无可用的端口");
-        }
-
-
-        /// <summary>
-        /// 获取可用的随机端口
-        /// </summary>
-        /// <param name="addressFamily"></param>
-        /// <param name="min">最小值</param>
-        /// <returns></returns>
-        public static int GetAvailablePort(AddressFamily addressFamily, int min = 1025)
-        {
-            var hashSet = new HashSet<int>();
-            var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
-            var udpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners();
-
-            foreach (var item in tcpListeners)
-            {
-                if (item.AddressFamily == addressFamily)
-                {
-                    hashSet.Add(item.Port);
-                }
-            }
-
-            foreach (var item in udpListeners)
-            {
-                if (item.AddressFamily == addressFamily)
-                {
-                    hashSet.Add(item.Port);
-                }
-            }
-
-            for (var port = min; port < ushort.MaxValue; port++)
-            {
-                if (hashSet.Contains(port) == false)
-                {
-                    return port;
-                }
-            }
-
-            throw new FastGithubException("当前无可用的端口");
-        }
-
-        /// <summary>
-        /// 是否可以监听指定tcp端口
-        /// </summary>
-        /// <param name="port"></param>
-        /// <returns></returns>
-        public static bool CanListenTcp(int port)
-        {
-            var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
-            return tcpListeners.Any(item => item.Port == port) == false;
-        }
-    }
-}

+ 1 - 1
FastGithub.Dns/DnsInterceptor.cs

@@ -162,7 +162,7 @@ namespace FastGithub.Dns
             }
 
             WinDivert.WinDivertHelperCalcChecksums(winDivertBuffer, packetLength, ref winDivertAddress, WinDivertChecksumHelperParam.All);
-            this.logger.LogInformation($"已拦截dns查询{domain}并伪造响应内容为{IPAddress.Loopback}");
+            this.logger.LogInformation($"已拦截dns查询{domain}并伪造解析结果为{IPAddress.Loopback}");
         }
     }
 }

+ 45 - 1
FastGithub.DomainResolve/DnscryptProxy.cs

@@ -1,9 +1,12 @@
 using FastGithub.Configuration;
 using System;
+using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
 using System.Linq;
 using System.Net;
+using System.Net.NetworkInformation;
+using System.Net.Sockets;
 using System.Threading;
 using System.Threading.Tasks;
 
@@ -35,7 +38,7 @@ namespace FastGithub.DomainResolve
         public async Task StartAsync(CancellationToken cancellationToken)
         {
             var tomlPath = Path.Combine(PATH, $"{NAME}.toml");
-            var port = LocalMachine.GetAvailablePort(IPAddress.Loopback.AddressFamily, min: 5533);
+            var port = GetAvailablePort(IPAddress.Loopback.AddressFamily);
             var localEndPoint = new IPEndPoint(IPAddress.Loopback, port);
             await TomlUtil.SetListensAsync(tomlPath, localEndPoint, cancellationToken);
 
@@ -65,6 +68,47 @@ namespace FastGithub.DomainResolve
             }
         }
 
+
+
+        /// <summary>
+        /// 获取可用的随机端口
+        /// </summary>
+        /// <param name="addressFamily"></param>
+        /// <param name="min">最小值</param>
+        /// <returns></returns>
+        private static int GetAvailablePort(AddressFamily addressFamily, int min = 5533)
+        {
+            var hashSet = new HashSet<int>();
+            var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
+            var udpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners();
+
+            foreach (var endPoint in tcpListeners)
+            {
+                if (endPoint.AddressFamily == addressFamily)
+                {
+                    hashSet.Add(endPoint.Port);
+                }
+            }
+
+            foreach (var endPoint in udpListeners)
+            {
+                if (endPoint.AddressFamily == addressFamily)
+                {
+                    hashSet.Add(endPoint.Port);
+                }
+            }
+
+            for (var port = min; port < IPEndPoint.MaxPort; port++)
+            {
+                if (hashSet.Contains(port) == false)
+                {
+                    return port;
+                }
+            }
+
+            throw new FastGithubException("当前无可用的端口");
+        }
+
         /// <summary>
         /// 进程退出时
         /// </summary>

+ 34 - 1
FastGithub.HttpServer/HttpsReverseProxyPort.cs

@@ -1,5 +1,8 @@
 using FastGithub.Configuration;
 using System;
+using System.Collections.Generic;
+using System.Net;
+using System.Net.NetworkInformation;
 using System.Net.Sockets;
 
 namespace FastGithub.HttpServer
@@ -12,6 +15,36 @@ namespace FastGithub.HttpServer
         /// <summary>
         /// 获取端口值
         /// </summary>
-        public static int Value { get; } = OperatingSystem.IsWindows() ? 443 : LocalMachine.GetAvailableTcpPort(AddressFamily.InterNetwork);
+        public static int Value { get; } = OperatingSystem.IsWindows() ? 443 : GetAvailableTcpPort(AddressFamily.InterNetwork);
+
+        /// <summary>
+        /// 获取可用的随机Tcp端口
+        /// </summary>
+        /// <param name="addressFamily"></param>
+        /// <param name="min">最小值</param>
+        /// <returns></returns>
+        private static int GetAvailableTcpPort(AddressFamily addressFamily, int min = 12345)
+        {
+            var hashSet = new HashSet<int>();
+            var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
+
+            foreach (var endpoint in tcpListeners)
+            {
+                if (endpoint.AddressFamily == addressFamily)
+                {
+                    hashSet.Add(endpoint.Port);
+                }
+            }
+
+            for (var port = min; port < IPEndPoint.MaxPort; port++)
+            {
+                if (hashSet.Contains(port) == false)
+                {
+                    return port;
+                }
+            }
+
+            throw new FastGithubException("当前无可用的端口");
+        }
     }
 }

+ 19 - 4
FastGithub.HttpServer/KestrelServerOptionsExtensions.cs

@@ -7,7 +7,10 @@ using Microsoft.Extensions.DependencyInjection;
 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
 {
@@ -36,7 +39,7 @@ namespace FastGithub
             var options = kestrel.ApplicationServices.GetRequiredService<IOptions<FastGithubOptions>>().Value;
             var httpProxyPort = options.HttpProxyPort;
 
-            if (LocalMachine.CanListenTcp(httpProxyPort) == false)
+            if (CanListenTcp(httpProxyPort) == false)
             {
                 throw new FastGithubException($"tcp端口{httpProxyPort}已经被其它进程占用,请在配置文件更换{nameof(FastGithubOptions.HttpProxyPort)}为其它端口");
             }
@@ -53,7 +56,7 @@ namespace FastGithub
         public static void ListenSshReverseProxy(this KestrelServerOptions kestrel)
         {
             const int SSH_PORT = 22;
-            if (LocalMachine.CanListenTcp(SSH_PORT) == true)
+            if (CanListenTcp(SSH_PORT) == true)
             {
                 kestrel.Listen(IPAddress.Loopback, SSH_PORT, listen => listen.UseConnectionHandler<SshReverseProxyHandler>());
                 kestrel.GetLogger().LogInformation($"已监听ssh://{IPAddress.Loopback}:{SSH_PORT},github的ssh反向代理服务启动完成");
@@ -67,7 +70,7 @@ namespace FastGithub
         public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel)
         {
             const int HTTP_PORT = 80;
-            if (LocalMachine.CanListenTcp(HTTP_PORT) == true)
+            if (CanListenTcp(HTTP_PORT) == true)
             {
                 kestrel.Listen(IPAddress.Loopback, HTTP_PORT);
                 kestrel.GetLogger().LogInformation($"已监听http://{IPAddress.Loopback}:{HTTP_PORT},http反向代理服务启动完成");
@@ -88,7 +91,7 @@ namespace FastGithub
                 TcpTable.KillPortOwner(httpsPort);
             }
 
-            if (LocalMachine.CanListenTcp(httpsPort) == false)
+            if (CanListenTcp(httpsPort) == false)
             {
                 throw new FastGithubException($"tcp端口{httpsPort}已经被其它进程占用");
             }
@@ -121,5 +124,17 @@ namespace FastGithub
             var loggerFactory = kestrel.ApplicationServices.GetRequiredService<ILoggerFactory>();
             return loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(HttpServer)}");
         }
+
+        /// <summary>
+        /// 是否可以监听指定tcp端口
+        /// </summary>
+        /// <param name="port"></param>
+        /// <param name="addressFamily"></param>
+        /// <returns></returns>
+        private static bool CanListenTcp(int port, AddressFamily addressFamily = AddressFamily.InterNetwork)
+        {
+            var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
+            return tcpListeners.Any(item => item.AddressFamily == addressFamily && item.Port == port) == false;
+        }
     }
 }