Archived
140 lines
4.2 KiB
Python
140 lines
4.2 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import subprocess
|
|
from typing import Any
|
|
|
|
from .settings import HELPER_PATH, WG_INTERFACE
|
|
|
|
|
|
class HelperError(RuntimeError):
|
|
pass
|
|
|
|
|
|
ALLOWED_ACTIONS = {
|
|
"snapshot",
|
|
"interfaces_list",
|
|
"interface_meta_set",
|
|
"interface_delete",
|
|
"metrics_sample",
|
|
"metrics_history",
|
|
"diagnostics",
|
|
"network_trace",
|
|
"wireguard_start",
|
|
"wireguard_stop",
|
|
"wireguard_restart",
|
|
"install_wireguard",
|
|
"create_server",
|
|
"create_client",
|
|
"update_client",
|
|
"delete_client",
|
|
"rotate_client_credentials",
|
|
"client_config",
|
|
"group_create",
|
|
"group_update",
|
|
"group_delete",
|
|
"raw_config_get",
|
|
"raw_config_apply",
|
|
"server_settings_apply",
|
|
"extended_snat_get",
|
|
"extended_host_ips",
|
|
"extended_snat_apply",
|
|
"extended_listen_get",
|
|
"extended_listen_apply",
|
|
"extended_ip_groups_get",
|
|
"extended_ip_groups_set",
|
|
"extended_forward_get",
|
|
"extended_forward_add",
|
|
"extended_forward_delete",
|
|
"route_create",
|
|
"route_update",
|
|
"route_delete",
|
|
"access_rule_create",
|
|
"access_rule_update",
|
|
"access_rule_delete",
|
|
"migration_backup_create",
|
|
"migration_restore",
|
|
"update_check",
|
|
"update_notice",
|
|
"update_status",
|
|
"update_schedule",
|
|
"update_file_schedule",
|
|
"traffic_reset",
|
|
"https_status",
|
|
"https_ports",
|
|
"https_issue",
|
|
"https_enable",
|
|
"https_disable",
|
|
}
|
|
|
|
|
|
def run_helper(action: str, payload: dict[str, Any] | None = None, *, interface: str | None = None) -> dict[str, Any]:
|
|
if action not in ALLOWED_ACTIONS:
|
|
raise HelperError(f"Unsupported helper action: {action}")
|
|
|
|
timeout = 8
|
|
if action == "install_wireguard":
|
|
timeout = 430
|
|
elif action == "create_server":
|
|
timeout = 60
|
|
elif action == "network_trace":
|
|
timeout = 85
|
|
elif action in {"wireguard_start", "wireguard_stop", "wireguard_restart", "interface_delete"}:
|
|
timeout = 45
|
|
elif action in {"create_client", "update_client", "delete_client", "rotate_client_credentials", "group_create", "group_update", "group_delete"}:
|
|
# Group policy reconciliation may rebuild many WGTools-managed firewall
|
|
# rules on installations with a large peer count. Give the privileged
|
|
# helper enough time to finish its transactional rollback/commit path
|
|
# instead of killing it mid-update.
|
|
timeout = 120
|
|
elif action in {"raw_config_apply", "server_settings_apply", "extended_snat_apply", "extended_listen_apply", "extended_forward_add", "extended_forward_delete", "route_create", "route_update", "route_delete", "access_rule_create", "access_rule_update", "access_rule_delete"}:
|
|
timeout = 90
|
|
elif action == "migration_backup_create":
|
|
timeout = 45
|
|
elif action == "migration_restore":
|
|
timeout = 480
|
|
elif action == "update_check":
|
|
timeout = 20
|
|
elif action == "update_notice":
|
|
timeout = 20
|
|
elif action == "update_status":
|
|
timeout = 10
|
|
elif action in {"update_schedule", "update_file_schedule"}:
|
|
timeout = 90
|
|
elif action == "https_issue":
|
|
timeout = 260
|
|
elif action in {"https_enable", "https_disable"}:
|
|
timeout = 40
|
|
|
|
iface = (interface or WG_INTERFACE).strip()
|
|
cmd = [HELPER_PATH, action, iface]
|
|
input_text = json.dumps(payload or {}) if payload is not None else None
|
|
try:
|
|
proc = subprocess.run(
|
|
cmd,
|
|
input=input_text,
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=timeout,
|
|
check=False,
|
|
)
|
|
except subprocess.TimeoutExpired as exc:
|
|
raise HelperError(f"Operation timed out after {timeout} seconds") from exc
|
|
|
|
if proc.returncode != 0:
|
|
detail = (proc.stderr or proc.stdout or "helper failed").strip()
|
|
try:
|
|
parsed = json.loads(proc.stdout)
|
|
detail = parsed.get("error") or detail
|
|
except Exception:
|
|
pass
|
|
raise HelperError(detail)
|
|
|
|
try:
|
|
data = json.loads(proc.stdout)
|
|
except json.JSONDecodeError as exc:
|
|
raise HelperError("Helper returned invalid JSON") from exc
|
|
if not data.get("ok", False):
|
|
raise HelperError(data.get("error", "Helper operation failed"))
|
|
return data
|