harden macOS networking port
This commit is contained in:
@@ -27,6 +27,9 @@ import winreg
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
if sys.platform == "darwin":
|
||||
from .macos_privileged import run_shell, run_shell_as_admin, shell_quote
|
||||
|
||||
_KEY_PATH = r"Software\Microsoft\Windows\CurrentVersion\Internet Settings"
|
||||
_CONN_KEY_PATH = (
|
||||
r"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections"
|
||||
@@ -73,11 +76,22 @@ def _mac_network_services() -> list[str]:
|
||||
return services
|
||||
|
||||
|
||||
def _mac_run_networksetup(args: list[str]) -> None:
|
||||
def _mac_run_networksetup(args: list[str]) -> bool:
|
||||
try:
|
||||
subprocess.run(["networksetup", *args], capture_output=True, text=True, timeout=20)
|
||||
r = subprocess.run(["networksetup", *args], capture_output=True, text=True, timeout=20)
|
||||
if r.returncode != 0:
|
||||
log.debug("networksetup failed (%s): %s", args, (r.stderr or r.stdout or "").strip())
|
||||
return r.returncode == 0
|
||||
except Exception as exc:
|
||||
log.debug("networksetup failed (%s): %s", args, exc)
|
||||
return False
|
||||
|
||||
|
||||
def _mac_networksetup_script(commands: list[list[str]]) -> str:
|
||||
lines = ["set -e"]
|
||||
for args in commands:
|
||||
lines.append("networksetup " + " ".join(shell_quote(a) for a in args))
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def _mac_set_system_proxy(host: str, port: int, bypass: str) -> None:
|
||||
@@ -86,21 +100,44 @@ def _mac_set_system_proxy(host: str, port: int, bypass: str) -> None:
|
||||
for h in bypass.replace(";", ",").split(",")
|
||||
if h.strip() and h.strip() != "<local>"
|
||||
]
|
||||
for service in _mac_network_services():
|
||||
_mac_run_networksetup(["-setwebproxy", service, host, str(port)])
|
||||
_mac_run_networksetup(["-setsecurewebproxy", service, host, str(port)])
|
||||
_mac_run_networksetup(["-setwebproxystate", service, "on"])
|
||||
_mac_run_networksetup(["-setsecurewebproxystate", service, "on"])
|
||||
services = _mac_network_services()
|
||||
commands: list[list[str]] = []
|
||||
for service in services:
|
||||
commands.extend([
|
||||
["-setwebproxy", service, host, str(port)],
|
||||
["-setsecurewebproxy", service, host, str(port)],
|
||||
["-setwebproxystate", service, "on"],
|
||||
["-setsecurewebproxystate", service, "on"],
|
||||
])
|
||||
if bypass_hosts:
|
||||
_mac_run_networksetup(["-setproxybypassdomains", service, *bypass_hosts])
|
||||
log.info("macOS system proxy set to %s:%s for %d service(s)", host, port, len(_mac_network_services()))
|
||||
commands.append(["-setproxybypassdomains", service, *bypass_hosts])
|
||||
script = _mac_networksetup_script(commands)
|
||||
code, _, err = run_shell(script)
|
||||
if code != 0:
|
||||
log.info("networksetup needs administrator approval; requesting macOS credentials.")
|
||||
code, _, err = run_shell_as_admin(script)
|
||||
if code != 0:
|
||||
raise RuntimeError(f"macOS system proxy apply failed: {err.strip() or 'networksetup failed'}")
|
||||
log.info("macOS system proxy set to %s:%s for %d service(s)", host, port, len(services))
|
||||
|
||||
|
||||
def _mac_clear_system_proxy() -> None:
|
||||
for service in _mac_network_services():
|
||||
_mac_run_networksetup(["-setwebproxystate", service, "off"])
|
||||
_mac_run_networksetup(["-setsecurewebproxystate", service, "off"])
|
||||
log.info("macOS system proxy cleared for %d service(s)", len(_mac_network_services()))
|
||||
services = _mac_network_services()
|
||||
commands = []
|
||||
for service in services:
|
||||
commands.extend([
|
||||
["-setwebproxystate", service, "off"],
|
||||
["-setsecurewebproxystate", service, "off"],
|
||||
])
|
||||
if not commands:
|
||||
return
|
||||
script = _mac_networksetup_script(commands)
|
||||
code, _, err = run_shell(script)
|
||||
if code != 0:
|
||||
code, _, err = run_shell_as_admin(script)
|
||||
if code != 0:
|
||||
raise RuntimeError(f"macOS system proxy clear failed: {err.strip() or 'networksetup failed'}")
|
||||
log.info("macOS system proxy cleared for %d service(s)", len(services))
|
||||
|
||||
|
||||
def _mac_system_proxy_set() -> bool:
|
||||
|
||||
Reference in New Issue
Block a user