From 9b579be2be05501a031d8a3b940f790b2ec54330 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Wed, 31 Jan 2024 09:56:47 +0800 Subject: [PATCH] Try to remove the tun device when turning on --- v2rayN/v2rayN/Common/Utils.cs | 375 ++++++++++++++------------- v2rayN/v2rayN/Handler/CoreHandler.cs | 6 +- 2 files changed, 198 insertions(+), 183 deletions(-) diff --git a/v2rayN/v2rayN/Common/Utils.cs b/v2rayN/v2rayN/Common/Utils.cs index 79b91be6..c0df6c8d 100644 --- a/v2rayN/v2rayN/Common/Utils.cs +++ b/v2rayN/v2rayN/Common/Utils.cs @@ -512,187 +512,6 @@ namespace v2rayN #endregion 数据检查 - #region 开机自动启动 - - /// - /// 开机自动启动 - /// - /// - /// - public static void SetAutoRun(string AutoRunRegPath, string AutoRunName, bool run) - { - try - { - var autoRunName = $"{AutoRunName}_{GetMD5(StartupPath())}"; - - //delete first - RegWriteValue(AutoRunRegPath, autoRunName, ""); - if (IsAdministrator()) - { - AutoStart(autoRunName, "", ""); - } - - if (run) - { - string exePath = $"\"{GetExePath()}\""; - if (IsAdministrator()) - { - AutoStart(autoRunName, exePath, ""); - } - else - { - RegWriteValue(AutoRunRegPath, autoRunName, exePath); - } - } - } - catch (Exception ex) - { - Logging.SaveLog(ex.Message, ex); - } - } - - /// - /// 获取启动了应用程序的可执行文件的路径 - /// - /// - public static string GetPath(string fileName) - { - string startupPath = StartupPath(); - if (IsNullOrEmpty(fileName)) - { - return startupPath; - } - return Path.Combine(startupPath, fileName); - } - - /// - /// 获取启动了应用程序的可执行文件的路径及文件名 - /// - /// - public static string GetExePath() - { - return Environment.ProcessPath ?? string.Empty; - } - - public static string StartupPath() - { - return AppDomain.CurrentDomain.BaseDirectory; - } - - public static string? RegReadValue(string path, string name, string def) - { - RegistryKey? regKey = null; - try - { - regKey = Registry.CurrentUser.OpenSubKey(path, false); - string? value = regKey?.GetValue(name) as string; - if (IsNullOrEmpty(value)) - { - return def; - } - else - { - return value; - } - } - catch (Exception ex) - { - Logging.SaveLog(ex.Message, ex); - } - finally - { - regKey?.Close(); - } - return def; - } - - public static void RegWriteValue(string path, string name, object value) - { - RegistryKey? regKey = null; - try - { - regKey = Registry.CurrentUser.CreateSubKey(path); - if (IsNullOrEmpty(value.ToString())) - { - regKey?.DeleteValue(name, false); - } - else - { - regKey?.SetValue(name, value); - } - } - catch (Exception ex) - { - Logging.SaveLog(ex.Message, ex); - } - finally - { - regKey?.Close(); - } - } - - /// - /// 判断.Net Framework的Release是否符合 - /// (.Net Framework 版本在4.0及以上) - /// - /// 需要的版本4.6.2=394802;4.8=528040 - /// - public static bool CheckForDotNetVersion(int release = 528040) - { - const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\"; - using RegistryKey? ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey); - if (ndpKey?.GetValue("Release") != null) - { - return (int)ndpKey.GetValue("Release") >= release; - } - return false; - } - - /// - /// Auto Start via TaskService - /// - /// - /// - /// - /// - public static void AutoStart(string taskName, string fileName, string description) - { - if (string.IsNullOrEmpty(taskName)) - { - return; - } - string TaskName = taskName; - var logonUser = WindowsIdentity.GetCurrent().Name; - string taskDescription = description; - string deamonFileName = fileName; - - using var taskService = new TaskService(); - var tasks = taskService.RootFolder.GetTasks(new Regex(TaskName)); - foreach (var t in tasks) - { - taskService.RootFolder.DeleteTask(t.Name); - } - if (string.IsNullOrEmpty(fileName)) - { - return; - } - - var task = taskService.NewTask(); - task.RegistrationInfo.Description = taskDescription; - task.Settings.DisallowStartIfOnBatteries = false; - task.Settings.StopIfGoingOnBatteries = false; - task.Settings.RunOnlyIfIdle = false; - task.Settings.IdleSettings.StopOnIdleEnd = false; - task.Settings.ExecutionTimeLimit = TimeSpan.Zero; - task.Triggers.Add(new LogonTrigger { UserId = logonUser, Delay = TimeSpan.FromSeconds(10) }); - task.Principal.RunLevel = TaskRunLevel.Highest; - task.Actions.Add(new ExecAction(deamonFileName)); - - taskService.RootFolder.RegisterTaskDefinition(TaskName, task); - } - - #endregion 开机自动启动 - #region 测速 /// @@ -962,7 +781,34 @@ namespace v2rayN #region TempPath - // return path to store temporary files + /// + /// 获取启动了应用程序的可执行文件的路径 + /// + /// + public static string GetPath(string fileName) + { + string startupPath = StartupPath(); + if (IsNullOrEmpty(fileName)) + { + return startupPath; + } + return Path.Combine(startupPath, fileName); + } + + /// + /// 获取启动了应用程序的可执行文件的路径及文件名 + /// + /// + public static string GetExePath() + { + return Environment.ProcessPath ?? string.Empty; + } + + public static string StartupPath() + { + return AppDomain.CurrentDomain.BaseDirectory; + } + public static string GetTempPath(string filename = "") { string _tempPath = Path.Combine(StartupPath(), "guiTemps"); @@ -1137,6 +983,171 @@ namespace v2rayN #endregion scan screen + #region 开机自动启动等 + + /// + /// 开机自动启动 + /// + /// + /// + public static void SetAutoRun(string AutoRunRegPath, string AutoRunName, bool run) + { + try + { + var autoRunName = $"{AutoRunName}_{GetMD5(StartupPath())}"; + + //delete first + RegWriteValue(AutoRunRegPath, autoRunName, ""); + if (IsAdministrator()) + { + AutoStart(autoRunName, "", ""); + } + + if (run) + { + string exePath = $"\"{GetExePath()}\""; + if (IsAdministrator()) + { + AutoStart(autoRunName, exePath, ""); + } + else + { + RegWriteValue(AutoRunRegPath, autoRunName, exePath); + } + } + } + catch (Exception ex) + { + Logging.SaveLog(ex.Message, ex); + } + } + + public static string? RegReadValue(string path, string name, string def) + { + RegistryKey? regKey = null; + try + { + regKey = Registry.CurrentUser.OpenSubKey(path, false); + string? value = regKey?.GetValue(name) as string; + if (IsNullOrEmpty(value)) + { + return def; + } + else + { + return value; + } + } + catch (Exception ex) + { + Logging.SaveLog(ex.Message, ex); + } + finally + { + regKey?.Close(); + } + return def; + } + + public static void RegWriteValue(string path, string name, object value) + { + RegistryKey? regKey = null; + try + { + regKey = Registry.CurrentUser.CreateSubKey(path); + if (IsNullOrEmpty(value.ToString())) + { + regKey?.DeleteValue(name, false); + } + else + { + regKey?.SetValue(name, value); + } + } + catch (Exception ex) + { + Logging.SaveLog(ex.Message, ex); + } + finally + { + regKey?.Close(); + } + } + + /// + /// Auto Start via TaskService + /// + /// + /// + /// + /// + public static void AutoStart(string taskName, string fileName, string description) + { + if (string.IsNullOrEmpty(taskName)) + { + return; + } + string TaskName = taskName; + var logonUser = WindowsIdentity.GetCurrent().Name; + string taskDescription = description; + string deamonFileName = fileName; + + using var taskService = new TaskService(); + var tasks = taskService.RootFolder.GetTasks(new Regex(TaskName)); + foreach (var t in tasks) + { + taskService.RootFolder.DeleteTask(t.Name); + } + if (string.IsNullOrEmpty(fileName)) + { + return; + } + + var task = taskService.NewTask(); + task.RegistrationInfo.Description = taskDescription; + task.Settings.DisallowStartIfOnBatteries = false; + task.Settings.StopIfGoingOnBatteries = false; + task.Settings.RunOnlyIfIdle = false; + task.Settings.IdleSettings.StopOnIdleEnd = false; + task.Settings.ExecutionTimeLimit = TimeSpan.Zero; + task.Triggers.Add(new LogonTrigger { UserId = logonUser, Delay = TimeSpan.FromSeconds(10) }); + task.Principal.RunLevel = TaskRunLevel.Highest; + task.Actions.Add(new ExecAction(deamonFileName)); + + taskService.RootFolder.RegisterTaskDefinition(TaskName, task); + } + + public static void RemoveTunDevice() + { + try + { + string pnputilPath = @"C:\Windows\System32\pnputil.exe"; + string arg = $" /remove-device /deviceid \"wintun\""; + + // 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 + { + } + } + + #endregion 开机自动启动等 + #region Windows API [Flags] diff --git a/v2rayN/v2rayN/Handler/CoreHandler.cs b/v2rayN/v2rayN/Handler/CoreHandler.cs index bbd8cd2e..b7fa62b4 100644 --- a/v2rayN/v2rayN/Handler/CoreHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreHandler.cs @@ -45,10 +45,10 @@ namespace v2rayN.Handler ShowMsg(false, msg); ShowMsg(true, $"{node.GetSummary()}"); CoreStop(); - if (_config.tunModeItem.enableTun) { Thread.Sleep(1000); + Utils.RemoveTunDevice(); } CoreStart(node); @@ -154,6 +154,8 @@ namespace v2rayN.Handler } } + #region Private + private string CoreFindexe(CoreInfo coreInfo) { string fileName = string.Empty; @@ -255,6 +257,8 @@ namespace v2rayN.Handler _updateFunc(updateToTrayTooltip, msg); } + #endregion Private + #region Process private Process? RunProcess(ProfileItem node, CoreInfo coreInfo, string configPath, bool displayLog, Action update)