From dabdbca566030c438ddc414c78a0af5c42eebabe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Thu, 18 Nov 2021 12:51:33 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AB=AF=E5=8F=A3=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Configuration/ReverseProxyPort.cs | 56 ++++++++++++++------ 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/FastGithub.Configuration/ReverseProxyPort.cs b/FastGithub.Configuration/ReverseProxyPort.cs index aa6e7fc..644ebca 100644 --- a/FastGithub.Configuration/ReverseProxyPort.cs +++ b/FastGithub.Configuration/ReverseProxyPort.cs @@ -13,42 +13,64 @@ namespace FastGithub.Configuration /// /// ssh端口 /// - public static int Ssh { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(22) : GetAvailableTcpPort(3822); + public static int Ssh { get; } /// /// http端口 /// - public static int Http { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(80) : GetAvailableTcpPort(3880); + public static int Http { get; } /// /// https端口 /// - public static int Https { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(443) : GetAvailableTcpPort(38443); + public static int Https { get; } /// - /// 获取可用的随机Tcp端口 + /// 反向代理端口 /// - /// - /// - private static int GetAvailableTcpPort(int minValue) + static ReverseProxyPort() { - var hashSet = new HashSet(); - var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners(); + var ports = new TcpListenerPortCollection(); + Ssh = OperatingSystem.IsWindows() ? ports.GetAvailablePort(22) : ports.GetAvailablePort(3822); + Http = OperatingSystem.IsWindows() ? ports.GetAvailablePort(80) : ports.GetAvailablePort(3880); + Https = OperatingSystem.IsWindows() ? ports.GetAvailablePort(443) : ports.GetAvailablePort(38443); + } - foreach (var endpoint in tcpListeners) - { - hashSet.Add(endpoint.Port); - } + /// + /// 已监听的tcp端口集合 + /// + private class TcpListenerPortCollection + { + private readonly HashSet tcpPorts = new(); - for (var port = minValue; port < IPEndPoint.MaxPort; port++) + /// + /// 已监听的tcp端口集合 + /// + public TcpListenerPortCollection() { - if (hashSet.Contains(port) == false) + var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners(); + foreach (var endpoint in tcpListeners) { - return port; + this.tcpPorts.Add(endpoint.Port); } } - throw new FastGithubException("当前无可用的端口"); + /// + /// 获取可用的随机Tcp端口 + /// + /// + /// + public int GetAvailablePort(int minValue) + { + for (var port = minValue; port < IPEndPoint.MaxPort; port++) + { + if (this.tcpPorts.Contains(port) == false) + { + return port; + } + } + throw new FastGithubException("当前无可用的端口"); + } } } }