LocalMachine.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /// <param name="remoteEndPoint">远程地址</param>
  61. /// <returns></returns>
  62. public static IPAddress? GetLocalIPAddress(EndPoint remoteEndPoint)
  63. {
  64. try
  65. {
  66. using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  67. socket.Connect(remoteEndPoint);
  68. return socket.LocalEndPoint is IPEndPoint localEndPoint ? localEndPoint.Address : default;
  69. }
  70. catch (Exception)
  71. {
  72. return default;
  73. }
  74. }
  75. /// <summary>
  76. /// 获取可用的随机端口
  77. /// </summary>
  78. /// <param name="addressFamily"></param>
  79. /// <param name="min">最小值</param>
  80. /// <returns></returns>
  81. public static int GetAvailablePort(AddressFamily addressFamily, int min = 1025)
  82. {
  83. var hashSet = new HashSet<int>();
  84. var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
  85. var udpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners();
  86. foreach (var item in tcpListeners)
  87. {
  88. if (item.AddressFamily == addressFamily)
  89. {
  90. hashSet.Add(item.Port);
  91. }
  92. }
  93. foreach (var item in udpListeners)
  94. {
  95. if (item.AddressFamily == addressFamily)
  96. {
  97. hashSet.Add(item.Port);
  98. }
  99. }
  100. for (var port = min; port < ushort.MaxValue; port++)
  101. {
  102. if (hashSet.Contains(port) == false)
  103. {
  104. return port;
  105. }
  106. }
  107. throw new FastGithubException("当前无可用的端口");
  108. }
  109. /// <summary>
  110. /// 是否可以监听指定tcp端口
  111. /// </summary>
  112. /// <param name="port"></param>
  113. /// <returns></returns>
  114. public static bool CanListenTcp(int port)
  115. {
  116. var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
  117. return tcpListeners.Any(item => item.Port == port) == false;
  118. }
  119. /// <summary>
  120. /// 是否可以监听指定udp端口
  121. /// </summary>
  122. /// <param name="port"></param>
  123. /// <returns></returns>
  124. public static bool CanListenUdp(int port)
  125. {
  126. var udpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners();
  127. return udpListeners.Any(item => item.Port == port) == false;
  128. }
  129. }
  130. }