GlobalListener.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 GlobalListener
  11. {
  12. private static readonly IPGlobalProperties global = IPGlobalProperties.GetIPGlobalProperties();
  13. private static readonly HashSet<int> tcpListenPorts = GetListenPorts(global.GetActiveTcpListeners);
  14. private static readonly HashSet<int> udpListenPorts = GetListenPorts(global.GetActiveUdpListeners);
  15. /// <summary>
  16. /// ssh端口
  17. /// </summary>
  18. public static int SshPort { get; } = GetAvailableTcpPort(22);
  19. /// <summary>
  20. /// git端口
  21. /// </summary>
  22. public static int GitPort { get; } = GetAvailableTcpPort(9418);
  23. /// <summary>
  24. /// http端口
  25. /// </summary>
  26. public static int HttpPort { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(80) : GetAvailableTcpPort(3880);
  27. /// <summary>
  28. /// https端口
  29. /// </summary>
  30. public static int HttpsPort { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(443) : GetAvailableTcpPort(38443);
  31. /// <summary>
  32. /// 获取已监听的端口
  33. /// </summary>
  34. /// <param name="func"></param>
  35. /// <returns></returns>
  36. private static HashSet<int> GetListenPorts(Func<IPEndPoint[]> func)
  37. {
  38. var hashSet = new HashSet<int>();
  39. try
  40. {
  41. foreach (var endpoint in func())
  42. {
  43. hashSet.Add(endpoint.Port);
  44. }
  45. }
  46. catch (Exception)
  47. {
  48. }
  49. return hashSet;
  50. }
  51. /// <summary>
  52. /// 是可以监听TCP
  53. /// </summary>
  54. /// <param name="port"></param>
  55. /// <returns></returns>
  56. public static bool CanListenTcp(int port)
  57. {
  58. return tcpListenPorts.Contains(port) == false;
  59. }
  60. /// <summary>
  61. /// 是可以监听UDP
  62. /// </summary>
  63. /// <param name="port"></param>
  64. /// <returns></returns>
  65. public static bool CanListenUdp(int port)
  66. {
  67. return udpListenPorts.Contains(port) == false;
  68. }
  69. /// <summary>
  70. /// 是可以监听TCP和Udp
  71. /// </summary>
  72. /// <param name="port"></param>
  73. /// <returns></returns>
  74. public static bool CanListen(int port)
  75. {
  76. return CanListenTcp(port) && CanListenUdp(port);
  77. }
  78. /// <summary>
  79. /// 获取可用的随机Tcp端口
  80. /// </summary>
  81. /// <param name="minPort"></param>
  82. /// <returns></returns>
  83. public static int GetAvailableTcpPort(int minPort)
  84. {
  85. return GetAvailablePort(CanListenTcp, minPort);
  86. }
  87. /// <summary>
  88. /// 获取可用的随机Udp端口
  89. /// </summary>
  90. /// <param name="minPort"></param>
  91. /// <returns></returns>
  92. public static int GetAvailableUdpPort(int minPort)
  93. {
  94. return GetAvailablePort(CanListenUdp, minPort);
  95. }
  96. /// <summary>
  97. /// 获取可用的随机端口
  98. /// </summary>
  99. /// <param name="minPort"></param>
  100. /// <returns></returns>
  101. public static int GetAvailablePort(int minPort)
  102. {
  103. return GetAvailablePort(CanListen, minPort);
  104. }
  105. /// <summary>
  106. /// 获取可用端口
  107. /// </summary>
  108. /// <param name="canFunc"></param>
  109. /// <param name="minPort"></param>
  110. /// <returns></returns>
  111. /// <exception cref="FastGithubException"></exception>
  112. private static int GetAvailablePort(Func<int, bool> canFunc, int minPort)
  113. {
  114. for (var port = minPort; port < IPEndPoint.MaxPort; port++)
  115. {
  116. if (canFunc(port) == true)
  117. {
  118. return port;
  119. }
  120. }
  121. throw new FastGithubException("当前无可用的端口");
  122. }
  123. }
  124. }