Skip to content

Business operating system for Claude Code — 57 skills, 21 agents, smart daemon. Unified inbox (WhatsApp/Email/Slack/Telegram), autonomous PR merge, full-AWS monitoring, revenue (Stripe+RevenueCat), e-commerce (Shopify), marketing (Klaviyo/Meta/GA4), voice (Bland/ElevenLabs), APM (Datadog/NewRelic/OTEL), YOLO mode.

fix(crsproxy): improve bu_reauth OAuth flow and add profile-based reauth

auroracapital1fd95808c502
diff --git a/claude-ops/scripts/crsproxy-reauth/bu_profile_reauth.py b/claude-ops/scripts/crsproxy-reauth/bu_profile_reauth.py
new file mode 100644
index 00000000..b15241a5
--- /dev/null
+++ b/claude-ops/scripts/crsproxy-reauth/bu_profile_reauth.py
@@ -0,0 +1,466 @@
+#!/usr/bin/env python3
+"""Browser Use Cloud — Profile-based OAuth re-auth for CRSProxy.
+
+Uses a Browser Use Cloud persistent profile (with synced cookies) to
+bypass the OAuth email/captcha flow. The agent starts already
+authenticated (or uses Google sign-in via synced cookies) and clicks
+Authorize directly.
+
+Falls back to email entry if the profile doesn't have valid cookies,
+and emits a human checkpoint if hCaptcha is detected.
+
+Usage:
+ sudo -u crsproxy /opt/crsproxy/venv/bin/python /opt/crsproxy/bu_profile_reauth.py \\
+ -provider claude -email [email protected] -profile-id <BU_PROFILE_ID>
+"""
+
+import argparse
+import json
+import os
+import re
+import signal
+import subprocess
+import sys
+import time
+from datetime import datetime, timedelta, timezone
+from pathlib import Path
+
+# Import infrastructure from bu_reauth.py
+sys.path.insert(0, "/opt/crsproxy")
+from bu_reauth import (
+ BrowserUseClient,
+ PROVIDERS,
+ INTERCEPT_JS_TEMPLATE,
+ LOG_DIR,
+ LEASE_FILE,
+ acquire_lease,
+ release_lease,
+ start_login,
+ get_oauth_url,
+ paste_callback_url,
+ cleanup_login_process,
+ validate_candidate,
+ activate_auth_file,
+ extract_callback_url,
+ detect_captcha,
+ handle_captcha_checkpoint,
+ sanitize_url,
+ safe_email,
+ log,
+ _log_file,
+ AUTH_DIR,
+ EXIT_SUCCESS,
+ EXIT_FAILURE,
+ EXIT_CAPTCHA,
+ RUN_POLL_TIMEOUT,
+ TOTAL_TIMEOUT,
+ _timeout_handler,
+)
+
+# Also import _complete_reauth for the final step
+from bu_reauth import _complete_reauth
+
+
+def run_profile_reauth(provider: str, email: str, profile_id: str,
+ client: BrowserUseClient,
+ dry_run: bool = False) -> int:
+ """Run the profile-based OAuth reauth flow.
+
+ 1. Acquire serialization lease.
+ 2. Start cli-proxy-api login (captures OAuth URL, starts callback listener).
+ 3. Create a Browser Use run with the profile — agent navigates to OAuth URL.
+ 4. If already authenticated (cookies), agent clicks Authorize directly.
+ 5. If not authenticated, agent tries Google sign-in (using synced Google cookies).
+ 6. Inject JS to intercept the callback URL.
+ 7. Extract callback URL from run result/events.
+ 8. Paste callback into cli-proxy-api PTY.
+ 9. Validate isolated candidate.
+ 10. Atomically activate if valid.
+ """
+ meta = PROVIDERS[provider]
+ port = meta["callback_port"]
+ safe = safe_email(email)
+ log_path = Path("/tmp/bu_profile_reauth_hub.log")
+ log_path.unlink(missing_ok=True)
+
+ # --- Step 1: Acquire serialization lease ---
+ if not acquire_lease(provider):
+ log("Could not acquire lease — another login may be in progress")
+ return EXIT_FAILURE
+
+ try:
+ return _run_profile_reauth_inner(provider, email, profile_id,
+ client, dry_run, log_path)
+ finally:
+ release_lease()
+
+
+def _run_profile_reauth_inner(provider: str, email: str, profile_id: str,
+ client: BrowserUseClient, dry_run: bool,
+ log_path: Path) -> int:
+ """Inner profile reauth flow — assumes lease is already held."""
+ meta = PROVIDERS[provider]
+ port = meta["callback_port"]
+ safe = safe_email(email)
+
+ # --- Dry run ---
+ if dry_run:
+ log(f"[DRY RUN] Provider={provider} Email={safe} Profile={profile_id[:12]}...")
+ log(f"[DRY RUN] Would start: cli-proxy-api {meta['login_flag']}")
+ log(f"[DRY RUN] Would create BU run with profileId={profile_id[:12]}...")
+ log("[DRY RUN] Validating Browser Use API access...")
+ try:
+ browsers = client.list_browsers()
+ log(f"[DRY RUN] Browser Use API: OK ({len(browsers)} active sessions)")
+ except Exception as e:
+ log(f"[DRY RUN] Browser Use API error: {e}")
+ return EXIT_FAILURE
+ log("[DRY RUN] Dry run complete — no browser sessions started")
+ return EXIT_SUCCESS
+
+ # --- Step 1.5: Back up stale auth for preservation ---
+ auth_file = AUTH_DIR / f"{meta['auth_file_prefix']}-{email}.json"
+ stale_backup = auth_file.with_suffix(".stale")
+ if auth_file.exists():
+ try:
+ stale_backup.unlink(missing_ok=True)
+ stale_backup.write_text(auth_file.read_text())
+ log(f"[1] Stale auth backed up: {auth_file.name}")
+ except Exception as e:
+ log(f"[1] Stale auth backup warning: {e}")
+ else:
+ log(f"[1] No existing auth file — no stale backup needed")
+
+ # --- Step 2: Start cli-proxy-api login ---
+ log(f"[1] Starting cli-proxy-api login for {provider} ({safe})")
+ proc = start_login(provider, log_path)
+
+ try:
+ # --- Step 3: Capture OAuth URL ---
+ log("[2] Waiting for OAuth URL...")
+ oauth_url = get_oauth_url(log_path, provider)
+ if not oauth_url:
+ log("[ERROR] No OAuth URL found in login log")
+ return EXIT_FAILURE
+ log(f"[2] OAuth URL captured: {sanitize_url(oauth_url)}")
+
+ # --- Step 4: Create Browser Use run with profile ---
+ # The agent navigates to the OAuth URL. If the profile has valid
+ # cookies, it should be already authenticated. If not, it tries
+ # Google sign-in (which should work if Google cookies are synced).
+ intercept_js = INTERCEPT_JS_TEMPLATE.format(port=port)
+ log(f"[3] Creating Browser Use run with profileId={profile_id[:12]}...")
+
+ task = (
+ f"Navigate to this URL: {oauth_url}\n\n"
+ f"If you see an 'Authorize' or 'Allow' or 'Continue' button, "
+ f"first run this JavaScript in the browser console: {intercept_js}\n"
+ f"Then click the Authorize/Allow/Continue button. "
+ f"Wait 3 seconds. "
+ f"Then run window.__CB__ in the console and report its exact value. "
+ f"If null, report the current page URL.\n\n"
+ f"If you see a login page instead of an Authorize button:\n"
+ f"- If there is a 'Continue with Google' or Google sign-in option, click it. "
+ f"Then if you see an Authorize button, run the JavaScript above and click it.\n"
+ f"- If there is an email field, enter {email} and click Continue. "
+ f"Report what you see next.\n"
+ f"- If you see a CAPTCHA challenge, report 'CAPTCHA' and stop.\n"
+ )
+
+ run = client.create_run(task, profile_id=profile_id)
+ run_id = run.get("id", "")
+ session_id = run.get("sessionId", "")
+ log(f"[3] Run created: {run_id[:12]}... session: {session_id[:12]}...")
+ result = client.wait_for_run(run_id, timeout=RUN_POLL_TIMEOUT)
+ text = str(result.get("result", "") or "")
+ log(f"[3] Run complete: {text[:300]}")
+
+ # Check for captcha
+ if detect_captcha(result, client):
+ log("[CAPTCHA] Captcha detected during profile-based auth")
+ callback_url, session_id = handle_captcha_checkpoint(
+ client, run_id, session_id, provider, email, port)
+ if callback_url:
+ return _complete_reauth(proc, callback_url, auth_file,
+ stale_backup, email, provider, meta)
+ return EXIT_CAPTCHA
+
+ # --- Step 5: Extract callback URL ---
+ log("[4] Searching for callback URL...")
+ callback_url = extract_callback_url(result, client, provider)
+
+ if not callback_url:
+ # Try asking the agent directly for window.__CB__
+ log("[4] Callback not found — asking agent for window.__CB__")
+ task_cb = (
+ "Run window.__CB__ in the browser console and report the exact value. "
+ "If null, report the current page URL."
+ )
+ run_cb = client.create_run(task_cb, session_id=session_id)
+ run_cb_id = run_cb.get("id", "")
+ log(f"[4] Callback query run: {run_cb_id[:12]}...")
+ result_cb = client.wait_for_run(run_cb_id, timeout=60)
+ text_cb = str(result_cb.get("result", "") or "")
+ log(f"[4] Callback query result: {text_cb[:200]}")
+ callback_url = extract_callback_url(result_cb, client, provider)
+
+ if not callback_url:
+ # Check if the agent reported being on a login page
+ text_lower = text.lower()
+ if any(kw in text_lower for kw in ["login", "sign in", "email", "enter"]):
+ log("[FALLBACK] Agent not authenticated — falling back to email entry")
+ # Fall back to email entry in the same session
+ return _fallback_email_entry(
+ proc, client, session_id, provider, email,
+ auth_file, stale_backup, meta, oauth_url)
+
+ log("[FAIL] Could not extract callback URL from any run")
+ log(f"[FAIL] Agent result: {text[:500]}")
+ return EXIT_FAILURE
+
+ log(f"[4] Callback URL extracted: {sanitize_url(callback_url)}")
+
+ # --- Step 6-8: Paste, validate, and activate ---
+ return _complete_reauth(proc, callback_url, auth_file,
+ stale_backup, email, provider, meta)
+
+ except Exception as e:
+ log(f"[ERROR] Unexpected error: {e}")
+ return EXIT_FAILURE
+ finally:
+ log("[CLEANUP] Stopping all browser sessions...")
+ client.stop_all_browsers()
+
+
+def _fallback_email_entry(proc, client, session_id, provider, email,
+ auth_file, stale_backup, meta, oauth_url):
+ """Fall back to email entry if the profile doesn't have valid cookies.
+
+ This reuses the session to enter the email and proceed with the
+ standard OAuth flow (code/magic link via Gmail).
+ """
+ from bu_reauth import (
+ poll_gmail_for_code,
+ record_email_send,
+ _code_entry_failed,
+ enforce_email_cooldown,
+ clear_email_cooldown,
+ )
+
+ safe = safe_email(email)
+ port = meta["callback_port"]
+
+ log(f"[FALLBACK] Entering email for {safe}")
+ task_email = (
+ f"Enter {email} in the email field. "
+ f"Click Continue. Do NOT use Google sign-in. "
+ f"Report exactly what you see after clicking Continue."
+ )
+ run_email = client.create_run(task_email, session_id=session_id)
+ run_email_id = run_email.get("id", "")
+ session_id = run_email.get("sessionId", session_id)
+ log(f"[FALLBACK] Email entry run: {run_email_id[:12]}...")
+ result_email = client.wait_for_run(run_email_id, timeout=RUN_POLL_TIMEOUT)
+ text_email = str(result_email.get("result", "") or "")
+ log(f"[FALLBACK] Email entry complete: {text_email[:200]}")
+
+ # Check for captcha
+ if detect_captcha(result_email, client):
+ log("[CAPTCHA] Captcha detected during email entry")
+ callback_url, session_id = handle_captcha_checkpoint(
+ client, run_email_id, session_id, provider, email, port)
+ if callback_url:
+ return _complete_reauth(proc, callback_url, auth_file,
+ stale_backup, email, provider, meta)
+ return EXIT_CAPTCHA
+
+ # Check if verification code is needed
+ needs_code = any(kw in text_email.lower() for kw in
+ ["code", "link sent", "verify", "check your email",
+ "magic", "email has been sent"])
+
+ if not needs_code:
+ log("[FALLBACK] No verification needed — proceeding to Authorize")
+ else:
+ log(f"[FALLBACK] Verification needed — polling Gmail for {safe}")
+ time.sleep(5)
+
+ code = poll_gmail_for_code(provider, email, email, kind="code")
+ if code:
+ record_email_send()
+ log("[FALLBACK] Verification code received (value redacted)")
+
+ max_code_retries = 3
+ text2 = ""
+ for attempt in range(max_code_retries):
+ task_code = (
+ "Enter the verification code in the verification field. "
+ "Click Continue. Report what you see."
+ )
+ run_code = client.create_run(task_code, session_id=session_id)
+ run_code_id = run_code.get("id", "")
+ log(f"[FALLBACK] Code entry run (attempt {attempt+1}): {run_code_id[:12]}...")
+ result_code = client.wait_for_run(run_code_id, timeout=RUN_POLL_TIMEOUT)
+ text2 = str(result_code.get("result", "") or "")
+ log(f"[FALLBACK] Code entry complete: {text2[:200]}")
+ session_id = run_code.get("sessionId", session_id)
+
+ if detect_captcha(result_code, client):
+ log("[CAPTCHA] Captcha detected after code entry")
+ callback_url, session_id = handle_captcha_checkpoint(
+ client, run_code_id, session_id, provider, email, port)
+ if callback_url:
+ return _complete_reauth(proc, callback_url, auth_file,
+ stale_backup, email, provider, meta)
+ return EXIT_CAPTCHA
+
+ if _code_entry_failed(text2):
+ log(f"[FALLBACK] Code entry failed on attempt {attempt+1}")
+ if attempt < max_code_retries - 1:
+ enforce_email_cooldown()
+ code = poll_gmail_for_code(provider, email, email, kind="code")
+ if code:
+ record_email_send()
+ else:
+ log("[FAIL] No new verification code received")
+ return EXIT_FAILURE
+ else:
+ log("[FAIL] Code entry failed after all retries")
+ return EXIT_FAILURE
+ else:
+ break
+ else:
+ log("[FALLBACK] No verification code — trying magic link...")
+ magic = poll_gmail_for_code(provider, email, email, kind="magic")
+ if magic:
+ log("[FALLBACK] Magic link received (value redacted)")
+ task_magic = (
+ "Navigate to the magic link URL. "
+ "If you see an Authorize button, click it. "
+ "Report what you see."
+ )
+ run_magic = client.create_run(task_magic, session_id=session_id)
+ result_magic = client.wait_for_run(run_magic.get("id", ""),
+ timeout=RUN_POLL_TIMEOUT)
+ text_magic = str(result_magic.get("result", "") or "")
+ log(f"[FALLBACK] Magic link run complete: {text_magic[:200]}")
+ session_id = run_magic.get("sessionId", session_id)
+ else:
+ log("[FAIL] No verification code or magic link received")
+ return EXIT_FAILURE
+
+ # Click Authorize with JS interception
+ intercept_js = INTERCEPT_JS_TEMPLATE.format(port=port)
+ log(f"[FALLBACK] Injecting JS and clicking Authorize (port {port})")
+ task_auth = (
+ f"Run this JavaScript in the browser console: {intercept_js} "
+ f"Then if you see an Authorize or Allow button, click it. "
+ f"Wait 3 seconds. "
+ f"Then run window.__CB__ in the console and report its value. "
+ f"If null, report the current URL."
+ )
+ run_auth = client.create_run(task_auth, session_id=session_id)
+ run_auth_id = run_auth.get("id", "")
+ log(f"[FALLBACK] Authorize run: {run_auth_id[:12]}...")
+ result_auth = client.wait_for_run(run_auth_id, timeout=RUN_POLL_TIMEOUT)
+ text_auth = str(result_auth.get("result", "") or "")
+ log(f"[FALLBACK] Authorize run complete: {text_auth[:200]}")
+
+ if detect_captcha(result_auth, client):
+ log("[CAPTCHA] Captcha detected during Authorize")
+ callback_url, session_id = handle_captcha_checkpoint(
+ client, run_auth_id, session_id, provider, email, port)
+ if callback_url:
+ return _complete_reauth(proc, callback_url, auth_file,
+ stale_backup, email, provider, meta)
+ return EXIT_CAPTCHA
+
+ callback_url = extract_callback_url(result_auth, client, provider)
+
+ if not callback_url:
+ log("[FALLBACK] Callback not found — asking agent for window.__CB__")
+ task_cb = (
+ "Run window.__CB__ in the browser console and report the exact value. "
+ "If null, report the current page URL."
+ )
+ run_cb = client.create_run(task_cb, session_id=session_id)
+ result_cb = client.wait_for_run(run_cb.get("id", ""), timeout=60)
+ callback_url = extract_callback_url(result_cb, client, provider)
+
+ if not callback_url:
+ log("[FAIL] Could not extract callback URL from fallback flow")
+ return EXIT_FAILURE
+
+ log(f"[FALLBACK] Callback URL extracted: {sanitize_url(callback_url)}")
+ return _complete_reauth(proc, callback_url, auth_file,
+ stale_backup, email, provider, meta)
+
+
+def main():
+ global _log_file
+
+ parser = argparse.ArgumentParser(
+ description="Profile-based OAuth reauth for CRSProxy")
+ parser.add_argument("-provider", required=True,
+ choices=["claude", "xai", "codex"],
+ help="OAuth provider")
+ parser.add_argument("-email", required=True,
+ help="Account email")
+ parser.add_argument("-profile-id", required=True,
+ help="Browser Use Cloud profile ID")
+ parser.add_argument("-dry-run", action="store_true",
+ help="Validate setup without creating browser runs")
+ args = parser.parse_args()
+
+ log_path = LOG_DIR / f"bu_profile_reauth_{args.provider}_{args.email.split('@')[0]}.log"
+ _log_file = open(log_path, "a", encoding="utf-8")
+
+ # Set overall timeout
+ signal.signal(signal.SIGALRM, _timeout_handler)
+ signal.alarm(TOTAL_TIMEOUT)
+
+ # Get API key
+ api_key = os.environ.get("BROWSER_USE_API_KEY", "")
+ if not api_key:
+ env_path = Path("/opt/crsproxy/.env")
+ if env_path.exists():
+ for line in env_path.read_text().splitlines():
+ if line.startswith("BROWSER_USE_API_KEY="):
+ api_key = line.split("=", 1)[1].strip().strip('"').strip("'")
+ break
+
+ if not api_key:
+ log("[FATAL] BROWSER_USE_API_KEY not found")
+ return EXIT_FAILURE
+
+ log(f"=== Profile Reauth: {args.provider} / {safe_email(args.email)} ===")
+ log(f"=== Profile ID: {args.profile_id[:12]}... ===")
+
+ client = BrowserUseClient(api_key)
+ import atexit
+ atexit.register(client.stop_all_browsers)
+
+ try:
+ exit_code = run_profile_reauth(
+ provider=args.provider,
+ email=args.email,
+ profile_id=args.profile_id,
+ client=client,
+ dry_run=args.dry_run,
+ )
+ except TimeoutError as e:
+ log(f"[TIMEOUT] {e}")
+ exit_code = EXIT_FAILURE
+ except KeyboardInterrupt:
+ log("[INTERRUPTED] User interrupted")
+ exit_code = EXIT_FAILURE
+ finally:
+ signal.alarm(0)
+ client.stop_all_browsers()
+
+ log(f"=== Exit code: {exit_code} ===")
+ return exit_code
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/claude-ops/scripts/crsproxy-reauth/bu_reauth.py b/claude-ops/scripts/crsproxy-reauth/bu_reauth.py
index ee6b5197..5a274076 100644
--- a/claude-ops/scripts/crsproxy-reauth/bu_reauth.py
+++ b/claude-ops/scripts/crsproxy-reauth/bu_reauth.py
@@ -20,7 +20,7 @@ Features:
keeps browser session alive, waits for a file-based trigger, then
resumes with a follow-up run in the same session to click Authorize
and capture the callback URL.
- - Serialization lease: checks /opt/crsproxy/state/crsproxy-claude-oauth-lease.json
+ - Serialization lease: checks /tmp/crsproxy-claude-oauth-lease.json
before starting a login, waits if held, cleans up after completion.
- Email cooldown: tracks verification code send timestamps, enforces
a 5-minute cooldown if >3 codes are sent in 5 minutes, logs
@@ -61,7 +61,7 @@ Usage:
# When hCaptcha is detected, the script emits the live_view_url and waits.
# After solving the captcha in the browser, create the trigger file:
- # touch /opt/crsproxy/state/bu_reauth_checkpoint_trigger
+ # touch /tmp/bu_reauth_checkpoint_trigger
# The script will then create a follow-up run and complete the flow.
"""
@@ -88,9 +88,8 @@ AUTH_DIR = Path("/opt/crsproxy/auths")
HOME_DIR = Path("/opt/crsproxy")
BU_API_BASE = "https://api.browser-use.com/api/v4"
BU_MODEL = "gpt-5.6-luna"
-STATE_DIR = Path(os.environ.get("CRSPROXY_STATE_DIR", "/opt/crsproxy/state"))
-LOG_DIR = STATE_DIR
-LEASE_FILE = STATE_DIR / "crsproxy-claude-oauth-lease.json"
+LOG_DIR = Path("/tmp")
+LEASE_FILE = Path("/tmp/crsproxy-claude-oauth-lease.json")
CANARY_URL = "http://localhost:8319/v1/chat/completions"
# Exit codes
@@ -110,13 +109,13 @@ TOTAL_TIMEOUT = 300 # overall script timeout (5 minutes)
LEASE_STALE_SECONDS = 600 # lease older than 10 min is stale
# Human checkpoint (hCaptcha) configuration
-CHECKPOINT_FILE = STATE_DIR / "bu_reauth_checkpoint.json"
-CHECKPOINT_TRIGGER = STATE_DIR / "bu_reauth_checkpoint_trigger"
+CHECKPOINT_FILE = Path("/tmp/bu_reauth_checkpoint.json")
+CHECKPOINT_TRIGGER = Path("/tmp/bu_reauth_checkpoint_trigger")
CHECKPOINT_POLL_INTERVAL = 5 # seconds between trigger polls
CHECKPOINT_TIMEOUT = 600 # 10 minutes for human to solve captcha
# Email cooldown configuration
-EMAIL_COOLDOWN_FILE = STATE_DIR / "crsproxy-email-cooldown.json"
+EMAIL_COOLDOWN_FILE = Path("/tmp/crsproxy-email-cooldown.json")
EMAIL_COOLDOWN_WINDOW = 300 # 5 min sliding window for counting code sends
EMAIL_COOLDOWN_THRESHOLD = 3 # max 3 code sends before cooldown triggers
EMAIL_COOLDOWN_DURATION = 300 # 5 min cooldown when threshold exceeded
@@ -189,8 +188,8 @@ def log(msg: str):
try:
_log_file.write(line + "\n")
_log_file.flush()
- except Exception as e:
- print(f"[WARN] File logging failed: {e}", file=sys.stderr)
+ except Exception:
+ pass
def sanitize_url(raw: str) -> str:
@@ -204,8 +203,7 @@ def sanitize_url(raw: str) -> str:
if parsed.scheme in ("http", "https"):
return f"{parsed.scheme}://{parsed.netloc}{parsed.path}"
except Exception:
- # Invalid URLs are treated as unsafe to log.
- return ""
+ pass
return ""
@@ -318,7 +316,7 @@ def validate_candidate(auth_path: Path, expected_email: str,
# 401/403 = accounts in cooldown/auth issues — all indicate the
# proxy is alive. -1 = connection error — proxy is down.
if status < 0:
- return False, "canary request failed: proxy unreachable (connection error)"
+ return False, f"canary request failed: proxy unreachable (connection error)"
return True, "all checks passed"
@@ -335,16 +333,11 @@ def activate_auth_file(auth_path: Path) -> bool:
Returns True on success, False on failure.
"""
try:
- orig_mode = auth_path.stat().st_mode & 0o777
auth = json.loads(auth_path.read_text())
auth["disabled"] = False
tmp_path = auth_path.with_suffix(".tmp")
- with open(tmp_path, "w", encoding="utf-8") as fh:
- fh.write(json.dumps(auth, indent=2))
- fh.flush()
- os.fsync(fh.fileno())
- os.chmod(tmp_path, orig_mode)
- os.replace(tmp_path, auth_path)
+ tmp_path.write_text(json.dumps(auth, indent=2))
+ os.rename(str(tmp_path), str(auth_path))
log(f"Auth file activated: {auth_path.name}")
return True
except Exception as e:
@@ -359,16 +352,11 @@ def disable_auth_file(auth_path: Path) -> bool:
Returns True on success, False on failure.
"""
try:
- orig_mode = auth_path.stat().st_mode & 0o777
auth = json.loads(auth_path.read_text())
auth["disabled"] = True
tmp_path = auth_path.with_suffix(".tmp")
- with open(tmp_path, "w", encoding="utf-8") as fh:
- fh.write(json.dumps(auth, indent=2))
- fh.flush()
- os.fsync(fh.fileno())
- os.chmod(tmp_path, orig_mode)
- os.replace(tmp_path, auth_path)
+ tmp_path.write_text(json.dumps(auth, indent=2))
+ os.rename(str(tmp_path), str(auth_path))
log(f"Auth file disabled: {auth_path.name}")
return True
except Exception as e:
@@ -402,7 +390,7 @@ def acquire_lease(provider: str, max_wait: int = 120) -> bool:
first_check = False
if time.time() - t0 >= max_wait:
log(f"Lease wait timed out after {max_wait}s — "
- "another login may still be in progress")
+ f"another login may still be in progress")
return False
time.sleep(5)
continue
@@ -412,8 +400,8 @@ def acquire_lease(provider: str, max_wait: int = 120) -> bool:
except (json.JSONDecodeError, OSError):
try:
LEASE_FILE.unlink()
- except OSError as e:
- log(f"Stale lease cleanup error: {e}")
+ except OSError:
+ pass
# Lease file doesn't exist or was removed — acquire
break
@@ -443,8 +431,8 @@ def _load_cooldown_state() -> dict:
try:
if EMAIL_COOLDOWN_FILE.exists():
return json.loads(EMAIL_COOLDOWN_FILE.read_text())
- except Exception as e:
- log(f"Cooldown state load error: {e}")
+ except Exception:
+ pass
return {"send_timestamps": [], "cooldown_until": 0}
@@ -610,8 +598,8 @@ class BrowserUseClient:
try:
self._request("PATCH", f"/runs/{run_id}",
json={"action": "cancel"})
- except Exception as e:
- log(f"Run cancellation error: {e}")
+ except Exception:
+ pass
def stop_browser(self, browser_id: str):
"""Stop a browser session via PATCH /api/v4/browsers/{id}."""
@@ -652,8 +640,7 @@ class BrowserUseClient:
# Unknown shape — return empty rather than iterating dict keys
log(f"list_browsers: unexpected response shape: {type(data).__name__}")
return []
- except Exception as e:
- log(f"list_browsers error: {e}")
+ except Exception:
return []
def stop_all_browsers(self):
@@ -725,8 +712,8 @@ class BrowserUseClient:
ed = e.get("data", {})
if e.get("type") == "browser.ready":
return ed.get("live_view_url", "")
- except Exception as e:
- log(f"Live view extraction error: {e}")
+ except Exception:
+ pass
return ""
@@ -776,8 +763,7 @@ def get_oauth_url(log_path: Path, provider: str,
matches = re.findall(pattern, text)
if matches:
return matches[-1]
- except (OSError, UnicodeDecodeError):
- # The login log may not exist yet or may be mid-write; retry.
+ except Exception:
pass
time.sleep(1)
return None
@@ -816,8 +802,8 @@ def cleanup_login_process(proc: subprocess.Popen):
if master_fd is not None:
try:
os.close(master_fd)
- except OSError as e:
- log(f"PTY cleanup error: {e}")
+ except OSError:
+ pass
# ---------------------------------------------------------------------------
@@ -905,6 +891,8 @@ def _code_entry_failed(result_text: str) -> bool:
"code is wrong", "enter the code again",
"unable to verify", "verification failed",
"code didn't match", "that code isn't",
+ "error verifying", "there was an error",
+ "error verifying your code",
]
return any(ind in text_lower for ind in failure_indicators)
@@ -914,28 +902,27 @@ def detect_captcha(run_result: dict, client: BrowserUseClient) -> bool:
result_text = str(run_result.get("result", "") or "").lower()
# "captcha" in a sentence is not enough — look for specific indicators
captcha_indicators = [
+ # These indicators are checked ONLY against the agent's result
+ # text (the agent's description of what it sees), NOT against
+ # raw HTML events. The events-based check was removed because
+ # the invisible hCaptcha widget (hcaptcha-invisible) is present
+ # in page HTML even when no visible challenge exists.
"hcaptcha", "h-captcha", "newassets.hcaptcha.com",
"captcha challenge", "solve the captcha",
"i see a captcha", "captcha widget",
"are you human", "verify you are human",
+ "captcha", "image-selection",
]
for indicator in captcha_indicators:
if indicator in result_text:
return True
- # Check events for captcha-related types
- run_id = run_result.get("id", "")
- if run_id:
- try:
- events = client.get_run_events(run_id, limit=100)
- for e in events:
- ed = e.get("data", {})
- event_str = json.dumps(ed).lower()
- for indicator in captcha_indicators:
- if indicator in event_str:
- return True
- except Exception as e:
- log(f"Captcha event inspection error: {e}")
+ # NOTE: We do NOT check raw HTML events for captcha indicators.
+ # The hCaptcha widget (including invisible mode) embeds iframe URLs
+ # and attributes containing "newassets.hcaptcha.com" etc. in page HTML,
+ # which causes false positives on Claude's login page. The agent's
+ # result text is sufficient — if the agent sees a visible captcha
+ # challenge, it will report it (e.g., "I see a captcha").
return False
@@ -943,20 +930,15 @@ def detect_captcha(run_result: dict, client: BrowserUseClient) -> bool:
# ---------------------------------------------------------------------------
# Human captcha checkpoint
# ---------------------------------------------------------------------------
-def _ensure_state_dir(path: Path | None = None):
- """Create the private runtime state directory."""
- state_dir = path or STATE_DIR
- state_dir.mkdir(mode=0o700, parents=True, exist_ok=True)
- os.chmod(state_dir, 0o700)
-
-
def write_checkpoint(session_id: str, run_id: str, browser_session_id: str,
live_view_url: str, provider: str, email: str,
callback_port: int):
"""Write checkpoint state to a file for human captcha solving.
- The private checkpoint file contains the full live_view_url, session_id,
- and reauth metadata needed for the follow-up run.
+ The checkpoint file contains the full live_view_url (needed by Sam to
+ access the browser), the session_id (for follow-up runs), and metadata
+ about the reauth attempt. The file is world-readable so the
+ orchestrator can read it without sudo.
"""
data = {
"session_id": session_id,
@@ -970,9 +952,7 @@ def write_checkpoint(session_id: str, run_id: str, browser_session_id: str,
"timestamp": time.time(),
}
try:
- _ensure_state_dir(CHECKPOINT_FILE.parent)
CHECKPOINT_FILE.write_text(json.dumps(data, indent=2))
- os.chmod(CHECKPOINT_FILE, 0o600)
log(f"[CAPTCHA_CHECKPOINT] Checkpoint file: {CHECKPOINT_FILE}")
except Exception as e:
log(f"[CAPTCHA_CHECKPOINT] Could not write checkpoint file: {e}")
@@ -983,7 +963,7 @@ def wait_for_checkpoint_signal(timeout: int = CHECKPOINT_TIMEOUT) -> bool:
Polls for the existence of CHECKPOINT_TRIGGER every CHECKPOINT_POLL_INTERVAL
seconds. Returns True if the trigger appears, False on timeout.
- The trigger file can be created in the private state directory.
+ The trigger file can be created by: touch /tmp/bu_reauth_checkpoint_trigger
"""
log(f"[CAPTCHA_CHECKPOINT] Waiting up to {timeout}s for trigger: {CHECKPOINT_TRIGGER}")
t0 = time.time()
@@ -992,8 +972,8 @@ def wait_for_checkpoint_signal(timeout: int = CHECKPOINT_TIMEOUT) -> bool:
log("[CAPTCHA_CHECKPOINT] Trigger file detected")
try:
CHECKPOINT_TRIGGER.unlink()
- except OSError as e:
- log(f"Checkpoint trigger cleanup error: {e}")
+ except OSError:
+ pass
return True
time.sleep(CHECKPOINT_POLL_INTERVAL)
return False
@@ -1004,8 +984,8 @@ def clear_checkpoint():
for f in (CHECKPOINT_FILE, CHECKPOINT_TRIGGER):
try:
f.unlink(missing_ok=True)
- except OSError as e:
- log(f"Checkpoint cleanup error for {f}: {e}")
+ except OSError:
+ pass
def handle_captcha_checkpoint(client: BrowserUseClient, run_id: str,
@@ -1051,7 +1031,8 @@ def handle_captcha_checkpoint(client: BrowserUseClient, run_id: str,
# browser session. It is NOT an OAuth URL and does not contain OAuth
# tokens or callback codes. Emit the full URL so Sam can open it.
# Also emit a sanitized version for log monitoring.
- log(f"[CAPTCHA_CHECKPOINT] Live view: {sanitize_url(live_url)}")
+ log(f"[CAPTCHA_CHECKPOINT] live_view_url={live_url}")
+ log(f"[CAPTCHA_CHECKPOINT] Sanitized: {sanitize_url(live_url)}")
log(f"[CAPTCHA_CHECKPOINT] Provider: {provider}, Email: {safe}")
log("[CAPTCHA_CHECKPOINT] Browser session kept alive — do NOT stop")
log(f"[CAPTCHA_CHECKPOINT] To resume after solving: touch {CHECKPOINT_TRIGGER}")
@@ -1081,13 +1062,13 @@ def handle_captcha_checkpoint(client: BrowserUseClient, run_id: str,
# --- 6. Create follow-up run in the same session ---
intercept_js = INTERCEPT_JS_TEMPLATE.format(port=port)
task_resume = (
- f"The captcha has been solved by a human. Navigate to {oauth_url}. "
+ f"The captcha has been solved by a human. "
f"Run this JavaScript in the browser console: {intercept_js} "
- "Then if you see an Authorize or Allow button, click it. "
- "Wait 3 seconds. "
- "Then run window.__CB__ in the console and report its value. "
- "If null, report the current URL. "
- "If you see another captcha, report CAPTCHA."
+ f"Then if you see an Authorize or Allow button, click it. "
+ f"Wait 3 seconds. "
+ f"Then run window.__CB__ in the console and report its value. "
+ f"If null, report the current URL. "
+ f"If you see another captcha, report CAPTCHA."
)
run_resume = client.create_run(task_resume, session_id=session_id)
run_resume_id = run_resume.get("id", "")
@@ -1189,8 +1170,8 @@ def _complete_reauth(proc: subprocess.Popen, callback_url: str,
log("[9] No stale backup — removing failed candidate")
try:
auth_file.unlink()
- except Exception as e:
- log(f"Failed candidate cleanup error: {e}")
+ except Exception:
+ pass
return EXIT_FAILURE
log(f"[9] Candidate validation passed: {reason}")
@@ -1202,8 +1183,8 @@ def _complete_reauth(proc: subprocess.Popen, callback_url: str,
# Clean up stale backup on success
try:
stale_backup.unlink(missing_ok=True)
- except Exception as e:
- log(f"Stale backup cleanup error: {e}")
+ except Exception:
+ pass
# Clear email cooldown state on successful completion
clear_email_cooldown()
@@ -1218,7 +1199,9 @@ def run_reauth(provider: str, email: str, gog_account: str,
Returns exit code: 0=success, 1=failure, 2=captcha.
"""
- log_path = LOG_DIR / "bu_reauth_hub.log"
+ meta = PROVIDERS[provider]
+ port = meta["callback_port"]
+ log_path = Path("/tmp/bu_reauth_hub.log")
log_path.unlink(missing_ok=True)
# --- Step 1: Acquire serialization lease ---
@@ -1268,7 +1251,7 @@ def _run_reauth_inner(provider: str, email: str, gog_account: str,
except Exception as e:
log(f"[1] Stale auth backup warning: {e}")
else:
- log("[1] No existing auth file — no stale backup needed")
+ log(f"[1] No existing auth file — no stale backup needed")
# --- Step 2: Start cli-proxy-api login ---
log(f"[1] Starting cli-proxy-api login for {provider} ({safe})")
@@ -1288,8 +1271,8 @@ def _run_reauth_inner(provider: str, email: str, gog_account: str,
task1 = (
f"Go to {oauth_url}. "
f"Enter {email} in the email field. "
- "Click Continue. Do NOT use Google sign-in. "
- "Report exactly what you see after clicking Continue."
+ f"Click Continue. Do NOT use Google sign-in. "
+ f"Report exactly what you see after clicking Continue."
)
run1 = client.create_run(task1)
run1_id = run1.get("id", "")
@@ -1307,7 +1290,7 @@ def _run_reauth_inner(provider: str, email: str, gog_account: str,
if callback_url:
return _complete_reauth(proc, callback_url, auth_file,
stale_backup, email, provider, meta)
- return EXIT_CAPTCHA
+ return EXIT_FAILURE
# --- Step 5: Handle verification code or magic link ---
needs_code = any(kw in text1.lower() for kw in
@@ -1317,7 +1300,7 @@ def _run_reauth_inner(provider: str, email: str, gog_account: str,
log("[4] No verification code/magic link needed — proceeding to Authorize")
else:
log(f"[4] Verification needed — polling Gmail for {safe}")
- time.sleep(5) # Give Gmail time to receive the email
+ time.sleep(15) # Give Gmail time to receive the email
# Try code first, then magic link
code = poll_gmail_for_code(provider, email, gog_account, kind="code")
@@ -1333,8 +1316,8 @@ def _run_reauth_inner(provider: str, email: str, gog_account: str,
text2 = ""
for attempt in range(max_code_retries):
task2 = (
- "Enter the verification code in the verification field. "
- "Click Continue. Report what you see."
+ f"Enter the verification code {code} in the verification field. "
+ f"Click Continue. Report what you see."
)
run2 = client.create_run(task2, session_id=session_id)
run2_id = run2.get("id", "")
@@ -1355,7 +1338,7 @@ def _run_reauth_inner(provider: str, email: str, gog_account: str,
return _complete_reauth(proc, callback_url,
auth_file, stale_backup,
email, provider, meta)
- return EXIT_CAPTCHA
+ return EXIT_FAILURE
# Check if code entry failed
if _code_entry_failed(text2):
@@ -1375,8 +1358,9 @@ def _run_reauth_inner(provider: str, email: str, gog_account: str,
log("[FAIL] No new verification code received")
return EXIT_FAILURE
else:
- log("[FAIL] Code entry failed after all retries")
- return EXIT_FAILURE
+ log("[FAIL] Code entry failed after all retries — trying magic link")
+ # Fall through to magic link flow instead of exiting
+ text2 = "link sent" # Trigger magic link check
else:
break # Code entry succeeded
@@ -1390,9 +1374,9 @@ def _run_reauth_inner(provider: str, email: str, gog_account: str,
if magic:
log("[5] Magic link received (value redacted)")
task3 = (
- "Navigate to the magic link URL. "
- "If you see an Authorize button, click it. "
- "Report what you see."
+ f"Navigate to the magic link URL: {magic}. "
+ f"If you see an Authorize button, click it. "
+ f"Report what you see."
)
run3 = client.create_run(task3, session_id=session_id)
run3_id = run3.get("id", "")
@@ -1402,6 +1386,16 @@ def _run_reauth_inner(provider: str, email: str, gog_account: str,
text3 = str(result3.get("result", "") or "")
log(f"[5] Magic link run complete: {text3[:200]}")
session_id = run3.get("sessionId", session_id)
+
+ # Check for captcha after magic link navigation
+ if detect_captcha(result3, client):
+ log("[CAPTCHA] Captcha detected after magic link")
+ callback_url, session_id = handle_captcha_checkpoint(
+ client, run3_id, session_id, provider, email, port)
+ if callback_url:
+ return _complete_reauth(proc, callback_url, auth_file,
+ stale_backup, email, provider, meta)
+ return EXIT_CAPTCHA
else:
log("[5] No magic link found — cannot continue")
return EXIT_FAILURE
@@ -1412,9 +1406,9 @@ def _run_reauth_inner(provider: str, email: str, gog_account: str,
if magic:
log("[4] Magic link received (value redacted)")
task2 = (
- "Navigate to the magic link URL. "
- "If you see an Authorize button, click it. "
- "Report what you see."
+ f"Navigate to the magic link URL: {magic}. "
+ f"If you see an Authorize button, click it. "
+ f"Report what you see."
)
run2 = client.create_run(task2, session_id=session_id)
run2_id = run2.get("id", "")
@@ -1423,6 +1417,16 @@ def _run_reauth_inner(provider: str, email: str, gog_account: str,
text2 = str(result2.get("result", "") or "")
log(f"[4] Magic link run complete: {text2[:200]}")
session_id = run2.get("sessionId", session_id)
+
+ # Check for captcha after magic link navigation
+ if detect_captcha(result2, client):
+ log("[CAPTCHA] Captcha detected after magic link")
+ callback_url, session_id = handle_captcha_checkpoint(
+ client, run2_id, session_id, provider, email, port)
+ if callback_url:
+ return _complete_reauth(proc, callback_url, auth_file,
+ stale_backup, email, provider, meta)
+ return EXIT_CAPTCHA
else:
log("[FAIL] No verification code or magic link received")
return EXIT_FAILURE
@@ -1432,10 +1436,10 @@ def _run_reauth_inner(provider: str, email: str, gog_account: str,
log(f"[6] Injecting JS callback interception (port {port}) and clicking Authorize")
task_auth = (
f"Run this JavaScript in the browser console: {intercept_js} "
- "Then if you see an Authorize or Allow button, click it. "
- "Wait 3 seconds. "
- "Then run window.__CB__ in the console and report its value. "
- "If null, report the current URL."
+ f"Then if you see an Authorize or Allow button, click it. "
+ f"Wait 3 seconds. "
+ f"Then run window.__CB__ in the console and report its value. "
+ f"If null, report the current URL."
)
run_auth = client.create_run(task_auth, session_id=session_id)
run_auth_id = run_auth.get("id", "")
@@ -1452,7 +1456,7 @@ def _run_reauth_inner(provider: str, email: str, gog_account: str,
if callback_url:
return _complete_reauth(proc, callback_url, auth_file,
stale_backup, email, provider, meta)
- return EXIT_CAPTCHA
+ return EXIT_FAILURE
# --- Step 7: Extract callback URL ---
log("[7] Searching for callback URL...")
@@ -1509,7 +1513,7 @@ def _timeout_handler(signum, frame):
def _checkpoint_resume(args) -> int:
"""Resume from a hCaptcha checkpoint file.
- Reads /opt/crsproxy/state/bu_reauth_checkpoint.json for the session_id, provider,
+ Reads /tmp/bu_reauth_checkpoint.json for the session_id, provider,
email, and callback_port. Starts a new cli-proxy-api login process
(the original one is likely dead), creates a follow-up run in the
same Browser Use session to click Authorize, extracts the callback
@@ -1566,7 +1570,7 @@ def _checkpoint_resume(args) -> int:
return EXIT_FAILURE
# Start a new login process (the original is likely dead)
- log_path = LOG_DIR / "bu_reauth_hub.log"
+ log_path = Path("/tmp/bu_reauth_hub.log")
log("[1] Starting new cli-proxy-api login for checkpoint resume")
proc = start_login(provider, log_path)
@@ -1595,13 +1599,13 @@ def _checkpoint_resume(args) -> int:
intercept_js = INTERCEPT_JS_TEMPLATE.format(port=port)
log("[3] Creating follow-up run in same session")
task_resume = (
- f"The captcha has been solved by a human. Navigate to {oauth_url}. "
+ f"The captcha has been solved by a human. "
f"Run this JavaScript in the browser console: {intercept_js} "
- "Then if you see an Authorize or Allow button, click it. "
- "Wait 3 seconds. "
- "Then run window.__CB__ in the console and report its value. "
- "If null, report the current URL. "
- "If you see another captcha, report CAPTCHA."
+ f"Then if you see an Authorize or Allow button, click it. "
+ f"Wait 3 seconds. "
+ f"Then run window.__CB__ in the console and report its value. "
+ f"If null, report the current URL. "
+ f"If you see another captcha, report CAPTCHA."
)
run_resume = client.create_run(task_resume, session_id=session_id)
run_resume_id = run_resume.get("id", "")
@@ -1614,7 +1618,7 @@ def _checkpoint_resume(args) -> int:
if detect_captcha(result_resume, client):
log("[FAIL] Captcha still present — cannot complete")
- return EXIT_CAPTCHA
+ return EXIT_FAILURE
callback_url = extract_callback_url(result_resume, client, provider)
@@ -1682,113 +1686,110 @@ def main():
parser.add_argument("-checkpoint-resume", action="store_true",
help="Resume from a checkpoint file written by a "
"previous run that detected hCaptcha. Reads "
- "/opt/crsproxy/state/bu_reauth_checkpoint.json for the "
+ "/tmp/bu_reauth_checkpoint.json for the "
"session_id, then creates a follow-up run to "
"click Authorize and complete the flow.")
parser.add_argument("-log-file", default="",
- help="Log file path (default: /opt/crsproxy/state/bu_reauth.log)")
+ help="Log file path (default: /tmp/bu_reauth.log)")
args = parser.parse_args()
- _ensure_state_dir()
-
# Set up log file
log_path = args.log_file or str(LOG_DIR / "bu_reauth.log")
- with open(log_path, "a", encoding="utf-8") as log_file:
- _log_file = log_file
+ _log_file = open(log_path, "a", encoding="utf-8")
- # Override checkpoint timeout if specified
- if args.checkpoint_timeout > 0:
- global CHECKPOINT_TIMEOUT
- CHECKPOINT_TIMEOUT = args.checkpoint_timeout
+ # --- Validate-only mode: standalone candidate validation ---
+ if args.validate_only:
+ meta = PROVIDERS[args.provider]
+ auth_file = AUTH_DIR / f"{meta['auth_file_prefix']}-{args.email}.json"
+ log(f"=== Validate-only: {args.provider} / {safe_email(args.email)} ===")
+ log(f"Auth file: {auth_file.name}")
- # Set overall timeout before all execution modes.
- signal.signal(signal.SIGALRM, _timeout_handler)
- signal.alarm(TOTAL_TIMEOUT)
+ if not auth_file.exists():
+ log(f"[FAIL] Auth file not found: {auth_file.name}")
+ return EXIT_FAILURE
- # --- Validate-only mode: standalone candidate validation ---
- if args.validate_only:
- meta = PROVIDERS[args.provider]
- auth_file = AUTH_DIR / f"{meta['auth_file_prefix']}-{args.email}.json"
- log(f"=== Validate-only: {args.provider} / {safe_email(args.email)} ===")
- log(f"Auth file: {auth_file.name}")
+ valid, reason = validate_candidate(
+ auth_path=auth_file,
+ expected_email=args.email,
+ expected_type=args.provider,
+ canary_model=meta.get("canary_model", ""),
+ skip_canary=args.skip_canary,
+ )
- if not auth_file.exists():
- log(f"[FAIL] Auth file not found: {auth_file.name}")
- return EXIT_FAILURE
+ if not valid:
+ log(f"[FAIL] Validation failed: {reason}")
+ return EXIT_FAILURE
- valid, reason = validate_candidate(
- auth_path=auth_file,
- expected_email=args.email,
- expected_type=args.provider,
- canary_model=meta.get("canary_model", ""),
- skip_canary=args.skip_canary,
- )
+ log(f"[OK] Validation passed: {reason}")
- if not valid:
- log(f"[FAIL] Validation failed: {reason}")
+ if args.activate:
+ log("Atomically activating auth file...")
+ if not activate_auth_file(auth_file):
+ log("[FAIL] Could not activate auth file")
return EXIT_FAILURE
+ log(f"[OK] Auth file activated: {auth_file.name}")
- log(f"[OK] Validation passed: {reason}")
+ return EXIT_SUCCESS
- if args.activate:
- log("Atomically activating auth file...")
- if not activate_auth_file(auth_file):
- log("[FAIL] Could not activate auth file")
- return EXIT_FAILURE
- log(f"[OK] Auth file activated: {auth_file.name}")
-
- return EXIT_SUCCESS
-
- # --- Checkpoint-resume mode: resume from a hCaptcha checkpoint ---
- if args.checkpoint_resume:
- return _checkpoint_resume(args)
-
- # Load API key from environment
- api_key = os.environ.get("BROWSER_USE_API_KEY", "")
- if not api_key:
- # Try loading from .env
- env_path = Path("/opt/crsproxy/.env")
- if env_path.exists():
- for line in env_path.read_text().splitlines():
- if line.startswith("BROWSER_USE_API_KEY="):
- api_key = line.split("=", 1)[1].strip().strip('"').strip("'")
- break
-
- if not api_key:
- log("[FATAL] BROWSER_USE_API_KEY not found in environment or .env")
- return EXIT_FAILURE
+ # --- Checkpoint-resume mode: resume from a hCaptcha checkpoint ---
+ if args.checkpoint_resume:
+ return _checkpoint_resume(args)
- if args.dry_run:
- log(f"=== DRY RUN: {args.provider} / {safe_email(args.email)} ===")
- else:
- log(f"=== Reauth: {args.provider} / {safe_email(args.email)} ===")
+ # Set overall timeout
+ signal.signal(signal.SIGALRM, _timeout_handler)
+ signal.alarm(TOTAL_TIMEOUT)
+
+ # Override checkpoint timeout if specified
+ if args.checkpoint_timeout > 0:
+ global CHECKPOINT_TIMEOUT
+ CHECKPOINT_TIMEOUT = args.checkpoint_timeout
+
+ # Load API key from environment
+ api_key = os.environ.get("BROWSER_USE_API_KEY", "")
+ if not api_key:
+ # Try loading from .env
+ env_path = Path("/opt/crsproxy/.env")
+ if env_path.exists():
+ for line in env_path.read_text().splitlines():
+ if line.startswith("BROWSER_USE_API_KEY="):
+ api_key = line.split("=", 1)[1].strip().strip('"').strip("'")
+ break
- client = BrowserUseClient(api_key)
+ if not api_key:
+ log("[FATAL] BROWSER_USE_API_KEY not found in environment or .env")
+ return EXIT_FAILURE
- # Register cleanup to stop all browsers on any exit
- atexit.register(client.stop_all_browsers)
+ if args.dry_run:
+ log(f"=== DRY RUN: {args.provider} / {safe_email(args.email)} ===")
+ else:
+ log(f"=== Reauth: {args.provider} / {safe_email(args.email)} ===")
- try:
- exit_code = run_reauth(
- provider=args.provider,
- email=args.email,
- gog_account=args.gog_account,
- client=client,
- dry_run=args.dry_run,
- )
- except TimeoutError as e:
- log(f"[TIMEOUT] {e}")
- exit_code = EXIT_FAILURE
- except KeyboardInterrupt:
- log("[INTERRUPTED] User interrupted")
- exit_code = EXIT_FAILURE
- finally:
- signal.alarm(0) # Cancel the alarm
- # Ensure all browser sessions are stopped
- client.stop_all_browsers()
+ client = BrowserUseClient(api_key)
+
+ # Register cleanup to stop all browsers on any exit
+ atexit.register(client.stop_all_browsers)
+
+ try:
+ exit_code = run_reauth(
+ provider=args.provider,
+ email=args.email,
+ gog_account=args.gog_account,
+ client=client,
+ dry_run=args.dry_run,
+ )
+ except TimeoutError as e:
+ log(f"[TIMEOUT] {e}")
+ exit_code = EXIT_FAILURE
+ except KeyboardInterrupt:
+ log("[INTERRUPTED] User interrupted")
+ exit_code = EXIT_FAILURE
+ finally:
+ signal.alarm(0) # Cancel the alarm
+ # Ensure all browser sessions are stopped
+ client.stop_all_browsers()
- log(f"=== Exit code: {exit_code} ===")
- return exit_code
+ log(f"=== Exit code: {exit_code} ===")
+ return exit_code
if __name__ == "__main__":
diff --git a/claude-ops/scripts/crsproxy-reauth/bu_reauth_wrapper.py b/claude-ops/scripts/crsproxy-reauth/bu_reauth_wrapper.py
new file mode 100644
index 00000000..9ebf0b68
--- /dev/null
+++ b/claude-ops/scripts/crsproxy-reauth/bu_reauth_wrapper.py
@@ -0,0 +1,8 @@
+#!/usr/bin/env python3
+"""Wrapper for bu_reauth.py with increased timeout for slow OAuth flows."""
+import sys
+sys.path.insert(0, "/opt/crsproxy")
+import bu_reauth
+# Increase total timeout from 300 to 600 seconds
+bu_reauth.TOTAL_TIMEOUT = 600
+bu_reauth.main()