Skip to content

Commit

Permalink
Try to remove the tun device when restarting the service in Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Jan 30, 2025
1 parent c0f8b6b commit 6c5011a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
28 changes: 25 additions & 3 deletions v2rayN/ServiceLib/Common/WindowsUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Microsoft.Win32;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Win32;

namespace ServiceLib.Common
{
Expand Down Expand Up @@ -50,5 +53,24 @@ public static void RegWriteValue(string path, string name, object value)
regKey?.Close();

Check warning on line 53 in v2rayN/ServiceLib/Common/WindowsUtils.cs

View workflow job for this annotation

GitHub Actions / build (Release)

This call site is reachable on all platforms. 'RegistryKey.Close()' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 53 in v2rayN/ServiceLib/Common/WindowsUtils.cs

View workflow job for this annotation

GitHub Actions / build (Release)

This call site is reachable on all platforms. 'RegistryKey.Close()' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 53 in v2rayN/ServiceLib/Common/WindowsUtils.cs

View workflow job for this annotation

GitHub Actions / build (Release)

This call site is reachable on all platforms. 'RegistryKey.Close()' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 53 in v2rayN/ServiceLib/Common/WindowsUtils.cs

View workflow job for this annotation

GitHub Actions / build (Release)

This call site is reachable on all platforms. 'RegistryKey.Close()' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
}
}
}
}

public static async Task RemoveTunDevice()
{
try
{
var sum = MD5.HashData(Encoding.UTF8.GetBytes("wintunsingbox_tun"));
var guid = new Guid(sum);
var pnpUtilPath = @"C:\Windows\System32\pnputil.exe";
var arg = $$""" /remove-device "SWD\Wintun\{{{guid}}}" """;

// Try to remove the device
await Utils.GetCliWrapOutput(pnpUtilPath, arg);

}
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
}
}
}
}
13 changes: 10 additions & 3 deletions v2rayN/ServiceLib/Handler/CoreHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics;
using System.Diagnostics;
using System.Text;

namespace ServiceLib.Handler
Expand Down Expand Up @@ -83,7 +83,14 @@ public async Task LoadCore(ProfileItem? node)
UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
await CoreStop();
await Task.Delay(100);
await CoreStart(node);

if (Utils.IsWindows() && _config.TunModeItem.EnableTun)
{
await Task.Delay(100);
await WindowsUtils.RemoveTunDevice();
}

await CoreStart(node);
await CoreStartPreService(node);
if (_process != null)
{
Expand Down Expand Up @@ -368,4 +375,4 @@ private async Task<string> CreateLinuxShellFile(string cmdLine, string fileName)

#endregion Linux
}
}
}
35 changes: 3 additions & 32 deletions v2rayN/v2rayN/Common/WindowsUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Win32;
using Microsoft.Win32;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -64,36 +64,7 @@ public static ImageSource IconToImageSource(Icon icon)
BitmapSizeOptions.FromEmptyOptions());
}

public static void RemoveTunDevice()
{
try
{
var sum = MD5.HashData(Encoding.UTF8.GetBytes("wintunsingbox_tun"));
var guid = new Guid(sum);
string pnputilPath = @"C:\Windows\System32\pnputil.exe";
string arg = $$""" /remove-device "SWD\Wintun\{{{guid}}}" """;

// Try to remove the device
Process proc = new()
{
StartInfo = new()
{
FileName = pnputilPath,
Arguments = arg,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
};

proc.Start();
var output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
}
catch
{
}
}


public static void SetDarkBorder(Window window, string? theme)
{
Expand Down Expand Up @@ -136,4 +107,4 @@ public enum DWMWINDOWATTRIBUTE : uint

#endregion Windows API
}
}
}

0 comments on commit 6c5011a

Please sign in to comment.