fix: close stale oauth tabs before recovery

This commit is contained in:
2026-05-10 16:18:03 +08:00
parent a803703a98
commit c257038d14
5 changed files with 134 additions and 1 deletions
@@ -50,6 +50,29 @@ def cdp(js, match='auth.openai.com', timeout=45):
p = subprocess.run(['bash', '-lc', f"CDP_MATCH='{match}' python3 /tmp/cdp_async_eval.py"], input=js, text=True, capture_output=True, timeout=timeout)
return (p.stdout or '') + (p.stderr or '')
def close_stale_oauth_tabs(reason='recover'):
# Avoid tab pile-up and CDP target ambiguity before starting a fresh OAuth/add-phone recovery.
try:
tabs = json.loads(urllib.request.urlopen('http://127.0.0.1:9223/json/list', timeout=5).read().decode())
except Exception as e:
emit(phase='close_stale_oauth_tabs_failed', reason=reason, error=str(e))
return 0
patterns = ('auth.openai.com', 'chatgpt.com/auth', 'localhost', '127.0.0.1')
closed = 0
for t in tabs:
url = t.get('url', '')
tid = t.get('id')
if tid and any(x in url for x in patterns):
try:
urllib.request.urlopen('http://127.0.0.1:9223/json/close/' + urllib.parse.quote(tid, safe=''), timeout=3).read()
closed += 1
time.sleep(0.05)
except Exception:
pass
if closed:
emit(phase='close_stale_oauth_tabs', reason=reason, closed_count=closed)
return closed
def current_state():
return cdp("""
return {url: location.href, title: document.title,
@@ -64,6 +87,7 @@ def restore_add_phone():
if 'auth.openai.com/add-phone' in st:
return True
# Generate fresh OAuth URL and login with existing account. This account is already email-verified.
close_stale_oauth_tabs('before_oauth_recover')
raw = get('CODEX2API_URL')
u = urllib.parse.urlsplit(raw)
origin = f'{u.scheme}://{u.netloc}' if u.scheme and u.netloc else raw.rstrip('/')
@@ -50,6 +50,29 @@ def cdp(js, match='auth.openai.com', timeout=45):
p = subprocess.run(['bash', '-lc', f"CDP_MATCH='{match}' python3 /tmp/cdp_async_eval.py"], input=js, text=True, capture_output=True, timeout=timeout)
return (p.stdout or '') + (p.stderr or '')
def close_stale_oauth_tabs(reason='recover'):
# Avoid tab pile-up and CDP target ambiguity before starting a fresh OAuth/add-phone recovery.
try:
tabs = json.loads(urllib.request.urlopen('http://127.0.0.1:9223/json/list', timeout=5).read().decode())
except Exception as e:
emit(phase='close_stale_oauth_tabs_failed', reason=reason, error=str(e))
return 0
patterns = ('auth.openai.com', 'chatgpt.com/auth', 'localhost', '127.0.0.1')
closed = 0
for t in tabs:
url = t.get('url', '')
tid = t.get('id')
if tid and any(x in url for x in patterns):
try:
urllib.request.urlopen('http://127.0.0.1:9223/json/close/' + urllib.parse.quote(tid, safe=''), timeout=3).read()
closed += 1
time.sleep(0.05)
except Exception:
pass
if closed:
emit(phase='close_stale_oauth_tabs', reason=reason, closed_count=closed)
return closed
def current_state():
return cdp("""
return {url: location.href, title: document.title,
@@ -64,6 +87,7 @@ def restore_add_phone():
if 'auth.openai.com/add-phone' in st:
return True
# Generate fresh OAuth URL and login with existing account. This account is already email-verified.
close_stale_oauth_tabs('before_oauth_recover')
raw = get('CODEX2API_URL')
u = urllib.parse.urlsplit(raw)
origin = f'{u.scheme}://{u.netloc}' if u.scheme and u.netloc else raw.rstrip('/')
+24
View File
@@ -47,6 +47,29 @@ def cdp(js, match='auth.openai.com', timeout=45):
p = subprocess.run(['bash', '-lc', f"CDP_MATCH='{match}' python3 /tmp/cdp_async_eval.py"], input=js, text=True, capture_output=True, timeout=timeout)
return (p.stdout or '') + (p.stderr or '')
def close_stale_oauth_tabs(reason='recover'):
# Avoid tab pile-up and CDP target ambiguity before starting a fresh OAuth/add-phone recovery.
try:
tabs = json.loads(urllib.request.urlopen('http://127.0.0.1:9223/json/list', timeout=5).read().decode())
except Exception as e:
emit(phase='close_stale_oauth_tabs_failed', reason=reason, error=str(e))
return 0
patterns = ('auth.openai.com', 'chatgpt.com/auth', 'localhost', '127.0.0.1')
closed = 0
for t in tabs:
url = t.get('url', '')
tid = t.get('id')
if tid and any(x in url for x in patterns):
try:
urllib.request.urlopen('http://127.0.0.1:9223/json/close/' + urllib.parse.quote(tid, safe=''), timeout=3).read()
closed += 1
time.sleep(0.05)
except Exception:
pass
if closed:
emit(phase='close_stale_oauth_tabs', reason=reason, closed_count=closed)
return closed
def current_state():
return cdp("""
return {url: location.href, title: document.title,
@@ -61,6 +84,7 @@ def restore_add_phone():
if 'auth.openai.com/add-phone' in st:
return True
# Generate fresh OAuth URL and login with existing account. This account is already email-verified.
close_stale_oauth_tabs('before_oauth_recover')
raw = get('CODEX2API_URL')
u = urllib.parse.urlsplit(raw)
origin = f'{u.scheme}://{u.netloc}' if u.scheme and u.netloc else raw.rstrip('/')