2
0

LocalMachine.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.NetworkInformation;
  6. using System.Net.Sockets;
  7. namespace FastGithub.Configuration
  8. {
  9. /// <summary>
  10. /// 提供本机设备信息
  11. /// </summary>
  12. public static class LocalMachine
  13. {
  14. /// <summary>
  15. /// 获取设备名
  16. /// </summary>
  17. public static string Name => Environment.MachineName;
  18. /// <summary>
  19. /// 获取本机设备所有IP
  20. /// </summary>
  21. /// <returns></returns>
  22. public static IEnumerable<IPAddress> GetAllIPAddresses()
  23. {
  24. yield return IPAddress.Loopback;
  25. yield return IPAddress.IPv6Loopback;
  26. foreach (var @interface in NetworkInterface.GetAllNetworkInterfaces())
  27. {
  28. foreach (var addressInfo in @interface.GetIPProperties().UnicastAddresses)
  29. {
  30. yield return addressInfo.Address;
  31. }
  32. }
  33. }
  34. /// <summary>
  35. /// 获取本机设备所有IPv4
  36. /// </summary>
  37. /// <returns></returns>
  38. public static IEnumerable<IPAddress> GetAllIPv4Addresses()
  39. {
  40. foreach (var address in GetAllIPAddresses())
  41. {
  42. if (address.AddressFamily == AddressFamily.InterNetwork)
  43. {
  44. yield return address;
  45. }
  46. }
  47. }
  48. /// <summary>
  49. /// 返回本机设备是否包含指定IP
  50. /// </summary>
  51. /// <param name="address"></param>
  52. /// <returns></returns>
  53. public static bool ContainsIPAddress(IPAddress address)
  54. {
  55. return GetAllIPAddresses().Contains(address);
  56. }
  57. /// <summary>
  58. /// 获取所有域名和ip
  59. /// </summary>
  60. /// <returns></returns>
  61. public static HashSet<string> GetAllHostNames()
  62. {
  63. var hashSet = new HashSet<string>
  64. {
  65. Name,
  66. "localhost",
  67. };
  68. foreach (var address in GetAllIPAddresses())
  69. {
  70. hashSet.Add(address.ToString());
  71. }
  72. return hashSet;
  73. }
  74. /// <summary>
  75. /// 获取与远程节点通讯的的本机IP地址
  76. /// </summary>
  77. /// <param name="remoteEndPoint">远程地址</param>
  78. /// <returns></returns>
  79. public static IPAddress? GetLocalIPAddress(EndPoint remoteEndPoint)
  80. {
  81. try
  82. {
  83. using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  84. socket.Connect(remoteEndPoint);
  85. return socket.LocalEndPoint is IPEndPoint localEndPoint ? localEndPoint.Address : default;
  86. }
  87. catch (Exception)
  88. {
  89. return default;
  90. }
  91. }
  92. /// <summary>
  93. /// 获取可用的随机端口
  94. /// </summary>
  95. /// <param name="addressFamily"></param>
  96. /// <param name="min">最小值</param>
  97. /// <returns></returns>
  98. public static int GetAvailablePort(AddressFamily addressFamily, int min = 1024)
  99. {
  100. var hashSet = new HashSet<int>();
  101. var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
  102. var udpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners();
  103. foreach (var item in tcpListeners)
  104. {
  105. if (item.AddressFamily == addressFamily)
  106. {
  107. hashSet.Add(item.Port);
  108. }
  109. }
  110. foreach (var item in udpListeners)
  111. {
  112. if (item.AddressFamily == addressFamily)
  113. {
  114. hashSet.Add(item.Port);
  115. }
  116. }
  117. for (var port = min; port < ushort.MaxValue; port++)
  118. {
  119. if (hashSet.Contains(port) == false)
  120. {
  121. return port;
  122. }
  123. }
  124. throw new FastGithubException("当前无可用的端口");
  125. }
  126. /// <summary>
  127. /// 是否可以监听指定tcp端口
  128. /// </summary>
  129. /// <param name="port"></param>
  130. /// <returns></returns>
  131. public static bool CanListenTcp(int port)
  132. {
  133. var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
  134. return tcpListeners.Any(item => item.Port == port) == false;
  135. }
  136. /// <summary>
  137. /// 是否可以监听指定udp端口
  138. /// </summary>
  139. /// <param name="port"></param>
  140. /// <returns></returns>
  141. public static bool CanListenUdp(int port)
  142. {
  143. var udpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners();
  144. return udpListeners.Any(item => item.Port == port) == false;
  145. }
  146. }
  147. }