瀏覽代碼

修正有虚拟网卡时获取硬件信息的bug

懒得勤快 1 年之前
父節點
當前提交
0a67687181

+ 8 - 30
Masuit.Tools.Abstractions/Hardware/SystemInfo.cs

@@ -502,36 +502,14 @@ namespace Masuit.Tools.Hardware
         /// 获取网卡硬件地址
         /// </summary>
         /// <returns></returns>
-        public static IList<string> GetMacAddress()
+        public static IEnumerable<PhysicalAddress> GetMacAddress()
         {
-            //获取网卡硬件地址
-            try
-            {
-                if (!IsWinPlatform) return new List<string>();
-
-                return Cache.GetOrAdd(nameof(GetMacAddress), () =>
-                {
-                    IList<string> list = new List<string>();
-                    using var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
-                    using var moc = mc.GetInstances();
-                    foreach (var mo in moc)
-                    {
-                        using (mo)
-                        {
-                            if ((bool)mo["IPEnabled"])
-                            {
-                                list.Add(mo["MacAddress"].ToString());
-                            }
-                        }
-                    }
-
-                    return list;
-                });
-            }
-            catch (Exception)
-            {
-                return new List<string>();
-            }
+            return from adapter in NetworkInterface.GetAllNetworkInterfaces().Where(c => c.NetworkInterfaceType != NetworkInterfaceType.Loopback && c.OperationalStatus == OperationalStatus.Up)
+                   let properties = adapter.GetIPProperties()
+                   let unicastAddresses = properties.UnicastAddresses
+                   where unicastAddresses.Any(temp => temp.Address.AddressFamily == AddressFamily.InterNetwork)
+                   select adapter.GetPhysicalAddress() into address
+                   select address;
         }
 
         /// <summary>
@@ -877,4 +855,4 @@ namespace Masuit.Tools.Hardware
 
         #endregion Win32API声明
     }
-}
+}

+ 13 - 3
Masuit.Tools.Abstractions/HtmlSanitizer/HtmlSanitizer.cs

@@ -99,6 +99,11 @@ namespace Ganss.Xss
             AllowedAtRules = new HashSet<CssRuleType>(options.AllowedAtRules);
         }
 
+        /// <summary>
+        /// Gets or sets the default <see cref="Action{IComment}"/> method that encodes comments.
+        /// </summary>
+        public Action<IComment> EncodeComment { get; set; } = DefaultEncodeComment;
+
         /// <summary>
         /// Gets or sets the default <see cref="Action{IElement}"/> method that encodes literal text content.
         /// </summary>
@@ -458,9 +463,7 @@ namespace Ganss.Xss
         {
             foreach (var comment in GetAllNodes(context).OfType<IComment>().ToList())
             {
-                var escapedText = comment.TextContent.Replace("<", "&lt;").Replace(">", "&gt;");
-                if (escapedText != comment.TextContent)
-                    comment.TextContent = escapedText;
+                EncodeComment(comment);
 
                 var e = new RemovingCommentEventArgs(comment);
                 OnRemovingComment(e);
@@ -470,6 +473,13 @@ namespace Ganss.Xss
             }
         }
 
+        private static void DefaultEncodeComment(IComment comment)
+        {
+            var escapedText = comment.TextContent.Replace("<", "&lt;").Replace(">", "&gt;");
+            if (escapedText != comment.TextContent)
+                comment.TextContent = escapedText;
+        }
+
         private static void DefaultEncodeLiteralTextElementContent(IElement tag)
         {
             var escapedHtml = tag.InnerHtml.Replace("<", "&lt;").Replace(">", "&gt;");

+ 1 - 1
Masuit.Tools.Abstractions/Masuit.Tools.Abstractions.csproj

@@ -3,7 +3,7 @@
         <TargetFrameworks>netstandard2.0;netstandard2.1;net461;net5;net6;net7;net8</TargetFrameworks>
         <LangVersion>latest</LangVersion>
         <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
-        <Version>2.6.9</Version>
+        <Version>2.6.9.1</Version>
         <Authors>懒得勤快</Authors>
         <Description>全龄段友好的C#万能工具库,码数吐司库,不管你是菜鸟新手还是骨灰级玩家都能轻松上手,Masuit.Tools基础公共库(适用于.NET4.6.1/.NET Standard2.0及以上项目),包含一些常用的操作类,大都是静态类,加密解密,反射操作,Excel简单导出,权重随机筛选算法,分布式短id,表达式树,linq扩展,文件压缩,多线程下载和FTP客户端,硬件信息,字符串扩展方法,日期时间扩展操作,中国农历,大文件拷贝,图像裁剪,验证码,断点续传,集合扩展等常用封装。
             官网教程:https://tools.masuit.org

+ 19 - 3
Masuit.Tools.Abstractions/Systems/SnowFlake.cs

@@ -3,6 +3,9 @@ using Masuit.Tools.Strings;
 using System;
 using System.Linq;
 using System.Net.NetworkInformation;
+using Masuit.Tools.Hardware;
+using System.Collections.Generic;
+using System.Net.Sockets;
 
 namespace Masuit.Tools.Systems
 {
@@ -64,8 +67,21 @@ namespace Masuit.Tools.Systems
         /// </summary>
         static SnowFlake()
         {
-            var bytes = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault().GetPhysicalAddress().GetAddressBytes();
-            SetMachienId(bytes[4] << 2 | bytes[5]);
+            var mac = SystemInfo.GetMacAddress().FirstOrDefault(a => a.GetAddressBytes().Length > 0);
+            if (mac != null)
+            {
+                var bytes = mac.GetAddressBytes();
+                SetMachienId(bytes[4] << 2 | bytes[5]);
+            }
+            else
+            {
+                var ip = SystemInfo.GetLocalUsedIP();
+                if (ip != null)
+                {
+                    var bytes = ip.GetAddressBytes();
+                    SetMachienId(bytes[3]);
+                }
+            }
         }
 
         /// <summary>
@@ -170,4 +186,4 @@ namespace Masuit.Tools.Systems
             return id.Substring(index);
         }
     }
-}
+}

+ 17 - 3
Masuit.Tools.Abstractions/Systems/SnowFlakeNew.cs

@@ -2,6 +2,7 @@
 using System.Linq;
 using System.Net.NetworkInformation;
 using Masuit.Tools.DateTimeExt;
+using Masuit.Tools.Hardware;
 using Masuit.Tools.Strings;
 
 namespace Masuit.Tools.Systems;
@@ -52,8 +53,21 @@ public class SnowFlakeNew
     /// </summary>
     static SnowFlakeNew()
     {
-        var bytes = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault().GetPhysicalAddress().GetAddressBytes();
-        _workerId = bytes[4] << 2 | bytes[5];
+        var mac = SystemInfo.GetMacAddress().FirstOrDefault(a => a.GetAddressBytes().Length > 0);
+        if (mac != null)
+        {
+            var bytes = mac.GetAddressBytes();
+            _workerId = bytes[4] << 2 | bytes[5];
+        }
+        else
+        {
+            var ip = SystemInfo.GetLocalUsedIP();
+            if (ip != null)
+            {
+                var bytes = ip.GetAddressBytes();
+                _workerId = bytes[3];
+            }
+        }
     }
 
     /// <summary>
@@ -158,4 +172,4 @@ public class SnowFlakeNew
 
         return id.Substring(index);
     }
-}
+}

+ 2 - 2
Masuit.Tools.Abstractions/Win32/Windows.cs

@@ -237,7 +237,7 @@ public class WindowsServer
         CpuId = cpuInfo[0].DeviceID;
         CpuCount = cpuInfo.Count;
         CpuMhz = cpuInfo.Select(c => c.CurrentClockSpeed).ToArray();
-        MacAddress = SystemInfo.GetMacAddress()[0];
+        MacAddress = SystemInfo.GetMacAddress().FirstOrDefault(a => a.GetAddressBytes().Length > 0)?.ToString();
         DiskId = GetDiskID();
         DiskSize = GetSizeOfDisk();
         IpAddress = SystemInfo.GetLocalUsedIP().ToString();
@@ -370,4 +370,4 @@ public class WindowsServer
             return "unknow ";
         }
     }
-}
+}

+ 1 - 1
Masuit.Tools.AspNetCore/Masuit.Tools.AspNetCore.csproj

@@ -18,7 +18,7 @@
         <Product>Masuit.Tools.AspNetCore</Product>
         <PackageId>Masuit.Tools.AspNetCore</PackageId>
         <LangVersion>latest</LangVersion>
-        <Version>1.2.9</Version>
+        <Version>1.2.9.1</Version>
         <RepositoryType></RepositoryType>
         <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
         <FileVersion>1.1.9</FileVersion>

+ 1 - 1
Masuit.Tools.Core/Masuit.Tools.Core.csproj

@@ -6,7 +6,7 @@
 官网教程:https://tools.masuit.org
 github:https://github.com/ldqk/Masuit.Tools
         </Description>
-        <Version>2.6.9</Version>
+        <Version>2.6.9.1</Version>
         <Copyright>Copyright © 懒得勤快</Copyright>
         <PackageProjectUrl>https://github.com/ldqk/Masuit.Tools</PackageProjectUrl>
         <PackageTags>Masuit.Tools,工具库,Utility,Crypt,Extensions</PackageTags>

+ 1 - 1
Masuit.Tools.Excel/Masuit.Tools.Excel.csproj

@@ -3,7 +3,7 @@
         <TargetFramework>netstandard2.0</TargetFramework>
         <LangVersion>latest</LangVersion>
         <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
-        <Version>1.2.9</Version>
+        <Version>1.2.9.1</Version>
         <Authors>懒得勤快</Authors>
         <Description>Masuit.Tools.Excel导出库,支持一些简单数据的导出,支持图片列</Description>
         <Copyright>懒得勤快</Copyright>

+ 0 - 6
Masuit.Tools.Net45/Masuit.Tools.Net45.csproj

@@ -258,12 +258,6 @@
     <Compile Include="..\Masuit.Tools.Abstractions\Systems\SerializeIgnoreAttribute.cs">
       <Link>Systems\SerializeIgnoreAttribute.cs</Link>
     </Compile>
-    <Compile Include="..\Masuit.Tools.Abstractions\Systems\SnowFlake.cs">
-      <Link>Systems\SnowFlake.cs</Link>
-    </Compile>
-    <Compile Include="..\Masuit.Tools.Abstractions\Systems\SnowFlakeNew.cs">
-      <Link>Systems\SnowFlakeNew.cs</Link>
-    </Compile>
     <Compile Include="..\Masuit.Tools.Abstractions\Systems\StopwatchHelper.cs">
       <Link>Systems\StopwatchHelper.cs</Link>
     </Compile>

+ 15 - 15
Masuit.Tools.Net45/package.nuspec

@@ -1,18 +1,18 @@
 <?xml version="1.0"?>
 <package>
-  <metadata>
-    <id>Masuit.Tools.Net45</id>
-    <version>2.6.9</version>
-    <title>Masuit.Tools</title>
-    <authors>懒得勤快</authors>
-    <owners>masuit.com</owners>
-    <licenseUrl>https://github.com/ldqk/Masuit.Tools/blob/master/LICENSE</licenseUrl>
-    <projectUrl>https://github.com/ldqk/Masuit.Tools</projectUrl>
-    <!--<iconUrl></iconUrl>-->
-    <requireLicenseAcceptance>false</requireLicenseAcceptance>
-    <description>全龄段友好的C#万能工具库,码数吐司库(适用于.NET Core项目),Masuit.Tools的.NET Framework 4.5专用版本,相比4.6.1及.NET Core的版本,阉割了HTML、文件压缩、ASP.NET扩展、硬件监测、Session扩展等功能。</description>
-    <releaseNotes>如有问题请联系作者QQ:3444764617,或者到项目的github反馈问题,详细的API文档在github上:https://github.com/ldqk/Masuit.Tools</releaseNotes>
-    <copyright>Copyright ©  懒得勤快</copyright>
-    <tags>Masuit.Tools,工具库,Utility,Crypt,Extensions</tags>
-  </metadata>
+    <metadata>
+        <id>Masuit.Tools.Net45</id>
+        <version>2.6.9.1</version>
+        <title>Masuit.Tools</title>
+        <authors>懒得勤快</authors>
+        <owners>masuit.com</owners>
+        <licenseUrl>https://github.com/ldqk/Masuit.Tools/blob/master/LICENSE</licenseUrl>
+        <projectUrl>https://github.com/ldqk/Masuit.Tools</projectUrl>
+        <!--<iconUrl></iconUrl>-->
+        <requireLicenseAcceptance>false</requireLicenseAcceptance>
+        <description>全龄段友好的C#万能工具库,码数吐司库(适用于.NET Core项目),Masuit.Tools的.NET Framework 4.5专用版本,相比4.6.1及.NET Core的版本,阉割了HTML、文件压缩、ASP.NET扩展、硬件监测、Session扩展等功能。</description>
+        <releaseNotes>如有问题请联系作者QQ:3444764617,或者到项目的github反馈问题,详细的API文档在github上:https://github.com/ldqk/Masuit.Tools</releaseNotes>
+        <copyright>Copyright ©  懒得勤快</copyright>
+        <tags>Masuit.Tools,工具库,Utility,Crypt,Extensions</tags>
+    </metadata>
 </package>

+ 13 - 13
Masuit.Tools/package.nuspec

@@ -1,18 +1,18 @@
 <?xml version="1.0"?>
 <package>
-  <metadata>
-    <id>Masuit.Tools.Net</id>
-    <version>2.6.9</version>
-    <title>Masuit.Tools</title>
-    <authors>懒得勤快</authors>
-    <owners>masuit.com</owners>
-      <projectUrl>https://github.com/ldqk/Masuit.Tools</projectUrl>
-    <requireLicenseAcceptance>false</requireLicenseAcceptance>
-    <description>全龄段友好的C#万能工具库,码数吐司库(适用于.NET Framework 4.6.1+项目),不管你是菜鸟新手还是骨灰级玩家都能轻松上手,包含一些常用的操作类,大都是静态类,加密解密,反射操作,权重随机筛选算法,分布式短id,表达式树,linq扩展,文件压缩,多线程下载和FTP客户端,硬件信息,字符串扩展方法,日期时间扩展操作,中国农历,大文件拷贝,图像裁剪,验证码,断点续传,集合扩展等常用封装。
+    <metadata>
+        <id>Masuit.Tools.Net</id>
+        <version>2.6.9.1</version>
+        <title>Masuit.Tools</title>
+        <authors>懒得勤快</authors>
+        <owners>masuit.com</owners>
+        <projectUrl>https://github.com/ldqk/Masuit.Tools</projectUrl>
+        <requireLicenseAcceptance>false</requireLicenseAcceptance>
+        <description>全龄段友好的C#万能工具库,码数吐司库(适用于.NET Framework 4.6.1+项目),不管你是菜鸟新手还是骨灰级玩家都能轻松上手,包含一些常用的操作类,大都是静态类,加密解密,反射操作,权重随机筛选算法,分布式短id,表达式树,linq扩展,文件压缩,多线程下载和FTP客户端,硬件信息,字符串扩展方法,日期时间扩展操作,中国农历,大文件拷贝,图像裁剪,验证码,断点续传,集合扩展等常用封装。
 官网教程:https://tools.masuit.org
 github:https://github.com/ldqk/Masuit.Tools</description>
-    <releaseNotes>如有问题请联系作者QQ:3444764617,或者到项目的github反馈问题,详细的API文档在github上:https://github.com/ldqk/Masuit.Tools</releaseNotes>
-    <copyright>Copyright ©  懒得勤快</copyright>
-    <tags>Masuit.Tools,工具库,Utility,Crypt,Extensions</tags>
-  </metadata>
+        <releaseNotes>如有问题请联系作者QQ:3444764617,或者到项目的github反馈问题,详细的API文档在github上:https://github.com/ldqk/Masuit.Tools</releaseNotes>
+        <copyright>Copyright ©  懒得勤快</copyright>
+        <tags>Masuit.Tools,工具库,Utility,Crypt,Extensions</tags>
+    </metadata>
 </package>

+ 3 - 2
Test/Masuit.Tools.Abstractions.Test/Hardware/SystemInfo_UnitTest.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Linq;
 using System.Threading.Tasks;
 using Masuit.Tools.Hardware;
 using Xunit;
@@ -193,7 +194,7 @@ public class SystemInfo_UnitTest
     public void SystemInfo_GetMacAddress_IfNotWinPlatform()
     {
         var res = SystemInfo.GetMacAddress();
-        Assert.True(res.Count == 0);
+        Assert.True(!res.Any());
     }
 
     [Fact]
@@ -244,4 +245,4 @@ public class SystemInfo_UnitTest
         var res = SystemInfo.GetSystemType();
         Assert.True(!string.IsNullOrEmpty(res));
     }
-}
+}