feat(auth): auto-launch background browser if CDP is down during cookie extraction

This commit is contained in:
operator
2026-10-05 15:59:58 +00:00
parent d529d9ebab
commit fd3ef5dc2d
+23
View File
@@ -6,6 +6,8 @@ instance inside its isolated network namespace and save to ~/.config/muse-cli/<n
import sys import sys
import os import os
import json import json
import time
import subprocess
import urllib.request import urllib.request
from pathlib import Path from pathlib import Path
@@ -36,6 +38,27 @@ def export_cookies(node):
print("websocket-client not installed", file=sys.stderr) print("websocket-client not installed", file=sys.stderr)
return False return False
# Check if CDP port is responding; if not, attempt background browser launch
try:
urllib.request.urlopen(f"http://127.0.0.1:{port}/json", timeout=2)
except Exception:
print(f"CDP unreachable on port {port}. Attempting background browser launch for {node}...", file=sys.stderr)
try:
subprocess.Popen([
str(Path(__file__).resolve().parent / "netvm-chrome.sh"),
"--headless", node, "https://muse.ai"
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# Give it up to 6 seconds to bind
for _ in range(12):
time.sleep(0.5)
try:
urllib.request.urlopen(f"http://127.0.0.1:{port}/json", timeout=1)
break
except Exception:
pass
except Exception as e:
print(f"Failed to launch browser: {e}", file=sys.stderr)
try: try:
req = urllib.request.urlopen(f"http://127.0.0.1:{port}/json", timeout=3) req = urllib.request.urlopen(f"http://127.0.0.1:{port}/json", timeout=3)
tabs = json.loads(req.read().decode()) tabs = json.loads(req.read().decode())