refine macOS service selection

This commit is contained in:
drjones
2026-05-23 22:13:14 -07:00
parent 35899ba1d1
commit 267755912c

View File

@@ -67,13 +67,31 @@ def _mac_network_services() -> list[str]:
) )
except Exception: except Exception:
return [] return []
services: list[str] = [] listed: list[str] = []
for line in (r.stdout or "").splitlines(): for line in (r.stdout or "").splitlines():
name = line.strip() raw = line.strip()
if not name or name.startswith("An asterisk"): if not raw or raw.startswith("An asterisk"):
continue continue
services.append(name.lstrip("*").strip()) # networksetup marks disabled services with a leading '*'.
return services 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: def _mac_run_networksetup(args: list[str]) -> bool:
@@ -156,6 +174,30 @@ def _mac_system_proxy_set() -> bool:
return False 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: def _is_admin() -> bool:
try: try:
return ctypes.windll.shell32.IsUserAnAdmin() != 0 # type: ignore[attr-defined] 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]: def diagnose_system_proxy() -> list[str]:
if sys.platform == "darwin": if sys.platform == "darwin":
out = [] 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: try:
r = subprocess.run( r = subprocess.run(
["networksetup", "-getwebproxy", service], ["networksetup", "-getwebproxy", service],