加密敏感数据并优化补CK流程

This commit is contained in:
yml2213
2026-06-24 09:10:31 +08:00
parent 8e65af538c
commit 177ec42416
9 changed files with 315 additions and 23 deletions
+29 -4
View File
@@ -105,6 +105,7 @@ class DouyuLogin:
self.proxy_manager = None
self._current_proxy_url: Optional[str] = None
self._cookie_enrich_error = ""
self._setup_session()
def _setup_session(self) -> None:
@@ -374,12 +375,15 @@ class DouyuLogin:
# 7️⃣ 完成登录获取Cookie
logger.info("步骤7: 完成登录,获取Cookie...")
cookie = self._complete_login(login_url)
message = "登录成功"
if self._cookie_enrich_error:
message = f"登录成功,补CK失败: {self._cookie_enrich_error}"
logger.success(f"登录成功! Cookie长度: {len(cookie)}")
# 成功后归还代理到池,让其他账号复用
if self.proxy_manager and self._current_proxy_url:
self.proxy_manager.release_proxy(self._current_proxy_url)
return LoginResult(success=True, cookie=cookie, message="登录成功")
return LoginResult(success=True, cookie=cookie, message=message)
except Exception as e:
elapsed = time.monotonic() - start_time
@@ -736,12 +740,33 @@ class DouyuLogin:
except Exception as e:
logger.warning(f"WebLogin请求失败(不影响登录): {e}")
# 登录成功后补齐 Web 侧 Cookie,再导出完整 CK
self._generate_csrf_cookie()
self._generate_acf_ccn_cookie()
# 登录成功后补齐 Web 侧 Cookie。补 CK 失败只影响完整度,不回滚已成功的登录态
self._cookie_enrich_error = ""
try:
self._enrich_web_cookies_with_retry()
except Exception as e:
self._cookie_enrich_error = self._truncate_error(str(e), 160)
logger.warning(f"补CK最终失败,本次仍按登录成功保存基础CK: {self._cookie_enrich_error}")
return self._format_cookie_string()
def _enrich_web_cookies_with_retry(self, max_attempts: int = 3) -> None:
"""独立重试补齐 cvl_csrf_token 和 acf_ccn,不触发整条登录链路重跑。"""
last_error: Exception | None = None
for attempt in range(1, max_attempts + 1):
try:
logger.info(f"补CK尝试 {attempt}/{max_attempts}...")
self._generate_csrf_cookie()
self._generate_acf_ccn_cookie()
logger.info("补CK完成")
return
except Exception as e:
last_error = e
logger.warning(f"补CK失败 {attempt}/{max_attempts}: {e}")
if attempt < max_attempts:
time.sleep(1)
raise ValueError(f"已重试 {max_attempts} 次仍未补齐CK: {last_error}") from last_error
def _generate_csrf_cookie(self) -> str:
"""
访问斗鱼 generateCsrf 接口,从 Set-Cookie 中同步 cvl_csrf_token。