按上游意见回滚 TUN 提权时保留启用状态的改动

This commit is contained in:
nirvanalinlei 2026-04-02 04:29:06 +08:00
parent 935d9e0aa3
commit 4521e022aa
2 changed files with 16 additions and 30 deletions

View file

@ -144,16 +144,10 @@ public sealed class AppManager
AppEvents.ShutdownRequested.Publish(byUser);
}
public async Task<bool> RebootAsAdmin()
public async Task RebootAsAdmin()
{
var started = ProcUtils.RebootAsAdmin();
if (!started)
{
return false;
}
ProcUtils.RebootAsAdmin();
await AppManager.Instance.AppExitAsync(true);
return true;
}
#endregion App

View file

@ -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())