feat(token): add automatic ST refresh functionality for personal mode
Browse files- Implement automatic ST (Session Token) refresh when AT refresh fails
- Add refresh_session_token method in BrowserCaptchaService to extract
__Secure-next-auth.session-token from browser cookies
- Enhance token_manager to automatically retry AT refresh after ST update
- Add validation for AT tokens to ensure they are actually working
- Update README to document AT/ST automatic refresh feature
- Improve error handling and logging for token refresh operations
- Add conditional ST refresh support only available in personal mode
- README.md +1 -1
- src/api/admin.py +28 -4
- src/services/browser_captcha_personal.py +115 -0
- src/services/token_manager.py +122 -35
README.md
CHANGED
|
@@ -16,7 +16,7 @@
|
|
| 16 |
- 🎨 **文生图** / **图生图**
|
| 17 |
- 🎬 **文生视频** / **图生视频**
|
| 18 |
- 🎞️ **首尾帧视频**
|
| 19 |
-
- 🔄 **AT自动刷新**
|
| 20 |
- 📊 **余额显示** - 实时查询和显示 VideoFX Credits
|
| 21 |
- 🚀 **负载均衡** - 多 Token 轮询和并发控制
|
| 22 |
- 🌐 **代理支持** - 支持 HTTP/SOCKS5 代理
|
|
|
|
| 16 |
- 🎨 **文生图** / **图生图**
|
| 17 |
- 🎬 **文生视频** / **图生视频**
|
| 18 |
- 🎞️ **首尾帧视频**
|
| 19 |
+
- 🔄 **AT/ST自动刷新** - AT 过期自动刷新,ST 过期时自动通过浏览器更新(personal 模式)
|
| 20 |
- 📊 **余额显示** - 实时查询和显示 VideoFX Credits
|
| 21 |
- 🚀 **负载均衡** - 多 Token 轮询和并发控制
|
| 22 |
- 🌐 **代理支持** - 支持 HTTP/SOCKS5 代理
|
src/api/admin.py
CHANGED
|
@@ -353,17 +353,32 @@ async def refresh_at(
|
|
| 353 |
token_id: int,
|
| 354 |
token: str = Depends(verify_admin_token)
|
| 355 |
):
|
| 356 |
-
"""手动刷新Token的AT (使用ST转换) 🆕
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
try:
|
| 358 |
-
# 调用token_manager的内部刷新方法
|
| 359 |
success = await token_manager._refresh_at(token_id)
|
| 360 |
|
| 361 |
if success:
|
| 362 |
# 获取更新后的token信息
|
| 363 |
updated_token = await token_manager.get_token(token_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
return {
|
| 365 |
"success": True,
|
| 366 |
-
"message":
|
| 367 |
"token": {
|
| 368 |
"id": updated_token.id,
|
| 369 |
"email": updated_token.email,
|
|
@@ -371,8 +386,17 @@ async def refresh_at(
|
|
| 371 |
}
|
| 372 |
}
|
| 373 |
else:
|
| 374 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
except Exception as e:
|
|
|
|
| 376 |
raise HTTPException(status_code=500, detail=f"刷新AT失败: {str(e)}")
|
| 377 |
|
| 378 |
|
|
|
|
| 353 |
token_id: int,
|
| 354 |
token: str = Depends(verify_admin_token)
|
| 355 |
):
|
| 356 |
+
"""手动刷新Token的AT (使用ST转换) 🆕
|
| 357 |
+
|
| 358 |
+
如果 AT 刷新失败且处于 personal 模式,会自动尝试通过浏览器刷新 ST
|
| 359 |
+
"""
|
| 360 |
+
from ..core.logger import debug_logger
|
| 361 |
+
from ..core.config import config
|
| 362 |
+
|
| 363 |
+
debug_logger.log_info(f"[API] 手动刷新 AT 请求: token_id={token_id}, captcha_method={config.captcha_method}")
|
| 364 |
+
|
| 365 |
try:
|
| 366 |
+
# 调用token_manager的内部刷新方法(包含 ST 自动刷新逻辑)
|
| 367 |
success = await token_manager._refresh_at(token_id)
|
| 368 |
|
| 369 |
if success:
|
| 370 |
# 获取更新后的token信息
|
| 371 |
updated_token = await token_manager.get_token(token_id)
|
| 372 |
+
|
| 373 |
+
message = "AT刷新成功"
|
| 374 |
+
if config.captcha_method == "personal":
|
| 375 |
+
message += "(支持ST自动刷新)"
|
| 376 |
+
|
| 377 |
+
debug_logger.log_info(f"[API] AT 刷新成功: token_id={token_id}")
|
| 378 |
+
|
| 379 |
return {
|
| 380 |
"success": True,
|
| 381 |
+
"message": message,
|
| 382 |
"token": {
|
| 383 |
"id": updated_token.id,
|
| 384 |
"email": updated_token.email,
|
|
|
|
| 386 |
}
|
| 387 |
}
|
| 388 |
else:
|
| 389 |
+
debug_logger.log_error(f"[API] AT 刷新失败: token_id={token_id}")
|
| 390 |
+
|
| 391 |
+
error_detail = "AT刷新失败"
|
| 392 |
+
if config.captcha_method != "personal":
|
| 393 |
+
error_detail += f"(当前打码模式: {config.captcha_method},ST自动刷新仅在 personal 模式下可用)"
|
| 394 |
+
|
| 395 |
+
raise HTTPException(status_code=500, detail=error_detail)
|
| 396 |
+
except HTTPException:
|
| 397 |
+
raise
|
| 398 |
except Exception as e:
|
| 399 |
+
debug_logger.log_error(f"[API] 刷新AT异常: {str(e)}")
|
| 400 |
raise HTTPException(status_code=500, detail=f"刷新AT失败: {str(e)}")
|
| 401 |
|
| 402 |
|
src/services/browser_captcha_personal.py
CHANGED
|
@@ -541,6 +541,121 @@ class BrowserCaptchaService:
|
|
| 541 |
debug_logger.log_info("[BrowserCaptcha] 请在打开的浏览器中登录账号。登录完成后,无需关闭浏览器,脚本下次运行时会自动使用此状态。")
|
| 542 |
print("请在打开的浏览器中登录账号。登录完成后,无需关闭浏览器,脚本下次运行时会自动使用此状态。")
|
| 543 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 544 |
# ========== 状态查询 ==========
|
| 545 |
|
| 546 |
def is_resident_mode_active(self) -> bool:
|
|
|
|
| 541 |
debug_logger.log_info("[BrowserCaptcha] 请在打开的浏览器中登录账号。登录完成后,无需关闭浏览器,脚本下次运行时会自动使用此状态。")
|
| 542 |
print("请在打开的浏览器中登录账号。登录完成后,无需关闭浏览器,脚本下次运行时会自动使用此状态。")
|
| 543 |
|
| 544 |
+
# ========== Session Token 刷新 ==========
|
| 545 |
+
|
| 546 |
+
async def refresh_session_token(self, project_id: str) -> Optional[str]:
|
| 547 |
+
"""从常驻标签页获取最新的 Session Token
|
| 548 |
+
|
| 549 |
+
复用 reCAPTCHA 常驻标签页,通过刷新页面并从 cookies 中提取
|
| 550 |
+
__Secure-next-auth.session-token
|
| 551 |
+
|
| 552 |
+
Args:
|
| 553 |
+
project_id: 项目ID,用于定位常驻标签页
|
| 554 |
+
|
| 555 |
+
Returns:
|
| 556 |
+
新的 Session Token,如果获取失败返回 None
|
| 557 |
+
"""
|
| 558 |
+
# 确保浏览器已初始化
|
| 559 |
+
await self.initialize()
|
| 560 |
+
|
| 561 |
+
start_time = time.time()
|
| 562 |
+
debug_logger.log_info(f"[BrowserCaptcha] 开始刷新 Session Token (project: {project_id})...")
|
| 563 |
+
|
| 564 |
+
# 尝试获取或创建常驻标签页
|
| 565 |
+
async with self._resident_lock:
|
| 566 |
+
resident_info = self._resident_tabs.get(project_id)
|
| 567 |
+
|
| 568 |
+
# 如果该 project_id 没有常驻标签页,则创建
|
| 569 |
+
if resident_info is None:
|
| 570 |
+
debug_logger.log_info(f"[BrowserCaptcha] project_id={project_id} 没有常驻标签页,正在创建...")
|
| 571 |
+
resident_info = await self._create_resident_tab(project_id)
|
| 572 |
+
if resident_info is None:
|
| 573 |
+
debug_logger.log_warning(f"[BrowserCaptcha] 无法为 project_id={project_id} 创建常驻标签页")
|
| 574 |
+
return None
|
| 575 |
+
self._resident_tabs[project_id] = resident_info
|
| 576 |
+
|
| 577 |
+
if not resident_info or not resident_info.tab:
|
| 578 |
+
debug_logger.log_error(f"[BrowserCaptcha] 无法获取常驻标签页")
|
| 579 |
+
return None
|
| 580 |
+
|
| 581 |
+
tab = resident_info.tab
|
| 582 |
+
|
| 583 |
+
try:
|
| 584 |
+
# 刷新页面以获取最新的 cookies
|
| 585 |
+
debug_logger.log_info(f"[BrowserCaptcha] 刷新常驻标签页以获取最新 cookies...")
|
| 586 |
+
await tab.reload()
|
| 587 |
+
|
| 588 |
+
# 等待页面加载完成
|
| 589 |
+
for i in range(30):
|
| 590 |
+
await asyncio.sleep(1)
|
| 591 |
+
try:
|
| 592 |
+
ready_state = await tab.evaluate("document.readyState")
|
| 593 |
+
if ready_state == "complete":
|
| 594 |
+
break
|
| 595 |
+
except Exception:
|
| 596 |
+
pass
|
| 597 |
+
|
| 598 |
+
# 额外等待确保 cookies 已设置
|
| 599 |
+
await asyncio.sleep(2)
|
| 600 |
+
|
| 601 |
+
# 从 cookies 中提取 __Secure-next-auth.session-token
|
| 602 |
+
# nodriver 可以通过 browser 获取 cookies
|
| 603 |
+
session_token = None
|
| 604 |
+
|
| 605 |
+
try:
|
| 606 |
+
# 使用 nodriver 的 cookies API 获取所有 cookies
|
| 607 |
+
cookies = await self.browser.cookies.get_all()
|
| 608 |
+
|
| 609 |
+
for cookie in cookies:
|
| 610 |
+
if cookie.name == "__Secure-next-auth.session-token":
|
| 611 |
+
session_token = cookie.value
|
| 612 |
+
break
|
| 613 |
+
|
| 614 |
+
except Exception as e:
|
| 615 |
+
debug_logger.log_warning(f"[BrowserCaptcha] 通过 cookies API 获取失败: {e},尝试从 document.cookie 获取...")
|
| 616 |
+
|
| 617 |
+
# 备选方案:通过 JavaScript 获取 (注意:HttpOnly cookies 可能无法通过此方式获取)
|
| 618 |
+
try:
|
| 619 |
+
all_cookies = await tab.evaluate("document.cookie")
|
| 620 |
+
if all_cookies:
|
| 621 |
+
for part in all_cookies.split(";"):
|
| 622 |
+
part = part.strip()
|
| 623 |
+
if part.startswith("__Secure-next-auth.session-token="):
|
| 624 |
+
session_token = part.split("=", 1)[1]
|
| 625 |
+
break
|
| 626 |
+
except Exception as e2:
|
| 627 |
+
debug_logger.log_error(f"[BrowserCaptcha] document.cookie 获取失败: {e2}")
|
| 628 |
+
|
| 629 |
+
duration_ms = (time.time() - start_time) * 1000
|
| 630 |
+
|
| 631 |
+
if session_token:
|
| 632 |
+
debug_logger.log_info(f"[BrowserCaptcha] ✅ Session Token 获取成功(耗时 {duration_ms:.0f}ms)")
|
| 633 |
+
return session_token
|
| 634 |
+
else:
|
| 635 |
+
debug_logger.log_error(f"[BrowserCaptcha] ❌ 未找到 __Secure-next-auth.session-token cookie")
|
| 636 |
+
return None
|
| 637 |
+
|
| 638 |
+
except Exception as e:
|
| 639 |
+
debug_logger.log_error(f"[BrowserCaptcha] 刷新 Session Token 异常: {str(e)}")
|
| 640 |
+
|
| 641 |
+
# 常驻标签页可能已失效,尝试重建
|
| 642 |
+
async with self._resident_lock:
|
| 643 |
+
await self._close_resident_tab(project_id)
|
| 644 |
+
resident_info = await self._create_resident_tab(project_id)
|
| 645 |
+
if resident_info:
|
| 646 |
+
self._resident_tabs[project_id] = resident_info
|
| 647 |
+
# 重建后再次尝试获取
|
| 648 |
+
try:
|
| 649 |
+
cookies = await self.browser.cookies.get_all()
|
| 650 |
+
for cookie in cookies:
|
| 651 |
+
if cookie.name == "__Secure-next-auth.session-token":
|
| 652 |
+
debug_logger.log_info(f"[BrowserCaptcha] ✅ 重建后 Session Token 获取成功")
|
| 653 |
+
return cookie.value
|
| 654 |
+
except Exception:
|
| 655 |
+
pass
|
| 656 |
+
|
| 657 |
+
return None
|
| 658 |
+
|
| 659 |
# ========== 状态查询 ==========
|
| 660 |
|
| 661 |
def is_resident_mode_active(self) -> bool:
|
src/services/token_manager.py
CHANGED
|
@@ -268,9 +268,13 @@ class TokenManager:
|
|
| 268 |
# AT有效
|
| 269 |
return True
|
| 270 |
|
|
|
|
| 271 |
async def _refresh_at(self, token_id: int) -> bool:
|
| 272 |
"""内部方法: 刷新AT
|
| 273 |
|
|
|
|
|
|
|
|
|
|
| 274 |
Returns:
|
| 275 |
True if refresh successful, False otherwise
|
| 276 |
"""
|
|
@@ -279,49 +283,132 @@ class TokenManager:
|
|
| 279 |
if not token:
|
| 280 |
return False
|
| 281 |
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
result = await self.flow_client.st_to_at(token.st)
|
| 287 |
-
new_at = result["access_token"]
|
| 288 |
-
expires = result.get("expires")
|
| 289 |
-
|
| 290 |
-
# 解析过期时间
|
| 291 |
-
new_at_expires = None
|
| 292 |
-
if expires:
|
| 293 |
-
try:
|
| 294 |
-
new_at_expires = datetime.fromisoformat(expires.replace('Z', '+00:00'))
|
| 295 |
-
except:
|
| 296 |
-
pass
|
| 297 |
-
|
| 298 |
-
# 更新数据库
|
| 299 |
-
await self.db.update_token(
|
| 300 |
-
token_id,
|
| 301 |
-
at=new_at,
|
| 302 |
-
at_expires=new_at_expires
|
| 303 |
-
)
|
| 304 |
|
| 305 |
-
|
| 306 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
|
| 308 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
try:
|
| 310 |
-
|
| 311 |
-
await self.db.update_token(
|
| 312 |
-
token_id,
|
| 313 |
-
credits=credits_result.get("credits", 0)
|
| 314 |
-
)
|
| 315 |
except:
|
| 316 |
pass
|
| 317 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
|
| 326 |
async def ensure_project_exists(self, token_id: int) -> str:
|
| 327 |
"""确保Token有可用的Project
|
|
|
|
| 268 |
# AT有效
|
| 269 |
return True
|
| 270 |
|
| 271 |
+
|
| 272 |
async def _refresh_at(self, token_id: int) -> bool:
|
| 273 |
"""内部方法: 刷新AT
|
| 274 |
|
| 275 |
+
如果 AT 刷新失败(ST 可能过期),会尝试通过浏览器自动刷新 ST,
|
| 276 |
+
然后重试 AT 刷新。
|
| 277 |
+
|
| 278 |
Returns:
|
| 279 |
True if refresh successful, False otherwise
|
| 280 |
"""
|
|
|
|
| 283 |
if not token:
|
| 284 |
return False
|
| 285 |
|
| 286 |
+
# 第一次尝试刷新 AT
|
| 287 |
+
result = await self._do_refresh_at(token_id, token.st)
|
| 288 |
+
if result:
|
| 289 |
+
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
+
# AT 刷新失败,尝试自动更新 ST
|
| 292 |
+
debug_logger.log_info(f"[AT_REFRESH] Token {token_id}: 第一次 AT 刷新失败,尝试自动更新 ST...")
|
| 293 |
+
|
| 294 |
+
new_st = await self._try_refresh_st(token_id, token)
|
| 295 |
+
if new_st:
|
| 296 |
+
# ST 更新成功,重试 AT 刷新
|
| 297 |
+
debug_logger.log_info(f"[AT_REFRESH] Token {token_id}: ST 已更新,重试 AT 刷新...")
|
| 298 |
+
result = await self._do_refresh_at(token_id, new_st)
|
| 299 |
+
if result:
|
| 300 |
+
return True
|
| 301 |
+
|
| 302 |
+
# 所有刷新尝试都失败,禁用 Token
|
| 303 |
+
debug_logger.log_error(f"[AT_REFRESH] Token {token_id}: 所有刷新尝试失败,禁用 Token")
|
| 304 |
+
await self.disable_token(token_id)
|
| 305 |
+
return False
|
| 306 |
+
|
| 307 |
+
async def _do_refresh_at(self, token_id: int, st: str) -> bool:
|
| 308 |
+
"""执行 AT 刷新的核心逻辑
|
| 309 |
+
|
| 310 |
+
Args:
|
| 311 |
+
token_id: Token ID
|
| 312 |
+
st: Session Token
|
| 313 |
|
| 314 |
+
Returns:
|
| 315 |
+
True if refresh successful AND AT is valid, False otherwise
|
| 316 |
+
"""
|
| 317 |
+
try:
|
| 318 |
+
debug_logger.log_info(f"[AT_REFRESH] Token {token_id}: 开始刷新AT...")
|
| 319 |
+
|
| 320 |
+
# 使用ST转AT
|
| 321 |
+
result = await self.flow_client.st_to_at(st)
|
| 322 |
+
new_at = result["access_token"]
|
| 323 |
+
expires = result.get("expires")
|
| 324 |
+
|
| 325 |
+
# 解析过期时间
|
| 326 |
+
new_at_expires = None
|
| 327 |
+
if expires:
|
| 328 |
try:
|
| 329 |
+
new_at_expires = datetime.fromisoformat(expires.replace('Z', '+00:00'))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
except:
|
| 331 |
pass
|
| 332 |
|
| 333 |
+
# 更新数据库
|
| 334 |
+
await self.db.update_token(
|
| 335 |
+
token_id,
|
| 336 |
+
at=new_at,
|
| 337 |
+
at_expires=new_at_expires
|
| 338 |
+
)
|
| 339 |
+
|
| 340 |
+
debug_logger.log_info(f"[AT_REFRESH] Token {token_id}: AT刷新成功")
|
| 341 |
+
debug_logger.log_info(f" - 新过期时间: {new_at_expires}")
|
| 342 |
+
|
| 343 |
+
# 验证 AT 有效性:通过 get_credits 测试
|
| 344 |
+
try:
|
| 345 |
+
credits_result = await self.flow_client.get_credits(new_at)
|
| 346 |
+
await self.db.update_token(
|
| 347 |
+
token_id,
|
| 348 |
+
credits=credits_result.get("credits", 0)
|
| 349 |
+
)
|
| 350 |
+
debug_logger.log_info(f"[AT_REFRESH] Token {token_id}: AT 验证成功(余额: {credits_result.get('credits', 0)})")
|
| 351 |
return True
|
| 352 |
+
except Exception as verify_err:
|
| 353 |
+
# AT 验证失败(可能返回 401),说明 ST 已过期
|
| 354 |
+
error_msg = str(verify_err)
|
| 355 |
+
if "401" in error_msg or "UNAUTHENTICATED" in error_msg:
|
| 356 |
+
debug_logger.log_warning(f"[AT_REFRESH] Token {token_id}: AT 验证失败 (401),ST 可能已过期")
|
| 357 |
+
return False
|
| 358 |
+
else:
|
| 359 |
+
# 其他错误(如网络问题),仍视为成功
|
| 360 |
+
debug_logger.log_warning(f"[AT_REFRESH] Token {token_id}: AT 验证时发生非认证错误: {error_msg}")
|
| 361 |
+
return True
|
| 362 |
|
| 363 |
+
except Exception as e:
|
| 364 |
+
debug_logger.log_error(f"[AT_REFRESH] Token {token_id}: AT刷新失败 - {str(e)}")
|
| 365 |
+
return False
|
| 366 |
+
|
| 367 |
+
async def _try_refresh_st(self, token_id: int, token) -> Optional[str]:
|
| 368 |
+
"""尝试通过浏览器刷新 Session Token
|
| 369 |
+
|
| 370 |
+
使用常驻 tab 获取新的 __Secure-next-auth.session-token
|
| 371 |
+
|
| 372 |
+
Args:
|
| 373 |
+
token_id: Token ID
|
| 374 |
+
token: Token 对象
|
| 375 |
+
|
| 376 |
+
Returns:
|
| 377 |
+
新的 ST 字符串,如果失败返回 None
|
| 378 |
+
"""
|
| 379 |
+
try:
|
| 380 |
+
from ..core.config import config
|
| 381 |
+
|
| 382 |
+
# 仅在 personal 模式下支持 ST 自动刷新
|
| 383 |
+
if config.captcha_method != "personal":
|
| 384 |
+
debug_logger.log_info(f"[ST_REFRESH] 非 personal 模式,跳过 ST 自动刷新")
|
| 385 |
+
return None
|
| 386 |
+
|
| 387 |
+
if not token.current_project_id:
|
| 388 |
+
debug_logger.log_warning(f"[ST_REFRESH] Token {token_id} 没有 project_id,无法刷新 ST")
|
| 389 |
+
return None
|
| 390 |
+
|
| 391 |
+
debug_logger.log_info(f"[ST_REFRESH] Token {token_id}: 尝试通过浏览器刷新 ST...")
|
| 392 |
+
|
| 393 |
+
from .browser_captcha_personal import BrowserCaptchaService
|
| 394 |
+
service = await BrowserCaptchaService.get_instance(self.db)
|
| 395 |
+
|
| 396 |
+
new_st = await service.refresh_session_token(token.current_project_id)
|
| 397 |
+
if new_st and new_st != token.st:
|
| 398 |
+
# 更新数据库中的 ST
|
| 399 |
+
await self.db.update_token(token_id, st=new_st)
|
| 400 |
+
debug_logger.log_info(f"[ST_REFRESH] Token {token_id}: ST 已自动更新")
|
| 401 |
+
return new_st
|
| 402 |
+
elif new_st == token.st:
|
| 403 |
+
debug_logger.log_warning(f"[ST_REFRESH] Token {token_id}: 获取到的 ST 与原 ST 相同,可能登录已失效")
|
| 404 |
+
return None
|
| 405 |
+
else:
|
| 406 |
+
debug_logger.log_warning(f"[ST_REFRESH] Token {token_id}: 无法获取新 ST")
|
| 407 |
+
return None
|
| 408 |
+
|
| 409 |
+
except Exception as e:
|
| 410 |
+
debug_logger.log_error(f"[ST_REFRESH] Token {token_id}: 刷新 ST 失败 - {str(e)}")
|
| 411 |
+
return None
|
| 412 |
|
| 413 |
async def ensure_project_exists(self, token_id: int) -> str:
|
| 414 |
"""确保Token有可用的Project
|