| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.NetworkInformation;
- using System.Net.Sockets;
- using NetTool.Client.Models;
- namespace NetTool.Client.Services;
- public sealed class NetworkAdapterService
- {
- public IReadOnlyList<AdapterInfo> GetAdapters()
- {
- return NetworkInterface
- .GetAllNetworkInterfaces()
- .Select(BuildAdapterInfo)
- .OrderByDescending(adapter => adapter.RecommendationScore)
- .ThenBy(adapter => adapter.Name)
- .ToList();
- }
- public AdapterInfo? GetRecommendedAdapter(IReadOnlyList<AdapterInfo> adapters)
- {
- return adapters
- .OrderByDescending(adapter => adapter.RecommendationScore)
- .ThenBy(adapter => adapter.Name)
- .FirstOrDefault();
- }
- private static AdapterInfo BuildAdapterInfo(NetworkInterface adapter)
- {
- var ipv4Address = adapter
- .GetIPProperties()
- .UnicastAddresses
- .FirstOrDefault(address => address.Address.AddressFamily == AddressFamily.InterNetwork)
- ?.Address
- .ToString() ?? string.Empty;
- var score = GetRecommendationScore(adapter, ipv4Address);
- var (label, reason) = GetRecommendation(score, adapter, ipv4Address);
- return AdapterInfo.FromNetworkInterface(adapter, ipv4Address, score, label, reason);
- }
- private static int GetRecommendationScore(NetworkInterface adapter, string ipv4Address)
- {
- var score = 0;
- var description = adapter.Description;
- var isVirtual = IsVirtualAdapter(description);
- if (adapter.NetworkInterfaceType is NetworkInterfaceType.Ethernet or NetworkInterfaceType.GigabitEthernet)
- {
- score += 50;
- }
- if (adapter.OperationalStatus == OperationalStatus.Up)
- {
- score += 40;
- }
- if (ipv4Address.StartsWith("169.254.", System.StringComparison.Ordinal))
- {
- score += 30;
- }
- if (!isVirtual)
- {
- score += 20;
- }
- if (description.Contains("Intel", System.StringComparison.OrdinalIgnoreCase)
- || description.Contains("Realtek", System.StringComparison.OrdinalIgnoreCase)
- || description.Contains("Broadcom", System.StringComparison.OrdinalIgnoreCase))
- {
- score += 10;
- }
- if (isVirtual)
- {
- score -= 40;
- }
- return score;
- }
- private static (string Label, string Reason) GetRecommendation(int score, NetworkInterface adapter, string ipv4Address)
- {
- if (score >= 90)
- {
- if (ipv4Address.StartsWith("169.254.", System.StringComparison.Ordinal))
- {
- return ("推荐", "已连接且当前 IPv4 为 169.254 网段,最像初始化直连网卡。");
- }
- return ("推荐", "已检测到已连接的网卡,适合作为初始化连接口。");
- }
- if (score >= 40)
- {
- return ("可选", "该网卡可用,但不是最优候选,请确认是否为实际直连接口。");
- }
- return ("不建议", "该网卡更像虚拟或非直连接口,通常不建议用于设备初始化。");
- }
- private static bool IsVirtualAdapter(string description)
- {
- return description.Contains("VMware", System.StringComparison.OrdinalIgnoreCase)
- || description.Contains("Virtual", System.StringComparison.OrdinalIgnoreCase)
- || description.Contains("Hyper-V", System.StringComparison.OrdinalIgnoreCase)
- || description.Contains("TAP", System.StringComparison.OrdinalIgnoreCase)
- || description.Contains("VPN", System.StringComparison.OrdinalIgnoreCase)
- || description.Contains("Loopback", System.StringComparison.OrdinalIgnoreCase);
- }
- }
|