using Apewer.Network; using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Runtime.InteropServices; using System.Text; namespace Apewer { /// public class NetworkUtility { #region UDP /// 唤醒局域网中拥有指定 MAC 地址的设备。 /// 被唤醒设备的 MAC 地址,必须是长度为 6 的字节数组。 public static void WakeOnLan(byte[] mac) { if (mac.Length != 6) return; var uc = new System.Net.Sockets.UdpClient(); uc.Connect(IPAddress.Broadcast, 65535); var pack = new List(); // 前 6 字节为 0xFF。 for (int i = 0; i < 6; i++) pack.Add(255); // 目标 MAC 地址重复 16 次。 for (int i = 0; i < 16; i++) { for (int j = 0; j < 6; j++) pack.Add(mac[j]); } // 发送 102 字节数据。 uc.Send(pack.ToArray(), pack.Count); uc.Close(); } /// 从 NTP 服务器获取 UTC 时间。 /// /// 通用 NTP 服务器:
/// Worldwide: pool.ntp.org
/// Asia: asia.pool.ntp.org
/// China: edu.ntp.org.cn
/// China: us.ntp.org.cn
/// Europe: europe.pool.ntp.org
/// North: America north-america.pool.ntp.org
/// Oceania: oceania.pool.ntp.org
/// South America: south-america.pool.ntp.org
/// Windows: time.windows.com
/// Windows: time.nist.gov
///
public static Class GetUtcFromNtp(string server = "pool.ntp.org", int port = 123, int timeout = 1000) { try { var ipa = null as IPAddress; var parseIP = IPAddress.TryParse(server, out ipa); if (!parseIP) { var addresses = Dns.GetHostEntry(server).AddressList; if (addresses.Length > 0) ipa = addresses[0]; } if (ipa == null) return null; var endpoint = new IPEndPoint(ipa, port); return GetUtcFromNtp(endpoint, timeout); } catch { } return null; } /// 从 NTP 服务器获取 UTC 时间。 public static Class GetUtcFromNtp(IPEndPoint endpoint, int timeout = 1000) { try { var request = new byte[48]; request[0] = 0x1B; var response = new byte[48]; response[0] = 0x1B; var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.Connect(endpoint); socket.SendTimeout = timeout; socket.ReceiveTimeout = timeout; socket.Send(request); socket.Receive(response); socket.Close(); const byte replytime = 40; ulong secondspart = BitConverter.ToUInt32(response, replytime); ulong secondsfraction = BitConverter.ToUInt32(response, replytime + 4); secondspart = SwapEndian(secondspart); secondsfraction = SwapEndian(secondsfraction); ulong milliseconds = (secondspart * 1000) + ((secondsfraction * 1000) / 0x100000000UL); var utc = (new DateTime(1900, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).AddMilliseconds(milliseconds); return new Class(utc); } catch { } return null; } private static uint SwapEndian(ulong x) { var a = ((x & 0x000000ff) << 24); var b = ((x & 0x0000ff00) << 8); var c = ((x & 0x00ff0000) >> 8); var d = ((x & 0xff000000) >> 24); return (uint)(a + b + c + d); } #endregion #region Puny Code /// public static string ToPunyCode(string chinese) { if (string.IsNullOrEmpty(chinese)) return ""; try { return new IdnMapping().GetAscii(chinese); } catch { return chinese; } } /// public static string FromPunyCode(string punycode) { if (string.IsNullOrEmpty(punycode)) return ""; try { return new IdnMapping().GetUnicode(punycode); } catch { return punycode; } } #endregion #region IP /// 获取本地计算机的计算机名。 public static string LocalHost { get => Dns.GetHostName() ?? ""; } /// 本地计算机的所有 IP 地址。 public static IPAddress[] LocalIP { get => Dns.GetHostEntry(Dns.GetHostName()).AddressList; } /// 判断 IPv4 地址格式是否正确。 public static bool IsIP(string ipv4) { try { if (string.IsNullOrEmpty(ipv4)) return false; var split = ipv4.Split('.'); if (split.Length != 4) return false; for (int i = 0; i < 4; i++) { var n = Convert.ToInt32(split[i]); if (n < 0 || n > 255) return false; if (n.ToString() != split[i]) return false; } return true; } catch { } return false; } /// 对目标地址进行解析。 public static string Resolve(string host) { try { if (IsIP(host)) { var ip = IPAddress.Parse(host); var he = Dns.GetHostEntry(ip); return he.HostName; } else { var ip = ""; var dn = host.ToLower(); var he = Dns.GetHostEntry(dn); var ts = ""; var on = he.Aliases; he = Dns.GetHostEntry(dn); for (int i = 0; i < on.Length; i++) ts = ts + on[i].ToString() + ","; ts = ""; var al = he.AddressList; for (int i = 0; i < al.Length; i++) ts = ts + al[i].ToString() + ","; ip = ts; if (ip.Length > 0) { if (ip.Substring(ip.Length - 1, 1) == ",") ip = ip.Substring(0, ip.Length - 1); } return ip; } } catch { } return ""; } /// 将由字符串表示的 IPv4 地址转换为 32 位整数。 public static int GetNumber(IPAddress ipv4) { try { var ba = ipv4.GetAddressBytes(); return BitConverter.ToInt32(ba, 0); } catch { return 0; } } /// 转换 IP 地址格式。 public static string GetPlainAddress(IPAddress address) { try { return address.ToString(); } catch { return ""; } } /// 转换 IP 地址格式。 public static string GetPlainAddress(IPEndPoint endpoint) { try { return GetPlainAddress(endpoint.Address); } catch { return ""; } } /// 转换 IP 地址格式。 public static IPAddress GetIPAddress(string address) { try { return IPAddress.Parse(address); } catch { return new IPAddress(0); } } /// 转换 IP 地址格式。 public static IPEndPoint GetIPEndPoint(string address, int port) { try { return new IPEndPoint(IPAddress.Parse(address), NumberUtility.Restrict(port, 0, ushort.MaxValue)); } catch { return new IPEndPoint(0, 0); } } /// 转换 IP 地址格式。 public static IPEndPoint GetIPEndPoint(IPAddress address, int port) { try { return new IPEndPoint(address, NumberUtility.Restrict(port, 0, ushort.MaxValue)); } catch { return new IPEndPoint(0, 0); } } /// 转换 IP 地址格式。 public static IPEndPoint GetIPEndPoint(System.Net.EndPoint endpoint) { try { return (IPEndPoint)endpoint; } catch { return new IPEndPoint(0, 0); } } /// 判断私有 IP 地址。 public static bool FromLAN(string ipv4) { if (ipv4.IsEmpty()) return false; // localhost if (ipv4 == "::1") return true; if (ipv4 == "127.0.0.1") return true; // IPv4 var a = ipv4.Split('.'); if (a.Length != 4) return false; switch (a[0]) { case "10": return true; case "172": var a1 = NumberUtility.Int32(a[1]); if (a1 >= 16 && a1 <= 31) return true; break; case "192": if (a[1] == "168") return true; break; } return false; } #endregion #region ICMP /// 发送 PING 命令,命令中包含 32 位零数据。 /// 目标地址。 /// 等待响应的超时时间(毫秒)。 /// 命令的起始 TTL 值(在丢弃数据之前可以转发该数据的路由节点数)。 /// 是否分段。 /// 命令的返回结果。 /// /// /// public static PingReply Ping(string address, int timeout = 1000, byte ttl = 255, bool df = true) { if (string.IsNullOrEmpty(address)) throw new ArgumentNullException(nameof(address)); if (timeout < 1) throw new ArgumentOutOfRangeException(nameof(timeout)); var ip = IsIP(address) ? address : Resolve(address); if (ip.Contains(",")) ip = ip.Split(',')[0]; var options = new PingOptions(); options.DontFragment = df; options.Ttl = ttl; var buffer = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; var ping = new Ping(); var reply = ping.Send(ip, timeout, buffer, options); return reply; } #endregion #region HTTP /// 解析 HTTP 方法。 public static HttpMethod ParseHttpMethod(string method) { if (!string.IsNullOrEmpty(method)) { var upper = TextUtility.Upper(method); if (upper.Contains("OPTIONS")) return HttpMethod.OPTIONS; else if (upper.Contains("POST")) return HttpMethod.POST; else if (upper.Contains("GET")) return HttpMethod.GET; else if (upper.Contains("CONNECT")) return HttpMethod.CONNECT; else if (upper.Contains("DELETE")) return HttpMethod.DELETE; else if (upper.Contains("HEAD")) return HttpMethod.HEAD; else if (upper.Contains("PATCH")) return HttpMethod.PATCH; else if (upper.Contains("PUT")) return HttpMethod.PUT; else if (upper.Contains("TRACE")) return HttpMethod.TRACE; } return HttpMethod.NULL; } /// GET public static HttpClient HttpGet(string url, int timeout = 30000) => HttpClient.Get(url, timeout); /// POST public static HttpClient HttpPost(string url, byte[] data, int timeout = 30000, string type = "application/octet-stream") => HttpClient.Post(url, data, timeout, type); /// POST text/plain public static HttpClient HttpPost(string url, string text, int timeout = 30000, string type = "text/plain") => HttpClient.Text(url, text, timeout, type); /// POST application/x-www-form-urlencoded public static HttpClient HttpPost(string url, IDictionary form, int timeout = 30000) => HttpClient.Form(url, form, timeout); /// POST application/x-www-form-urlencoded public static HttpClient HttpPost(string url, Dictionary form, int timeout = 30000) => HttpClient.Form(url, form, timeout); /// 获取 HTTP 状态的文本。 public static string HttpStatusDescription(int code) { switch (code) { case 100: return "Continue"; case 101: return "Switching Protocols"; case 102: return "Processing"; case 200: return "OK"; case 201: return "Created"; case 202: return "Accepted"; case 203: return "Non-Authoritative Information"; case 204: return "No Content"; case 205: return "Reset Content"; case 206: return "Partial Content"; case 207: return "Multi-Status"; case 300: return "Multiple Choices"; case 301: return "Moved Permanently"; case 302: return "Found"; case 303: return "See Other"; case 304: return "Not Modified"; case 305: return "Use Proxy"; case 307: return "Temporary Redirect"; case 400: return "Bad Request"; case 401: return "Unauthorized"; case 402: return "Payment Required"; case 403: return "Forbidden"; case 404: return "Not Found"; case 405: return "Method Not Allowed"; case 406: return "Not Acceptable"; case 407: return "Proxy Authentication Required"; case 408: return "Request Timeout"; case 409: return "Conflict"; case 410: return "Gone"; case 411: return "Length Required"; case 412: return "Precondition Failed"; case 413: return "Request Entity Too Large"; case 414: return "Request-Uri Too Long"; case 415: return "Unsupported Media Type"; case 416: return "Requested Range Not Satisfiable"; case 417: return "Expectation Failed"; case 422: return "Unprocessable Entity"; case 423: return "Locked"; case 424: return "Failed Dependency"; case 426: return "Upgrade Required"; // RFC 2817 case 500: return "Internal Server Error"; case 501: return "Not Implemented"; case 502: return "Bad Gateway"; case 503: return "Service Unavailable"; case 504: return "Gateway Timeout"; case 505: return "Http Version Not Supported"; case 507: return "Insufficient Storage"; default: return null; } } /// 按文件扩展名获取 Content-Type 值。 public static string Mime(string extension) { const string Default = "application/octet-stream"; if (string.IsNullOrEmpty(extension)) return Default; var split = extension.Split('.'); var lower = split.Length < 1 ? null : split[split.Length - 1].Lower(); switch (lower) { // text/plain; charset=utf-8 case "css": return "text/css"; case "htm": return "text/html"; case "html": return "text/html"; case "ini": return "text/ini"; case "js": return "application/javascript"; case "json": return "text/json"; case "shtml": return "text/html"; case "sh": return "text/plain"; case "txt": return "text/plain"; } switch (lower) { case "jad": return "text/vnd.sun.j2me.app-descriptor"; case "m3u8": return "text/vnd.apple.mpegurl"; // application/vnd.apple.mpegurl case "xml": return "text/xml"; case "htc": return "text/x-component"; case "mml": return "text/mathml"; case "wml": return "text/vnd.wap.wml"; } switch (lower) { case "3gp": return "video/3gpp"; case "3gpp": return "video/3gpp"; case "7z": return "application/x-7z-compressed"; case "ai": return "application/postscript"; case "asf": return "video/x-ms-asf"; case "asx": return "video/x-ms-asf"; case "atom": return "application/atom+xml"; case "avi": return "video/x-msvideo"; case "bmp": return "image/x-ms-bmp"; case "cco": return "application/x-cocoa"; case "crt": return "application/x-x509-ca-cert"; case "der": return "application/x-x509-ca-cert"; case "doc": return "application/msword"; case "docx": return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; case "ear": return "application/java-archive"; case "eot": return "application/vnd.ms-fontobject"; case "eps": return "application/postscript"; case "flv": return "video/x-flv"; case "gif": return "image/gif"; case "hqx": return "application/mac-binhex40"; case "ico": return "image/x-icon"; case "jar": return "application/java-archive"; case "jardiff": return "application/x-java-archive-diff"; case "jng": return "image/x-jng"; case "jnlp": return "application/x-java-jnlp-file"; case "jpeg": return "image/jpeg"; case "jpg": return "image/jpeg"; case "kar": return "audio/midi"; case "kml": return "application/vnd.google-earth.kml+xml"; case "kmz": return "application/vnd.google-earth.kmz"; case "m4a": return "audio/x-m4a"; case "m4v": return "video/x-m4v"; case "mid": return "audio/midi"; case "midi": return "audio/midi"; case "mkv": return "video/x-matroska"; case "mng": return "video/x-mng"; case "mov": return "video/quicktime"; case "mp3": return "audio/mpeg"; case "mp4": return "video/mp4"; case "mpeg": return "video/mpeg"; case "mpg": return "video/mpeg"; case "odg": return "application/vnd.oasis.opendocument.graphics"; case "odp": return "application/vnd.oasis.opendocument.presentation"; case "ods": return "application/vnd.oasis.opendocument.spreadsheet"; case "odt": return "application/vnd.oasis.opendocument.text"; case "ogg": return "audio/ogg"; case "pdb": return "application/x-pilot"; case "pdf": return "application/pdf"; case "pem": return "application/x-x509-ca-cert"; case "pl": return "application/x-perl"; case "pm": return "application/x-perl"; case "png": return "image/png"; case "ppt": return "application/vnd.ms-powerpoint"; case "pptx": return "application/vnd.openxmlformats-officedocument.presentationml.presentation"; case "prc": return "application/x-pilot"; case "ps": return "application/postscript"; case "ra": return "audio/x-realaudio"; case "rar": return "application/x-rar-compressed"; case "rpm": return "application/x-redhat-package-manager"; case "rss": return "application/rss+xml"; case "rtf": return "application/rtf"; case "run": return "application/x-makeself"; case "sea": return "application/x-sea"; case "sit": return "application/x-stuffit"; case "svg": return "image/svg+xml"; case "svgz": return "image/svg+xml"; case "swf": return "application/x-shockwave-flash"; case "tcl": return "application/x-tcl"; case "tif": return "image/tiff"; case "tiff": return "image/tiff"; case "tk": return "application/x-tcl"; case "ts": return "video/mp2t"; case "war": return "application/java-archive"; case "wbmp": return "image/vnd.wap.wbmp"; case "webm": return "video/webm"; case "webp": return "image/webp"; case "wmlc": return "application/vnd.wap.wmlc"; case "wmv": return "video/x-ms-wmv"; case "woff": return "font/woff"; case "woff2": return "font/woff2"; case "xhtml": return "application/xhtml+xml"; case "xls": return "application/vnd.ms-excel"; case "xlsx": return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; case "xpi": return "application/x-xpinstall"; case "xspf": return "application/xspf+xml"; case "zip": return "application/zip"; } return Default; } #endregion #region Port private static int[] ActivePorts(IPEndPoint[] endpoints) { var list = new List(endpoints.Length); foreach (var endpoint in endpoints) { var port = endpoint.Port; if (list.Contains(port)) continue; list.Add(port); } list.Sort(); list.Capacity = list.Count; return list.ToArray(); } /// 列出活动的 TCP 端口。 public static int[] ActiveTcpPorts() => ActivePorts(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners()); /// 列出活动的 UDP 端口。 public static int[] ActiveUdpPorts() => ActivePorts(IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners()); #endregion } }