You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

450 lines
15 KiB

  1. using Apewer.Internals.Interop;
  2. using Apewer.Network;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.Net;
  8. using System.Net.NetworkInformation;
  9. using System.Net.Sockets;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. namespace Apewer
  13. {
  14. /// <summary></summary>
  15. public class NetworkUtility
  16. {
  17. #region UDP
  18. /// <summary>唤醒局域网中拥有指定 MAC 地址的设备。</summary>
  19. /// <param name="mac">被唤醒设备的 MAC 地址,必须是长度为 6 的字节数组。</param>
  20. public static void WakeOnLan(byte[] mac)
  21. {
  22. if (mac.Length != 6) return;
  23. var uc = new System.Net.Sockets.UdpClient();
  24. uc.Connect(IPAddress.Broadcast, 65535);
  25. var pack = new List<byte>();
  26. // 前 6 字节为 0xFF。
  27. for (int i = 0; i < 6; i++) pack.Add(255);
  28. // 目标 MAC 地址重复 16 次。
  29. for (int i = 0; i < 16; i++)
  30. {
  31. for (int j = 0; j < 6; j++) pack.Add(mac[j]);
  32. }
  33. // 发送 102 字节数据。
  34. uc.Send(pack.ToArray(), pack.Count);
  35. uc.Close();
  36. }
  37. /// <summary>从 NTP 服务器获取 UTC 时间。</summary>
  38. /// <remarks>
  39. /// Worldwide: pool.ntp.org<br/>
  40. /// Asia: asia.pool.ntp.org<br/>
  41. /// Europe: europe.pool.ntp.org<br/>
  42. /// North: America north-america.pool.ntp.org<br/>
  43. /// Oceania: oceania.pool.ntp.org<br/>
  44. /// South America: south-america.pool.ntp.org<br/>
  45. /// Windows: time.windows.com<br/>
  46. /// Windows: time.nist.gov<br/>
  47. /// China: us.ntp.org.cn<br/>
  48. /// China: edu.ntp.org.cn<br/>
  49. /// </remarks>
  50. public static DateTime GetUtcFromNtp(string server = "pool.ntp.org", int port = 123, int timeout = 1000)
  51. {
  52. try
  53. {
  54. var addresses = Dns.GetHostEntry(server).AddressList;
  55. if (addresses.Length > 0)
  56. {
  57. var endpoint = new IPEndPoint(addresses[0], port);
  58. return GetUtcFromNtp(endpoint, timeout);
  59. }
  60. }
  61. catch { }
  62. return ClockUtility.Zero;
  63. }
  64. /// <summary>从 NTP 服务器获取 UTC 时间。</summary>
  65. public static DateTime GetUtcFromNtp(IPEndPoint endpoint, int timeout = 1000)
  66. {
  67. try
  68. {
  69. var request = new byte[48];
  70. request[0] = 0x1B;
  71. var response = new byte[48];
  72. response[0] = 0x1B;
  73. var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  74. socket.Connect(endpoint);
  75. socket.ReceiveTimeout = timeout;
  76. socket.Send(request);
  77. socket.Receive(response);
  78. socket.Close();
  79. const byte replytime = 40;
  80. ulong secondspart = BitConverter.ToUInt32(response, replytime);
  81. ulong secondsfraction = BitConverter.ToUInt32(response, replytime + 4);
  82. secondspart = SwapEndian(secondspart);
  83. secondsfraction = SwapEndian(secondsfraction);
  84. ulong milliseconds = (secondspart * 1000) + ((secondsfraction * 1000) / 0x100000000UL);
  85. var utc = (new DateTime(1900, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).AddMilliseconds(milliseconds);
  86. return utc;
  87. }
  88. catch { }
  89. return ClockUtility.Zero;
  90. }
  91. private static uint SwapEndian(ulong x)
  92. {
  93. var a = ((x & 0x000000ff) << 24);
  94. var b = ((x & 0x0000ff00) << 8);
  95. var c = ((x & 0x00ff0000) >> 8);
  96. var d = ((x & 0xff000000) >> 24);
  97. return (uint)(a + b + c + d);
  98. }
  99. #endregion
  100. #region ARP
  101. /// <summary>发送 ARP 请求,获取目标 IP 地址对应设备的 MAC 地址,MAC 格式为 01-23-45-67-89-AB。</summary>
  102. /// <param name="ip">目标 IP 地址。</param>
  103. /// <remarks>注:<br />仅 Windows 可用。</remarks>
  104. /// <returns>MAC 地址。</returns>
  105. public static string SendARP(string ip)
  106. {
  107. if (!string.IsNullOrEmpty(ip))
  108. {
  109. try
  110. {
  111. var ipa = IPAddress.Parse(ip);
  112. uint dip = BitConverter.ToUInt32(ipa.GetAddressBytes(), 0);
  113. ulong mac = 0;
  114. uint len = 6;
  115. uint err = IpHlpApi.SendARP(dip, 0, ref mac, ref len);
  116. byte[] bytes = BitConverter.GetBytes(mac);
  117. return BitConverter.ToString(bytes, 0, 6);
  118. }
  119. catch { }
  120. }
  121. return "";
  122. }
  123. #endregion
  124. #region Puny Code
  125. /// <summary></summary>
  126. public static string ToPunyCode(string chinese)
  127. {
  128. if (string.IsNullOrEmpty(chinese)) return "";
  129. try { return new IdnMapping().GetAscii(chinese); }
  130. catch { return chinese; }
  131. }
  132. /// <summary></summary>
  133. public static string FromPunyCode(string punycode)
  134. {
  135. if (string.IsNullOrEmpty(punycode)) return "";
  136. try { return new IdnMapping().GetUnicode(punycode); }
  137. catch { return punycode; }
  138. }
  139. #endregion
  140. #region IP
  141. /// <summary>获取本地计算机的计算机名。</summary>
  142. public static string LocalHost
  143. {
  144. get
  145. {
  146. var hn = Dns.GetHostName();
  147. return string.IsNullOrEmpty(hn) ? "" : hn;
  148. }
  149. }
  150. /// <summary>本地计算机的 IP 地址。</summary>
  151. public static List<string> LocalIP
  152. {
  153. get
  154. {
  155. var list = new List<string>();
  156. var he = Dns.GetHostEntry(Dns.GetHostName());
  157. foreach (var ip in he.AddressList)
  158. {
  159. list.Add(ip.ToString());
  160. }
  161. return list;
  162. }
  163. }
  164. /// <summary>判断 IPv4 地址格式是否正确。</summary>
  165. public static bool IsIP(string ipv4)
  166. {
  167. try
  168. {
  169. if (string.IsNullOrEmpty(ipv4)) return false;
  170. var split = ipv4.Split('.');
  171. if (split.Length != 4) return false;
  172. for (int i = 0; i < 4; i++)
  173. {
  174. var n = Convert.ToInt32(split[i]);
  175. if (n < 0 || n > 255) return false;
  176. if (n.ToString() != split[i]) return false;
  177. }
  178. return true;
  179. }
  180. catch { }
  181. return false;
  182. }
  183. /// <summary>对目标地址进行解析。</summary>
  184. public static string Resolve(string host)
  185. {
  186. try
  187. {
  188. if (IsIP(host))
  189. {
  190. var ip = IPAddress.Parse(host);
  191. var he = Dns.GetHostEntry(ip);
  192. return he.HostName;
  193. }
  194. else
  195. {
  196. var ip = "";
  197. var dn = host.ToLower();
  198. var he = Dns.GetHostEntry(dn);
  199. var ts = "";
  200. var on = he.Aliases;
  201. he = Dns.GetHostEntry(dn);
  202. for (int i = 0; i < on.Length; i++) ts = ts + on[i].ToString() + ",";
  203. ts = "";
  204. var al = he.AddressList;
  205. for (int i = 0; i < al.Length; i++) ts = ts + al[i].ToString() + ",";
  206. ip = ts;
  207. if (ip.Length > 0)
  208. {
  209. if (ip.Substring(ip.Length - 1, 1) == ",") ip = ip.Substring(0, ip.Length - 1);
  210. }
  211. return ip;
  212. }
  213. }
  214. catch { }
  215. return "";
  216. }
  217. /// <summary>对目标地址进行解析。</summary>
  218. private static IPHostEntry InternalResolve(string hostName)
  219. {
  220. if (hostName.Length <= 255 && (hostName.Length != 255 || hostName[254] == '.'))
  221. {
  222. var intPtr = WS2_32.gethostbyname(hostName);
  223. if (intPtr != IntPtr.Zero) return NativeToHostEntry(intPtr);
  224. }
  225. return null;
  226. }
  227. private static IntPtr IntPtrAdd(IntPtr a, int b)
  228. {
  229. return (IntPtr)((long)a + b);
  230. }
  231. private static IPHostEntry NativeToHostEntry(IntPtr nativePointer)
  232. {
  233. hostent hostent = (hostent)Marshal.PtrToStructure(nativePointer, typeof(hostent));
  234. IPHostEntry iPHostEntry = new IPHostEntry();
  235. if (hostent.h_name != IntPtr.Zero)
  236. {
  237. iPHostEntry.HostName = Marshal.PtrToStringAnsi(hostent.h_name);
  238. }
  239. ArrayList arrayList = new ArrayList();
  240. IntPtr intPtr = hostent.h_addr_list;
  241. nativePointer = Marshal.ReadIntPtr(intPtr);
  242. while (nativePointer != IntPtr.Zero)
  243. {
  244. int newAddress = Marshal.ReadInt32(nativePointer);
  245. arrayList.Add(new IPAddress(newAddress));
  246. intPtr = IntPtrAdd(intPtr, IntPtr.Size);
  247. nativePointer = Marshal.ReadIntPtr(intPtr);
  248. }
  249. iPHostEntry.AddressList = new IPAddress[arrayList.Count];
  250. arrayList.CopyTo(iPHostEntry.AddressList, 0);
  251. arrayList.Clear();
  252. intPtr = hostent.h_aliases;
  253. nativePointer = Marshal.ReadIntPtr(intPtr);
  254. while (nativePointer != IntPtr.Zero)
  255. {
  256. string value = Marshal.PtrToStringAnsi(nativePointer);
  257. arrayList.Add(value);
  258. intPtr = IntPtrAdd(intPtr, IntPtr.Size);
  259. nativePointer = Marshal.ReadIntPtr(intPtr);
  260. }
  261. iPHostEntry.Aliases = new string[arrayList.Count];
  262. arrayList.CopyTo(iPHostEntry.Aliases, 0);
  263. return iPHostEntry;
  264. }
  265. /// <summary>将由字符串表示的 IPv4 地址转换为 32 位整数。</summary>
  266. public static int GetNumber(IPAddress ipv4)
  267. {
  268. try
  269. {
  270. var ba = ipv4.GetAddressBytes();
  271. return BitConverter.ToInt32(ba, 0);
  272. }
  273. catch { return 0; }
  274. }
  275. /// <summary>转换 IP 地址格式。</summary>
  276. public static string GetPlainAddress(IPAddress address)
  277. {
  278. try { return address.ToString(); }
  279. catch { return ""; }
  280. }
  281. /// <summary>转换 IP 地址格式。</summary>
  282. public static string GetPlainAddress(IPEndPoint endpoint)
  283. {
  284. try { return GetPlainAddress(endpoint.Address); }
  285. catch { return ""; }
  286. }
  287. /// <summary>转换 IP 地址格式。</summary>
  288. public static IPAddress GetIPAddress(string address)
  289. {
  290. try { return IPAddress.Parse(address); }
  291. catch { return new IPAddress(0); }
  292. }
  293. /// <summary>转换 IP 地址格式。</summary>
  294. public static IPEndPoint GetIPEndPoint(string address, int port)
  295. {
  296. try { return new IPEndPoint(IPAddress.Parse(address), NumberUtility.RestrictValue(port, 0, ushort.MaxValue)); }
  297. catch { return new IPEndPoint(0, 0); }
  298. }
  299. /// <summary>转换 IP 地址格式。</summary>
  300. public static IPEndPoint GetIPEndPoint(IPAddress address, int port)
  301. {
  302. try { return new IPEndPoint(address, NumberUtility.RestrictValue(port, 0, ushort.MaxValue)); }
  303. catch { return new IPEndPoint(0, 0); }
  304. }
  305. /// <summary>转换 IP 地址格式。</summary>
  306. public static IPEndPoint GetIPEndPoint(EndPoint endpoint)
  307. {
  308. try { return (IPEndPoint)endpoint; }
  309. catch { return new IPEndPoint(0, 0); }
  310. }
  311. /// <summary>判断私有 IP 地址。</summary>
  312. public static bool FromLAN(string ipv4)
  313. {
  314. if (ipv4.IsEmpty()) return false;
  315. // localhost
  316. if (ipv4 == "::1") return true;
  317. if (ipv4 == "127.0.0.1") return true;
  318. // IPv4
  319. var a = ipv4.Split('.');
  320. if (a.Length != 4) return false;
  321. switch (a[0])
  322. {
  323. case "10":
  324. return true;
  325. case "172":
  326. var a1 = TextUtility.GetInt32(a[1]);
  327. if (a1 >= 16 && a1 <= 31) return true;
  328. break;
  329. case "192":
  330. if (a[1] == "168") return true;
  331. break;
  332. }
  333. return false;
  334. }
  335. #endregion
  336. #region HTTP
  337. /// <summary>GET</summary>
  338. public static HttpClient HttpGet(string url, int timeout = 30000)
  339. {
  340. return HttpClient.SimpleGet(url, timeout);
  341. }
  342. /// <summary>POST</summary>
  343. public static HttpClient HttpPost(string url, byte[] data, int timeout = 30000, string type = "application/octet-stream")
  344. {
  345. return HttpClient.SimplePost(url, data, timeout, type);
  346. }
  347. /// <summary>POST text/plain</summary>
  348. public static HttpClient HttpPost(string url, string text, int timeout = 30000, string type = "text/plain")
  349. {
  350. return HttpClient.SimpleText(url, text, timeout, type);
  351. }
  352. /// <summary>POST application/x-www-form-urlencoded</summary>
  353. public static HttpClient HttpPost(string url, IDictionary<string, string> form, int timeout = 30000)
  354. {
  355. return HttpClient.SimpleForm(url, form, timeout);
  356. }
  357. /// <summary>POST application/x-www-form-urlencoded</summary>
  358. public static HttpClient HttpPost(string url, TextSet form, int timeout = 30000)
  359. {
  360. return HttpClient.SimpleForm(url, form, timeout);
  361. }
  362. #endregion
  363. #region Port
  364. private static List<int> ListActivePort(IPEndPoint[] endpoints)
  365. {
  366. var list = new List<int>(endpoints.Length);
  367. foreach (var endpoint in endpoints)
  368. {
  369. var port = endpoint.Port;
  370. if (list.Contains(port)) continue;
  371. list.Add(port);
  372. }
  373. list.Sort();
  374. list.Capacity = list.Count;
  375. return list;
  376. }
  377. /// <summary>列出活动的 TCP 端口。</summary>
  378. public static List<int> ListActiveTcpPort()
  379. {
  380. return ListActivePort(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners());
  381. }
  382. /// <summary>列出活动的 UDP 端口。</summary>
  383. public static List<int> ListActiveUdpPort()
  384. {
  385. return ListActivePort(IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners());
  386. }
  387. #endregion
  388. }
  389. }