From 267755912c697405d024031628f4da9e068b0f5f Mon Sep 17 00:00:00 2001 From: drjones Date: Sat, 23 May 2026 22:13:14 -0700 Subject: [PATCH] refine macOS service selection --- proxy_chain_manager/sysproxy.py | 57 +++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/proxy_chain_manager/sysproxy.py b/proxy_chain_manager/sysproxy.py index a37e4da..c3d0087 100644 --- a/proxy_chain_manager/sysproxy.py +++ b/proxy_chain_manager/sysproxy.py @@ -67,13 +67,31 @@ def _mac_network_services() -> list[str]: ) except Exception: return [] - services: list[str] = [] + listed: list[str] = [] for line in (r.stdout or "").splitlines(): - name = line.strip() - if not name or name.startswith("An asterisk"): + raw = line.strip() + if not raw or raw.startswith("An asterisk"): continue - services.append(name.lstrip("*").strip()) - return services + # networksetup marks disabled services with a leading '*'. + if raw.startswith("*"): + continue + listed.append(raw) + + enabled: list[str] = [] + for service in listed: + try: + chk = subprocess.run( + ["networksetup", "-getnetworkserviceenabled", service], + capture_output=True, + text=True, + timeout=8, + ) + out = (chk.stdout or "").strip().lower() + if "enabled" in out and "disabled" not in out: + enabled.append(service) + except Exception: + continue + return enabled or listed def _mac_run_networksetup(args: list[str]) -> bool: @@ -156,6 +174,30 @@ def _mac_system_proxy_set() -> bool: return False +def _mac_service_has_live_ip(service: str) -> bool: + try: + r = subprocess.run( + ["networksetup", "-getinfo", service], + capture_output=True, + text=True, + timeout=8, + ) + info = (r.stdout or "").splitlines() + except Exception: + return False + for line in info: + s = line.strip() + if s.startswith("IP address:"): + ip = s.split(":", 1)[1].strip().lower() + if ip and ip != "none" and ip != "0.0.0.0": + return True + if s.startswith("IPv6:"): + v = s.split(":", 1)[1].strip().lower() + if v not in ("off", "none"): + return True + return False + + def _is_admin() -> bool: try: return ctypes.windll.shell32.IsUserAnAdmin() != 0 # type: ignore[attr-defined] @@ -461,7 +503,10 @@ def _read_winhttp_proxy() -> str: def diagnose_system_proxy() -> list[str]: if sys.platform == "darwin": out = [] - for service in _mac_network_services()[:8]: + services = _mac_network_services() + active = [s for s in services if _mac_service_has_live_ip(s)] + inspect = active[:8] if active else services[:8] + for service in inspect: try: r = subprocess.run( ["networksetup", "-getwebproxy", service],