MRCUtility.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Net;
  4. using System.Net.NetworkInformation;
  5. using System.Net.Sockets;
  6. using UnityEngine;
  7. namespace Rokid.MRC
  8. {
  9. //获取IP地址
  10. public class MRCUtility
  11. {
  12. public static List<string> interfaceList = new List<string>();
  13. /// <summary>
  14. /// 获取本机IP
  15. /// </summary>
  16. /// <param name="addType">要获取的IP类型</param>
  17. /// <returns></returns>
  18. public static List<string> GetLocalIP(AddressType addType)
  19. {
  20. interfaceList.Clear();
  21. if(addType == AddressType.IPv6 && !Socket.OSSupportsIPv6)
  22. {
  23. //如果想要IPv6,但是系统不支持
  24. return null;
  25. }
  26. string output = string.Empty;
  27. //遍历所有网卡NetworkInterface(真实和虚拟)
  28. foreach(NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
  29. {
  30. Debug.Log($"NetworkInterface Name : {networkInterface.Name} Desc : {networkInterface.Description}");
  31. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  32. //无线网卡
  33. NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211;
  34. //以太网卡
  35. NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet;
  36. if((networkInterface.NetworkInterfaceType == _type1 || networkInterface.NetworkInterfaceType == _type2) && networkInterface.OperationalStatus == OperationalStatus.Up)
  37. #endif
  38. {
  39. //该网卡的所有IP地址
  40. foreach(UnicastIPAddressInformation ip in networkInterface.GetIPProperties().UnicastAddresses)
  41. {
  42. if(addType == AddressType.IPv4)
  43. {
  44. //IPv4
  45. if(ip.Address.AddressFamily == AddressFamily.InterNetwork)
  46. {
  47. interfaceList.Add($"Name : {networkInterface.Name} IP : {ip.Address}");
  48. Debug.Log($"Name : {networkInterface.Name} IP : {ip.Address}");
  49. output = ip.Address.ToString();
  50. }
  51. }
  52. else if(addType == AddressType.IPv6)
  53. {
  54. //IPv6
  55. if(ip.Address.AddressFamily == AddressFamily.InterNetworkV6)
  56. {
  57. output = ip.Address.ToString();
  58. }
  59. }
  60. }
  61. }
  62. }
  63. return interfaceList;
  64. }
  65. public static string GetWirelessIP(AddressType addType)
  66. {
  67. interfaceList.Clear();
  68. if(addType == AddressType.IPv6 && !Socket.OSSupportsIPv6)
  69. {
  70. //如果想要IPv6,但是系统不支持
  71. return null;
  72. }
  73. string output = string.Empty;
  74. //遍历所有网卡NetworkInterface(真实和虚拟)
  75. foreach(NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
  76. {
  77. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  78. //无线网卡
  79. NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211;
  80. //以太网卡
  81. NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet;
  82. if((networkInterface.NetworkInterfaceType == _type1 || networkInterface.NetworkInterfaceType == _type2) && networkInterface.OperationalStatus == OperationalStatus.Up)
  83. #endif
  84. {
  85. //该网卡的所有IP地址
  86. foreach(UnicastIPAddressInformation ip in networkInterface.GetIPProperties().UnicastAddresses)
  87. {
  88. if(addType == AddressType.IPv4)
  89. {
  90. //IPv4
  91. if(ip.Address.AddressFamily == AddressFamily.InterNetwork && networkInterface.Name.Contains("wlan"))
  92. {
  93. interfaceList.Add($"Name : {networkInterface.Name} IP : {ip.Address}");
  94. Debug.Log($"Name : {networkInterface.Name} IP : {ip.Address}");
  95. output = ip.Address.ToString();
  96. break;
  97. }
  98. }
  99. else if(addType == AddressType.IPv6)
  100. {
  101. //IPv6
  102. if(ip.Address.AddressFamily == AddressFamily.InterNetworkV6 && networkInterface.Name.Contains("wlan"))
  103. {
  104. output = ip.Address.ToString();
  105. break;
  106. }
  107. }
  108. }
  109. }
  110. }
  111. return output;
  112. }
  113. //本机的IP
  114. public static List<string> localAddresses = new List<string>();
  115. //本机所属的网段
  116. public static List<string> localSubAddresses = new List<string>();
  117. //获取本机的IP和网段
  118. public static void ScanLocalHost()
  119. {
  120. IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
  121. foreach(IPAddress ip in host.AddressList)
  122. {
  123. if(ip.AddressFamily == AddressFamily.InterNetwork)
  124. {
  125. //ipv4
  126. string address = ip.ToString();
  127. //局域网的网段
  128. string subAddress = address.Remove(address.LastIndexOf('.'));
  129. localAddresses.Add(address);
  130. Debug.Log($"Addr : {address} SubAddr : {subAddress}");
  131. if(!localSubAddresses.Contains(subAddress))
  132. {
  133. localSubAddresses.Add(subAddress);
  134. }
  135. }
  136. }
  137. }
  138. public static IPAddress[] GetLoacalIpv4()
  139. {
  140. IPAddress[] addresses = Dns.GetHostAddresses("localhost");
  141. return (from x in addresses where x.AddressFamily == AddressFamily.InterNetwork select x).ToArray();
  142. }
  143. public static IPAddress[] GetLoacalIpv6()
  144. {
  145. IPAddress[] addresses = Dns.GetHostAddresses("localhost");
  146. return (from x in addresses where x.AddressFamily == AddressFamily.InterNetworkV6 select x).ToArray();
  147. }
  148. //通过请求外网IP
  149. public static string GetOutSideIP()
  150. {
  151. using(WebClient wc = new WebClient())
  152. {
  153. return wc.DownloadString(@"http://icanhazip.com/").Replace("\n", "");
  154. }
  155. }
  156. }
  157. public enum AddressType
  158. {
  159. IPv4, IPv6
  160. }
  161. }