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.
Shell agent-teamsai-agentai-automationanthropicautonomous-agentawsbusiness-operationsclaudeclaude-codeclaude-code-plugindevopsecommerceinbox-zeroklaviyomarketingmcpmorning-briefingpr-automationshopifyterminal
fix(crsproxy): fix Gmail polling and add direct reauth for aurora
auroracapital04c4c856c4eb
diff --git a/scripts/crsproxy-reauth/aurora_reauth_direct.py b/scripts/crsproxy-reauth/aurora_reauth_direct.pynew file mode 100644index 00000000..c4a44553--- /dev/null+++ b/scripts/crsproxy-reauth/aurora_reauth_direct.py@@ -0,0 +1,401 @@+#!/usr/bin/env python3+"""Direct OAuth reauth for Aurora using BU Cloud API.++This script bypasses the bu_profile_reauth.py fallback flow and directly+controls the BU Cloud API to:+1. Start cli-proxy-api login (captures OAuth URL)+2. Create a BU Cloud run with the profile that enters the email+3. Poll Gmail for the magic link+4. Create a follow-up run that navigates to the magic link+5. Create another run that injects JS interception and clicks Authorize via JS+6. Capture the callback URL+7. Paste it into cli-proxy-api PTY+8. Validate and activate++Usage:+ sudo -u crsproxy bash -c "cd /opt/crsproxy && set -a && source /opt/crsproxy/.env && source /opt/crsproxy/.gog-keyring-env && set +a && /opt/crsproxy/venv/bin/python /tmp/aurora_reauth_direct.py"+"""++import json+import os+import re+import signal+import subprocess+import sys+import time+from pathlib import Path++sys.path.insert(0, "/opt/crsproxy")+from bu_reauth import (+ BrowserUseClient,+ PROVIDERS,+ acquire_lease,+ release_lease,+ start_login,+ get_oauth_url,+ paste_callback_url,+ cleanup_login_process,+ validate_candidate,+ activate_auth_file,+ sanitize_url,+ safe_email,+ log,+ AUTH_DIR,+ EXIT_SUCCESS,+ EXIT_FAILURE,+ EXIT_CAPTCHA,+ _timeout_handler,+ _complete_reauth,+)+from bu_reauth import poll_gmail_for_code++PROVIDER = "claude"+EMAIL = "[email protected]"+PROFILE_ID = "8774a731-f0e0-4506-971a-885a00f7dabc"+GOG_ACCOUNT = os.environ.get("GMAIL_TARGET_INBOX", "<PERSONAL_EMAIL>")+PORT = 54545++# JS to intercept the callback URL - uses multiple methods+INTERCEPT_JS = """+window.__CB__ = null;+// Method 1: Navigation API+if (window.navigation) {+ window.navigation.addEventListener('navigate', function(e) {+ var u = e.destination.url;+ if (u.includes('localhost:%PORT%') || u.includes('callback')) {+ window.__CB__ = u;+ e.preventDefault();+ }+ });+}+// Method 2: Override location methods+var origAssign = window.location.assign.bind(window.location);+window.location.assign = function(url) {+ if (String(url).includes('localhost:%PORT%') || String(url).includes('callback')) {+ window.__CB__ = String(url);+ return;+ }+ return origAssign(url);+};+var origReplace = window.location.replace.bind(window.location);+window.location.replace = function(url) {+ if (String(url).includes('localhost:%PORT%') || String(url).includes('callback')) {+ window.__CB__ = String(url);+ return;+ }+ return origReplace(url);+};+// Method 3: beforeunload+window.addEventListener('beforeunload', function(e) {+ if (window.location.href.includes('callback') || window.location.href.includes('localhost:%PORT%')) {+ window.__CB__ = window.location.href;+ }+});+// Method 4: Intercept form submissions+document.addEventListener('submit', function(e) {+ var form = e.target;+ if (form.action && (String(form.action).includes('localhost:%PORT%') || String(form.action).includes('callback'))) {+ window.__CB__ = String(form.action);+ }+}, true);+""".replace("%PORT%", str(PORT))++# JS to find and click the Authorize button+CLICK_AUTHORIZE_JS = """+var btn = null;+var buttons = document.querySelectorAll('button, input[type=submit], a[role=button]');+for (var i = 0; i < buttons.length; i++) {+ var text = (buttons[i].textContent || buttons[i].value || '').trim();+ if (text.match(/authorize|allow|continue|grant/i)) {+ btn = buttons[i];+ break;+ }+}+if (btn) {+ btn.click();+ 'CLICKED: ' + btn.textContent.trim();+} else {+ 'NO_BUTTON_FOUND';+}+"""+++def run_reauth():+ """Run the direct reauth flow."""+ safe = safe_email(EMAIL)+ log_path = Path("/tmp/aurora_reauth_direct.log")+ log_path.unlink(missing_ok=True)++ # 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++ client = BrowserUseClient(api_key)+ import atexit+ atexit.register(client.stop_all_browsers)++ # Acquire lease+ if not acquire_lease(PROVIDER):+ log("Could not acquire lease")+ return EXIT_FAILURE++ auth_file = AUTH_DIR / f"claude-{EMAIL}.json"+ stale_backup = auth_file.with_suffix(".stale")+ if auth_file.exists():+ stale_backup.unlink(missing_ok=True)+ stale_backup.write_text(auth_file.read_text())+ log(f"Stale auth backed up: {auth_file.name}")++ try:+ return _run_reauth_inner(client, auth_file, stale_backup, log_path)+ finally:+ release_lease()+ client.stop_all_browsers()+++def _run_reauth_inner(client, auth_file, stale_backup, log_path):+ """Inner reauth flow - assumes lease is held."""+ safe = safe_email(EMAIL)+ meta = PROVIDERS[PROVIDER]++ # Step 1: Start cli-proxy-api login+ log("[1] Starting cli-proxy-api login")+ proc = start_login(PROVIDER, log_path)++ try:+ # Step 2: 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")+ return EXIT_FAILURE+ log(f"[2] OAuth URL captured: {sanitize_url(oauth_url)}")++ # Step 3: Create BU Cloud run - enter email+ log("[3] Creating BU Cloud run to enter email")+ task_enter = (+ f"Navigate to this URL: {oauth_url}\n"+ f"If you see an email field, enter {EMAIL} and click Continue. "+ f"Do NOT use Google sign-in. "+ f"Report exactly what you see after clicking Continue."+ )+ run = client.create_run(task_enter, 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=120)+ text = str(result.get("result", "") or "")+ log(f"[3] Run complete: {text[:300]}")+ session_id = result.get("sessionId", session_id)++ # Check if already authenticated (Authorize button visible)+ if "authorize" in text.lower() or "allow" in text.lower():+ log("[3] Already authenticated - proceeding to Authorize")+ callback_url = _click_authorize(client, session_id)+ if callback_url:+ return _complete_reauth(proc, callback_url, auth_file,+ stale_backup, EMAIL, PROVIDER, meta)+ log("[FAIL] Could not capture callback after direct Authorize")+ return EXIT_FAILURE++ # Step 4: Poll Gmail for magic link+ log(f"[4] Polling Gmail for magic link ({safe})")+ time.sleep(5)+ magic = poll_gmail_for_code(PROVIDER, EMAIL, GOG_ACCOUNT, kind="magic")+ if not magic:+ log("[4] No magic link - trying verification code")+ code = poll_gmail_for_code(PROVIDER, EMAIL, GOG_ACCOUNT, kind="code")+ if code:+ log("[4] Verification code found - entering code")+ task_code = (+ f"Click 'Enter verification code' if you see that option. "+ f"Then enter the verification code {code} in the verification field. "+ f"Click Continue. Report what you see."+ )+ run_code = client.create_run(task_code, session_id=session_id)+ result_code = client.wait_for_run(run_code.get("id", ""), timeout=120)+ text_code = str(result_code.get("result", "") or "")+ log(f"[4] Code entry complete: {text_code[:200]}")+ session_id = result_code.get("sessionId", session_id)++ # Check if we need to click Authorize+ if "authorize" in text_code.lower() or "allow" in text_code.lower():+ callback_url = _click_authorize(client, session_id)+ if callback_url:+ return _complete_reauth(proc, callback_url, auth_file,+ stale_backup, EMAIL, PROVIDER, meta)+ # Try to extract callback from the result+ callback_url = _extract_callback(text_code)+ if callback_url:+ return _complete_reauth(proc, callback_url, auth_file,+ stale_backup, EMAIL, PROVIDER, meta)+ log("[FAIL] No magic link or verification code received")+ return EXIT_FAILURE++ log("[4] Magic link received (value redacted)")++ # Step 5: Navigate to magic link (DON'T click Authorize yet)+ log("[5] Navigating to magic link")+ task_magic = (+ f"Navigate to this URL: {magic}\n"+ f"Wait for the page to fully load. "+ f"Report the current page URL and what you see on the page. "+ f"Do NOT click any buttons yet."+ )+ run_magic = client.create_run(task_magic, session_id=session_id)+ result_magic = client.wait_for_run(run_magic.get("id", ""), timeout=120)+ text_magic = str(result_magic.get("result", "") or "")+ log(f"[5] Magic link navigation complete: {text_magic[:200]}")+ session_id = result_magic.get("sessionId", session_id)++ # Check if we're on the OAuth authorize page+ if "authorize" not in text_magic.lower() and "allow" not in text_magic.lower():+ # Maybe we need to navigate to the OAuth URL+ log("[5] Not on authorize page - navigating to OAuth URL")+ task_oauth = (+ f"Navigate to this URL: {oauth_url}\n"+ f"Report what you see. Do NOT click any buttons yet."+ )+ run_oauth = client.create_run(task_oauth, session_id=session_id)+ result_oauth = client.wait_for_run(run_oauth.get("id", ""), timeout=120)+ text_oauth = str(result_oauth.get("result", "") or "")+ log(f"[5] OAuth navigation complete: {text_oauth[:200]}")+ session_id = result_oauth.get("sessionId", session_id)++ # Step 6: Inject JS and click Authorize+ callback_url = _click_authorize(client, session_id)+ if callback_url:+ log(f"[6] Callback URL captured: {sanitize_url(callback_url)}")+ return _complete_reauth(proc, callback_url, auth_file,+ stale_backup, EMAIL, PROVIDER, meta)++ # Step 7: Try asking agent for the URL directly+ log("[7] Asking agent for current URL")+ task_url = (+ "Report the current page URL exactly as it appears in the address bar. "+ "Also report window.__CB__ if it has a value."+ )+ run_url = client.create_run(task_url, session_id=session_id)+ result_url = client.wait_for_run(run_url.get("id", ""), timeout=60)+ text_url = str(result_url.get("result", "") or "")+ log(f"[7] URL query result: {text_url[:200]}")++ callback_url = _extract_callback(text_url)+ if callback_url:+ log(f"[7] Callback URL extracted: {sanitize_url(callback_url)}")+ return _complete_reauth(proc, callback_url, auth_file,+ stale_backup, EMAIL, PROVIDER, meta)++ log("[FAIL] Could not capture callback URL")+ return EXIT_FAILURE++ except Exception as e:+ log(f"[ERROR] {e}")+ return EXIT_FAILURE+ finally:+ cleanup_login_process(proc)+++def _click_authorize(client, session_id):+ """Inject JS interception and click Authorize via JavaScript."""+ log("[6] Injecting JS interception and clicking Authorize")++ # First, inject the JS interception+ task_inject = (+ f"Run this JavaScript in the browser console: {INTERCEPT_JS}\n"+ f"Then run this JavaScript to find and click the Authorize button: {CLICK_AUTHORIZE_JS}\n"+ f"Wait 5 seconds. "+ f"Then run window.__CB__ in the console and report its exact value. "+ f"If null, report the current page URL exactly."+ )+ run = client.create_run(task_inject, session_id=session_id)+ result = client.wait_for_run(run.get("id", ""), timeout=120)+ text = str(result.get("result", "") or "")+ log(f"[6] Authorize run complete: {text[:200]}")++ # Try to extract callback URL from the result+ callback_url = _extract_callback(text)+ if callback_url:+ return callback_url++ # Try asking for window.__CB__ directly+ task_cb = (+ "Run window.__CB__ in the browser console and report the exact value. "+ "If null, report the current page URL exactly as it appears in the address bar."+ )+ run_cb = client.create_run(task_cb, session_id=result.get("sessionId", session_id))+ result_cb = client.wait_for_run(run_cb.get("id", ""), timeout=60)+ text_cb = str(result_cb.get("result", "") or "")+ log(f"[6] CB query result: {text_cb[:200]}")++ callback_url = _extract_callback(text_cb)+ if callback_url:+ return callback_url++ return None+++def _extract_callback(text):+ """Extract a callback URL from text."""+ # Look for localhost:54545/callback?code=... pattern+ pattern = r'https?://localhost:54545/callback\?[^\s"\'`]+'+ match = re.search(pattern, text)+ if match:+ return match.group(0)++ # Also look for any URL with callback and code+ pattern2 = r'https?://[^\s"\'`]*callback[^\s"\'`]*code=[^\s"\'`]+'+ match = re.search(pattern2, text)+ if match:+ return match.group(0)++ # Look for window.__CB__ value+ pattern3 = r'window\.__CB__\s*(?:is|=)\s*["\']?(https?://[^\s"\'`]+)'+ match = re.search(pattern3, text, re.IGNORECASE)+ if match:+ return match.group(1)++ return None+++def main():+ # Set up logging+ from bu_reauth import _log_file, LOG_DIR, TOTAL_TIMEOUT+ global _log_file+ log_path = LOG_DIR / "aurora_reauth_direct.log"+ _log_file = open(log_path, "a", encoding="utf-8")++ # Set timeout+ signal.signal(signal.SIGALRM, _timeout_handler)+ signal.alarm(TOTAL_TIMEOUT)++ log(f"=== Aurora Direct Reauth: {safe_email(EMAIL)} ===")+ log(f"=== Profile: {PROFILE_ID[:12]}... ===")+ log(f"=== GOG Account: {GOG_ACCOUNT} ===")++ try:+ exit_code = run_reauth()+ except TimeoutError as e:+ log(f"[TIMEOUT] {e}")+ exit_code = EXIT_FAILURE+ except KeyboardInterrupt:+ log("[INTERRUPTED]")+ exit_code = EXIT_FAILURE+ finally:+ signal.alarm(0)++ log(f"=== Exit code: {exit_code} ===")+ return exit_code+++if __name__ == "__main__":+ sys.exit(main())diff --git a/scripts/crsproxy-reauth/bu_profile_reauth.py b/scripts/crsproxy-reauth/bu_profile_reauth.pynew file mode 100644index 00000000..56e4a454--- /dev/null+++ b/scripts/crsproxy-reauth/bu_profile_reauth.py@@ -0,0 +1,487 @@+#!/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"]+ # Use GMAIL_TARGET_INBOX (where emails are forwarded to) for Gmail polling,+ # not the account email itself. Falls back to email if env var not set.+ gog_account = os.environ.get("GMAIL_TARGET_INBOX", email)++ 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)++ # Claude's default flow is magic link ("click the link sent to").+ # Try magic link first, then fall back to verification code.+ magic = poll_gmail_for_code(provider, email, gog_account, kind="magic")+ if magic:+ log("[FALLBACK] Magic link received (value redacted)")+ task_magic = (+ f"Navigate to this URL: {magic}\n"+ "If you see an Authorize or Allow 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)++ # Check if we got the callback or need to click Authorize+ callback_url = extract_callback_url(result_magic, client, provider)+ if callback_url:+ log(f"[FALLBACK] Callback URL from magic link: {sanitize_url(callback_url)}")+ return _complete_reauth(proc, callback_url, auth_file,+ stale_backup, email, provider, meta)++ if detect_captcha(result_magic, client):+ log("[CAPTCHA] Captcha detected after magic link")+ callback_url, session_id = handle_captcha_checkpoint(+ client, run_magic.get("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("[FALLBACK] No magic link — trying verification code...")+ code = poll_gmail_for_code(provider, email, gog_account, 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 = (+ f"Enter the verification code {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, gog_account, 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("[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())