From 4521e022aaad2479a29294946b516db72690d2f8 Mon Sep 17 00:00:00 2001 From: nirvanalinlei Date: Thu, 2 Apr 2026 04:29:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=89=E4=B8=8A=E6=B8=B8=E6=84=8F=E8=A7=81?= =?UTF-8?q?=E5=9B=9E=E6=BB=9A=20TUN=20=E6=8F=90=E6=9D=83=E6=97=B6=E4=BF=9D?= =?UTF-8?q?=E7=95=99=E5=90=AF=E7=94=A8=E7=8A=B6=E6=80=81=E7=9A=84=E6=94=B9?= =?UTF-8?q?=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- v2rayN/ServiceLib/Manager/AppManager.cs | 10 ++---- .../ViewModels/StatusBarViewModel.cs | 36 ++++++++----------- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/v2rayN/ServiceLib/Manager/AppManager.cs b/v2rayN/ServiceLib/Manager/AppManager.cs index 7354c305..35847d71 100644 --- a/v2rayN/ServiceLib/Manager/AppManager.cs +++ b/v2rayN/ServiceLib/Manager/AppManager.cs @@ -144,16 +144,10 @@ public sealed class AppManager AppEvents.ShutdownRequested.Publish(byUser); } - public async Task RebootAsAdmin() + public async Task RebootAsAdmin() { - var started = ProcUtils.RebootAsAdmin(); - if (!started) - { - return false; - } - + ProcUtils.RebootAsAdmin(); await AppManager.Instance.AppExitAsync(true); - return true; } #endregion App diff --git a/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs b/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs index aac51c5c..0ce40dbb 100644 --- a/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs @@ -469,40 +469,32 @@ public class StatusBarViewModel : MyReactiveObject return; } - var allowEnableTun = AllowEnableTun(); - if (PrepareTunConfigForPrivilegeEscalation(_config, EnableTun, allowEnableTun, Utils.IsWindows())) - { - await ConfigHandler.SaveConfig(_config); - if (!await AppManager.Instance.RebootAsAdmin()) - { - _config.TunModeItem.EnableTun = false; - EnableTun = false; - await ConfigHandler.SaveConfig(_config); - NoticeManager.Instance.SendMessageEx(ResUI.OperationFailed); - } - return; - } + _config.TunModeItem.EnableTun = EnableTun; - if (EnableTun && !allowEnableTun) + if (EnableTun && AllowEnableTun() == false) { - bool? passwordResult = await _updateView?.Invoke(EViewAction.PasswordInput, null); - if (passwordResult == false) + // When running as a non-administrator, reboot to administrator mode + if (Utils.IsWindows()) { _config.TunModeItem.EnableTun = false; + await AppManager.Instance.RebootAsAdmin(); return; } + else + { + bool? passwordResult = await _updateView?.Invoke(EViewAction.PasswordInput, null); + if (passwordResult == false) + { + _config.TunModeItem.EnableTun = false; + return; + } + } } await ConfigHandler.SaveConfig(_config); AppEvents.ReloadRequested.Publish(); } - private static bool PrepareTunConfigForPrivilegeEscalation(Config config, bool enableTun, bool allowEnableTun, bool isWindows) - { - config.TunModeItem.EnableTun = enableTun; - return enableTun && !allowEnableTun && isWindows; - } - private bool AllowEnableTun() { if (Utils.IsWindows())