LocalMachine.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /// 获取可用的随机Tcp端口
  59. /// </summary>
  60. /// <param name="addressFamily"></param>
  61. /// <param name="min">最小值</param>
  62. /// <returns></returns>
  63. public static int GetAvailableTcpPort(AddressFamily addressFamily, int min = 1025)
  64. {
  65. var hashSet = new HashSet<int>();
  66. var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
  67. foreach (var item in tcpListeners)
  68. {
  69. if (item.AddressFamily == addressFamily)
  70. {
  71. hashSet.Add(item.Port);
  72. }
  73. }
  74. for (var port = min; port < ushort.MaxValue; port++)
  75. {
  76. if (hashSet.Contains(port) == false)
  77. {
  78. return port;
  79. }
  80. }
  81. throw new FastGithubException("当前无可用的端口");
  82. }
  83. /// <summary>
  84. /// 获取可用的随机端口
  85. /// </summary>
  86. /// <param name="addressFamily"></param>
  87. /// <param name="min">最小值</param>
  88. /// <returns></returns>
  89. public static int GetAvailablePort(AddressFamily addressFamily, int min = 1025)
  90. {
  91. var hashSet = new HashSet<int>();
  92. var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
  93. var udpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners();
  94. foreach (var item in tcpListeners)
  95. {
  96. if (item.AddressFamily == addressFamily)
  97. {
  98. hashSet.Add(item.Port);
  99. }
  100. }
  101. foreach (var item in udpListeners)
  102. {
  103. if (item.AddressFamily == addressFamily)
  104. {
  105. hashSet.Add(item.Port);
  106. }
  107. }
  108. for (var port = min; port < ushort.MaxValue; port++)
  109. {
  110. if (hashSet.Contains(port) == false)
  111. {
  112. return port;
  113. }
  114. }
  115. throw new FastGithubException("当前无可用的端口");
  116. }
  117. /// <summary>
  118. /// 是否可以监听指定tcp端口
  119. /// </summary>
  120. /// <param name="port"></param>
  121. /// <returns></returns>
  122. public static bool CanListenTcp(int port)
  123. {
  124. var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
  125. return tcpListeners.Any(item => item.Port == port) == false;
  126. }
  127. }
  128. }