using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Text; namespace Apewer.Network { /// 扩展方法。 public static class Extension { /// 添加邮件账户。 public static void Add(this List value, string address, string name) { if (value == null) return; value.Add(new MailAddress(address, name)); } /// 添加邮件账户。 public static void Add(this List value, string address) { if (value == null) return; value.Add(new MailAddress(address)); } /// 发送邮件。 public static MailRecord Send(this MailClient value, MailMessage message) { return MailMethods.Send(value, message); } /// 发送邮件。 public static MailRecord Send(this MailClient value, string sender, string receiver, string content = null, string title = null) { return MailMethods.Send(value, sender, receiver, content, title); } /// 生成 Json 数组。 static Json ToJsonArray(this IEnumerable items, Func serializer) { if (items == null) return null; var array = Apewer.Json.NewArray(); if (serializer != null) { foreach (var item in items) { if (item == null) continue; var json = serializer.Invoke(item); array.AddItem(json); } } return array; } /// 生成 Json 对象。 public static Json ToJson(this IPAddress address) { if (address == null) return null; var json = Json.NewObject(); json.SetProperty("text", address.ToString()); json.SetProperty("family", address.AddressFamily.ToString()); if (address.AddressFamily == AddressFamily.InterNetworkV6) { json.SetProperty("isLinkLocal", address.IsIPv6LinkLocal); json.SetProperty("scopeId", address.ScopeId); } return json; } /// 生成 Json 数组。 public static Json ToJson(this IEnumerable addresses) => ToJsonArray(addresses, ToJson); /// 生成 Json 数组。 public static Json ToJson(this NetworkInterface @interface) { if (@interface == null) return null; var item = @interface; var json = Json.NewObject(); json.SetProperty("text", item.ToString()); json.SetProperty("description", item.Description); json.SetProperty("id", item.Id); json.SetProperty("isReceiveOnly", item.IsReceiveOnly); json.SetProperty("name", item.Name); json.SetProperty("type", item.NetworkInterfaceType.ToString()); json.SetProperty("operational", item.OperationalStatus.ToString()); json.SetProperty("speed", item.Speed); json.SetProperty("multicast", item.SupportsMulticast); json.SetProperty("mac", item.GetPhysicalAddress().ToString()); json.SetProperty("ipProperties", ToJson(item.GetIPProperties())); return json; } /// 生成 Json 数组。 public static Json ToJson(this IEnumerable items) => ToJsonArray(items, ToJson); /// 生成 Json 对象。 public static Json ToJson(this IPInterfaceProperties properties) { if (properties == null) return null; var json = Json.NewObject(); json.SetProperty("anycast", ToJson(properties.AnycastAddresses.Select(x => x.Address))); json.SetProperty("dhcp", ToJson(properties.DhcpServerAddresses)); json.SetProperty("dns", ToJson(properties.DnsAddresses)); json.SetProperty("suffix", properties.DnsSuffix); json.SetProperty("gateway", ToJson(properties.GatewayAddresses.Select(x => x.Address))); json.SetProperty("multicast", ToJson(properties.MulticastAddresses.Select(x => x.Address))); json.SetProperty("unicast", ToJson(properties.UnicastAddresses.Select(x => x.Address))); json.SetProperty("wins", ToJson(properties.WinsServersAddresses)); return json; } /// 生成 Json 对象。 public static Json ToJson(this IPAddressInformation information) { if (information == null) return null; var json = Json.NewObject(); json.SetProperty("address", ToJson(information.Address)); json.SetProperty("isDnsEligible", information.IsDnsEligible); json.SetProperty("isTransient", information.IsTransient); return json; } /// 生成 Json 对象。 public static Json ToJson(this PingReply reply) { if (reply == null) return null; var buffer = BytesUtility.ToX2(reply.Buffer); if (buffer.Replace("0", "").IsEmpty()) buffer = null; var json = Json.NewObject(); json.SetProperty("address", ToJson(reply.Address)); if (buffer != null) json.SetProperty("buffer", buffer); json.SetProperty("options", ToJson(reply.Options)); json.SetProperty("roundtripTime", reply.RoundtripTime); json.SetProperty("status", reply.Status.ToString()); return json; } /// 生成 Json 对象。 public static Json ToJson(this PingOptions options) { if (options == null) return null; var json = Json.NewObject(); json.SetProperty("dontFragment", options.DontFragment); json.SetProperty("ttl", options.Ttl); return json; } } }