mirror of
https://github.com/netchx/netch.git
synced 2026-05-11 23:45:06 +08:00
@@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using Netch.Interfaces;
|
||||
using Netch.Utils;
|
||||
using Vanara.PInvoke;
|
||||
|
||||
namespace Netch.Models.Adapter
|
||||
@@ -17,9 +17,7 @@ namespace Netch.Models.Adapter
|
||||
throw new MessageException("GetBestRoute 搜索失败");
|
||||
}
|
||||
|
||||
NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
|
||||
.First(ni => ni.Supports(NetworkInterfaceComponent.IPv4) &&
|
||||
ni.GetIPProperties().GetIPv4Properties().Index == pRoute.dwForwardIfIndex);
|
||||
NetworkInterface = NetworkInterfaceUtils.Get((int)pRoute.dwForwardIfIndex);
|
||||
|
||||
Address = new IPAddress(pRoute.dwForwardNextHop.S_addr);
|
||||
InterfaceIndex = (ulong)pRoute.dwForwardIfIndex;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using Netch.Interfaces;
|
||||
using Netch.Interops;
|
||||
using Netch.Utils;
|
||||
|
||||
namespace Netch.Models.Adapter
|
||||
{
|
||||
@@ -10,8 +10,9 @@ namespace Netch.Models.Adapter
|
||||
{
|
||||
public TunAdapter()
|
||||
{
|
||||
|
||||
InterfaceIndex = RouteHelper.ConvertLuidToIndex(tun2socks.tun_luid());
|
||||
NetworkInterface = NetworkInterface.GetAllNetworkInterfaces().First(i => i.GetIPProperties().GetIPv4Properties().Index == (int)InterfaceIndex);
|
||||
NetworkInterface = NetworkInterfaceUtils.Get((int)InterfaceIndex);
|
||||
Gateway = IPAddress.Parse(Global.Settings.TUNTAP.Gateway);
|
||||
|
||||
Global.Logger.Info($"WinTUN 适配器:{NetworkInterface.Name} {NetworkInterface.Id} {NetworkInterface.Description}, index: {InterfaceIndex}");
|
||||
|
||||
@@ -5,6 +5,32 @@ using System.Net.NetworkInformation;
|
||||
|
||||
namespace Netch.Utils
|
||||
{
|
||||
public static class NetworkInterfaceUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
/// <param name="interfaceIndex"></param>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
/// <returns></returns>
|
||||
public static NetworkInterface Get(int interfaceIndex)
|
||||
{
|
||||
return NetworkInterface.GetAllNetworkInterfaces()
|
||||
.First(n =>
|
||||
{
|
||||
var ipProperties = n.GetIPProperties();
|
||||
int index;
|
||||
if (n.Supports(NetworkInterfaceComponent.IPv4))
|
||||
index = ipProperties.GetIPv4Properties().Index;
|
||||
else if (n.Supports(NetworkInterfaceComponent.IPv6))
|
||||
index = ipProperties.GetIPv6Properties().Index;
|
||||
else
|
||||
return false;
|
||||
|
||||
return index == interfaceIndex;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static class NetworkInterfaceExtension
|
||||
{
|
||||
public static void SetDns(this NetworkInterface ni, string primaryDns, string? secondDns = null)
|
||||
@@ -25,7 +51,7 @@ namespace Netch.Utils
|
||||
|
||||
var mo = mos.First(m => m["Description"].ToString() == ni.Description);
|
||||
|
||||
var dns = new[] {primaryDns};
|
||||
var dns = new[] { primaryDns };
|
||||
if (secondDns != null)
|
||||
dns = dns.Append(secondDns).ToArray();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user