Remove AdapterUtil

This commit is contained in:
ChsBuffer
2021-03-28 03:29:24 +08:00
parent 8d6e4bdd96
commit 6c668684e9
4 changed files with 7 additions and 55 deletions

View File

@@ -62,7 +62,7 @@ namespace Netch.Interops
private static extern bool tun_free();
[DllImport(tun2socks_bin, CallingConvention = CallingConvention.Cdecl)]
private static extern ulong tun_luid();
public static extern ulong tun_luid();
[DllImport(tun2socks_bin, CallingConvention = CallingConvention.Cdecl)]
private static extern ulong tun_getUP();

View File

@@ -10,19 +10,16 @@ namespace Netch.Models
{
public class TunAdapter : IAdapter
{
private const string ComponentIdWintun = "wintun";
public TunAdapter()
{
AdapterId = AdapterUtils.GetAdapterId(ComponentIdWintun) ?? throw new Exception("wintun adapter not found");
NetworkInterface = NetworkInterface.GetAllNetworkInterfaces().First(i => i.Id == AdapterId);
InterfaceIndex = NetworkInterface.GetIPProperties().GetIPv4Properties().Index;
InterfaceIndex = (int) NativeMethods.ConvertLuidToIndex(TUNInterop.tun_luid());
NetworkInterface = NetworkInterface.GetAllNetworkInterfaces().First(i => i.GetIPProperties().GetIPv4Properties().Index == InterfaceIndex);
Gateway = IPAddress.Parse(Global.Settings.TUNTAP.Gateway);
Logging.Info($"WinTUN 适配器:{NetworkInterface.Name} {NetworkInterface.Id} {NetworkInterface.Description}, index: {InterfaceIndex}");
}
public string AdapterId { get; set; }
public string AdapterId => throw new NotImplementedException();
public int InterfaceIndex { get; }

View File

@@ -41,6 +41,9 @@ namespace Netch
[DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl, EntryPoint = "DeleteRoute")]
public static extern bool DeleteRoute(int inet, string address, int cidr, string gateway, int index, int metric = 0);
[DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)]
public static extern ulong ConvertLuidToIndex(ulong luid);
[DllImport("dnsapi", EntryPoint = "DnsFlushResolverCache")]
public static extern uint FlushDNSResolverCache();
}

View File

@@ -1,48 +0,0 @@
using System;
using System.Linq;
using Microsoft.Win32;
namespace Netch.Utils
{
public static class AdapterUtils
{
public const string NETWORK_KEY = @"SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}";
public const string ADAPTER_KEY = @"SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}";
/// <summary>
/// 获取 TUN/TAP 适配器名称
/// </summary>
/// <param name="componentId">适配器 ID</param>
/// <returns>适配器名称</returns>
public static string GetName(string componentId)
{
return Registry.LocalMachine.OpenSubKey($"{NETWORK_KEY}\\{componentId}\\Connection")?.GetValue("Name")?.ToString() ?? "";
}
public static string? GetAdapterId(params string[] componentIds)
{
try
{
var adaptersRegistry = Registry.LocalMachine.OpenSubKey(ADAPTER_KEY)!;
foreach (var keyName in adaptersRegistry.GetSubKeyNames().Where(s => s is not ("Configuration" or "Properties")))
{
var adapterRegistry = adaptersRegistry.OpenSubKey(keyName)!;
var componentId = adapterRegistry.GetValue("ComponentId")?.ToString();
if (componentId == null)
continue;
if (componentIds.Contains(componentId))
return (string) (adapterRegistry.GetValue("NetCfgInstanceId") ??
throw new Exception("Tap adapter have no NetCfgInstanceId key"));
}
}
catch (Exception e)
{
Logging.Warning(e.ToString());
}
return null;
}
}
}