精准匹配清除sing-tun残留的wintun设备驱动

This commit is contained in:
chao wan 2024-04-05 03:45:35 +08:00
parent abe3f64901
commit 639ffa6254
4 changed files with 42 additions and 3 deletions

View file

@ -22,6 +22,7 @@ using ZXing;
using ZXing.Common;
using ZXing.QrCode;
using ZXing.Windows.Compatibility;
using Vanara.PInvoke;
namespace v2rayN
{
@ -756,7 +757,7 @@ namespace v2rayN
public static void SetDarkBorder(System.Windows.Window window, bool dark)
{
// Make sure the handle is created before the window is shown
IntPtr hWnd = new System.Windows.Interop.WindowInteropHelper(window).EnsureHandle();
IntPtr hWnd = new WindowInteropHelper(window).EnsureHandle();
int attribute = dark ? 1 : 0;
uint attributeSize = (uint)Marshal.SizeOf(attribute);
DwmSetWindowAttribute(hWnd, DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1, ref attribute, attributeSize);
@ -1170,6 +1171,42 @@ namespace v2rayN
}
}
public static bool RemoveDeviceByDevIstId(string deviceInstanceId)
{
var isRemoved = false;
var devs = SetupAPI.SetupDiGetClassDevs(SetupAPI.GUID_DEVCLASS_NET, null, HWND.NULL, SetupAPI.DIGCF.DIGCF_PROFILE);
var instanceIdSize = deviceInstanceId.Length + 1;
var devInfo = new SetupAPI.SP_DEVINFO_DATA() { cbSize = (uint)Marshal.SizeOf<SetupAPI.SP_DEVINFO_DATA>() };
var instanceId = new StringBuilder(instanceIdSize);
uint index = 0;
while (SetupAPI.SetupDiEnumDeviceInfo(devs, index++, ref devInfo))
{
var success = SetupAPI.SetupDiGetDeviceInstanceId(devs, devInfo, instanceId, (uint)instanceIdSize, out uint size);
if (success && size == instanceIdSize && instanceId.ToString() == deviceInstanceId)
{
isRemoved = SetupAPI.SetupDiRemoveDevice(devs, ref devInfo);
}
}
SetupAPI.SetupDiDestroyDeviceInfoList(devs);
return isRemoved;
}
public static string GetSingboxTunDeviceInstanceId(string deviceName)
{
string deviceInstanceId = string.Empty;
string deviceId = "SWD\\WINTUN\\";
if (!string.IsNullOrEmpty(deviceName))
{
List<byte> data = [.. Encoding.UTF8.GetBytes("wintun"), .. Encoding.UTF8.GetBytes(deviceName)];
byte[] hash = MD5.HashData(data.ToArray());
deviceInstanceId = deviceId + new Guid(hash).ToString("B").ToUpper();
}
return deviceInstanceId;
}
#endregion
#region Windows API

View file

@ -43,6 +43,7 @@ namespace v2rayN
public const string DNSV2rayNormalFileName = "v2rayN.Sample.dns_v2ray_normal";
public const string DNSSingboxNormalFileName = "v2rayN.Sample.dns_singbox_normal";
public const string DefaultSingboxTunDeviceName = "tun_singbox";
public const string DefaultSecurity = "auto";
public const string DefaultNetwork = "tcp";
public const string TcpHeaderHttp = "http";

View file

@ -54,7 +54,7 @@ namespace v2rayN.Handler
{
ShowMsg(true, $"{node!.GetSummary()}");
CoreStop();
Utils.RemoveTunDevice();
Utils.RemoveDeviceByDevIstId(Utils.GetSingboxTunDeviceInstanceId(Global.DefaultSingboxTunDeviceName));
CoreStart(node);
}
executingResetEvents.ForEach(result => result.Set());

View file

@ -21,6 +21,7 @@
<PackageReference Include="QRCoder.Xaml" Version="1.4.3" />
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
<PackageReference Include="TaskScheduler" Version="2.10.1" />
<PackageReference Include="Vanara.PInvoke.SetupAPI" Version="4.0.0" />
<PackageReference Include="ZXing.Net.Bindings.Windows.Compatibility" Version="0.16.12" />
<PackageReference Include="ReactiveUI.Fody" Version="19.5.41" />
<PackageReference Include="ReactiveUI.Validation" Version="3.1.7" />