diff --git a/Netch/Interops/TUNInterop.cs b/Netch/Interops/TUNInterop.cs
index 376f1658..67c1e263 100644
--- a/Netch/Interops/TUNInterop.cs
+++ b/Netch/Interops/TUNInterop.cs
@@ -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();
diff --git a/Netch/Models/TunAdapter.cs b/Netch/Models/TunAdapter.cs
index 1ae12158..d3007536 100644
--- a/Netch/Models/TunAdapter.cs
+++ b/Netch/Models/TunAdapter.cs
@@ -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; }
diff --git a/Netch/NativeMethods.cs b/Netch/NativeMethods.cs
index a437fa2d..9270db19 100644
--- a/Netch/NativeMethods.cs
+++ b/Netch/NativeMethods.cs
@@ -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();
}
diff --git a/Netch/Utils/AdapterUtils.cs b/Netch/Utils/AdapterUtils.cs
deleted file mode 100644
index 6fafcb90..00000000
--- a/Netch/Utils/AdapterUtils.cs
+++ /dev/null
@@ -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}";
-
- ///
- /// 获取 TUN/TAP 适配器名称
- ///
- /// 适配器 ID
- /// 适配器名称
- 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;
- }
- }
-}
\ No newline at end of file