using System; using System.Collections.Generic; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; namespace FastGithub.Configuration { /// /// https反向代理端口 /// public static class HttpsReverseProxyPort { /// /// 获取端口值 /// public static int Value { get; } = GetAvailableTcpPort(AddressFamily.InterNetwork); /// /// 获取可用的随机Tcp端口 /// /// /// private static int GetAvailableTcpPort(AddressFamily addressFamily) { return OperatingSystem.IsWindows() ? GetAvailableTcpPort(addressFamily, 443) : GetAvailableTcpPort(addressFamily, 12345); } /// /// 获取可用的随机Tcp端口 /// /// /// 最小值 /// private static int GetAvailableTcpPort(AddressFamily addressFamily, int min) { 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("当前无可用的端口"); } } }