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.

184 lines
7.1 KiB

3 years ago
3 years ago
1 year ago
3 years ago
  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 Apewer.Network
  8. {
  9. /// <summary>扩展方法。</summary>
  10. public static class Extension
  11. {
  12. /// <summary>添加邮件账户。</summary>
  13. public static void Add(this List<MailAddress> value, string address, string name)
  14. {
  15. if (value == null) return;
  16. value.Add(new MailAddress(address, name));
  17. }
  18. /// <summary>添加邮件账户。</summary>
  19. public static void Add(this List<MailAddress> value, string address)
  20. {
  21. if (value == null) return;
  22. value.Add(new MailAddress(address));
  23. }
  24. /// <summary>发送邮件。</summary>
  25. public static MailRecord Send(this MailClient value, MailMessage message)
  26. {
  27. return MailMethods.Send(value, message);
  28. }
  29. /// <summary>发送邮件。</summary>
  30. public static MailRecord Send(this MailClient value, string sender, string receiver, string content = null, string title = null)
  31. {
  32. return MailMethods.Send(value, sender, receiver, content, title);
  33. }
  34. /// <summary>生成 Json 数组。</summary>
  35. static Json ToJsonArray<T>(this IEnumerable<T> items, Func<T, Json> serializer)
  36. {
  37. if (items == null) return null;
  38. var array = Apewer.Json.NewArray();
  39. if (serializer != null)
  40. {
  41. foreach (var item in items)
  42. {
  43. if (item == null) continue;
  44. var json = serializer.Invoke(item);
  45. array.AddItem(json);
  46. }
  47. }
  48. return array;
  49. }
  50. /// <summary>生成 Json 对象。</summary>
  51. public static Json ToJson(this IPAddress address)
  52. {
  53. if (address == null) return null;
  54. var json = Json.NewObject();
  55. json.SetProperty("text", address.ToString());
  56. json.SetProperty("family", address.AddressFamily.ToString());
  57. if (address.AddressFamily == AddressFamily.InterNetworkV6)
  58. {
  59. json.SetProperty("isLinkLocal", address.IsIPv6LinkLocal);
  60. json.SetProperty("scopeId", address.ScopeId);
  61. }
  62. return json;
  63. }
  64. /// <summary>生成 Json 数组。</summary>
  65. public static Json ToJson(this IEnumerable<IPAddress> addresses) => ToJsonArray(addresses, ToJson);
  66. /// <summary>生成 Json 数组。</summary>
  67. public static Json ToJson(this NetworkInterface @interface)
  68. {
  69. if (@interface == null) return null;
  70. var item = @interface;
  71. var json = Json.NewObject();
  72. json.SetProperty("text", item.ToString());
  73. json.SetProperty("description", item.Description);
  74. json.SetProperty("id", item.Id);
  75. json.SetProperty("isReceiveOnly", item.IsReceiveOnly);
  76. json.SetProperty("name", item.Name);
  77. json.SetProperty("type", item.NetworkInterfaceType.ToString());
  78. json.SetProperty("operational", item.OperationalStatus.ToString());
  79. json.SetProperty("speed", item.Speed);
  80. json.SetProperty("multicast", item.SupportsMulticast);
  81. json.SetProperty("mac", item.GetPhysicalAddress().ToString());
  82. json.SetProperty("ipProperties", ToJson(item.GetIPProperties()));
  83. return json;
  84. }
  85. /// <summary>生成 Json 数组。</summary>
  86. public static Json ToJson(this IEnumerable<NetworkInterface> items) => ToJsonArray(items, ToJson);
  87. /// <summary>生成 Json 对象。</summary>
  88. public static Json ToJson(this IPInterfaceProperties properties)
  89. {
  90. if (properties == null) return null;
  91. var json = Json.NewObject();
  92. json.SetProperty("anycast", ToJson(properties.AnycastAddresses.Select(x => x.Address)));
  93. json.SetProperty("dhcp", ToJson(properties.DhcpServerAddresses));
  94. json.SetProperty("dns", ToJson(properties.DnsAddresses));
  95. json.SetProperty("suffix", properties.DnsSuffix);
  96. json.SetProperty("gateway", ToJson(properties.GatewayAddresses.Select(x => x.Address)));
  97. json.SetProperty("multicast", ToJson(properties.MulticastAddresses.Select(x => x.Address)));
  98. json.SetProperty("unicast", ToJson(properties.UnicastAddresses.Select(x => x.Address)));
  99. json.SetProperty("wins", ToJson(properties.WinsServersAddresses));
  100. return json;
  101. }
  102. /// <summary>生成 Json 对象。</summary>
  103. public static Json ToJson(this IPAddressInformation information)
  104. {
  105. if (information == null) return null;
  106. var json = Json.NewObject();
  107. json.SetProperty("address", ToJson(information.Address));
  108. json.SetProperty("isDnsEligible", information.IsDnsEligible);
  109. json.SetProperty("isTransient", information.IsTransient);
  110. return json;
  111. }
  112. /// <summary>生成 Json 对象。</summary>
  113. public static Json ToJson(this PingReply reply)
  114. {
  115. if (reply == null) return null;
  116. var buffer = BytesUtility.ToX2(reply.Buffer);
  117. if (buffer.Replace("0", "").IsEmpty()) buffer = null;
  118. var json = Json.NewObject();
  119. json.SetProperty("address", ToJson(reply.Address));
  120. if (buffer != null) json.SetProperty("buffer", buffer);
  121. json.SetProperty("options", ToJson(reply.Options));
  122. json.SetProperty("roundtripTime", reply.RoundtripTime);
  123. json.SetProperty("status", reply.Status.ToString());
  124. return json;
  125. }
  126. /// <summary>生成 Json 对象。</summary>
  127. public static Json ToJson(this PingOptions options)
  128. {
  129. if (options == null) return null;
  130. var json = Json.NewObject();
  131. json.SetProperty("dontFragment", options.DontFragment);
  132. json.SetProperty("ttl", options.Ttl);
  133. return json;
  134. }
  135. /// <summary>从套接字接收数据。</summary>
  136. /// <exception cref="ArgumentNullException"></exception>
  137. /// <exception cref="ArgumentOutOfRangeException"></exception>
  138. /// <exception cref="SocketException"></exception>
  139. /// <exception cref="ObjectDisposedException"></exception>
  140. /// <exception cref="System.Security.SecurityException"></exception>
  141. public static byte[] Receive(this Socket socket, int maxLength = 1024)
  142. {
  143. if (socket == null) throw new ArgumentNullException(nameof(socket));
  144. var buffer = new byte[maxLength];
  145. var received = socket.Receive(buffer, 0, maxLength, SocketFlags.None);
  146. if (received < maxLength)
  147. {
  148. var newBuffer = new byte[received];
  149. Array.Copy(buffer, newBuffer, received);
  150. return newBuffer;
  151. }
  152. else
  153. {
  154. return buffer;
  155. }
  156. }
  157. }
  158. }