Skip to content

Commit

Permalink
获取网卡相关数据时,剔除本地环回和隧道网卡
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Apr 24, 2024
1 parent b4d9bc0 commit 0354a2e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
1 change: 1 addition & 0 deletions NewLife.Core/Common/MachineInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ public void RefreshSpeed()
var received = 0L;
try
{
// 包含本地环回和隧道网卡
// WSL获取网络列表时可能报错
foreach (var ni in NetworkInterface.GetAllNetworkInterfaces())
{
Expand Down
14 changes: 5 additions & 9 deletions NewLife.Core/Net/NetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public static IEnumerable<IPInterfaceProperties> GetActiveInterfaces()
foreach (var item in NetworkInterface.GetAllNetworkInterfaces())
{
if (item.OperationalStatus != OperationalStatus.Up) continue;
if (item.NetworkInterfaceType == NetworkInterfaceType.Loopback) continue;
if (item.NetworkInterfaceType is NetworkInterfaceType.Loopback or NetworkInterfaceType.Tunnel or NetworkInterfaceType.Unknown) continue;

var ip = item.GetIPProperties();
if (ip != null) yield return ip;
Expand Down Expand Up @@ -364,10 +364,8 @@ public static IEnumerable<IPAddress> GetIPs()
var dic = new Dictionary<UnicastIPAddressInformation, Int32>();
foreach (var item in NetworkInterface.GetAllNetworkInterfaces())
{
if (item.OperationalStatus != OperationalStatus.Up)
continue;
if (item.NetworkInterfaceType == NetworkInterfaceType.Loopback)
continue;
if (item.OperationalStatus != OperationalStatus.Up) continue;
if (item.NetworkInterfaceType is NetworkInterfaceType.Loopback or NetworkInterfaceType.Tunnel or NetworkInterfaceType.Unknown) continue;

var ipp = item.GetIPProperties();
if (ipp != null && ipp.UnicastAddresses.Count > 0)
Expand Down Expand Up @@ -465,16 +463,14 @@ public static IEnumerable<IPAddress> GetMulticasts()
}

private static readonly String[] _Excludes = ["Loopback", "VMware", "VBox", "Virtual", "Teredo", "Microsoft", "VPN", "VNIC", "IEEE"];
/// <summary>获取所有物理网卡MAC地址</summary>
/// <summary>获取所有物理网卡MAC地址。包括未启用网卡,剔除本地和隧道</summary>
/// <returns></returns>
public static IEnumerable<Byte[]> GetMacs()
{
foreach (var item in NetworkInterface.GetAllNetworkInterfaces())
{
// 只要物理网卡
if (item.NetworkInterfaceType is NetworkInterfaceType.Loopback or
NetworkInterfaceType.Tunnel or
NetworkInterfaceType.Unknown) continue;
if (item.NetworkInterfaceType is NetworkInterfaceType.Loopback or NetworkInterfaceType.Tunnel or NetworkInterfaceType.Unknown) continue;
if (_Excludes.Any(e => item.Description.Contains(e))) continue;
if (Runtime.Windows && item.Speed < 1_000_000) continue;

Expand Down

0 comments on commit 0354a2e

Please sign in to comment.