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.

161 lines
6.1 KiB

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