using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Reflection; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Text; namespace Infrastructure.Extensions { public static class HelperExtensions { public static string GetVersion(this Helper helper) { return Assembly.GetEntryAssembly().GetCustomAttribute().InformationalVersion; } public static string GetAssemblyVersion(this Helper helper) { return Assembly.GetEntryAssembly().GetName().Version?.ToString(); } public static string GetMacAddress(this Helper helper) { return NetworkInterface .GetAllNetworkInterfaces() .Where(nic => (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet || nic.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)) .Where(nic => !nic.Description.Contains("Adapter", StringComparison.OrdinalIgnoreCase)) .Where(nic => !nic.Description.Contains("Bluetooth", StringComparison.OrdinalIgnoreCase)) .OrderBy(o => o.NetworkInterfaceType) .Select(o => o.GetPhysicalAddress().ToString().ToLower(CultureInfo.InvariantCulture)) .FirstOrDefault(); } public static string MacEncrypt(this Helper helper, string value) { using var des = new DESCryptoServiceProvider(); var inputByteArray = Encoding.Default.GetBytes(value); using MD5 md5 = MD5.Create(); byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(value)); var md5Key = BitConverter.ToString(hash).Replace("-", string.Empty, StringComparison.Ordinal).ToLower(CultureInfo.InvariantCulture); des.Key = Encoding.ASCII.GetBytes(md5Key.Substring(0, 8)); des.IV = Encoding.ASCII.GetBytes(md5Key.Substring(0, 8)); using var ms = new MemoryStream(); using var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); var ret = new StringBuilder(); foreach (var item in ms.ToArray()) { ret.AppendFormat("{0:X2}", item); } return ret.ToString(); } public static List GetLocalIPList(this Helper helper) { var ipList = new List(); foreach (var ni in NetworkInterface.GetAllNetworkInterfaces()) { foreach (var ua in ni.GetIPProperties().UnicastAddresses) { if (ua.Address.AddressFamily == AddressFamily.InterNetwork) { ipList.Add(ua.Address); } } } return ipList; } public static IPAddress GetLocalIP(this Helper helper) { var ipList = new List(); foreach (var ni in NetworkInterface.GetAllNetworkInterfaces()) { foreach (var ua in ni.GetIPProperties().UnicastAddresses) { if (ua.Address.AddressFamily == AddressFamily.InterNetwork) { ipList.Add(ua.Address); } } } var ip = ipList.Where(o => !o.ToString().StartsWith("10.", StringComparison.Ordinal) && o.ToString().StartsWith("172.", StringComparison.Ordinal) && o.ToString().StartsWith("192.", StringComparison.Ordinal) && o.ToString().StartsWith("127.", StringComparison.Ordinal)).FirstOrDefault() ?? ipList.FirstOrDefault(o => !o.ToString().StartsWith("192.168.", StringComparison.Ordinal)) ?? ipList.FirstOrDefault(o => !o.ToString().StartsWith("192.", StringComparison.Ordinal)) ?? ipList.FirstOrDefault(); return ip; } public static string GetRunTime(this Helper helper) { var os = ""; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { os += "windows"; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { os += "linux"; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { os += "osx"; } os += $"-{RuntimeInformation.OSArchitecture.ToString().ToLower(CultureInfo.InvariantCulture)}"; return os; } } }