Windows
This commit is contained in:
+30
-8
@@ -48,7 +48,9 @@ def build_client_config(cfg: Config) -> str:
|
||||
|
||||
def get_client_config_path(cfg: Config) -> str:
|
||||
name = cfg.wg.get("interface_name", "wgs0")
|
||||
return os.path.join(get_config_dir(), f"{name}.conf")
|
||||
if is_windows():
|
||||
return os.path.join(get_config_dir(), f"{name}.conf")
|
||||
return os.path.join(get_wg_config_dir(), f"{name}.conf") # /etc/wireguard/
|
||||
|
||||
|
||||
def write_client_config(cfg: Config) -> tuple[bool, str]:
|
||||
@@ -61,8 +63,30 @@ def write_client_config(cfg: Config) -> tuple[bool, str]:
|
||||
if not is_windows():
|
||||
os.chmod(path, 0o600)
|
||||
return True, path
|
||||
except Exception as e:
|
||||
return False, str(e)
|
||||
except PermissionError:
|
||||
# sudo -n tee (NOPASSWD si configuré, sinon pkexec cp)
|
||||
import subprocess as _sp
|
||||
try:
|
||||
r = _sp.run(["sudo", "-n", "tee", path],
|
||||
input=content, text=True,
|
||||
capture_output=True, timeout=10)
|
||||
if r.returncode == 0:
|
||||
_sp.run(["sudo", "-n", "chmod", "600", path],
|
||||
capture_output=True, timeout=5)
|
||||
return True, path
|
||||
except Exception:
|
||||
pass
|
||||
# Repli sur pkexec cp (dialogue graphique)
|
||||
tmp = os.path.join(get_config_dir(), "wgs_tmp.conf")
|
||||
with open(tmp, "w") as f:
|
||||
f.write(content)
|
||||
os.chmod(tmp, 0o600)
|
||||
code, _, err = run_privileged(["cp", tmp, path])
|
||||
os.unlink(tmp)
|
||||
if code == 0:
|
||||
run_privileged(["chmod", "600", path])
|
||||
return True, path
|
||||
return False, err
|
||||
|
||||
|
||||
def is_connected(cfg: Config) -> bool:
|
||||
@@ -88,7 +112,8 @@ def connect(cfg: Config) -> tuple[bool, str]:
|
||||
code, _, err = run_command(["wireguard", "/installtunnel", result])
|
||||
return (code == 0), (err or "Connecté")
|
||||
else:
|
||||
code, _, err = run_privileged(["wg-quick", "up", result])
|
||||
# Passer le nom d'interface (pas le chemin) : AppArmor autorise /etc/wireguard/ seulement
|
||||
code, _, err = run_privileged(["wg-quick", "up", name])
|
||||
if code == 0:
|
||||
return True, "Tunnel WireGuard activé"
|
||||
return False, err or "Erreur lors de la connexion"
|
||||
@@ -101,10 +126,7 @@ def disconnect(cfg: Config) -> tuple[bool, str]:
|
||||
code, _, err = run_command(["wireguard", "/uninstalltunnel", name])
|
||||
return (code == 0), (err or "Déconnecté")
|
||||
else:
|
||||
path = get_client_config_path(cfg)
|
||||
if not os.path.exists(path):
|
||||
path = name
|
||||
code, _, err = run_privileged(["wg-quick", "down", path])
|
||||
code, _, err = run_privileged(["wg-quick", "down", name])
|
||||
if code == 0:
|
||||
return True, "Tunnel WireGuard désactivé"
|
||||
return False, err or "Erreur lors de la déconnexion"
|
||||
|
||||
Reference in New Issue
Block a user