ReverseProxyPort.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.NetworkInformation;
  5. namespace FastGithub.Configuration
  6. {
  7. /// <summary>
  8. /// 反向代理端口
  9. /// </summary>
  10. public static class ReverseProxyPort
  11. {
  12. /// <summary>
  13. /// ssh端口
  14. /// </summary>
  15. public static int Ssh { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(22) : GetAvailableTcpPort(3822);
  16. /// <summary>
  17. /// http端口
  18. /// </summary>
  19. public static int Http { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(80) : GetAvailableTcpPort(3880);
  20. /// <summary>
  21. /// https端口
  22. /// </summary>
  23. public static int Https { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(443) : GetAvailableTcpPort(38443);
  24. /// <summary>
  25. /// 获取可用的随机Tcp端口
  26. /// </summary>
  27. /// <param name="minValue"></param>
  28. /// <returns></returns>
  29. private static int GetAvailableTcpPort(int minValue)
  30. {
  31. var hashSet = new HashSet<int>();
  32. var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
  33. foreach (var endpoint in tcpListeners)
  34. {
  35. hashSet.Add(endpoint.Port);
  36. }
  37. for (var port = minValue; port < IPEndPoint.MaxPort; port++)
  38. {
  39. if (hashSet.Contains(port) == false)
  40. {
  41. return port;
  42. }
  43. }
  44. throw new FastGithubException("当前无可用的端口");
  45. }
  46. }
  47. }