Update
This commit is contained in:
@@ -56,17 +56,68 @@ def run_command(cmd: list[str], timeout: int = 10) -> tuple[int, str, str]:
|
||||
return -1, "", str(e)
|
||||
|
||||
|
||||
def run_privileged(cmd: list[str], timeout: int = 10) -> tuple[int, str, str]:
|
||||
def run_privileged(cmd: list[str], timeout: int = 60) -> tuple[int, str, str]:
|
||||
if is_windows():
|
||||
return run_command(cmd, timeout)
|
||||
if has_root_privileges():
|
||||
return run_command(cmd, timeout)
|
||||
# Tente pkexec puis sudo
|
||||
for elevator in ("pkexec", "sudo"):
|
||||
code, out, err = run_command([elevator] + cmd, timeout)
|
||||
if code != -1 or "introuvable" not in err:
|
||||
return code, out, err
|
||||
return -1, "", "Élévation de privilèges impossible"
|
||||
|
||||
# 1. pkexec — dialogue graphique polkit (GNOME/KDE), timeout long pour la saisie
|
||||
code, out, err = run_command(["pkexec"] + cmd, timeout)
|
||||
if code != -1 or "introuvable" not in err:
|
||||
return code, out, err
|
||||
|
||||
# 2. sudo avec programme askpass graphique (pas de TTY dans une app Qt)
|
||||
askpass = _find_askpass()
|
||||
if askpass:
|
||||
env = os.environ.copy()
|
||||
env["SUDO_ASKPASS"] = askpass
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["sudo", "-A"] + cmd,
|
||||
capture_output=True, text=True,
|
||||
timeout=timeout, env=env,
|
||||
)
|
||||
return result.returncode, result.stdout.strip(), result.stderr.strip()
|
||||
except subprocess.TimeoutExpired:
|
||||
return -1, "", "Timeout élévation sudo"
|
||||
except Exception as e:
|
||||
return -1, "", str(e)
|
||||
|
||||
# 3. sudo classique (fonctionne si NOPASSWD configuré dans sudoers)
|
||||
code, out, err = run_command(["sudo", "-n"] + cmd, 10)
|
||||
if code != -1:
|
||||
return code, out, err
|
||||
|
||||
return (
|
||||
-1, "",
|
||||
"Élévation de privilèges impossible.\n"
|
||||
"Solutions :\n"
|
||||
" • Installer pkexec (polkit) pour le dialogue graphique\n"
|
||||
" • Ou ajouter dans /etc/sudoers :\n"
|
||||
f" {os.environ.get('USER','<user>')} ALL=(ALL) NOPASSWD: /usr/bin/wg-quick"
|
||||
)
|
||||
|
||||
|
||||
def _find_askpass() -> str:
|
||||
"""Cherche un programme askpass graphique disponible."""
|
||||
candidates = [
|
||||
os.environ.get("SUDO_ASKPASS", ""),
|
||||
"/usr/lib/openssh/gnome-ssh-askpass",
|
||||
"/usr/bin/ssh-askpass",
|
||||
"/usr/bin/x11-ssh-askpass",
|
||||
"/usr/libexec/ssh-askpass",
|
||||
]
|
||||
for p in candidates:
|
||||
if p and os.path.isfile(p) and os.access(p, os.X_OK):
|
||||
return p
|
||||
# Cherche aussi via shutil
|
||||
import shutil
|
||||
for name in ("ssh-askpass", "x11-ssh-askpass", "gnome-ssh-askpass"):
|
||||
found = shutil.which(name)
|
||||
if found:
|
||||
return found
|
||||
return ""
|
||||
|
||||
|
||||
def wg_available() -> bool:
|
||||
|
||||
Reference in New Issue
Block a user