Spaces:
Paused
Paused
fix: 多项修复 — 验证码去重/无密码走验证码/auths同步/配置检查
Browse files- 验证码搜索用时间戳过滤,只接受请求之后收到的邮件(30秒容差)
- 无密码时自动点击"使用一次性验证码登录"(3处统一)
- sync_account_states 从 auths 目录恢复缺失的账号记录
- sync_account_states 不再因 accounts.json 为空就跳过
- 初始化向导检查所有缺失配置项(含 optional 的)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Files changed (2)
196 unmodified lines19719819920020120267 unmodified lines27027127227327427527627727827928028128211 unmodified lines294295296297298299300301302303304305306138 unmodified lines4454464474484494504511 unmodified line453454455456457458170 unmodified lines6296306316326336346356366376386396 unmodified lines6466476486496506516526536546556563 unmodified lines660661662663664665196 unmodified lines"""code_verifier, code_challenge = _generate_pkce()state = secrets.token_urlsafe(16)chatgpt_account_id = get_chatgpt_account_id()67 unmodified linesexcept Exception:pass# 输入密码try:pi = _page.locator('input[type="password"]').firstif pi.is_visible(timeout=5000):pi.fill(password)time.sleep(0.5)_click_primary_auth_button(_page, pi, ["Continue", "继续", "Log in"])time.sleep(8)except Exception:pass11 unmodified linesfor em in mail_client.search_emails_by_recipient(email, size=10):text = em.get("text", "") or em.get("content", "")m = _re2.search(r"\b(\d{6})\b", text)if m:otp = m.group(1)breakif otp:breaktime.sleep(3)if otp:ci.fill(otp)time.sleep(0.5)_page.locator('button[type="submit"]').first.click()138 unmodified linescontinuetext = em.get("text", "") or em.get("content", "")match = _re.search(r"\b(\d{6})\b", text)if match:otp_code = match.group(1)breakif otp_code:1 unmodified linetime.sleep(3)if otp_code:logger.info("[Codex] 获取到验证码: %s", otp_code)code_input.fill(otp_code)time.sleep(0.5)170 unmodified lines# 处理密码页面(可能在 consent 流程中出现)try:pwd_field = page.locator('input[name="password"], input[type="password"]').firstif pwd_field.is_visible(timeout=2000) and password:logger.info("[Codex] 需要重新输入密码 (step %d)...", step + 1)pwd_field.fill(password)time.sleep(0.5)_click_primary_auth_button(page, pwd_field, ["Continue", "继续", "Log in"])time.sleep(5)_screenshot(page, f"codex_04_password_{step + 1}.png")continue6 unmodified linesif otp_input.is_visible(timeout=2000) and mail_client:import re as _re3logger.info("[Codex] 需要邮箱验证码 (step %d)...", step + 1)otp = Nonet0 = time.time()while time.time() - t0 < 120:for em in mail_client.search_emails_by_recipient(email, size=10):text = em.get("text", "") or em.get("content", "")m = _re3.search(r"\b(\d{6})\b", text)if m:3 unmodified linesbreaktime.sleep(3)if otp:otp_input.fill(otp)time.sleep(0.5)page.locator(196 unmodified lines19719819920020120220367 unmodified lines27127227327427527627727827928028128228328428528628728828929029129229329429511 unmodified lines307308309310311312313314315316317318319320138 unmodified lines4594604614624634644651 unmodified line467468469470471472473170 unmodified lines6446456466476486496506516526536546556566576586596606616626636646656 unmodified lines6726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107113 unmodified lines715716717718719720721196 unmodified lines"""code_verifier, code_challenge = _generate_pkce()state = secrets.token_urlsafe(16)_used_codes: set[str] = set() # 记录已使用的验证码,避免重复提交chatgpt_account_id = get_chatgpt_account_id()67 unmodified linesexcept Exception:pass# 输入密码 / 点击一次性验证码登录try:pi = _page.locator('input[type="password"]').firstif pi.is_visible(timeout=5000):if password:pi.fill(password)time.sleep(0.5)_click_primary_auth_button(_page, pi, ["Continue", "继续", "Log in"])else:# 没有密码,点击"使用一次性验证码登录"otp_btn = _page.locator('button:has-text("一次性验证码"), button:has-text("one-time"), button:has-text("email login")').firstif otp_btn.is_visible(timeout=3000):logger.info("[Codex] 无密码,点击一次性验证码登录")otp_btn.click()else:# fallback: 提交空密码让页面报错,然后找验证码按钮_click_primary_auth_button(_page, pi, ["Continue", "继续", "Log in"])time.sleep(8)except Exception:pass11 unmodified linesfor em in mail_client.search_emails_by_recipient(email, size=10):text = em.get("text", "") or em.get("content", "")m = _re2.search(r"\b(\d{6})\b", text)if m and m.group(1) not in _used_codes:otp = m.group(1)breakif otp:breaktime.sleep(3)if otp:_used_codes.add(otp)ci.fill(otp)time.sleep(0.5)_page.locator('button[type="submit"]').first.click()138 unmodified linescontinuetext = em.get("text", "") or em.get("content", "")match = _re.search(r"\b(\d{6})\b", text)if match and match.group(1) not in _used_codes:otp_code = match.group(1)breakif otp_code:1 unmodified linetime.sleep(3)if otp_code:_used_codes.add(otp_code)logger.info("[Codex] 获取到验证码: %s", otp_code)code_input.fill(otp_code)time.sleep(0.5)170 unmodified lines# 处理密码页面(可能在 consent 流程中出现)try:pwd_field = page.locator('input[name="password"], input[type="password"]').firstif pwd_field.is_visible(timeout=2000):if password:logger.info("[Codex] 需要重新输入密码 (step %d)...", step + 1)pwd_field.fill(password)time.sleep(0.5)_click_primary_auth_button(page, pwd_field, ["Continue", "继续", "Log in"])else:# 没密码,点"使用一次性验证码登录"otp_btn = page.locator('button:has-text("一次性验证码"), button:has-text("one-time"), button:has-text("email login")').firstif otp_btn.is_visible(timeout=3000):logger.info("[Codex] 无密码,点击一次性验证码登录 (step %d)", step + 1)otp_btn.click()else:_click_primary_auth_button(page, pwd_field, ["Continue", "继续", "Log in"])time.sleep(5)_screenshot(page, f"codex_04_password_{step + 1}.png")continue6 unmodified linesif otp_input.is_visible(timeout=2000) and mail_client:import re as _re3# 记录当前时间,只接受之后收到的邮件from datetime import datetime_otp_request_time = datetime.utcnow()# 提前 30 秒容差,避免时钟偏差导致漏掉刚发的邮件from datetime import timedelta_otp_cutoff = _otp_request_time - timedelta(seconds=30)logger.info("[Codex] 需要邮箱验证码 (step %d),等待 %s 之后的新邮件...",step + 1,_otp_cutoff.strftime("%H:%M:%S"),)otp = Nonet0 = time.time()while time.time() - t0 < 120:for em in mail_client.search_emails_by_recipient(email, size=10):# 只接受 OpenAI 发的邮件sender = (em.get("sendEmail") or "").lower()if "openai" not in sender and "chatgpt" not in sender:continuesubj = (em.get("subject") or "").lower()if "invited" in subj or "invitation" in subj:continue# 检查邮件时间create_time_str = em.get("createTime", "")if create_time_str:try:mail_time = datetime.strptime(create_time_str, "%Y-%m-%d %H:%M:%S")if mail_time < _otp_cutoff:continueexcept Exception:passtext = em.get("text", "") or em.get("content", "")m = _re3.search(r"\b(\d{6})\b", text)if m:3 unmodified linesbreaktime.sleep(3)if otp:_used_codes.add(otp)otp_input.fill(otp)time.sleep(0.5)page.locator(
61 unmodified lines62636465666768697058 unmodified lines129130131132133134878 unmodified lines10131014101510161017101810191020102110221023102410251026102761 unmodified linesdef sync_account_states(chatgpt_api=None):"""根据 Team 实际成员列表同步本地账号状态"""account_id = get_chatgpt_account_id()accounts = load_accounts()if not accounts:return# 获取 Team 实际成员need_stop = False58 unmodified lineschanged = Truelogger.info("[同步] 发现 Team 中新成员: %s(已添加到本地)", email)if changed:save_accounts(accounts)878 unmodified linesexcept Exception:pass# 输入密码pwd_input = page.locator('input[type="password"]').firsttry:if pwd_input.is_visible(timeout=5000):pwd_input.fill(password)time.sleep(0.5)page.locator('button:has-text("Continue"), button:has-text("继续"), button[type="submit"]').first.click()time.sleep(8)except Exception:pass61 unmodified lines6263646566676869707158 unmodified lines130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168878 unmodified lines10471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107361 unmodified linesdef sync_account_states(chatgpt_api=None):"""根据 Team 实际成员列表同步本地账号状态"""account_id = get_chatgpt_account_id()if not account_id:returnaccounts = load_accounts()team_emails = set()# 获取 Team 实际成员need_stop = False58 unmodified lineschanged = Truelogger.info("[同步] 发现 Team 中新成员: %s(已添加到本地)", email)# auths 目录中有认证文件但本地无记录的 → 自动添加为 standbyfrom autoteam.codex_auth import AUTH_DIRlocal_email_set = {a["email"].lower() for a in accounts} # 刷新一下if AUTH_DIR.exists():for auth_file in AUTH_DIR.glob("codex-*.json"):try:auth_data = json.loads(auth_file.read_text())email = auth_data.get("email", "").lower()if not email or email in local_email_set:continue# 判断是否在 Team 中in_team = email in team_emailsstatus = STATUS_ACTIVE if in_team else STATUS_STANDBYaccounts.append({"email": email,"password": "","cloudmail_account_id": None,"status": status,"auth_file": str(auth_file),"quota_exhausted_at": None,"quota_resets_at": None,"created_at": time.time(),"last_active_at": None,})local_email_set.add(email)changed = Truelogger.info("[同步] 从 auths 目录恢复账号: %s(%s)", email, status)except Exception:continueif changed:save_accounts(accounts)878 unmodified linesexcept Exception:pass# 输入密码 / 点击一次性验证码登录pwd_input = page.locator('input[type="password"]').firsttry:if pwd_input.is_visible(timeout=5000):if password:pwd_input.fill(password)time.sleep(0.5)page.locator('button:has-text("Continue"), button:has-text("继续"), button[type="submit"]').first.click()else:otp_btn = page.locator('button:has-text("一次性验证码"), button:has-text("one-time"), button:has-text("email login")').firstif otp_btn.is_visible(timeout=3000):logger.info("[轮转] 无密码,点击一次性验证码登录")otp_btn.click()else:page.locator('button:has-text("Continue"), button:has-text("继续"), button[type="submit"]').first.click()time.sleep(8)except Exception:pass
src/autoteam/codex_auth.py+68-12
src/autoteam/manager.py+54-8