using FastGithub.Configuration;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
namespace FastGithub.HttpServer
{
    /// 
    /// https反向代理端口
    /// 
    static class HttpsReverseProxyPort
    {
        /// 
        /// 获取端口值
        /// 
        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("当前无可用的端口");
        }
    }
}