harden macOS networking port
This commit is contained in:
@@ -3,10 +3,53 @@ from __future__ import annotations
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
import plistlib
|
||||
|
||||
from .paths import app_data_dir
|
||||
|
||||
TASK_NAME = "ProxyChainManagerAutoStart"
|
||||
LAUNCH_AGENT_ID = "local.proxy-god.mac"
|
||||
|
||||
|
||||
def _launch_agent_path() -> Path:
|
||||
p = Path.home() / "Library" / "LaunchAgents"
|
||||
p.mkdir(parents=True, exist_ok=True)
|
||||
return p / f"{LAUNCH_AGENT_ID}.plist"
|
||||
|
||||
|
||||
def _mac_program_arguments() -> list[str]:
|
||||
root = Path(__file__).resolve().parent.parent
|
||||
return [str(root / "scripts" / "run_proxy_god.sh")]
|
||||
|
||||
|
||||
def _install_launch_agent() -> tuple[bool, str]:
|
||||
plist_path = _launch_agent_path()
|
||||
data = {
|
||||
"Label": LAUNCH_AGENT_ID,
|
||||
"ProgramArguments": _mac_program_arguments(),
|
||||
"RunAtLoad": True,
|
||||
"WorkingDirectory": str(Path(__file__).resolve().parent.parent),
|
||||
"StandardOutPath": str(Path.home() / "Library" / "Logs" / "ProxyGod.out.log"),
|
||||
"StandardErrorPath": str(Path.home() / "Library" / "Logs" / "ProxyGod.err.log"),
|
||||
}
|
||||
try:
|
||||
plist_path.write_bytes(plistlib.dumps(data))
|
||||
subprocess.run(["launchctl", "unload", str(plist_path)], capture_output=True, text=True, timeout=10)
|
||||
r = subprocess.run(["launchctl", "load", str(plist_path)], capture_output=True, text=True, timeout=10)
|
||||
out = (r.stdout or "") + (r.stderr or "")
|
||||
return r.returncode == 0, out.strip() or "LaunchAgent installed"
|
||||
except Exception as e:
|
||||
return False, str(e)
|
||||
|
||||
|
||||
def _uninstall_launch_agent() -> tuple[bool, str]:
|
||||
plist_path = _launch_agent_path()
|
||||
try:
|
||||
subprocess.run(["launchctl", "unload", str(plist_path)], capture_output=True, text=True, timeout=10)
|
||||
plist_path.unlink(missing_ok=True)
|
||||
return True, "LaunchAgent removed"
|
||||
except Exception as e:
|
||||
return False, str(e)
|
||||
|
||||
|
||||
def _write_launcher_cmd() -> Path:
|
||||
@@ -30,6 +73,8 @@ def _write_launcher_cmd() -> Path:
|
||||
|
||||
|
||||
def install_logon_task() -> tuple[bool, str]:
|
||||
if sys.platform == "darwin":
|
||||
return _install_launch_agent()
|
||||
launcher = _write_launcher_cmd()
|
||||
tr = str(launcher)
|
||||
try:
|
||||
@@ -58,6 +103,8 @@ def install_logon_task() -> tuple[bool, str]:
|
||||
|
||||
|
||||
def uninstall_logon_task() -> tuple[bool, str]:
|
||||
if sys.platform == "darwin":
|
||||
return _uninstall_launch_agent()
|
||||
try:
|
||||
r = subprocess.run(
|
||||
["schtasks", "/Delete", "/F", "/TN", TASK_NAME],
|
||||
@@ -72,6 +119,8 @@ def uninstall_logon_task() -> tuple[bool, str]:
|
||||
|
||||
|
||||
def task_exists() -> bool:
|
||||
if sys.platform == "darwin":
|
||||
return _launch_agent_path().is_file()
|
||||
try:
|
||||
r = subprocess.run(
|
||||
["schtasks", "/Query", "/TN", TASK_NAME],
|
||||
|
||||
Reference in New Issue
Block a user