From 351be8c207b8d14a61ae21d40648d93b092cbb09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E4=B9=9D?= <366193849@qq.com> Date: Sun, 19 Sep 2021 15:19:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4LocalMachine=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Configuration/LocalMachine.cs | 94 ------------------- FastGithub.Dns/DnsInterceptor.cs | 2 +- FastGithub.DomainResolve/DnscryptProxy.cs | 46 ++++++++- .../HttpsReverseProxyPort.cs | 35 ++++++- .../KestrelServerOptionsExtensions.cs | 23 ++++- 5 files changed, 99 insertions(+), 101 deletions(-) delete mode 100644 FastGithub.Configuration/LocalMachine.cs diff --git a/FastGithub.Configuration/LocalMachine.cs b/FastGithub.Configuration/LocalMachine.cs deleted file mode 100644 index 94d3ea5..0000000 --- a/FastGithub.Configuration/LocalMachine.cs +++ /dev/null @@ -1,94 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Net.NetworkInformation; -using System.Net.Sockets; - -namespace FastGithub.Configuration -{ - /// - /// 提供本机设备信息 - /// - public static class LocalMachine - { - /// - /// 获取可用的随机Tcp端口 - /// - /// - /// 最小值 - /// - public static int GetAvailableTcpPort(AddressFamily addressFamily, int min = 1025) - { - var hashSet = new HashSet(); - 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("当前无可用的端口"); - } - - - /// - /// 获取可用的随机端口 - /// - /// - /// 最小值 - /// - public static int GetAvailablePort(AddressFamily addressFamily, int min = 1025) - { - var hashSet = new HashSet(); - 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("当前无可用的端口"); - } - - /// - /// 是否可以监听指定tcp端口 - /// - /// - /// - public static bool CanListenTcp(int port) - { - var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners(); - return tcpListeners.Any(item => item.Port == port) == false; - } - } -} diff --git a/FastGithub.Dns/DnsInterceptor.cs b/FastGithub.Dns/DnsInterceptor.cs index f812ced..fbfa18a 100644 --- a/FastGithub.Dns/DnsInterceptor.cs +++ b/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}"); } } } diff --git a/FastGithub.DomainResolve/DnscryptProxy.cs b/FastGithub.DomainResolve/DnscryptProxy.cs index c986f19..8868b74 100644 --- a/FastGithub.DomainResolve/DnscryptProxy.cs +++ b/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 } } + + + /// + /// 获取可用的随机端口 + /// + /// + /// 最小值 + /// + private static int GetAvailablePort(AddressFamily addressFamily, int min = 5533) + { + var hashSet = new HashSet(); + 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("当前无可用的端口"); + } + /// /// 进程退出时 /// diff --git a/FastGithub.HttpServer/HttpsReverseProxyPort.cs b/FastGithub.HttpServer/HttpsReverseProxyPort.cs index 4427715..f3d33db 100644 --- a/FastGithub.HttpServer/HttpsReverseProxyPort.cs +++ b/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 /// /// 获取端口值 /// - public static int Value { get; } = OperatingSystem.IsWindows() ? 443 : LocalMachine.GetAvailableTcpPort(AddressFamily.InterNetwork); + public static int Value { get; } = OperatingSystem.IsWindows() ? 443 : GetAvailableTcpPort(AddressFamily.InterNetwork); + + /// + /// 获取可用的随机Tcp端口 + /// + /// + /// 最小值 + /// + private static int GetAvailableTcpPort(AddressFamily addressFamily, int min = 12345) + { + var hashSet = new HashSet(); + 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("当前无可用的端口"); + } } } diff --git a/FastGithub.HttpServer/KestrelServerOptionsExtensions.cs b/FastGithub.HttpServer/KestrelServerOptionsExtensions.cs index 3493e98..fb9e3d1 100644 --- a/FastGithub.HttpServer/KestrelServerOptionsExtensions.cs +++ b/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>().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()); 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(); return loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(HttpServer)}"); } + + /// + /// 是否可以监听指定tcp端口 + /// + /// + /// + /// + 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; + } } }