fix: Allow TUN mode to auto-enable for NOPASSWD sudo users on Linux

Previously, AllowEnableTun() only checked if LinuxSudoPwd was non-empty,
which is always null on startup. This caused the password dialog to always
appear when enabling TUN mode, even for users with NOPASSWD sudo configured.

Add a check using 'sudo -n true' to detect NOPASSWD configuration.
This allows users with NOPASSWD sudo to auto-enable TUN mode without
being prompted for a password on each startup, matching the behavior
of Windows (which auto-enables when run as administrator).

Fixes behavior discussed in #7116 and #8069.
This commit is contained in:
think 2026-05-06 20:51:27 +08:00
parent 177ad7db3d
commit 4ca8c90e5e

View file

@ -503,7 +503,27 @@ public class StatusBarViewModel : MyReactiveObject
} }
else if (Utils.IsLinux()) else if (Utils.IsLinux())
{ {
return AppManager.Instance.LinuxSudoPwd.IsNotEmpty(); if (AppManager.Instance.LinuxSudoPwd.IsNotEmpty())
return true;
// Check if sudo is configured with NOPASSWD
try
{
var psi = new ProcessStartInfo
{
FileName = "/usr/bin/sudo",
Arguments = "-n true",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
};
using var proc = Process.Start(psi);
proc?.WaitForExit();
return proc?.ExitCode == 0;
}
catch
{
return false;
}
} }
else if (Utils.IsMacOS()) else if (Utils.IsMacOS())
{ {