Skip to content

Commit

Permalink
处理遍历适配器时的异常
Browse files Browse the repository at this point in the history
  • Loading branch information
Henning742 committed Mar 9, 2020
1 parent b76a22d commit 8ff8582
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions Netch/Utils/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,39 +109,45 @@ public static bool SearchOutbounds()
var AddressGot = false;
foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
{
var adapterProperties = adapter.GetIPProperties();
var p = adapterProperties.GetIPv4Properties();
Logging.Info($"检测适配器:{adapter.Name} {adapter.Id} {adapter.Description}, index: {p.Index}");

// 通过索引查找对应适配器的 IPv4 地址
if (p.Index == Global.Adapter.Index)
try
{
var AdapterIPs = "";
var adapterProperties = adapter.GetIPProperties();
var p = adapterProperties.GetIPv4Properties();
Logging.Info($"检测适配器:{adapter.Name} {adapter.Id} {adapter.Description}, index: {p.Index}");

foreach (var ip in adapterProperties.UnicastAddresses)
// 通过索引查找对应适配器的 IPv4 地址
if (p.Index == Global.Adapter.Index)
{
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
var AdapterIPs = "";

foreach (var ip in adapterProperties.UnicastAddresses)
{
AddressGot = true;
Global.Adapter.Address = ip.Address;
Logging.Info($"当前出口 IPv4 地址:{Global.Adapter.Address}");
break;
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
{
AddressGot = true;
Global.Adapter.Address = ip.Address;
Logging.Info($"当前出口 IPv4 地址:{Global.Adapter.Address}");
break;
}
AdapterIPs = $"{ip.Address} | ";
}
AdapterIPs = $"{ip.Address} | ";
}

if (!AddressGot)
{
if (AdapterIPs.Length > 3)
if (!AddressGot)
{
AdapterIPs = AdapterIPs.Substring(0, AdapterIPs.Length - 3);
Logging.Info($"所有出口地址:{AdapterIPs}");
if (AdapterIPs.Length > 3)
{
AdapterIPs = AdapterIPs.Substring(0, AdapterIPs.Length - 3);
Logging.Info($"所有出口地址:{AdapterIPs}");
}
Logging.Info("出口无 IPv4 地址,当前只支持 IPv4 地址");
return false;
}
Logging.Info("出口无 IPv4 地址,当前只支持 IPv4 地址");
return false;
break;
}
break;

}
catch (Exception)
{ }
}

if (!AddressGot)
Expand Down

0 comments on commit 8ff8582

Please sign in to comment.