Compare commits
4
Commits
8309072120
...
8dea9e3244
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8dea9e3244 | ||
|
|
cbc7f7c9c5 | ||
|
|
d3005478a9 | ||
|
|
4f9a6864eb |
@@ -28,9 +28,13 @@ evidence/
|
||||
scripts/*
|
||||
!scripts/migrate_sqlite_to_mysql.py
|
||||
!scripts/install-git-hooks.sh
|
||||
!scripts/capture_huya_captcha.py
|
||||
# ── 逆向工具集(frida hook、unidbg hydev、虎牙协议复刻脚本等)
|
||||
tools/
|
||||
|
||||
# 验证码抓取数据(本地分析,不入库)
|
||||
data/captcha_captures/
|
||||
|
||||
# Node 依赖
|
||||
**/node_modules/
|
||||
web/frontend/dist/
|
||||
@@ -41,3 +45,4 @@ apks/
|
||||
work/
|
||||
|
||||
analysis/
|
||||
artifacts/
|
||||
|
||||
@@ -41,6 +41,7 @@ RUN if [ "$USE_CHINA_MIRRORS" = "true" ]; then \
|
||||
libgl1 \
|
||||
libglib2.0-0 \
|
||||
nodejs \
|
||||
fonts-wqy-microhei \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# 安装 uv:服务器走阿里云镜像,本地直连官方 PyPI。
|
||||
|
||||
+51
-7
@@ -90,6 +90,14 @@ class HuyaAppQrAuthRequiredError(HuyaAppLoginError):
|
||||
"""风控要求扫码验证 (qr_auth)。"""
|
||||
|
||||
|
||||
class HuyaAppSmsAuthRequiredError(HuyaAppLoginError):
|
||||
"""风控要求短信验证 (sms_auth / sms_auth2)。"""
|
||||
|
||||
def __init__(self, message: str = "", mobile: str = ""):
|
||||
self.mobile = mobile
|
||||
super().__init__(message or f"虎牙风控要求短信验证,手机号: {mobile or '?'}")
|
||||
|
||||
|
||||
def _golden_session_assets() -> tuple[dict, str, str]:
|
||||
"""生成本次登录的会话元数据和行为轨迹。"""
|
||||
from .wup_encoder import make_trace_id, make_user_action
|
||||
@@ -202,7 +210,7 @@ def parse_risk_url(resp: bytes) -> str | None:
|
||||
urls.append(u)
|
||||
if not urls:
|
||||
return None
|
||||
pt = [u for u in urls if "pt_auth" in u]
|
||||
pt = [u for u in urls if "pt_auth" in u or "dx_auth" in u]
|
||||
return (pt or urls)[0]
|
||||
|
||||
|
||||
@@ -221,10 +229,13 @@ def solve_safe_auth(
|
||||
proxies=None,
|
||||
max_retry: int = 3,
|
||||
device_info: Mapping[str, object] | None = None,
|
||||
account: str = "",
|
||||
sms_url: str = "",
|
||||
) -> dict:
|
||||
"""解 safe_auth 风控滑块。"""
|
||||
"""解 safe_auth 风控滑块/点选/短信。"""
|
||||
from .verification.solver import (
|
||||
HuyaQrAuthRequiredError,
|
||||
HuyaSmsAuthRequiredError,
|
||||
HuyaVerificationSolver,
|
||||
)
|
||||
|
||||
@@ -243,11 +254,16 @@ def solve_safe_auth(
|
||||
app_id=app_id,
|
||||
page_url=risk_url,
|
||||
use_touch_events=True,
|
||||
account=account,
|
||||
sms_url=sms_url,
|
||||
)
|
||||
try:
|
||||
result = solver.solve(risk_url)
|
||||
except HuyaQrAuthRequiredError:
|
||||
raise
|
||||
except HuyaSmsAuthRequiredError as exc:
|
||||
mobile = getattr(exc, "mobile", "")
|
||||
raise HuyaAppSmsAuthRequiredError(str(exc), mobile=mobile) from exc
|
||||
except Exception as exc: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
last_err = exc
|
||||
logger.warning(f"[safe_auth] 第 {attempt + 1} 次过验异常: {exc}")
|
||||
@@ -255,9 +271,9 @@ def solve_safe_auth(
|
||||
continue
|
||||
auth_id = str((result or {}).get("authId") or "")
|
||||
if auth_id:
|
||||
logger.info(f"[safe_auth] 滑块通过 authId={auth_id[:24]}...")
|
||||
logger.info(f"[safe_auth] 验证码过验成功 authId={auth_id[:24]}...")
|
||||
return result
|
||||
raise HuyaAppLoginError(f"safe_auth 滑块过验失败: {last_err or '未返回 authId'}")
|
||||
raise HuyaAppLoginError(f"safe_auth 验证码过验失败: {last_err or '未返回 authId'}")
|
||||
|
||||
|
||||
def parse_real_uid(resp: bytes) -> int:
|
||||
@@ -284,6 +300,7 @@ def login_cred_with_flow(
|
||||
device_info: dict | None = None,
|
||||
proxies: dict | None = None,
|
||||
include_device_token: bool = False,
|
||||
sms_url: str = "",
|
||||
) -> tuple[bytes, int] | tuple[bytes, int, str]:
|
||||
"""新注册设备后登录,返回 ``(新鲜 cred, 真实 uid)``。
|
||||
|
||||
@@ -326,15 +343,25 @@ def login_cred_with_flow(
|
||||
kind = (
|
||||
"pt_auth(滑块)"
|
||||
if "pt_auth" in risk_url
|
||||
else ("qr_auth(扫码)" if "qr_auth" in risk_url else "未知")
|
||||
else (
|
||||
"dx_auth(点选)"
|
||||
if "dx_auth" in risk_url
|
||||
else ("qr_auth(扫码)" if "qr_auth" in risk_url else "未知")
|
||||
)
|
||||
)
|
||||
logger.info(f"[huya-app] 第 {rnd + 1} 轮触发安全验证: {kind}")
|
||||
if "qr_auth" in risk_url:
|
||||
raise HuyaAppQrAuthRequiredError(
|
||||
"该账号 App 渠道要求扫码验证(qr_auth),请先在手机虎牙 App 上正常登录一次建立设备信任。"
|
||||
)
|
||||
solve_safe_auth(risk_url, proxies=proxies, device_info=dev)
|
||||
logger.info("[huya-app] safe_auth 滑块过验成功,重发 WUP 登录...")
|
||||
solve_safe_auth(
|
||||
risk_url,
|
||||
proxies=proxies,
|
||||
device_info=dev,
|
||||
account=account,
|
||||
sms_url=sms_url,
|
||||
)
|
||||
logger.info(f"[huya-app] safe_auth ({kind}) 过验成功,重发 WUP 登录...")
|
||||
continue
|
||||
markers = _response_markers(resp)
|
||||
detail = f",服务端字段: {markers}" if markers else ""
|
||||
@@ -446,12 +473,14 @@ class HuyaAppPasswordLogin:
|
||||
timeout: tuple[float, float] | None = None,
|
||||
force_new_device: bool = False,
|
||||
device_info: dict | None = None,
|
||||
sms_url: str = "",
|
||||
):
|
||||
self.username = username.strip()
|
||||
self.password = password
|
||||
self.proxies = dict(proxies) if proxies else None
|
||||
self.timeout = timeout or (10.0, 25.0)
|
||||
self.force_new_device = force_new_device
|
||||
self.sms_url = sms_url.strip()
|
||||
if force_new_device:
|
||||
reset_account_state(self.username)
|
||||
self.device_info = device_info or get_profile(
|
||||
@@ -631,14 +660,27 @@ class HuyaAppPasswordLogin:
|
||||
device_info=self.device_info,
|
||||
proxies=self.proxies,
|
||||
include_device_token=True,
|
||||
sms_url=self.sms_url,
|
||||
)
|
||||
except HuyaAppQrAuthRequiredError as exc:
|
||||
logger.warning(f"[huya-app] 账号 {acct} 登录受阻: {exc}")
|
||||
return HuyaLoginResult(
|
||||
success=False,
|
||||
message=str(exc),
|
||||
code="QR_AUTH_REQUIRED",
|
||||
)
|
||||
except HuyaAppSmsAuthRequiredError as exc:
|
||||
logger.warning(
|
||||
f"[huya-app] 账号 {acct} 登录中断:滑块与点选已全部通过,"
|
||||
f"虎牙下发了手机短信二次验证 ({exc.mobile or '手机号见验证链接'}),需人工接码"
|
||||
)
|
||||
return HuyaLoginResult(
|
||||
success=False,
|
||||
message=str(exc),
|
||||
code="SMS_AUTH_REQUIRED",
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
logger.warning(f"[huya-app] 账号 {acct} App 登录异常: {exc}")
|
||||
return HuyaLoginResult(
|
||||
success=False,
|
||||
message=f"App 登录凭证获取失败: {exc}",
|
||||
@@ -906,6 +948,7 @@ def login_huya_app_password(
|
||||
proxies: Mapping[str, str] | None = None,
|
||||
timeout: tuple[float, float] | None = None,
|
||||
force_new_device: bool = False,
|
||||
sms_url: str = "",
|
||||
) -> HuyaLoginResult:
|
||||
"""函数式入口:使用 App 协议执行虎牙密码登录并获取全套 Cookie。"""
|
||||
return HuyaAppPasswordLogin(
|
||||
@@ -914,4 +957,5 @@ def login_huya_app_password(
|
||||
proxies=proxies,
|
||||
timeout=timeout,
|
||||
force_new_device=force_new_device,
|
||||
sms_url=sms_url,
|
||||
).login()
|
||||
|
||||
@@ -0,0 +1,468 @@
|
||||
"""虎牙验证码全要素数据捕获与落盘模块。
|
||||
|
||||
每次触发验证码(滑块 pt_auth / 点选 dx_auth)时,将图片、提示语、识别结果、
|
||||
请求元数据及提交响应完整落盘保存到 data/captcha_captures/,便于分析与排查。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime as dt
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
from loguru import logger
|
||||
|
||||
PROJECT_ROOT = Path(__file__).resolve().parents[3]
|
||||
DEFAULT_CAPTURE_DIR = PROJECT_ROOT / "data" / "captcha_captures"
|
||||
|
||||
|
||||
class CaptchaCaptureRecord:
|
||||
"""单次验证码捕获会话。"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
auth_type: str,
|
||||
account: str = "",
|
||||
page_url: str = "",
|
||||
scene_type: str = "",
|
||||
output_base_dir: Path | None = None,
|
||||
):
|
||||
self.auth_type = auth_type
|
||||
self.account = account
|
||||
self.page_url = page_url
|
||||
self.scene_type = scene_type
|
||||
self.output_base_dir = Path(output_base_dir or DEFAULT_CAPTURE_DIR)
|
||||
self.created_at = dt.datetime.now(dt.timezone(dt.timedelta(hours=8)))
|
||||
|
||||
self.session_id = self.created_at.strftime("%Y%m%d_%H%M%S")
|
||||
self.target_dir: Path | None = None
|
||||
|
||||
self.meta: dict[str, Any] = {
|
||||
"timestamp": self.created_at.isoformat(),
|
||||
"account": account,
|
||||
"auth_type": auth_type,
|
||||
"auth_name": (
|
||||
"点选验证 (dx_auth)"
|
||||
if "dx" in auth_type
|
||||
else (
|
||||
"旋转验证 (rotate_auth)"
|
||||
if "rotate" in auth_type or "xz" in auth_type
|
||||
else (
|
||||
"短信二次验证 (sms_auth)"
|
||||
if "sms" in auth_type
|
||||
else (
|
||||
"扫码验证 (qr_auth)"
|
||||
if "qr" in auth_type
|
||||
else "滑块验证 (pt_auth)"
|
||||
)
|
||||
)
|
||||
)
|
||||
),
|
||||
"page_url": page_url,
|
||||
"scene_type": scene_type,
|
||||
"prompts": {},
|
||||
"images": {},
|
||||
"solution": {},
|
||||
"submit_result": {},
|
||||
}
|
||||
|
||||
def _ensure_target_dir(self, code: str = "") -> Path:
|
||||
if self.target_dir is None:
|
||||
short_code = code[:8] if code else "sample"
|
||||
dir_name = f"{self.session_id}_{self.auth_type}_{short_code}"
|
||||
self.target_dir = self.output_base_dir / dir_name
|
||||
self.target_dir.mkdir(parents=True, exist_ok=True)
|
||||
return self.target_dir
|
||||
|
||||
def capture_slider_data(
|
||||
self,
|
||||
*,
|
||||
code: str,
|
||||
dtu_bytes: bytes,
|
||||
puzzle_bytes: bytes,
|
||||
restored_bg_bytes: bytes | None = None,
|
||||
composite_bytes: bytes | None = None,
|
||||
point_x: int | None = None,
|
||||
raw_data: dict[str, Any] | None = None,
|
||||
) -> Path:
|
||||
"""捕获滑块验证码的所有图片和元数据。"""
|
||||
folder = self._ensure_target_dir(code)
|
||||
|
||||
# 1. 提示语与服务端描述
|
||||
raw = raw_data or {}
|
||||
self.meta["code"] = code
|
||||
self.meta["prompts"] = {
|
||||
"title": "安全验证 (滑块拼图)",
|
||||
"default_instruction": "拖动下方滑块完成拼图",
|
||||
"location_y": raw.get("location_y"),
|
||||
"description": raw.get("description") or raw.get("message") or "",
|
||||
"tip": raw.get("tip") or "",
|
||||
}
|
||||
|
||||
# 2. 保存原始与合成图片
|
||||
images_info = {}
|
||||
if dtu_bytes:
|
||||
(folder / "bg_scrambled.jpg").write_bytes(dtu_bytes)
|
||||
images_info["bg_scrambled"] = "bg_scrambled.jpg"
|
||||
|
||||
if puzzle_bytes:
|
||||
(folder / "puzzle.png").write_bytes(puzzle_bytes)
|
||||
images_info["puzzle"] = "puzzle.png"
|
||||
|
||||
if restored_bg_bytes:
|
||||
(folder / "bg_restored.jpg").write_bytes(restored_bg_bytes)
|
||||
images_info["bg_restored"] = "bg_restored.jpg"
|
||||
|
||||
if composite_bytes:
|
||||
(folder / "composite.png").write_bytes(composite_bytes)
|
||||
images_info["composite"] = "composite.png"
|
||||
|
||||
# 3. 绘制识别标注图(在还原后的背景图上标注缺口 X 坐标与 Y 坐标)
|
||||
if restored_bg_bytes and point_x is not None:
|
||||
try:
|
||||
bg_arr = cv2.imdecode(
|
||||
np.frombuffer(restored_bg_bytes, np.uint8), cv2.IMREAD_COLOR
|
||||
)
|
||||
if bg_arr is not None:
|
||||
loc_y = int(raw.get("location_y") or 0)
|
||||
cv2.rectangle(
|
||||
bg_arr,
|
||||
(point_x, loc_y),
|
||||
(point_x + 50, loc_y + 50),
|
||||
(0, 0, 255),
|
||||
2,
|
||||
)
|
||||
cv2.putText(
|
||||
bg_arr,
|
||||
f"X={point_x}",
|
||||
(max(0, point_x - 10), max(20, loc_y - 10)),
|
||||
cv2.FONT_HERSHEY_SIMPLEX,
|
||||
0.6,
|
||||
(0, 0, 255),
|
||||
2,
|
||||
)
|
||||
cv2.imwrite(str(folder / "annotated.jpg"), bg_arr)
|
||||
images_info["annotated"] = "annotated.jpg"
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.debug(f"[captcha_capture] 标注滑块图异常: {exc}")
|
||||
|
||||
self.meta["images"] = images_info
|
||||
if point_x is not None:
|
||||
self.meta["solution"] = {"type": "slider", "distance_x": point_x}
|
||||
|
||||
if raw_data:
|
||||
clean_raw = {
|
||||
k: v for k, v in raw_data.items() if not str(v).startswith("data:image")
|
||||
}
|
||||
(folder / "raw_data.json").write_text(
|
||||
json.dumps(clean_raw, ensure_ascii=False, indent=2), encoding="utf-8"
|
||||
)
|
||||
|
||||
self._flush_meta()
|
||||
self._log_summary()
|
||||
return folder
|
||||
|
||||
def capture_rotate_data(
|
||||
self,
|
||||
*,
|
||||
code: str,
|
||||
dtu_bytes: bytes,
|
||||
puzzle_bytes: bytes,
|
||||
restored_bg_bytes: bytes | None = None,
|
||||
angle: int | float | None = None,
|
||||
raw_data: dict[str, Any] | None = None,
|
||||
) -> Path:
|
||||
"""捕获旋转验证码的所有图片和元数据。"""
|
||||
folder = self._ensure_target_dir(code)
|
||||
|
||||
raw = raw_data or {}
|
||||
self.meta["code"] = code
|
||||
self.meta["prompts"] = {
|
||||
"title": "安全验证 (旋转验证 rotate_auth)",
|
||||
"default_instruction": "请拖动滑块,将图片旋转至正确位置",
|
||||
"description": raw.get("description") or raw.get("message") or "",
|
||||
"tip": raw.get("tip") or "",
|
||||
}
|
||||
|
||||
images_info = {}
|
||||
if dtu_bytes:
|
||||
(folder / "bg_scrambled.jpg").write_bytes(dtu_bytes)
|
||||
images_info["bg_scrambled"] = "bg_scrambled.jpg"
|
||||
|
||||
if puzzle_bytes:
|
||||
(folder / "puzzle.png").write_bytes(puzzle_bytes)
|
||||
images_info["puzzle"] = "puzzle.png"
|
||||
|
||||
if restored_bg_bytes:
|
||||
(folder / "bg_restored.jpg").write_bytes(restored_bg_bytes)
|
||||
images_info["bg_restored"] = "bg_restored.jpg"
|
||||
|
||||
# 绘制旋转合成标注图
|
||||
if restored_bg_bytes and puzzle_bytes and angle is not None:
|
||||
try:
|
||||
bg = cv2.imdecode(
|
||||
np.frombuffer(restored_bg_bytes, np.uint8), cv2.IMREAD_COLOR
|
||||
)
|
||||
puzzle_raw = cv2.imdecode(
|
||||
np.frombuffer(puzzle_bytes, np.uint8), cv2.IMREAD_UNCHANGED
|
||||
)
|
||||
if bg is not None and puzzle_raw is not None:
|
||||
M = cv2.getRotationMatrix2D((90.0, 90.0), -float(angle), 1.0)
|
||||
rotated = cv2.warpAffine(puzzle_raw, M, (180, 180))
|
||||
alpha = rotated[:, :, 3] / 255.0
|
||||
composite = bg.copy()
|
||||
for c in range(3):
|
||||
composite[0:180, 105:285, c] = (
|
||||
rotated[:, :, c] * alpha
|
||||
+ composite[0:180, 105:285, c] * (1 - alpha)
|
||||
).astype(np.uint8)
|
||||
cv2.putText(
|
||||
composite,
|
||||
f"Angle={int(angle)} deg",
|
||||
(15, 30),
|
||||
cv2.FONT_HERSHEY_SIMPLEX,
|
||||
0.8,
|
||||
(0, 255, 0),
|
||||
2,
|
||||
)
|
||||
success, buffer = cv2.imencode(".jpg", composite)
|
||||
if success:
|
||||
(folder / "annotated.jpg").write_bytes(buffer.tobytes())
|
||||
images_info["annotated"] = "annotated.jpg"
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.debug(f"[captcha_capture] 标注旋转图异常: {exc}")
|
||||
|
||||
self.meta["images"] = images_info
|
||||
if angle is not None:
|
||||
self.meta["solution"] = {"type": "rotate", "angle": int(angle)}
|
||||
|
||||
if raw_data:
|
||||
clean_raw = {
|
||||
k: v for k, v in raw_data.items() if not str(v).startswith("data:image")
|
||||
}
|
||||
(folder / "raw_data.json").write_text(
|
||||
json.dumps(clean_raw, ensure_ascii=False, indent=2),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
self._flush_meta()
|
||||
self._log_summary()
|
||||
return folder
|
||||
|
||||
def capture_click_data(
|
||||
self,
|
||||
*,
|
||||
code: str,
|
||||
dtu_bytes: bytes,
|
||||
hzi_bytes: bytes,
|
||||
points: list[list[int]] | None = None,
|
||||
raw_data: dict[str, Any] | None = None,
|
||||
) -> Path:
|
||||
"""捕获文字/图标点选验证码的所有图片和元数据。"""
|
||||
folder = self._ensure_target_dir(code)
|
||||
|
||||
raw = raw_data or {}
|
||||
self.meta["code"] = code
|
||||
question_text = str(raw.get("question") or "")
|
||||
if not question_text:
|
||||
question_text = "按顺序点击图中对应内容"
|
||||
word_size = raw.get("wordSize")
|
||||
self.meta["prompts"] = {
|
||||
"title": str(raw.get("title") or "安全验证 (点选验证 dx_auth)"),
|
||||
"question": question_text,
|
||||
"word_size": word_size,
|
||||
"tu_ming": raw.get("tuMing"),
|
||||
"description": raw.get("description") or raw.get("message") or "",
|
||||
}
|
||||
|
||||
images_info = {}
|
||||
if dtu_bytes:
|
||||
(folder / "bg_main.jpg").write_bytes(dtu_bytes)
|
||||
images_info["bg_main"] = "bg_main.jpg"
|
||||
|
||||
if hzi_bytes:
|
||||
(folder / "tip_text.png").write_bytes(hzi_bytes)
|
||||
images_info["tip_text"] = "tip_text.png"
|
||||
|
||||
# 绘制点选标注预览图
|
||||
if dtu_bytes and points:
|
||||
try:
|
||||
bg_arr = cv2.imdecode(
|
||||
np.frombuffer(dtu_bytes, np.uint8), cv2.IMREAD_COLOR
|
||||
)
|
||||
if bg_arr is not None:
|
||||
for idx, (px, py) in enumerate(points):
|
||||
# 绘制点击圆圈
|
||||
cv2.circle(bg_arr, (int(px), int(py)), 14, (0, 0, 255), 2)
|
||||
cv2.circle(bg_arr, (int(px), int(py)), 4, (0, 255, 0), -1)
|
||||
# 标注点击序号 1, 2, 3...
|
||||
cv2.putText(
|
||||
bg_arr,
|
||||
str(idx + 1),
|
||||
(int(px) - 5, int(py) - 18),
|
||||
cv2.FONT_HERSHEY_SIMPLEX,
|
||||
0.7,
|
||||
(0, 0, 255),
|
||||
2,
|
||||
)
|
||||
cv2.imwrite(str(folder / "annotated.jpg"), bg_arr)
|
||||
images_info["annotated"] = "annotated.jpg"
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.debug(f"[captcha_capture] 标注点选图异常: {exc}")
|
||||
|
||||
self.meta["images"] = images_info
|
||||
if points is not None:
|
||||
self.meta["solution"] = {"type": "click", "points": points}
|
||||
|
||||
if raw_data:
|
||||
clean_raw = {
|
||||
k: v for k, v in raw_data.items() if not str(v).startswith("data:image")
|
||||
}
|
||||
(folder / "raw_data.json").write_text(
|
||||
json.dumps(clean_raw, ensure_ascii=False, indent=2), encoding="utf-8"
|
||||
)
|
||||
|
||||
self._flush_meta()
|
||||
self._log_summary()
|
||||
return folder
|
||||
|
||||
def capture_sms_auth_data(
|
||||
self,
|
||||
*,
|
||||
mobile: str = "",
|
||||
page_url: str = "",
|
||||
scene_type: str = "",
|
||||
step: int = 1,
|
||||
raw_params: dict[str, Any] | None = None,
|
||||
) -> Path:
|
||||
"""捕获短信风控验证拦截数据并落盘。"""
|
||||
folder = self._ensure_target_dir(code="sms_auth")
|
||||
self.meta["auth_type"] = "sms_auth"
|
||||
self.meta["auth_name"] = "短信二次验证 (sms_auth)"
|
||||
self.meta["page_url"] = page_url
|
||||
self.meta["scene_type"] = scene_type
|
||||
self.meta["mobile"] = mobile
|
||||
self.meta["step"] = step
|
||||
self.meta["prompts"] = {
|
||||
"title": "安全验证 (短信二次验证 sms_auth)",
|
||||
"instruction": f"前置图形验证码已通过,虎牙触发第 {step} 级短信安全验证,需向手机 {mobile} 发送验证码",
|
||||
"mobile": mobile,
|
||||
}
|
||||
self.meta["submit_result"] = {
|
||||
"status": "sms_auth_required",
|
||||
"mobile": mobile,
|
||||
"nextAuthUrl": page_url,
|
||||
}
|
||||
if raw_params:
|
||||
(folder / "params.json").write_text(
|
||||
json.dumps(raw_params, ensure_ascii=False, indent=2), encoding="utf-8"
|
||||
)
|
||||
self._flush_meta()
|
||||
|
||||
logger.warning(
|
||||
f"\n┌── [虎牙风控拦截捕获] ──────────────────────────────────────────\n"
|
||||
f"│ 类型: 短信二次验证 (sms_auth) (sceneType: {self.scene_type or scene_type or '4'})\n"
|
||||
f"│ 账号: {self.account or '未指定'}\n"
|
||||
f"│ 阶段: 第 {step} 阶段(前置滑块/点选已成功通过)\n"
|
||||
f"│ 手机: {mobile or '未知'}\n"
|
||||
f"│ 提示: 虎牙官方下发短信接码页面,协议自动化无法跳过,需人工接码\n"
|
||||
f"│ 页面: {page_url[:80]}...\n"
|
||||
f"│ 存储目录: {self.target_dir}\n"
|
||||
f"└─────────────────────────────────────────────────────────────"
|
||||
)
|
||||
return folder
|
||||
|
||||
def capture_qr_auth_data(
|
||||
self,
|
||||
*,
|
||||
mobile: str = "",
|
||||
page_url: str = "",
|
||||
scene_type: str = "",
|
||||
step: int = 1,
|
||||
raw_params: dict[str, Any] | None = None,
|
||||
) -> Path:
|
||||
"""捕获扫码风控验证拦截数据并落盘。"""
|
||||
folder = self._ensure_target_dir(code="qr_auth")
|
||||
self.meta["auth_type"] = "qr_auth"
|
||||
self.meta["auth_name"] = "扫码二次验证 (qr_auth)"
|
||||
self.meta["page_url"] = page_url
|
||||
self.meta["scene_type"] = scene_type
|
||||
self.meta["mobile"] = mobile
|
||||
self.meta["step"] = step
|
||||
self.meta["prompts"] = {
|
||||
"title": "安全验证 (扫码二次验证 qr_auth)",
|
||||
"instruction": f"虎牙触发第 {step} 级扫码安全验证,需在手机 App 上确认",
|
||||
"mobile": mobile,
|
||||
}
|
||||
self.meta["submit_result"] = {
|
||||
"status": "qr_auth_required",
|
||||
"nextAuthUrl": page_url,
|
||||
}
|
||||
if raw_params:
|
||||
(folder / "params.json").write_text(
|
||||
json.dumps(raw_params, ensure_ascii=False, indent=2), encoding="utf-8"
|
||||
)
|
||||
self._flush_meta()
|
||||
|
||||
logger.warning(
|
||||
f"\n┌── [虎牙风控拦截捕获] ──────────────────────────────────────────\n"
|
||||
f"│ 类型: 扫码二次验证 (qr_auth) (sceneType: {self.scene_type or scene_type or '4'})\n"
|
||||
f"│ 账号: {self.account or '未指定'}\n"
|
||||
f"│ 阶段: 第 {step} 阶段\n"
|
||||
f"│ 提示: 虎牙要求在手机 App 上扫码确认或使用常用设备登录\n"
|
||||
f"│ 页面: {page_url[:80]}...\n"
|
||||
f"│ 存储目录: {self.target_dir}\n"
|
||||
f"└─────────────────────────────────────────────────────────────"
|
||||
)
|
||||
return folder
|
||||
|
||||
def record_submit_result(self, result: dict[str, Any]) -> None:
|
||||
"""记录验证码提交验证结果(authId、是否通过、是否流转下一阶段等)。"""
|
||||
has_auth = bool(result.get("authId"))
|
||||
has_next = bool(result.get("nextAuthUrl"))
|
||||
status = (
|
||||
"auth_success"
|
||||
if has_auth
|
||||
else ("passed_to_next_auth" if has_next else "failed")
|
||||
)
|
||||
self.meta["submit_result"] = {
|
||||
"status": status,
|
||||
"success": has_auth or has_next,
|
||||
"authId": result.get("authId"),
|
||||
"nextAuthUrl": result.get("nextAuthUrl"),
|
||||
"returnCode": result.get("returnCode"),
|
||||
"message": result.get("message") or result.get("description") or "",
|
||||
}
|
||||
self._flush_meta()
|
||||
|
||||
def _flush_meta(self) -> None:
|
||||
if self.target_dir:
|
||||
(self.target_dir / "meta.json").write_text(
|
||||
json.dumps(self.meta, ensure_ascii=False, indent=2), encoding="utf-8"
|
||||
)
|
||||
|
||||
def _log_summary(self) -> None:
|
||||
prompts = self.meta.get("prompts") or {}
|
||||
title = prompts.get("title") or "安全验证"
|
||||
question = (
|
||||
prompts.get("question")
|
||||
or prompts.get("default_instruction")
|
||||
or "按顺序点击"
|
||||
)
|
||||
word_size = prompts.get("word_size")
|
||||
word_info = f", 目标字数: {word_size}" if word_size else ""
|
||||
img_list = ", ".join(self.meta.get("images", {}).values())
|
||||
|
||||
logger.info(
|
||||
f"\n┌── [虎牙验证码捕获] ──────────────────────────────────────────\n"
|
||||
f"│ 类型: {self.meta.get('auth_name')} (sceneType: {self.scene_type or '默认'})\n"
|
||||
f"│ 账号: {self.account or '未指定'}\n"
|
||||
f"│ 标题: {title}\n"
|
||||
f"│ 提示语: {question}{word_info}\n"
|
||||
f"│ 存储目录: {self.target_dir}\n"
|
||||
f"│ 保存图片: {img_list}\n"
|
||||
f"└─────────────────────────────────────────────────────────────"
|
||||
)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,986 @@
|
||||
一声何满子
|
||||
一夜征人尽望乡
|
||||
一宿行人自可愁
|
||||
一岁一枯荣
|
||||
一弦一柱思华年
|
||||
一时回首月中看
|
||||
一望关山泪满巾
|
||||
一权寒梅白玉条
|
||||
一树梨花落晚风
|
||||
一树碧无情
|
||||
一水中分白鹭洲
|
||||
一片冰心在玉壶
|
||||
一片降幡出石头
|
||||
一行白鹭上青天
|
||||
一览众山小
|
||||
一路经行处
|
||||
一道残阳铺水中
|
||||
万径人踪灭
|
||||
万户捣衣声
|
||||
万方多难此登临
|
||||
万条垂下绿丝绦
|
||||
万里云罗一雁飞
|
||||
万里悲秋常作客
|
||||
万里长征人未还
|
||||
万里黄河绕黑山
|
||||
三山半落青天外
|
||||
三峡星河影动摇
|
||||
三日入厨下
|
||||
三春白雪归青冢
|
||||
上有黄鹂深树鸣
|
||||
下拂波纹破
|
||||
不及石尤风
|
||||
不堪人事日萧条
|
||||
不寝听金钥
|
||||
不尽长江滚滚来
|
||||
不得到辽西
|
||||
不才明主弃
|
||||
不把双眉斗画长
|
||||
不教胡马度阴山
|
||||
不敢过临洮
|
||||
不敢问来人
|
||||
不望入口复上身
|
||||
不着心源傍古人
|
||||
不知何处吹芦管
|
||||
不知心恨谁
|
||||
不知明镜里
|
||||
不知细叶谁裁出
|
||||
不知转入此中来
|
||||
不知近水花先发
|
||||
不见县门身即乐
|
||||
不见沙场愁杀人
|
||||
不解藏踪迹
|
||||
不论平地与山尖
|
||||
与汝救凶年
|
||||
且免向城卖黄犊
|
||||
世事波上舟
|
||||
世事茫茫难自料
|
||||
业复五铢钱
|
||||
东边日出西边雨
|
||||
东风不与周郎便
|
||||
两三星火是瓜洲
|
||||
两个黄鹂鸣翠柳
|
||||
两岸猿声啼不住
|
||||
两鬓苍苍十指黑
|
||||
中军置酒饮归客
|
||||
临行密密缝
|
||||
为他人作嫁衣裳
|
||||
为谁辛苦为谁甜
|
||||
乌衣巷口夕阳斜
|
||||
乍见翻疑梦
|
||||
乐游原上望昭陵
|
||||
九曲黄河万里沙
|
||||
乡书不可寄
|
||||
乡书何处达
|
||||
乡泪客中尽
|
||||
乱山残雪夜
|
||||
乾坤日夜浮
|
||||
二十一人同入蜀
|
||||
二十四桥明月夜
|
||||
二月春风似剪刀
|
||||
云峰隔水深
|
||||
云开远见洛阳城
|
||||
云母屏风烛影深
|
||||
五千貂锦丧胡尘
|
||||
五日雪纷纷
|
||||
五更疏欲断
|
||||
五更鼓角声悲壮
|
||||
五月虽热麦风清
|
||||
亲朋无一字
|
||||
人世几回伤往事
|
||||
人事音书漫寂寥
|
||||
人家不怨言语别
|
||||
人归山郭暗
|
||||
人归暮雪时
|
||||
人闲桂花落
|
||||
人间四月芳菲尽
|
||||
人间能得几回闻
|
||||
今上岳阳楼
|
||||
今夜偏知春气暖
|
||||
今夜未知何处宿
|
||||
今年开州杀刺吏
|
||||
今日水犹寒
|
||||
今日花开又一年
|
||||
今朝此为别
|
||||
今来县宰加朱绂
|
||||
从今四海为家日
|
||||
从来幽并客
|
||||
他乡复行役
|
||||
他年锦里经祠庙
|
||||
仙人垂两足
|
||||
以我独沉久
|
||||
伐薪烧炭南山中
|
||||
会当凌绝顶
|
||||
估客昼眠知浪静
|
||||
但使龙城飞将在
|
||||
但恐贼路多
|
||||
但见泪痕湿
|
||||
但见群鸥日日来
|
||||
但闻人语响
|
||||
低空有断云
|
||||
何处得秋霜
|
||||
何处还相遇
|
||||
何年致此身
|
||||
何当重相见
|
||||
何当金络脑
|
||||
何日平胡虏
|
||||
何物如人意
|
||||
余亦能高咏
|
||||
依旧烟笼十里堤
|
||||
便是生灵血染成
|
||||
借问酒家何处有
|
||||
偏在洛城东
|
||||
停舫临孤驿
|
||||
停车坐爱枫林晚
|
||||
偷采白莲回
|
||||
先遣小姑尝
|
||||
八年十二月
|
||||
八月萧关道
|
||||
六朝如梦鸟空啼
|
||||
共怜时世俭梳妆
|
||||
关山万里远征人
|
||||
关山征戍远
|
||||
关张无命欲何如
|
||||
关月冷相随
|
||||
农夫犹饿死
|
||||
农饥足旱田
|
||||
冥冥日沉夕
|
||||
冥冥花正开
|
||||
冥冥鸟去迟
|
||||
冰簟银床梦不成
|
||||
决眦入归鸟
|
||||
况彼无衣民
|
||||
况是蔡家亲
|
||||
凄凄去亲爱
|
||||
凄凉宝剑篇
|
||||
凄凉蜀故伎
|
||||
凝情自悄然
|
||||
几度隔山川
|
||||
几行归塞尽
|
||||
凤凰台上凤凰游
|
||||
凤去台空江自流
|
||||
凭君传语报平安
|
||||
凭轩涕泗流
|
||||
出塞复入塞
|
||||
初日郢门山
|
||||
前不见古人
|
||||
前年戍月支
|
||||
前年渝州杀刺史
|
||||
劝君更尽一杯酒
|
||||
功盖三分国
|
||||
势分三足鼎
|
||||
北斗七星高
|
||||
北斗阑干南斗斜
|
||||
北极朝廷终不改
|
||||
北阙休上书
|
||||
北风利如剑
|
||||
北风卷地白草折
|
||||
北风吹雁雪纷纷
|
||||
北风江上寒
|
||||
十二楼中月自明
|
||||
十五即乘潮
|
||||
十室八九贫
|
||||
十年一觉扬中梦
|
||||
千寻铁锁沉江底
|
||||
千山鸟飞绝
|
||||
千朵万朵压枝低
|
||||
千树万树梨花开
|
||||
千秋尚凛然
|
||||
千里江陵一日还
|
||||
千里莺啼绿映红
|
||||
千里黄云白日曛
|
||||
半入江风半入云
|
||||
半匹红纱一丈绫
|
||||
半卷红旗临易水
|
||||
半江瑟瑟半江红
|
||||
卖炭得钱何所营
|
||||
南山归敝庐
|
||||
南朝四百八十寺
|
||||
南浦清江万里桥
|
||||
卧龙跃马终黄土
|
||||
危樯独夜舟
|
||||
却喜晒谷天晴
|
||||
去岁曾经此县城
|
||||
去年花里逢君别
|
||||
去时雪满天山路
|
||||
县民无口不冤声
|
||||
参差连曲陌
|
||||
又疑瑶台镜
|
||||
双泪落君前
|
||||
双袖龙钟泪不干
|
||||
受降城外月如霜
|
||||
古台摇落后
|
||||
古寺残灯石壁间
|
||||
只在芦花浅水边
|
||||
只是当时已惘然
|
||||
只是近黄昏
|
||||
可怜九月初三夜
|
||||
可怜后主还祠庙
|
||||
可怜无定河边骨
|
||||
可怜身上衣正单
|
||||
叶间扑扑秋蛾生
|
||||
同到牵牛织女家
|
||||
名岂文章著
|
||||
名成八阵图
|
||||
后不见来者
|
||||
向晚意不适
|
||||
君看似花处
|
||||
含情欲说宫中事
|
||||
听钟未眠客
|
||||
吴宫花草埋幽径
|
||||
吴楚东南坼
|
||||
吹沙复喷石
|
||||
呼作白玉盘
|
||||
呼儿登山收橡实
|
||||
咸言意气高
|
||||
哀哀夜哭向秋云
|
||||
哥舒夜带刀
|
||||
唯烧蒿棘火
|
||||
唯留一湖水
|
||||
唯见林花落
|
||||
商女不知亡国恨
|
||||
啼时惊妾梦
|
||||
啾啾栖鸟过
|
||||
嗟君此别意何如
|
||||
四海无闲田
|
||||
回乐烽前沙似雪
|
||||
回头却向秦云器
|
||||
回看天际下中流
|
||||
回观村闾间
|
||||
回车叱牛牵向北
|
||||
因风想玉珂
|
||||
圣代即今多雨露
|
||||
坐看牵牛织女星
|
||||
垂死病中惊坐起
|
||||
城下没全师
|
||||
城中增暮寒
|
||||
城里月明时
|
||||
城阙夜千重
|
||||
城高遮得贼
|
||||
塞上燕脂凝夜紫
|
||||
塞外征行无尽日
|
||||
壮发上冲冠
|
||||
壶浆满别筵
|
||||
处处共芦草
|
||||
复照青苔上
|
||||
夕阳下通津
|
||||
夕阳依旧垒
|
||||
夕阳无限好
|
||||
多少楼台烟雨中
|
||||
多病故人疏
|
||||
多难识君迟
|
||||
夜半钟声到客船
|
||||
夜来城外一尺雪
|
||||
夜泊秦淮近酒家
|
||||
夜深还过女墙来
|
||||
夜长人自起
|
||||
夜静春山空
|
||||
大漠无兵阻
|
||||
大漠沙如雪
|
||||
天下谁人不识君
|
||||
天地一沙鸥
|
||||
天地英雄气
|
||||
天寒白屋贫
|
||||
天山雪后海风寒
|
||||
天涯哭此时
|
||||
天涯孤棹还
|
||||
天涯涕泪一身遥
|
||||
天涯霜雪霁寒宵
|
||||
天秋月又满
|
||||
天街小雨润如酥
|
||||
天阶夜色凉如水
|
||||
夷歌数处起渔樵
|
||||
如今直上银河去
|
||||
妇女多在官军中
|
||||
始见沙上鸟
|
||||
姑苏城外寒山寺
|
||||
委曲结绳文
|
||||
嫁与弄潮儿
|
||||
嫁得瞿塘贾
|
||||
嫦娥应悔偷灵药
|
||||
孤壁野僧邻
|
||||
孤帆天际看
|
||||
孤帆远影碧空尽
|
||||
孤灯寒照雨
|
||||
孤灯闻楚角
|
||||
孤独异乡人
|
||||
孤舟蓑笠翁
|
||||
孤飞自可疑
|
||||
官应老病休
|
||||
客从东方来
|
||||
客舍青青柳色新
|
||||
客路青山外
|
||||
宫使驱将惜不得
|
||||
宫花寂寞红
|
||||
家书到隔年
|
||||
寂寂花时闭院门
|
||||
寄卧郊扉久
|
||||
寒云夜夜自飞还
|
||||
寒塘欲下迟
|
||||
寒天古木悲
|
||||
寒灯思旧事
|
||||
寒灯独夜人
|
||||
寒磬满空林
|
||||
寒衣不要宽
|
||||
寒雨连江夜入吴
|
||||
寒食东风御柳斜
|
||||
寥落古行宫
|
||||
对棋陪谢傅
|
||||
将军夜引弓
|
||||
将军角弓不得控
|
||||
将牛何处去
|
||||
尊酒慰离颜
|
||||
小园花乱飞
|
||||
小娃撑小艇
|
||||
小时不识月
|
||||
少孤为客早
|
||||
屈子怨何深
|
||||
山中相送罢
|
||||
山回路转不见君
|
||||
山围故国周遭在
|
||||
山寺桃花始盛开
|
||||
山店门前一妇人
|
||||
山形依旧枕寒流
|
||||
山色有无中
|
||||
山风吹空林
|
||||
岁岁金河复玉关
|
||||
岁暮锄犁傍空室
|
||||
岁暮阴阳催短景
|
||||
岐王宅里寻常见
|
||||
岩上无心云相逐
|
||||
岭外音书绝
|
||||
岱宗夫如何
|
||||
崔九堂前几度闻
|
||||
川原杳何极
|
||||
巫峡啼猿数行泪
|
||||
市南门外泥中歇
|
||||
布絮不蔽身
|
||||
帝乡明日到
|
||||
平明寻白羽
|
||||
平明送客楚江孤
|
||||
平沙万里绝人烟
|
||||
平沙日未没
|
||||
平海夕漫漫
|
||||
平生自有分
|
||||
年年移帐雪中天
|
||||
幽人应未眠
|
||||
庄生晓梦迷蝴蝶
|
||||
应傍战场开
|
||||
庭树不知人去尽
|
||||
建业暮钟时
|
||||
开轩面场圃
|
||||
弹琴复长啸
|
||||
强欲登高去
|
||||
归时每日斜
|
||||
归棹洛阳人
|
||||
归雁洛阳边
|
||||
归马识残旗
|
||||
征人倚戍楼
|
||||
待到重阳日
|
||||
徒令上将挥神笔
|
||||
徒劳恨费声
|
||||
得相能开国
|
||||
徘徊乱绕空
|
||||
心忧炭贱愿天寒
|
||||
心断新丰酒
|
||||
快走踏清秋
|
||||
念天地之悠悠
|
||||
念尔独何之
|
||||
忽如一夜春风来
|
||||
怀君属秋夜
|
||||
怅卧新春白袷衣
|
||||
怜渠直道当时语
|
||||
总为浮云能蔽日
|
||||
总是玉关情
|
||||
悠扬绿萝影
|
||||
惆怅南朝事
|
||||
惊秋风水凉
|
||||
惊飞远映碧山去
|
||||
惟将迟暮供多病
|
||||
惟残一人出骆谷
|
||||
惟见长江天际流
|
||||
愁云惨淡万里凝
|
||||
愁坐夜待晨
|
||||
意恐迟迟归
|
||||
愧君相见频
|
||||
愿君多采撷
|
||||
慈母手中线
|
||||
戎马关山北
|
||||
我亦举家清
|
||||
我家襄水曲
|
||||
所得是沾衣
|
||||
手把文书口称敕
|
||||
打起黄莺儿
|
||||
扣门无犬吠
|
||||
把剑觅徐君
|
||||
把酒话桑麻
|
||||
折戟沉沙铁未销
|
||||
报君黄金台上意
|
||||
报得三春晖
|
||||
报道山中去
|
||||
拟托良媒益自伤
|
||||
掩泣空相向
|
||||
提携玉龙为君死
|
||||
故乡杳无际
|
||||
故人具鸡黍
|
||||
故人殊未来
|
||||
故人江海别
|
||||
故人西辞黄鹤楼
|
||||
故关衰草遍
|
||||
故园东望路漫漫
|
||||
故园芜已平
|
||||
故国三千里
|
||||
故垒萧萧芦荻秋
|
||||
敢将十指夸针巧
|
||||
散入珠帘湿罗幕
|
||||
散步咏凉天
|
||||
数问夜如何
|
||||
断雁警愁眠
|
||||
斯人不可闻
|
||||
新妆宜面下朱楼
|
||||
新知遭薄俗
|
||||
旅馆无良伴
|
||||
无人收废帐
|
||||
无人送酒来
|
||||
无将故人酒
|
||||
无情最是台城柳
|
||||
无边落木萧萧下
|
||||
无限风光尽被占
|
||||
日暮东风怨啼鸟
|
||||
日暮乡关何处是
|
||||
日暮客愁新
|
||||
日暮掩柴扉
|
||||
日暮欲何之
|
||||
日暮汉宫传蜡烛
|
||||
日暮秋风起
|
||||
日暮苍山远
|
||||
日暮飞鸟还
|
||||
旧好隔良缘
|
||||
旧时王谢堂前燕
|
||||
早知潮有信
|
||||
早雁忽为双
|
||||
早雁拂金河
|
||||
时时误拂弦
|
||||
时鸣春涧中
|
||||
明日岁华新
|
||||
明月来相照
|
||||
明朝挂帆席
|
||||
明朝有封事
|
||||
昔人已乘黄鹤去
|
||||
昔日经行人尽去
|
||||
昔日长城战
|
||||
昔时人已没
|
||||
昔闻洞庭水
|
||||
星临万户动
|
||||
星垂平野阔
|
||||
星月满空江
|
||||
春城无处不飞花
|
||||
春愁黯黯独成眠
|
||||
春来发几枝
|
||||
春来还发旧时花
|
||||
春潮带雨晚来急
|
||||
春种一粒粟
|
||||
春草明年绿
|
||||
春风吹又生
|
||||
春风对青冢
|
||||
昨别今已春
|
||||
晋代衣冠成古丘
|
||||
晓汲清湘燃楚竹
|
||||
晓驾炭车辗冰辙
|
||||
晚来天欲雪
|
||||
晚见雁行频
|
||||
晴川历历汉阳树
|
||||
暂时分手莫踌躇
|
||||
暗风吹雨入寒窗
|
||||
暮雨相呼失
|
||||
曲折仍圆旋
|
||||
更有明朝恨
|
||||
更深月色半人家
|
||||
曾驱十万师
|
||||
最是一年春好处
|
||||
月傍九霄多
|
||||
月出惊山鸟
|
||||
月殿影开闻夜漏
|
||||
月涌大江流
|
||||
月落乌啼霜满天
|
||||
有城遮不得
|
||||
望帝春心托杜鹃
|
||||
朝朝误妾期
|
||||
朝朝马策与刀环
|
||||
朝来逢着旧将军
|
||||
朝辞白帝彩云间
|
||||
木落雁南度
|
||||
未必逢矰缴
|
||||
未有涓埃答圣朝
|
||||
未谙姑食性
|
||||
本以高难饱
|
||||
朱雀桥边野草花
|
||||
杜甫天才颇绝伦
|
||||
来舞魏宫前
|
||||
杨柳青青江水平
|
||||
杯粘落花片
|
||||
杳杳种声晚
|
||||
松月夜窗虚
|
||||
板桥人渡泉声
|
||||
极目箫条三两家
|
||||
林暗草惊风
|
||||
林表明霁色
|
||||
枫叶落纷纷
|
||||
柴门闻犬吠
|
||||
树色随关迥
|
||||
桂树何团团
|
||||
梁园日暮乱飞鸦
|
||||
梁父吟成恨有余
|
||||
楚塞三湘接
|
||||
楚江微雨里
|
||||
楚腰纤细掌中轻
|
||||
樽酒家贫只旧醅
|
||||
檐头索索缲车鸣
|
||||
欲去问西家
|
||||
欲得周郎顾
|
||||
欲把一麾江海去
|
||||
欲祭疑君在
|
||||
欸乃一声山水绿
|
||||
正是江南好风景
|
||||
此地别燕丹
|
||||
此地空余黄鹤楼
|
||||
此夕闻君谪九江
|
||||
此情可待成追忆
|
||||
此曲只应天上有
|
||||
此物最相思
|
||||
死生长别离
|
||||
残云归太华
|
||||
残宵犹得梦依稀
|
||||
残月下章台
|
||||
残灯无焰影幢幢
|
||||
残萤西玉露
|
||||
残钟广陵树
|
||||
殿前兵马虽骁雄
|
||||
每寻诗卷似情亲
|
||||
每逢佳节倍思亲
|
||||
水寒风似刀
|
||||
水村山郭酒旗风
|
||||
水精帘卷近秋河
|
||||
永怀愁不寐
|
||||
汗滴禾下土
|
||||
江上几人在
|
||||
江春入旧年
|
||||
江村月落正堪眠
|
||||
江枫渔火对愁眠
|
||||
江流天地外
|
||||
江流石不转
|
||||
江清月近人
|
||||
江皋闻曙钟
|
||||
江雨霏霏江草齐
|
||||
沅湘流不尽
|
||||
没在石棱中
|
||||
沧江好烟月
|
||||
沧海月明珠有泪
|
||||
河声入海遥
|
||||
沾襟比散丝
|
||||
沿洄安得住
|
||||
泛泛入烟雾
|
||||
波澜动远空
|
||||
洗手作羹汤
|
||||
洛阳亲友如相问
|
||||
流水无情草自春
|
||||
流落征南将
|
||||
浦树远含滋
|
||||
浩浩风起波
|
||||
浩然离故关
|
||||
浪淘风簸自天涯
|
||||
浮萍一道开
|
||||
海内风尘诸弟隔
|
||||
海日生残夜
|
||||
海门深不见
|
||||
淮南一叶下
|
||||
淮水东边旧时月
|
||||
深坐颦蛾眉
|
||||
深宫二十年
|
||||
深林人不知
|
||||
深竹暗浮烟
|
||||
深锁春光一院愁
|
||||
清明时节雨纷纷
|
||||
清明有味是无能
|
||||
清瑟怨遥夜
|
||||
渐与骨肉远
|
||||
渔翁夜傍西岩宿
|
||||
渚云低暗度
|
||||
渚清沙白鸟飞回
|
||||
渭城朝雨浥轻尘
|
||||
游子身上衣
|
||||
湖光秋月两相和
|
||||
湛湛碧涟漪
|
||||
溪花与禅意
|
||||
满面尘灰烟火色
|
||||
漠漠帆来重
|
||||
潦倒新停浊酒杯
|
||||
潭面无风镜未磨
|
||||
潮平两岸阔
|
||||
潮打空城寂寞回
|
||||
潮落夜江斜月里
|
||||
瀚海阑干百丈冰
|
||||
灞原风雨定
|
||||
灯下白头人
|
||||
烟波江上使人愁
|
||||
烟笼寒水月笼沙
|
||||
烟花三月下扬州
|
||||
烟销日出不见人
|
||||
烦君最相警
|
||||
燕山月似钩
|
||||
牛困人饥日已高
|
||||
牛渚西江夜
|
||||
牛马千群逐暖川
|
||||
牧童遥指杏花村
|
||||
犹似造书时
|
||||
犹埋云外峰
|
||||
犹是春闺梦里人
|
||||
犹自梦渔樵
|
||||
犹见孤帆一日程
|
||||
狐裘不暖锦衾薄
|
||||
独在异乡为异客
|
||||
独坐幽篁里
|
||||
独夜忆秦关
|
||||
独怆然而涕下
|
||||
独怜幽草涧边生
|
||||
独立三边静
|
||||
独钓寒江雪
|
||||
猿鸟犹疑畏简书
|
||||
玉人何处教吹箫
|
||||
玉垒浮云变古今
|
||||
玉楼天半起笙歌
|
||||
玉铛缄札何由达
|
||||
王孙归不归
|
||||
王浚楼船下益州
|
||||
珠箔飘灯独自归
|
||||
甘棠一无树
|
||||
生儿不象贤
|
||||
田家衣食无厚薄
|
||||
甲光向日金鳞开
|
||||
男声欣欣女颜悦
|
||||
留连戏蝶时时舞
|
||||
留醉与山翁
|
||||
疏雨过中条
|
||||
疑是经冬雪未销
|
||||
疲马卧长坂
|
||||
登舟望秋月
|
||||
白云依静渚
|
||||
白云千载空悠悠
|
||||
白云生处有人家
|
||||
白兔捣药成
|
||||
白发三千丈
|
||||
白发催年老
|
||||
白头宫女在
|
||||
白帝城边古木疏
|
||||
白日落梁州
|
||||
白银盘里一青螺
|
||||
白门寥落意多违
|
||||
白骨乱蓬蒿
|
||||
百年多病独登台
|
||||
的知输得官家足
|
||||
皆共尘沙老
|
||||
盘飧市远无兼味
|
||||
相伴有田父
|
||||
相对亦忘言
|
||||
相悲各问年
|
||||
相欢惟牧童
|
||||
相留畏晓钟
|
||||
相送临高台
|
||||
相送情无限
|
||||
眼穿仍仍欲归
|
||||
矜夸紫骝好
|
||||
知有前期在
|
||||
碛里征人三十万
|
||||
碧天如水夜云轻
|
||||
碧海青天夜夜心
|
||||
碧玉妆成一树高
|
||||
离别正堪悲
|
||||
离披草书字
|
||||
离杯惜共传
|
||||
离离原上草
|
||||
秋入望乡心
|
||||
秋尽江南草木凋
|
||||
秋收万颗子
|
||||
秋来未著花
|
||||
秋雁又南回
|
||||
秋风吹不尽
|
||||
秋风落叶满空山
|
||||
秦时明月汉时关
|
||||
积雪浮云端
|
||||
移家虽带郭
|
||||
移舟泊烟渚
|
||||
税重多贫户
|
||||
穷边有客游
|
||||
空园白露滴
|
||||
空山不见人
|
||||
空山无鸟迹
|
||||
空山松子落
|
||||
空忆谢将军
|
||||
空阶有鸟迹
|
||||
窗含西岭千秋雪
|
||||
竹柏皆冻死
|
||||
筑城须努力
|
||||
管乐有才真不忝
|
||||
粒粒皆辛苦
|
||||
精灵自来去
|
||||
系向牛头充炭值
|
||||
素手玉房前
|
||||
繁华事散逐香尘
|
||||
红叶晚萧萧
|
||||
红楼隔雨相望冷
|
||||
红泥小火炉
|
||||
红豆生南国
|
||||
纵暴略与羌浑同
|
||||
纵然一夜风吹去
|
||||
纷纷暮雪下辕门
|
||||
细草微风岸
|
||||
终南阴岭秀
|
||||
终见降王走传车
|
||||
经冬复立春
|
||||
绕弦风雨哀
|
||||
绝胜烟柳满皇都
|
||||
绿树村边合
|
||||
绿蚁新醅酒
|
||||
缘愁似个长
|
||||
罢归无旧业
|
||||
羁危万里身
|
||||
羁旅长堪醉
|
||||
羁泊欲穷年
|
||||
美人卷珠帘
|
||||
美人相并立琼轩
|
||||
群捕鱼儿溪影中
|
||||
群盗相随剧虎狼
|
||||
翩翩两骑来是谁
|
||||
翻疑梦里逢
|
||||
老农家贫在山住
|
||||
老去恋明时
|
||||
老松欹侧卧
|
||||
老病有孤舟
|
||||
耆老遮归路
|
||||
耕彼故城东
|
||||
耕种山田三四亩
|
||||
肠断未忍扫
|
||||
肯与邻翁相对饮
|
||||
胡天八月即飞雪
|
||||
胡琴琵琶与羌笛
|
||||
胡风冻合鸊鹈泉
|
||||
能饮一杯无
|
||||
自在娇莺恰恰啼
|
||||
自将磨洗认前朝
|
||||
自觉洞庭波
|
||||
自说二女啮臂时
|
||||
自说夫因征战死
|
||||
至今窥牧马
|
||||
舍南舍北皆春水
|
||||
舟人夜语觉潮生
|
||||
船中养犬长食肉
|
||||
良人罢远征
|
||||
艰难苦恨繁霜鬓
|
||||
花径不曾缘客扫
|
||||
花近高楼伤客心
|
||||
花隐掖垣暮
|
||||
芳心向春尽
|
||||
芳草已云暮
|
||||
芳草萋萋鹦鹉洲
|
||||
芳草闭闲门
|
||||
苍苍竹林寺
|
||||
苗疏税多不得食
|
||||
苜蓿峰边逢立春
|
||||
苦恨年年压金线
|
||||
苦战应憔悴
|
||||
茅檐日午鸡鸣
|
||||
茫茫江汉上
|
||||
荆歌艳楚腰
|
||||
荆门九派通
|
||||
草没城边路
|
||||
草色遥看近却无
|
||||
荒居旧业贫
|
||||
荒戍落黄叶
|
||||
荡胸生层云
|
||||
荷笠带斜阳
|
||||
莓苔见屐痕
|
||||
莫嗔焙茶烟暗
|
||||
莫学游侠儿
|
||||
莫愁前路无知己
|
||||
莫教枝上啼
|
||||
莺啼送客闻
|
||||
萧萧枫树林
|
||||
落叶他乡树
|
||||
落帆逗淮镇
|
||||
落日清江里
|
||||
落花时节又逢君
|
||||
落花犹似坠楼人
|
||||
落魄江湖载酒行
|
||||
葫芦河上泪沾巾
|
||||
蓝田日暖玉生烟
|
||||
蓬门今始为君开
|
||||
蓬门未识绮罗香
|
||||
蕃情似此水
|
||||
蕃汉断消息
|
||||
薄宦梗犹泛
|
||||
虫声新透绿窗纱
|
||||
蜻蜓飞上玉搔头
|
||||
蝉鸣空桑林
|
||||
行人去不息
|
||||
行到中庭数花朵
|
||||
行舟绿水前
|
||||
衡阳归雁几封书
|
||||
衣上灞陵雨
|
||||
襄阳好风日
|
||||
西出阳关无故人
|
||||
西山寇盗莫相侵
|
||||
西山白雪三城戍
|
||||
西楼望月几回圆
|
||||
西江贾客珠百斛
|
||||
西风生翠萝
|
||||
角声满天秋色里
|
||||
誓扫匈奴不顾身
|
||||
谁爱风流高格调
|
||||
谁知盘中餐
|
||||
谁言寸草心
|
||||
调角断清秋
|
||||
赢得青楼薄幸名
|
||||
走马西来欲到天
|
||||
跨马出郊时极目
|
||||
路上行人欲断魂
|
||||
路出寒云外
|
||||
身上衣裳口中食
|
||||
身多疾病思田里
|
||||
转于童仆亲
|
||||
轮台东门送君去
|
||||
轻烟散入五侯家
|
||||
轻生一剑知
|
||||
轻罗小扇扑流萤
|
||||
轻舟已过万重山
|
||||
输入官仓化为土
|
||||
辞家见月两回圆
|
||||
过雨看松色
|
||||
近乡情更怯
|
||||
近泪无干土
|
||||
近种篱边菊
|
||||
返影入深林
|
||||
还作江南会
|
||||
还来就菊花
|
||||
远山晴更多
|
||||
远梦归侵晓
|
||||
远目寒山石径斜
|
||||
远路应悲春晼晚
|
||||
迢递三巴路
|
||||
迢递送斜晖
|
||||
迥临村路傍溪桥
|
||||
迷津欲有问
|
||||
造化钟神秀
|
||||
遍插茱萸少一人
|
||||
道是无晴还有晴
|
||||
遗恨失吞吴
|
||||
遥夜泛清瑟
|
||||
遥怜故园菊
|
||||
遥望洞庭山水翠
|
||||
遥知兄弟登高处
|
||||
遥隔楚云端
|
||||
邀我至田家
|
||||
邑有流亡愧俸钱
|
||||
那堪正漂泊
|
||||
那得泪潸然
|
||||
郡邑浮前浦
|
||||
都护铁衣冷难著
|
||||
采山因买斧
|
||||
采得百花成蜜后
|
||||
采莲从小惯
|
||||
野哭几家闻战伐
|
||||
野客漱流时
|
||||
野寺来人少
|
||||
野寺荒台晚
|
||||
野径入桑麻
|
||||
野旷天低树
|
||||
野渡无人舟自横
|
||||
野火烧不尽
|
||||
野蚕作茧人不取
|
||||
金陵津渡小山楼
|
||||
金陵王气黯然收
|
||||
钓罢归来不系船
|
||||
铜雀春深锁二乔
|
||||
银烛秋光冷画屏
|
||||
销愁又几千
|
||||
锄禾日当午
|
||||
锦城丝管日纷纷
|
||||
锦江春色来天地
|
||||
锦瑟无端五十弦
|
||||
长亭酒一瓢
|
||||
长安一片月
|
||||
长安不见使人愁
|
||||
长恨春归无觅处
|
||||
长愿向南流
|
||||
长江独至今
|
||||
长河渐落晓星沉
|
||||
门泊东吴万里船
|
||||
门系钓鱼船
|
||||
问客何为来
|
||||
问言与谁餐
|
||||
闲坐说玄宗
|
||||
闲爱孤云静爱僧
|
||||
闺中只是空相忆
|
||||
闺阁别离难
|
||||
闻道杀人汉水上
|
||||
闻道欲来相问讯
|
||||
闻郎江上唱歌声
|
||||
阴阳割昏晓
|
||||
随山到水源
|
||||
隔江犹唱后庭花
|
||||
隔篱呼取尽馀杯
|
||||
难分此夜中
|
||||
雁下芦洲白
|
||||
雁声远过潇湘去
|
||||
雨中黄叶树
|
||||
雪上空留马行处
|
||||
雪衣雪发青玉嘴
|
||||
霜叶红于二月花
|
||||
霜重鼓寒声不起
|
||||
露似珍珠月似弓
|
||||
露草泣寒虫
|
||||
青天无片云
|
||||
青山独归远
|
||||
青山郭外斜
|
||||
青山隐隐水迢迢
|
||||
青枫江上秋帆远
|
||||
青楼自管弦
|
||||
青海戍头空有月
|
||||
青阳逼岁除
|
||||
静夜四无邻
|
||||
风云长为护储胥
|
||||
风吹城上树
|
||||
风尘何所期
|
||||
风急天高猿啸哀
|
||||
风掣红旗冻不翻
|
||||
风枝惊暗鹊
|
||||
风正一帆悬
|
||||
风送宫嫔笑语和
|
||||
风雪夜归人
|
||||
飏飏燕新乳
|
||||
飒飒如有人
|
||||
飘飘何所似
|
||||
飞入寻常百姓家
|
||||
飞在青云端
|
||||
飞雪带春风
|
||||
食人更肯留妻子
|
||||
饮马渡秋水
|
||||
马上相逢无纸笔
|
||||
驱车登古原
|
||||
驻马别孤坟
|
||||
驻马衔杯问谪居
|
||||
高树晓还密
|
||||
高阁客竟去
|
||||
高风汉阳渡
|
||||
鬓丝生几缕
|
||||
鸣筝金粟柱
|
||||
鹦鹉前头不敢言
|
||||
麦收上场绢在轴
|
||||
黄叶仍风雨
|
||||
黄四娘家花满蹊
|
||||
黄尘足今古
|
||||
黄沙碛里本无春
|
||||
黄衣使者白衫儿
|
||||
黄鹤一去不复返
|
||||
黑云压城城欲催
|
||||
黯黯见临洮
|
||||
齐鲁青未了
|
||||
@@ -9,17 +9,21 @@ import random
|
||||
import time
|
||||
import uuid
|
||||
from collections.abc import Mapping
|
||||
from datetime import UTC, datetime
|
||||
from http.cookies import SimpleCookie
|
||||
from pathlib import Path
|
||||
from typing import Any, cast
|
||||
from urllib.parse import parse_qs, quote, urlparse
|
||||
|
||||
import re
|
||||
import cv2
|
||||
import execjs
|
||||
import numpy as np
|
||||
import requests
|
||||
from loguru import logger
|
||||
from PIL import Image
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
|
||||
from .capture import CaptchaCaptureRecord
|
||||
from .ocr import HuyaCaptchaOcr, default_ocr
|
||||
from .track import format_track, generate_slide_track
|
||||
|
||||
@@ -43,11 +47,14 @@ class HuyaQrAuthRequiredError(HuyaVerificationError):
|
||||
|
||||
|
||||
class HuyaSmsAuthRequiredError(HuyaVerificationError):
|
||||
"""风控策略为短信验证(dx_auth),需接码/收码后提交。"""
|
||||
"""风控策略为短信验证(sms_auth/sms_auth2),需接码/收码后提交。"""
|
||||
|
||||
def __init__(self, challenge_payload: dict | str, message: str = ""):
|
||||
def __init__(
|
||||
self, challenge_payload: dict | str, message: str = "", mobile: str = ""
|
||||
):
|
||||
self.challenge_payload = challenge_payload
|
||||
super().__init__(message or "虎牙风控要求短信验证")
|
||||
self.mobile = mobile
|
||||
super().__init__(message or f"虎牙风控要求短信验证,手机号: {mobile or '?'}")
|
||||
|
||||
|
||||
def _cookie_mapping(cookie: Mapping[str, str] | str | None) -> dict[str, str]:
|
||||
@@ -64,6 +71,8 @@ def _cookie_mapping(cookie: Mapping[str, str] | str | None) -> dict[str, str]:
|
||||
class HuyaVerificationSolver:
|
||||
"""处理虎牙 returnCode=10030/10039 后的滑块或点选验证。"""
|
||||
|
||||
_cloze_corpus: set[str] | None = None
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
cookie: Mapping[str, str] | str | None = None,
|
||||
@@ -76,6 +85,10 @@ class HuyaVerificationSolver:
|
||||
app_id: str = "5002",
|
||||
page_url: str | None = None,
|
||||
use_touch_events: bool = False,
|
||||
account: str = "",
|
||||
capture_dir: str | Path | None = None,
|
||||
enable_capture: bool = True,
|
||||
sms_url: str = "",
|
||||
):
|
||||
self.cookie = _cookie_mapping(cookie)
|
||||
self.ua = ua or DEFAULT_UA
|
||||
@@ -102,6 +115,13 @@ class HuyaVerificationSolver:
|
||||
self.timeout = timeout
|
||||
self.ocr = ocr or default_ocr()
|
||||
|
||||
self.account = account
|
||||
self.capture_dir = capture_dir
|
||||
self.enable_capture = enable_capture
|
||||
self.sms_url = sms_url
|
||||
self.current_capture: CaptchaCaptureRecord | None = None
|
||||
self.auth_type = ""
|
||||
|
||||
self.huyapk = ""
|
||||
self.scene_type = ""
|
||||
self.code = ""
|
||||
@@ -121,7 +141,7 @@ class HuyaVerificationSolver:
|
||||
@staticmethod
|
||||
def _decode_image(base64_string: str | None) -> bytes:
|
||||
if not base64_string:
|
||||
raise HuyaVerificationError("验证码图片为空")
|
||||
return b""
|
||||
if ";base64," in base64_string:
|
||||
base64_string = base64_string.split(";base64,")[-1]
|
||||
return base64.b64decode(base64_string)
|
||||
@@ -196,6 +216,162 @@ class HuyaVerificationSolver:
|
||||
_, _, _, max_loc = cv2.minMaxLoc(result)
|
||||
return int(max_loc[0])
|
||||
|
||||
@staticmethod
|
||||
def _opencv_rotate_angle(
|
||||
background_bytes: bytes,
|
||||
puzzle_bytes: bytes,
|
||||
default_cx: float = 195.0,
|
||||
default_cy: float = 90.0,
|
||||
) -> int:
|
||||
"""通过圆周边界颜色与边缘连续性计算旋转验证码的最佳旋转角度 (0~359度)。"""
|
||||
bg = cv2.imdecode(np.frombuffer(background_bytes, np.uint8), cv2.IMREAD_COLOR)
|
||||
puzzle_raw = cv2.imdecode(
|
||||
np.frombuffer(puzzle_bytes, np.uint8), cv2.IMREAD_UNCHANGED
|
||||
)
|
||||
if bg is None or puzzle_raw is None:
|
||||
raise HuyaVerificationError("旋转验证码图片解码失败")
|
||||
|
||||
bg_gray = cv2.cvtColor(bg, cv2.COLOR_BGR2GRAY).astype(np.float32)
|
||||
p_gray = cv2.cvtColor(puzzle_raw[:, :, :3], cv2.COLOR_BGR2GRAY).astype(
|
||||
np.float32
|
||||
)
|
||||
|
||||
gray_u8 = cv2.cvtColor(bg, cv2.COLOR_BGR2GRAY)
|
||||
black_mask = (gray_u8 < 25).astype(np.uint8) * 255
|
||||
contours, _ = cv2.findContours(
|
||||
black_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
|
||||
)
|
||||
cx, cy, r_hole = default_cx, default_cy, 58.0
|
||||
for c in contours:
|
||||
if cv2.contourArea(c) > 3000:
|
||||
(tcx, tcy), tr = cv2.minEnclosingCircle(c)
|
||||
if abs(tcx - default_cx) < 35 and abs(tcy - default_cy) < 25:
|
||||
cx, cy, r_hole = tcx, tcy, tr
|
||||
break
|
||||
|
||||
ph, pw = puzzle_raw.shape[:2]
|
||||
pcx, pcy = pw / 2.0, ph / 2.0
|
||||
|
||||
angles = np.linspace(0, 2 * np.pi, 360, endpoint=False)
|
||||
r_out_list = [r_hole + 2.0, r_hole + 4.0]
|
||||
r_in_list = [r_hole - 5.0, r_hole - 7.0]
|
||||
|
||||
bg_outer_vals_list = []
|
||||
for ro in r_out_list:
|
||||
bx = cx + ro * np.cos(angles)
|
||||
by = cy + ro * np.sin(angles)
|
||||
vals = cv2.remap(
|
||||
bg_gray,
|
||||
bx.astype(np.float32),
|
||||
by.astype(np.float32),
|
||||
cv2.INTER_LINEAR,
|
||||
).flatten()
|
||||
bg_outer_vals_list.append(vals)
|
||||
bg_outer_vals = np.mean(bg_outer_vals_list, axis=0)
|
||||
|
||||
best_deg = 0
|
||||
best_corr = -1.0
|
||||
for deg in range(360):
|
||||
rad = np.radians(deg)
|
||||
p_inner_vals_list = []
|
||||
for ri in r_in_list:
|
||||
px = pcx + ri * np.cos(angles - rad)
|
||||
py = pcy + ri * np.sin(angles - rad)
|
||||
pvals = cv2.remap(
|
||||
p_gray,
|
||||
px.astype(np.float32),
|
||||
py.astype(np.float32),
|
||||
cv2.INTER_LINEAR,
|
||||
).flatten()
|
||||
p_inner_vals_list.append(pvals)
|
||||
p_inner_vals = np.mean(p_inner_vals_list, axis=0)
|
||||
|
||||
corr = np.corrcoef(bg_outer_vals, p_inner_vals)[0, 1]
|
||||
if corr > best_corr:
|
||||
best_corr = corr
|
||||
best_deg = deg
|
||||
|
||||
return best_deg
|
||||
|
||||
@classmethod
|
||||
def _get_cloze_corpus(cls) -> set[str]:
|
||||
if cls._cloze_corpus is None:
|
||||
corpus: set[str] = set()
|
||||
base_dir = Path(__file__).resolve().parent
|
||||
for filename in ("idioms.txt", "poems.txt"):
|
||||
file_path = base_dir / filename
|
||||
if file_path.is_file():
|
||||
try:
|
||||
corpus.update(
|
||||
file_path.read_text(encoding="utf-8").splitlines()
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.debug(f"[solver] 加载语料 {filename} 失败: {exc}")
|
||||
cls._cloze_corpus = corpus
|
||||
return cls._cloze_corpus
|
||||
|
||||
@classmethod
|
||||
def _find_chinese_font(
|
||||
cls, size: int = 30
|
||||
) -> ImageFont.FreeTypeFont | ImageFont.ImageFont:
|
||||
font_candidates = [
|
||||
"/System/Library/Fonts/PingFang.ttc",
|
||||
"/System/Library/Fonts/STHeiti Medium.ttc",
|
||||
"/System/Library/Fonts/STHeiti Light.ttc",
|
||||
"/System/Library/Fonts/Hiragino Sans GB.ttc",
|
||||
"/System/Library/Fonts/Supplemental/Songti.ttc",
|
||||
"/usr/share/fonts/truetype/wqy/wqy-microhei.ttc",
|
||||
"/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc",
|
||||
"/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
|
||||
"/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf",
|
||||
"C:/Windows/Fonts/msyh.ttc",
|
||||
"C:/Windows/Fonts/simhei.ttf",
|
||||
]
|
||||
for font_path in font_candidates:
|
||||
if Path(font_path).is_file():
|
||||
try:
|
||||
return ImageFont.truetype(font_path, size)
|
||||
except Exception:
|
||||
continue
|
||||
return ImageFont.load_default()
|
||||
|
||||
@classmethod
|
||||
def _synthesize_text_tip(cls, chars: list[str]) -> bytes:
|
||||
"""为没有提示小图的文本点选题目动态合成 44px 高度的提示图像。"""
|
||||
if not chars:
|
||||
return b""
|
||||
font = cls._find_chinese_font(30)
|
||||
char_box_size = 44
|
||||
total_width = len(chars) * char_box_size
|
||||
image = Image.new("RGB", (total_width, char_box_size), (255, 255, 255))
|
||||
draw = ImageDraw.Draw(image)
|
||||
for i, ch in enumerate(chars):
|
||||
offset_x = i * char_box_size + 7
|
||||
offset_y = 5
|
||||
draw.text((offset_x, offset_y), ch, fill=(0, 0, 0), font=font)
|
||||
buf = io.BytesIO()
|
||||
image.save(buf, format="PNG")
|
||||
return buf.getvalue()
|
||||
|
||||
@classmethod
|
||||
def _extract_target_chars_from_question(cls, question: str) -> list[str]:
|
||||
"""从点选题目中提取需要点击的目标汉字(支持完整成语/菜名及古诗/成语填空题)。"""
|
||||
if not question:
|
||||
return []
|
||||
m = re.search(r'["“]([^"”]+)["”]', question)
|
||||
if not m:
|
||||
return []
|
||||
text = m.group(1).strip()
|
||||
if "_" in text:
|
||||
regex = "^" + text.replace("_", "(.)") + "$"
|
||||
compiled = re.compile(regex)
|
||||
for phrase in cls._get_cloze_corpus():
|
||||
matched = compiled.match(phrase)
|
||||
if matched:
|
||||
return list(matched.groups())
|
||||
return []
|
||||
return [ch for ch in text if "\u4e00" <= ch <= "\u9fa5"]
|
||||
|
||||
def _request_json(self, method: str, url: str, **kwargs) -> dict:
|
||||
response = self.session.request(method, url, timeout=self.timeout, **kwargs)
|
||||
response.raise_for_status()
|
||||
@@ -275,7 +451,7 @@ class HuyaVerificationSolver:
|
||||
except (KeyError, IndexError, TypeError) as exc:
|
||||
raise HuyaVerificationError("虎牙响应中没有 strategys[0].data") from exc
|
||||
|
||||
def _parse_strategy(self, challenge_payload: dict | str) -> None:
|
||||
def _parse_strategy(self, challenge_payload: dict | str, step: int = 1) -> None:
|
||||
strategy_url = self._extract_strategy_url(challenge_payload)
|
||||
|
||||
query_params = parse_qs(urlparse(strategy_url).query)
|
||||
@@ -306,15 +482,83 @@ class HuyaVerificationSolver:
|
||||
# 实测(2026-08-25): strategy=64 时 pt_auth.html 是滑块、qr_auth.html 才是扫码,
|
||||
# 旧的 `strategy==64 → 扫码` 硬编码会把可自动化的滑块误杀成死路。
|
||||
is_qr_auth = "qr_auth" in strategy_url_lower
|
||||
if is_qr_auth and "dx_auth" not in strategy_url_lower:
|
||||
account_id = self.account or self.url_param_map.get("mobile", "")
|
||||
if is_qr_auth:
|
||||
if self.enable_capture:
|
||||
cap = CaptchaCaptureRecord(
|
||||
auth_type="qr_auth",
|
||||
account=account_id,
|
||||
page_url=self.strategy_url,
|
||||
scene_type=self.scene_type,
|
||||
output_base_dir=Path(self.capture_dir)
|
||||
if self.capture_dir
|
||||
else None,
|
||||
)
|
||||
cap.capture_qr_auth_data(
|
||||
mobile=self.url_param_map.get("mobile", "?"),
|
||||
page_url=self.strategy_url,
|
||||
scene_type=self.scene_type,
|
||||
step=step,
|
||||
raw_params=self.url_param_map,
|
||||
)
|
||||
logger.warning(
|
||||
"[solver] 虎牙风控在第 {} 步要求扫码验证 (qr_auth),需 App 确认,账号: {}",
|
||||
step,
|
||||
self.url_param_map.get("mobile", "?"),
|
||||
)
|
||||
raise HuyaQrAuthRequiredError(
|
||||
"虎牙风控要求扫码验证(qr_auth),需已登录App设备确认,"
|
||||
f"账号: {self.url_param_map.get('mobile', '?')}"
|
||||
)
|
||||
if "dx_auth" in strategy_url_lower:
|
||||
if "sms_auth" in strategy_url_lower:
|
||||
mobile = self.url_param_map.get("mobile", "?")
|
||||
if self.enable_capture:
|
||||
cap = CaptchaCaptureRecord(
|
||||
auth_type="sms_auth",
|
||||
account=account_id,
|
||||
page_url=self.strategy_url,
|
||||
scene_type=self.scene_type,
|
||||
output_base_dir=Path(self.capture_dir)
|
||||
if self.capture_dir
|
||||
else None,
|
||||
)
|
||||
cap.capture_sms_auth_data(
|
||||
mobile=mobile,
|
||||
page_url=self.strategy_url,
|
||||
scene_type=self.scene_type,
|
||||
step=step,
|
||||
raw_params=self.url_param_map,
|
||||
)
|
||||
if self.sms_url:
|
||||
logger.info(
|
||||
"[solver] 虎牙风控在第 {} 步要求短信验证 (sms_auth),已配置接码链接,准备自动发码并接码过验",
|
||||
step,
|
||||
)
|
||||
self.auth_type = "sms_auth"
|
||||
return
|
||||
logger.warning(
|
||||
"[solver] 虎牙风控在第 {} 步要求短信验证 (sms_auth),手机号: {}",
|
||||
step,
|
||||
mobile,
|
||||
)
|
||||
raise HuyaSmsAuthRequiredError(
|
||||
challenge_payload,
|
||||
f"虎牙风控要求短信验证(dx_auth),手机号: {self.url_param_map.get('mobile', '?')}",
|
||||
f"虎牙风控要求短信验证(sms_auth),手机号: {mobile}",
|
||||
mobile=mobile,
|
||||
)
|
||||
if "rotate_auth" in strategy_url_lower or "/auth/xz/" in strategy_url_lower:
|
||||
self.auth_type = "rotate_auth"
|
||||
elif "dx_auth" in strategy_url_lower or "/auth/dx/" in strategy_url_lower:
|
||||
self.auth_type = "dx_auth"
|
||||
else:
|
||||
self.auth_type = "pt_auth"
|
||||
if self.enable_capture:
|
||||
self.current_capture = CaptchaCaptureRecord(
|
||||
auth_type=self.auth_type,
|
||||
account=account_id,
|
||||
page_url=self.strategy_url,
|
||||
scene_type=self.scene_type,
|
||||
output_base_dir=Path(self.capture_dir) if self.capture_dir else None,
|
||||
)
|
||||
self.behavior_events = []
|
||||
|
||||
@@ -342,9 +586,39 @@ class HuyaVerificationSolver:
|
||||
self.code = data["code"]
|
||||
self.huyapk = data["huyapk"]
|
||||
segment = data.get("segment")
|
||||
is_rotate = (
|
||||
self.auth_type == "rotate_auth"
|
||||
or "/auth/xz/" in url
|
||||
or "rotate" in self.strategy_url
|
||||
)
|
||||
if is_rotate:
|
||||
logger.info("虎牙验证类型: 旋转验证码 (rotate_auth / xz)")
|
||||
bg_bytes = self._decode_image(data.get("dtuStr") or data.get("dTuStr"))
|
||||
tip_bytes = self._decode_image(data.get("puzzleStr"))
|
||||
restored_bg = (
|
||||
self._restore_slider_background(bg_bytes, segment, self.huyapk)
|
||||
if segment
|
||||
else bg_bytes
|
||||
)
|
||||
angle = self._opencv_rotate_angle(restored_bg, tip_bytes)
|
||||
logger.info(f"虎牙旋转验证码识别角度: {angle}°")
|
||||
if self.current_capture:
|
||||
try:
|
||||
self.current_capture.capture_rotate_data(
|
||||
code=self.code,
|
||||
dtu_bytes=bg_bytes,
|
||||
puzzle_bytes=tip_bytes,
|
||||
restored_bg_bytes=restored_bg,
|
||||
angle=angle,
|
||||
raw_data=data,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.debug(f"[solver] 记录旋转验证码捕获数据异常: {exc}")
|
||||
return angle
|
||||
|
||||
if segment:
|
||||
logger.info("虎牙验证类型: 滑块")
|
||||
bg_bytes = self._decode_image(data.get("dtuStr"))
|
||||
bg_bytes = self._decode_image(data.get("dtuStr") or data.get("dTuStr"))
|
||||
tip_bytes = self._decode_image(data.get("puzzleStr"))
|
||||
restored_bg = self._restore_slider_background(
|
||||
bg_bytes, segment, self.huyapk
|
||||
@@ -357,18 +631,97 @@ class HuyaVerificationSolver:
|
||||
opencv_x = self._opencv_slider_x(tip_bytes, restored_bg)
|
||||
if abs(opencv_x - yolo_x) < 10:
|
||||
logger.debug("虎牙滑块识别距离: yolo={}, opencv={}", yolo_x, opencv_x)
|
||||
return int((opencv_x + yolo_x) / 2)
|
||||
logger.debug(
|
||||
"虎牙滑块识别距离: yolo={}, opencv={}, 使用yolo", yolo_x, opencv_x
|
||||
)
|
||||
return yolo_x
|
||||
final_x = int((opencv_x + yolo_x) / 2)
|
||||
else:
|
||||
logger.debug(
|
||||
"虎牙滑块识别距离: yolo={}, opencv={}, 使用yolo", yolo_x, opencv_x
|
||||
)
|
||||
final_x = yolo_x
|
||||
|
||||
logger.info("虎牙验证类型: 点选")
|
||||
bg_bytes = self._decode_image(data.get("dTuStr"))
|
||||
tip_bytes = self._decode_image(data.get("hZiStr"))
|
||||
merged = self._vertical_concat_align_left(bg_bytes, tip_bytes)
|
||||
points = self.ocr.ocr_click_points(merged)
|
||||
return [[int(x), int(y) - 44] for x, y in points]
|
||||
if self.current_capture:
|
||||
try:
|
||||
self.current_capture.capture_slider_data(
|
||||
code=self.code,
|
||||
dtu_bytes=bg_bytes,
|
||||
puzzle_bytes=tip_bytes,
|
||||
restored_bg_bytes=restored_bg,
|
||||
composite_bytes=composite,
|
||||
point_x=final_x,
|
||||
raw_data=data,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.debug(f"[solver] 记录滑块捕获数据异常: {exc}")
|
||||
|
||||
return final_x
|
||||
|
||||
logger.info("虎牙验证类型: 点选 (dx_auth)")
|
||||
bg_raw = data.get("dTuStr") or data.get("dtuStr") or data.get("bgStr")
|
||||
bg_bytes = self._decode_image(bg_raw)
|
||||
if not bg_bytes:
|
||||
raise HuyaVerificationError(
|
||||
f"点选验证码背景图为空: keys={list(data.keys())}"
|
||||
)
|
||||
tip_raw = data.get("hZiStr") or data.get("hziStr") or data.get("puzzleStr")
|
||||
tip_bytes = self._decode_image(tip_raw) if tip_raw else b""
|
||||
if tip_bytes:
|
||||
merged = self._vertical_concat_align_left(bg_bytes, tip_bytes)
|
||||
points = self.ocr.ocr_click_points(merged)
|
||||
final_points = [[int(x), int(y) - 44] for x, y in points]
|
||||
else:
|
||||
question = str(data.get("question") or "")
|
||||
target_chars = self._extract_target_chars_from_question(question)
|
||||
if target_chars:
|
||||
logger.info(f"虎牙点选从题目解析出目标字: {target_chars}")
|
||||
synth_tip = self._synthesize_text_tip(target_chars)
|
||||
merged = self._vertical_concat_align_left(bg_bytes, synth_tip)
|
||||
points = self.ocr.ocr_click_points(merged)
|
||||
final_points = [[int(x), int(y) - 44] for x, y in points]
|
||||
tip_bytes = synth_tip
|
||||
else:
|
||||
logger.info(f"虎牙点选题目未能提取明确目标字: {question}")
|
||||
final_points = []
|
||||
|
||||
if self.current_capture:
|
||||
try:
|
||||
self.current_capture.capture_click_data(
|
||||
code=self.code,
|
||||
dtu_bytes=bg_bytes,
|
||||
hzi_bytes=tip_bytes,
|
||||
points=final_points,
|
||||
raw_data=data,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.debug(f"[solver] 记录点选捕获数据异常: {exc}")
|
||||
|
||||
return final_points
|
||||
|
||||
def _send_rotate_verify(self, url: str, angle: float) -> dict:
|
||||
drag_distance = max(5, int((angle / 360.0) * 260))
|
||||
track_points = generate_slide_track(drag_distance)
|
||||
act_data = {
|
||||
"point": int(round(angle)),
|
||||
"travel": format_track(track_points),
|
||||
"code": self.code,
|
||||
"sceneType": self.scene_type,
|
||||
"endTime": int(track_points[-1][-1]) - int(track_points[0][-1]),
|
||||
}
|
||||
act = self.js_ctx.call("get_act", act_data, self.huyapk)
|
||||
self.behavior_events.append(
|
||||
{
|
||||
"action": self._pointer_action("mousedown"),
|
||||
"id": "",
|
||||
"txt": "",
|
||||
"clientX": track_points[0][0],
|
||||
"clientY": track_points[0][1],
|
||||
"clientWidth": 350,
|
||||
"clientHeight": 268,
|
||||
"isTrusted": True,
|
||||
"timeStamp": random.randint(1800, 5500),
|
||||
"nowTime": int(time.time() * 1000),
|
||||
}
|
||||
)
|
||||
payload = self._build_common_payload({"act": act, "ver": 5})
|
||||
return self._submit_verify(url, payload)
|
||||
|
||||
def _send_slider_verify(self, url: str, point: int) -> dict:
|
||||
track_points = generate_slide_track(point)
|
||||
@@ -451,10 +804,88 @@ class HuyaVerificationSolver:
|
||||
bool(data.get("authId")),
|
||||
bool(data.get("nextAuthUrl")),
|
||||
)
|
||||
if self.current_capture:
|
||||
try:
|
||||
self.current_capture.record_submit_result(data)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.debug(f"[solver] 记录提交结果异常: {exc}")
|
||||
return data
|
||||
|
||||
def _solve_sms_strategy(self, wait_seconds: float = 90.0) -> dict:
|
||||
"""自动通过 sms_url 接码并提交虎牙 sms_auth 验证。"""
|
||||
from core.sms_provider import SmsLine, SmsProviderClient, detect_provider
|
||||
|
||||
mobile = self.url_param_map.get("mobile", "?")
|
||||
logger.info(
|
||||
f"[solver] 开始自动接码流程 (手机: {mobile}, 接码URL: {self.sms_url[:40]}...)"
|
||||
)
|
||||
|
||||
# 1. 触发虎牙官方发送短信验证码
|
||||
send_url = "https://udbrtt.huya.com/auth/sms/send-sms-code"
|
||||
send_payload = {
|
||||
"data": {
|
||||
"urlParamMap": self.url_param_map,
|
||||
"ver": 5,
|
||||
}
|
||||
}
|
||||
sent_at = datetime.now(UTC)
|
||||
try:
|
||||
body = json.dumps(send_payload, separators=(",", ":"))
|
||||
send_res = self._request_json("post", send_url, data=body)
|
||||
logger.info(f"[solver] 虎牙短信验证码发送结果: {send_res}")
|
||||
except Exception as exc:
|
||||
logger.warning(f"[solver] 虎牙安全验证发送短信失败: {exc}")
|
||||
raise HuyaVerificationError(f"发送虎牙短信验证码失败: {exc}") from exc
|
||||
|
||||
# 2. 轮询接码链接获取验证码
|
||||
client = SmsProviderClient(timeout=(8, 15))
|
||||
item = SmsLine(
|
||||
phone=mobile,
|
||||
url=self.sms_url,
|
||||
provider=detect_provider(self.sms_url),
|
||||
raw=f"{mobile}----{self.sms_url}",
|
||||
)
|
||||
deadline = time.monotonic() + wait_seconds
|
||||
poll_interval = 2.5
|
||||
sms_code = ""
|
||||
while time.monotonic() < deadline:
|
||||
poll_result = client.poll(item, after_time=sent_at)
|
||||
if poll_result.has_code:
|
||||
sms_code = poll_result.code
|
||||
logger.info(f"[solver] 成功从接码链接收到验证码: {sms_code}")
|
||||
break
|
||||
time.sleep(poll_interval)
|
||||
|
||||
if not sms_code:
|
||||
raise HuyaSmsAuthRequiredError(
|
||||
self.strategy_url,
|
||||
f"接码超时({int(wait_seconds)}秒内未从接码链接获取到新验证码)",
|
||||
mobile=mobile,
|
||||
)
|
||||
|
||||
# 3. 提交验证码给虎牙官方
|
||||
verify_url = "https://udbrtt.huya.com/auth/sms/verify-sms-code"
|
||||
sms_code_hex = sms_code.encode("utf-8").hex()
|
||||
verify_payload = {
|
||||
"encrypt": 1,
|
||||
"data": {
|
||||
"urlParamMap": self.url_param_map,
|
||||
"behavior": self._encode_behavior(),
|
||||
"info": self.sdid,
|
||||
"page": self.page_url,
|
||||
"smsCode": sms_code_hex,
|
||||
"wupData": "",
|
||||
"ver": 5,
|
||||
},
|
||||
}
|
||||
logger.debug(f"[solver] 提交虎牙短信验证码: {sms_code} (hex={sms_code_hex})")
|
||||
return self._submit_verify(verify_url, verify_payload)
|
||||
|
||||
def _solve_current_strategy(self) -> dict:
|
||||
"""提交当前 strategy_url 对应的验证。"""
|
||||
if self.auth_type == "sms_auth":
|
||||
return self._solve_sms_strategy()
|
||||
|
||||
config_payload = self._build_common_payload({"cTuIndex": 0})
|
||||
config = {}
|
||||
gurl = ""
|
||||
@@ -522,7 +953,13 @@ class HuyaVerificationSolver:
|
||||
|
||||
point = self._get_track_point(gurl, point_payload)
|
||||
logger.debug(f"虎牙验证识别结果: {point}")
|
||||
if isinstance(point, list):
|
||||
if (
|
||||
self.auth_type == "rotate_auth"
|
||||
or "/auth/xz/" in vurl
|
||||
or "rotate" in self.strategy_url
|
||||
):
|
||||
last_result = self._send_rotate_verify(vurl, float(point))
|
||||
elif isinstance(point, list):
|
||||
last_result = self._send_click_verify(vurl, point)
|
||||
else:
|
||||
last_result = self._send_slider_verify(vurl, int(point))
|
||||
@@ -542,15 +979,24 @@ class HuyaVerificationSolver:
|
||||
return last_result
|
||||
|
||||
def solve(self, challenge_payload: dict | str) -> dict:
|
||||
"""提交虎牙风控验证,返回 vurl 响应中的 data。"""
|
||||
self._parse_strategy(challenge_payload)
|
||||
result = self._solve_current_strategy()
|
||||
|
||||
next_auth_url = result.get("nextAuthUrl") if isinstance(result, dict) else None
|
||||
if next_auth_url:
|
||||
logger.info("虎牙滑块后继续点选验证")
|
||||
self._parse_strategy(next_auth_url)
|
||||
"""提交虎牙风控验证,支持多级风控流转 (如 pt_auth -> dx_auth -> ...)。"""
|
||||
current_challenge: dict | str = challenge_payload
|
||||
result: dict = {}
|
||||
for step in range(5):
|
||||
self._parse_strategy(current_challenge, step=step + 1)
|
||||
result = self._solve_current_strategy()
|
||||
if not isinstance(result, dict):
|
||||
break
|
||||
if result.get("authId"):
|
||||
logger.info(f"虎牙验证在第 {step + 1} 步成功获取 authId")
|
||||
break
|
||||
next_auth_url = result.get("nextAuthUrl")
|
||||
if not next_auth_url:
|
||||
break
|
||||
logger.info(
|
||||
f"虎牙验证第 {step + 1} 步完成,进入下一阶段验证: {next_auth_url[:80]}..."
|
||||
)
|
||||
current_challenge = next_auth_url
|
||||
return result
|
||||
|
||||
|
||||
@@ -564,6 +1010,10 @@ def solve_huya_verification(
|
||||
app_id: str = "5002",
|
||||
page_url: str | None = None,
|
||||
use_touch_events: bool = False,
|
||||
account: str = "",
|
||||
capture_dir: str | Path | None = None,
|
||||
enable_capture: bool = True,
|
||||
sms_url: str = "",
|
||||
) -> dict:
|
||||
"""函数式入口,便于登录流程直接调用。"""
|
||||
solver = HuyaVerificationSolver(
|
||||
@@ -575,5 +1025,9 @@ def solve_huya_verification(
|
||||
app_id=app_id,
|
||||
page_url=page_url,
|
||||
use_touch_events=use_touch_events,
|
||||
account=account,
|
||||
capture_dir=capture_dir,
|
||||
enable_capture=enable_capture,
|
||||
sms_url=sms_url,
|
||||
)
|
||||
return solver.solve(challenge_payload)
|
||||
|
||||
@@ -0,0 +1,189 @@
|
||||
"""虎牙验证码数据采集与分析脚本。
|
||||
|
||||
功能:
|
||||
1. 【URL 采集模式】直接传入 safe_auth URL(dx_auth 点选 / pt_auth 滑块),
|
||||
拉取验证码图片、提示语与元数据并落盘保存;
|
||||
2. 【登录探测模式】输入账号密码触发风控,捕获验证码全套图片与提示语。
|
||||
|
||||
用法示例:
|
||||
# 模式一:直接解析已有风控 URL
|
||||
python scripts/capture_huya_captcha.py url "https://aq.huya.com/p/safe_auth/dx_auth.html?param=..."
|
||||
|
||||
# 模式二:仅拉取图片和提示语,不向服务端提交答案
|
||||
python scripts/capture_huya_captcha.py url "https://aq.huya.com/p/safe_auth/dx_auth.html?param=..." --dump-only
|
||||
|
||||
# 模式三:登录触发捕获
|
||||
python scripts/capture_huya_captcha.py login <账号> <密码> --channel app
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from urllib.parse import parse_qs, urlparse
|
||||
|
||||
from loguru import logger
|
||||
|
||||
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
||||
if str(PROJECT_ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(PROJECT_ROOT))
|
||||
|
||||
from core.huya.verification.capture import DEFAULT_CAPTURE_DIR
|
||||
from core.huya.verification.solver import (
|
||||
HuyaQrAuthRequiredError,
|
||||
HuyaSmsAuthRequiredError,
|
||||
HuyaVerificationSolver,
|
||||
)
|
||||
|
||||
|
||||
def capture_from_url(
|
||||
url: str,
|
||||
*,
|
||||
dump_only: bool = False,
|
||||
account: str = "",
|
||||
proxies: dict | None = None,
|
||||
) -> None:
|
||||
"""从 safe_auth URL 抓取并保存验证码数据。"""
|
||||
logger.info(f"开始从 URL 采集验证码: {url[:100]}...")
|
||||
q = parse_qs(urlparse(url).query)
|
||||
app_id = q.get("appId", ["5002"])[0]
|
||||
|
||||
solver = HuyaVerificationSolver(
|
||||
app_id=app_id,
|
||||
page_url=url,
|
||||
account=account,
|
||||
proxies=proxies,
|
||||
enable_capture=True,
|
||||
)
|
||||
solver._parse_strategy(url)
|
||||
|
||||
if dump_only:
|
||||
# 只拉取 config3 和 gurl,不提交 vurl
|
||||
config_payload = solver._build_common_payload({"cTuIndex": 0})
|
||||
config = solver._request_json(
|
||||
"post",
|
||||
"https://udbrtt.huya.com/auth/client/config3",
|
||||
json=config_payload,
|
||||
)
|
||||
data = config.get("data") or {}
|
||||
gurl = str(data.get("gurl") or "")
|
||||
dx_js = str(data.get("js") or "")
|
||||
if not gurl:
|
||||
logger.error(f"config3 未下发 gurl: {config}")
|
||||
return
|
||||
if dx_js:
|
||||
solver._compile_js(dx_js)
|
||||
point_payload = solver._build_common_payload({"ver": 5})
|
||||
point = solver._get_track_point(gurl, point_payload)
|
||||
logger.info(f"【仅采集模式】验证码解析完成,识别结果: {point}")
|
||||
if solver.current_capture and solver.current_capture.target_dir:
|
||||
logger.info(f"验证码数据已成功保存至: {solver.current_capture.target_dir}")
|
||||
return
|
||||
|
||||
# 完整求解并记录提交结果
|
||||
try:
|
||||
result = solver.solve(url)
|
||||
logger.info(f"验证码提交完成: authId={result.get('authId')}")
|
||||
except HuyaQrAuthRequiredError as exc:
|
||||
logger.warning(f"该 URL 属于扫码验证: {exc}")
|
||||
except HuyaSmsAuthRequiredError as exc:
|
||||
logger.warning(f"该 URL 触发短信二次验证: {exc}")
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.error(f"验证码处理异常: {exc}")
|
||||
|
||||
|
||||
def capture_from_login(
|
||||
account: str,
|
||||
password: str,
|
||||
channel: str = "app",
|
||||
proxies: dict | None = None,
|
||||
) -> None:
|
||||
"""通过尝试登录触发风控并自动落盘验证码。"""
|
||||
logger.info(f"尝试登录账号 {account} (渠道: {channel}) 探测验证码...")
|
||||
if channel == "app":
|
||||
from core.huya.app_login import login_huya_app_password
|
||||
|
||||
res = login_huya_app_password(account, password, proxies=proxies)
|
||||
logger.info(
|
||||
f"登录结果: success={res.success}, code={res.code}, msg={res.message}"
|
||||
)
|
||||
else:
|
||||
from core.huya.login import login_huya_password
|
||||
|
||||
res = login_huya_password(account, password, proxies=proxies)
|
||||
logger.info(
|
||||
f"登录结果: success={res.success}, code={res.code}, msg={res.message}"
|
||||
)
|
||||
|
||||
|
||||
def list_recent_captures() -> None:
|
||||
"""列出最近采集到的验证码。"""
|
||||
if not DEFAULT_CAPTURE_DIR.exists():
|
||||
print("暂无捕获数据目录: ", DEFAULT_CAPTURE_DIR)
|
||||
return
|
||||
folders = sorted(
|
||||
DEFAULT_CAPTURE_DIR.glob("*"), key=lambda p: p.stat().st_mtime, reverse=True
|
||||
)
|
||||
if not folders:
|
||||
print("当前尚未捕获任何验证码数据。")
|
||||
return
|
||||
print(f"\n找到 {len(folders)} 条捕获记录 (位于 {DEFAULT_CAPTURE_DIR}):\n")
|
||||
for f in folders[:10]:
|
||||
meta_file = f / "meta.json"
|
||||
if meta_file.exists():
|
||||
try:
|
||||
meta = json.loads(meta_file.read_text(encoding="utf-8"))
|
||||
auth_name = meta.get("auth_name", "未知")
|
||||
prompts = meta.get("prompts", {})
|
||||
question = (
|
||||
prompts.get("question") or prompts.get("default_instruction") or ""
|
||||
)
|
||||
files = [p.name for p in f.glob("*") if p.name != "meta.json"]
|
||||
print(f"📁 {f.name}")
|
||||
print(f" 类型: {auth_name} | 提示语: {question}")
|
||||
print(f" 文件: {', '.join(files)}")
|
||||
except Exception: # noqa: BLE001
|
||||
print(f"📁 {f.name}")
|
||||
else:
|
||||
print(f"📁 {f.name}")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description="虎牙验证码采集与测试工具")
|
||||
subparsers = parser.add_subparsers(dest="cmd")
|
||||
|
||||
url_parser = subparsers.add_parser("url", help="从 safe_auth URL 抓取并落盘验证码")
|
||||
url_parser.add_argument("url", help="safe_auth 完整 URL")
|
||||
url_parser.add_argument("--account", default="", help="关联账号名 (可选)")
|
||||
url_parser.add_argument(
|
||||
"--dump-only",
|
||||
action="store_true",
|
||||
help="仅下载图片与提示语,不向服务器提交答案",
|
||||
)
|
||||
|
||||
login_parser = subparsers.add_parser("login", help="通过密码登录触发验证码")
|
||||
login_parser.add_argument("account", help="虎牙账号或手机号")
|
||||
login_parser.add_argument("password", help="密码")
|
||||
login_parser.add_argument(
|
||||
"--channel", choices=["app", "web"], default="app", help="登录渠道 (默认 app)"
|
||||
)
|
||||
|
||||
subparsers.add_parser("list", help="查看最近采集到的验证码记录")
|
||||
|
||||
args = parser.parse_args()
|
||||
if args.cmd == "url":
|
||||
capture_from_url(args.url, dump_only=args.dump_only, account=args.account)
|
||||
elif args.cmd == "login":
|
||||
capture_from_login(args.account, args.password, channel=args.channel)
|
||||
elif args.cmd == "list":
|
||||
list_recent_captures()
|
||||
else:
|
||||
parser.print_help()
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,209 @@
|
||||
"""虎牙 dx_auth 与验证码捕获落盘功能测试。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from PIL import Image
|
||||
|
||||
from core.huya.verification.capture import CaptchaCaptureRecord
|
||||
from core.huya.verification.solver import (
|
||||
HuyaQrAuthRequiredError,
|
||||
HuyaVerificationSolver,
|
||||
)
|
||||
|
||||
|
||||
def test_dx_auth_strategy_recognized_as_click_not_sms(tmp_path: Path):
|
||||
"""验证 dx_auth 不再被误杀为短信异常,而是识别为 dx_auth。"""
|
||||
url = (
|
||||
"https://aq.huya.com/p/safe_auth/dx_auth.html?"
|
||||
"param=test_param&mobile=13800000000&sceneType=16&appId=5002"
|
||||
)
|
||||
solver = HuyaVerificationSolver(
|
||||
page_url=url,
|
||||
account="13800000000",
|
||||
capture_dir=tmp_path,
|
||||
enable_capture=True,
|
||||
)
|
||||
|
||||
# 不抛出 HuyaSmsAuthRequiredError
|
||||
solver._parse_strategy(url)
|
||||
assert solver.auth_type == "dx_auth"
|
||||
assert solver.current_capture is not None
|
||||
assert solver.current_capture.meta["auth_type"] == "dx_auth"
|
||||
assert solver.current_capture.meta["account"] == "13800000000"
|
||||
|
||||
|
||||
def test_qr_auth_still_raises_qr_error():
|
||||
"""验证真正的 qr_auth 仍然正确抛出 HuyaQrAuthRequiredError。"""
|
||||
url = "https://aq.huya.com/p/safe_auth/qr_auth.html?param=test_param&mobile=13800000000"
|
||||
solver = HuyaVerificationSolver(page_url=url)
|
||||
try:
|
||||
solver._parse_strategy(url)
|
||||
except HuyaQrAuthRequiredError:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError("应该抛出 HuyaQrAuthRequiredError")
|
||||
|
||||
|
||||
def test_click_captcha_capture_saves_files_and_prompts(tmp_path: Path):
|
||||
"""验证点选验证码捕获时正确落盘图片、提示语与元数据。"""
|
||||
record = CaptchaCaptureRecord(
|
||||
auth_type="dx_auth",
|
||||
account="test_user",
|
||||
page_url="https://aq.huya.com/p/safe_auth/dx_auth.html?param=123",
|
||||
scene_type="16",
|
||||
output_base_dir=tmp_path,
|
||||
)
|
||||
|
||||
img = Image.new("RGB", (100, 100), color="blue")
|
||||
buf = io.BytesIO()
|
||||
img.save(buf, format="JPEG")
|
||||
fake_img_bytes = buf.getvalue()
|
||||
|
||||
tip_img = Image.new("RGBA", (80, 40), color="white")
|
||||
tip_buf = io.BytesIO()
|
||||
tip_img.save(tip_buf, format="PNG")
|
||||
fake_tip_bytes = tip_buf.getvalue()
|
||||
|
||||
folder = record.capture_click_data(
|
||||
code="code123456",
|
||||
dtu_bytes=fake_img_bytes,
|
||||
hzi_bytes=fake_tip_bytes,
|
||||
points=[[30, 40], [60, 70]],
|
||||
raw_data={
|
||||
"question": "按顺序点击:虎、牙",
|
||||
"wordSize": 2,
|
||||
"tuMing": 1,
|
||||
"description": "安全验证",
|
||||
},
|
||||
)
|
||||
|
||||
assert folder.exists()
|
||||
assert (folder / "bg_main.jpg").exists()
|
||||
assert (folder / "tip_text.png").exists()
|
||||
assert (folder / "annotated.jpg").exists()
|
||||
assert (folder / "meta.json").exists()
|
||||
|
||||
meta = json.loads((folder / "meta.json").read_text(encoding="utf-8"))
|
||||
assert meta["auth_type"] == "dx_auth"
|
||||
assert meta["prompts"]["question"] == "按顺序点击:虎、牙"
|
||||
assert meta["prompts"]["word_size"] == 2
|
||||
assert meta["solution"]["points"] == [[30, 40], [60, 70]]
|
||||
|
||||
|
||||
def test_slider_captcha_capture_saves_files_and_prompts(tmp_path: Path):
|
||||
"""验证滑块验证码捕获时正确落盘背景、拼图与标注图。"""
|
||||
record = CaptchaCaptureRecord(
|
||||
auth_type="pt_auth",
|
||||
account="test_slider_user",
|
||||
page_url="https://aq.huya.com/p/safe_auth/pt_auth.html?param=123",
|
||||
scene_type="4",
|
||||
output_base_dir=tmp_path,
|
||||
)
|
||||
|
||||
img = Image.new("RGB", (260, 180), color="green")
|
||||
buf = io.BytesIO()
|
||||
img.save(buf, format="JPEG")
|
||||
fake_bg_bytes = buf.getvalue()
|
||||
|
||||
puzzle = Image.new("RGBA", (50, 50), color="red")
|
||||
p_buf = io.BytesIO()
|
||||
puzzle.save(p_buf, format="PNG")
|
||||
fake_puzzle_bytes = p_buf.getvalue()
|
||||
|
||||
folder = record.capture_slider_data(
|
||||
code="slider_code_888",
|
||||
dtu_bytes=fake_bg_bytes,
|
||||
puzzle_bytes=fake_puzzle_bytes,
|
||||
restored_bg_bytes=fake_bg_bytes,
|
||||
point_x=120,
|
||||
raw_data={"location_y": 60, "tip": "拖动滑块完成拼图"},
|
||||
)
|
||||
|
||||
assert folder.exists()
|
||||
assert (folder / "bg_scrambled.jpg").exists()
|
||||
assert (folder / "puzzle.png").exists()
|
||||
assert (folder / "bg_restored.jpg").exists()
|
||||
assert (folder / "annotated.jpg").exists()
|
||||
|
||||
meta = json.loads((folder / "meta.json").read_text(encoding="utf-8"))
|
||||
assert meta["auth_type"] == "pt_auth"
|
||||
assert meta["solution"]["distance_x"] == 120
|
||||
assert meta["prompts"]["location_y"] == 60
|
||||
|
||||
|
||||
def test_submit_result_recorded_in_meta(tmp_path: Path):
|
||||
"""验证验证码提交结果正确更新进 meta.json。"""
|
||||
record = CaptchaCaptureRecord(
|
||||
auth_type="dx_auth",
|
||||
account="test_user",
|
||||
output_base_dir=tmp_path,
|
||||
)
|
||||
|
||||
img = Image.new("RGB", (10, 10))
|
||||
buf = io.BytesIO()
|
||||
img.save(buf, format="JPEG")
|
||||
folder = record.capture_click_data(
|
||||
code="c1", dtu_bytes=buf.getvalue(), hzi_bytes=b""
|
||||
)
|
||||
|
||||
record.record_submit_result({"authId": "auth_token_xyz999", "returnCode": 0})
|
||||
meta = json.loads((folder / "meta.json").read_text(encoding="utf-8"))
|
||||
assert meta["submit_result"]["success"] is True
|
||||
assert meta["submit_result"]["authId"] == "auth_token_xyz999"
|
||||
|
||||
|
||||
def test_sms_auth_capture_saves_meta_and_params(tmp_path: Path):
|
||||
"""验证遇到短信风控拦截时正确落盘元数据与参数。"""
|
||||
record = CaptchaCaptureRecord(
|
||||
auth_type="sms_auth",
|
||||
account="hy_test_sms",
|
||||
page_url="https://aq.huya.com/m/safe_auth/sms_auth2.html?param=xyz&mobile=0018888",
|
||||
scene_type="4",
|
||||
output_base_dir=tmp_path,
|
||||
)
|
||||
folder = record.capture_sms_auth_data(
|
||||
mobile="0018888",
|
||||
page_url="https://aq.huya.com/m/safe_auth/sms_auth2.html?param=xyz&mobile=0018888",
|
||||
scene_type="4",
|
||||
step=3,
|
||||
raw_params={"sceneType": "4", "mobile": "0018888", "param": "xyz"},
|
||||
)
|
||||
assert folder.exists()
|
||||
assert (folder / "meta.json").exists()
|
||||
assert (folder / "params.json").exists()
|
||||
|
||||
meta = json.loads((folder / "meta.json").read_text(encoding="utf-8"))
|
||||
assert meta["auth_type"] == "sms_auth"
|
||||
assert meta["step"] == 3
|
||||
assert meta["mobile"] == "0018888"
|
||||
assert meta["submit_result"]["status"] == "sms_auth_required"
|
||||
|
||||
|
||||
def test_qr_auth_capture_saves_meta_and_params(tmp_path: Path):
|
||||
"""验证遇到扫码风控拦截时正确落盘元数据与参数。"""
|
||||
record = CaptchaCaptureRecord(
|
||||
auth_type="qr_auth",
|
||||
account="hy_test_qr",
|
||||
page_url="https://aq.huya.com/m/safe_auth/qr_auth.html?param=abc",
|
||||
scene_type="4",
|
||||
output_base_dir=tmp_path,
|
||||
)
|
||||
folder = record.capture_qr_auth_data(
|
||||
mobile="13800138000",
|
||||
page_url="https://aq.huya.com/m/safe_auth/qr_auth.html?param=abc",
|
||||
scene_type="4",
|
||||
step=2,
|
||||
raw_params={"sceneType": "4", "mobile": "13800138000"},
|
||||
)
|
||||
assert folder.exists()
|
||||
assert (folder / "meta.json").exists()
|
||||
assert (folder / "params.json").exists()
|
||||
|
||||
meta = json.loads((folder / "meta.json").read_text(encoding="utf-8"))
|
||||
assert meta["auth_type"] == "qr_auth"
|
||||
assert meta["step"] == 2
|
||||
assert meta["submit_result"]["status"] == "qr_auth_required"
|
||||
@@ -0,0 +1,63 @@
|
||||
"""虎牙旋转验证码 (rotate_auth / xz) 求解与捕获测试。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from core.huya.verification.capture import CaptchaCaptureRecord
|
||||
from core.huya.verification.solver import HuyaVerificationSolver
|
||||
|
||||
|
||||
def test_rotate_strategy_recognized():
|
||||
"""验证 rotate_auth.html 能被正确解析为 rotate_auth 类型。"""
|
||||
url = (
|
||||
"https://aq.huya.com/p/safe_auth/rotate_auth.html?"
|
||||
"hideShareButton=1¶m=test_param&sceneType=4&appId=5008&mobile=001******5865"
|
||||
)
|
||||
solver = HuyaVerificationSolver(page_url=url, account="test_rotate")
|
||||
solver._parse_strategy(url)
|
||||
assert solver.auth_type == "rotate_auth"
|
||||
|
||||
|
||||
def test_opencv_rotate_angle_accuracy():
|
||||
"""验证真实旋转验证码图片能够精准计算出旋转还原角度。"""
|
||||
sample_dir = Path("data/captcha_captures/20260906_132605_pt_auth_348f71b3")
|
||||
if not sample_dir.exists():
|
||||
return
|
||||
|
||||
bg_bytes = (sample_dir / "bg_restored.jpg").read_bytes()
|
||||
puzzle_bytes = (sample_dir / "puzzle.png").read_bytes()
|
||||
|
||||
angle = HuyaVerificationSolver._opencv_rotate_angle(bg_bytes, puzzle_bytes)
|
||||
# 该样本的真值在 142 度左右
|
||||
assert 138 <= angle <= 146
|
||||
|
||||
|
||||
def test_capture_rotate_data(tmp_path: Path):
|
||||
"""测试旋转验证码捕获能正确落盘并生成标注图与元数据。"""
|
||||
sample_dir = Path("data/captcha_captures/20260906_132605_pt_auth_348f71b3")
|
||||
if not sample_dir.exists():
|
||||
return
|
||||
|
||||
bg_bytes = (sample_dir / "bg_restored.jpg").read_bytes()
|
||||
puzzle_bytes = (sample_dir / "puzzle.png").read_bytes()
|
||||
|
||||
record = CaptchaCaptureRecord(
|
||||
auth_type="rotate_auth",
|
||||
account="test_user",
|
||||
page_url="https://aq.huya.com/p/safe_auth/rotate_auth.html?param=123",
|
||||
scene_type="4",
|
||||
output_base_dir=tmp_path,
|
||||
)
|
||||
out_dir = record.capture_rotate_data(
|
||||
code="test_code_1234",
|
||||
dtu_bytes=bg_bytes,
|
||||
puzzle_bytes=puzzle_bytes,
|
||||
restored_bg_bytes=bg_bytes,
|
||||
angle=142,
|
||||
raw_data={"code": "test_code_1234", "title": "旋转验证"},
|
||||
)
|
||||
assert (out_dir / "annotated.jpg").exists()
|
||||
assert (out_dir / "meta.json").exists()
|
||||
assert record.meta["solution"]["angle"] == 142
|
||||
assert record.meta["auth_type"] == "rotate_auth"
|
||||
@@ -0,0 +1,159 @@
|
||||
"""虎牙四段式账号 (账号----密码----手机号----接码链接) 导入与短信自动求解测试。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from core.huya.verification.solver import (
|
||||
HuyaSmsAuthRequiredError,
|
||||
HuyaVerificationSolver,
|
||||
)
|
||||
from web.backend.services.huya_service import (
|
||||
import_huya_password_accounts,
|
||||
split_huya_password_line,
|
||||
)
|
||||
|
||||
|
||||
def test_parse_password_line_with_sms_url():
|
||||
"""测试四段式账号行解析 (账号----密码----手机号----接码链接)。"""
|
||||
line = (
|
||||
"hy_299112483----aa778899----+14504409332----"
|
||||
"http://194.163.45.144/api/get_sms?key=c16fe33219924363fa362ed69dc6fd4f"
|
||||
)
|
||||
parsed = split_huya_password_line(line)
|
||||
assert parsed is not None
|
||||
assert parsed.username == "hy_299112483"
|
||||
assert parsed.password == "aa778899"
|
||||
assert parsed.phone == "+14504409332"
|
||||
assert (
|
||||
parsed.sms_url
|
||||
== "http://194.163.45.144/api/get_sms?key=c16fe33219924363fa362ed69dc6fd4f"
|
||||
)
|
||||
|
||||
|
||||
def test_parse_password_line_backward_compatible():
|
||||
"""测试老格式向后兼容性。"""
|
||||
# 2段
|
||||
p2 = split_huya_password_line("user123----pass456")
|
||||
assert p2 is not None
|
||||
assert p2.username == "user123"
|
||||
assert p2.password == "pass456"
|
||||
assert p2.phone == ""
|
||||
assert p2.sms_url == ""
|
||||
|
||||
# 3段 (带手机号或cookie)
|
||||
p3 = split_huya_password_line("user123----pass456----13800138000")
|
||||
assert p3 is not None
|
||||
assert p3.username == "user123"
|
||||
assert p3.password == "pass456"
|
||||
assert p3.cookie == "13800138000"
|
||||
assert p3.sms_url == ""
|
||||
|
||||
|
||||
def test_import_password_accounts_persists_sms_url():
|
||||
"""测试导入账号密码服务会正确写入 sms_url 和 game_phone。"""
|
||||
mock_db = MagicMock()
|
||||
# 模拟无现有账号
|
||||
mock_db.query().filter().first.return_value = None
|
||||
|
||||
text = (
|
||||
"hy_299112483----aa778899----+14504409332----"
|
||||
"http://194.163.45.144/api/get_sms?key=c16fe33219924363fa362ed69dc6fd4f"
|
||||
)
|
||||
created, skipped = import_huya_password_accounts(mock_db, text, tag="测试接码")
|
||||
assert created == 1
|
||||
assert skipped == 0
|
||||
|
||||
added_account = mock_db.add.call_args[0][0]
|
||||
assert added_account.username == "hy_299112483"
|
||||
assert added_account.game_phone == "+14504409332"
|
||||
assert (
|
||||
added_account.sms_url
|
||||
== "http://194.163.45.144/api/get_sms?key=c16fe33219924363fa362ed69dc6fd4f"
|
||||
)
|
||||
assert added_account.tag == "测试接码"
|
||||
|
||||
|
||||
def test_import_password_accounts_strictly_four_segments():
|
||||
"""测试账号密码导入默认严格限制仅允许四段格式。"""
|
||||
mock_db = MagicMock()
|
||||
mock_db.query().filter().first.return_value = None
|
||||
|
||||
# 两段、三段会被跳过;仅四段有效行会被导入
|
||||
text = (
|
||||
"user1----pass1\n"
|
||||
"user2----pass2----13800138000\n"
|
||||
"user3----pass3----+14504409332----http://194.163.45.144/api/get_sms?key=xxx\n"
|
||||
"user4----pass4----not_a_url\n"
|
||||
)
|
||||
created, skipped = import_huya_password_accounts(mock_db, text, tag="四段测试")
|
||||
assert created == 1
|
||||
assert skipped == 3
|
||||
|
||||
|
||||
def test_solver_strategy_with_and_without_sms_url():
|
||||
"""测试在遇到 sms_auth2 时,有接码链接不抛异常,无接码链接抛出短信异常。"""
|
||||
url = "https://aq.huya.com/p/safe_auth/sms_auth2.html?param=xyz&mobile=14504409332"
|
||||
|
||||
# 无 sms_url,应触发 HuyaSmsAuthRequiredError
|
||||
solver_no_sms = HuyaVerificationSolver(page_url=url)
|
||||
with pytest.raises(HuyaSmsAuthRequiredError):
|
||||
solver_no_sms._parse_strategy(url)
|
||||
|
||||
# 有 sms_url,应正常解析为 sms_auth
|
||||
solver_with_sms = HuyaVerificationSolver(
|
||||
page_url=url,
|
||||
sms_url="http://194.163.45.144/api/get_sms?key=test",
|
||||
)
|
||||
solver_with_sms._parse_strategy(url)
|
||||
assert solver_with_sms.auth_type == "sms_auth"
|
||||
|
||||
|
||||
def test_solver_sms_flow():
|
||||
"""测试 solver._solve_sms_strategy 自动发码、接码与提码闭环。"""
|
||||
from core.sms_provider import SmsPollResult
|
||||
|
||||
url = "https://aq.huya.com/p/safe_auth/sms_auth2.html?param=mock_param&mobile=14504409332&appId=5002"
|
||||
solver = HuyaVerificationSolver(
|
||||
page_url=url,
|
||||
sms_url="http://194.163.45.144/api/get_sms?key=test_key",
|
||||
account="hy_test",
|
||||
)
|
||||
solver._parse_strategy(url)
|
||||
|
||||
mock_send_res = {"returnCode": 0, "message": "发送成功"}
|
||||
mock_verify_res = {
|
||||
"returnCode": 0,
|
||||
"data": {"authId": "auth_token_success_123"},
|
||||
}
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
solver,
|
||||
"_request_json",
|
||||
return_value=mock_send_res,
|
||||
) as mock_req_json,
|
||||
patch.object(
|
||||
solver,
|
||||
"_submit_verify",
|
||||
return_value=mock_verify_res["data"],
|
||||
) as mock_submit,
|
||||
patch(
|
||||
"core.sms_provider.SmsProviderClient.poll",
|
||||
return_value=SmsPollResult(status="code", code="654321"),
|
||||
) as mock_poll,
|
||||
):
|
||||
data = solver._solve_sms_strategy()
|
||||
|
||||
assert data["authId"] == "auth_token_success_123"
|
||||
mock_req_json.assert_called_once()
|
||||
assert "send-sms-code" in mock_req_json.call_args[0][1]
|
||||
|
||||
mock_poll.assert_called_once()
|
||||
|
||||
mock_submit.assert_called_once()
|
||||
verify_url, verify_payload = mock_submit.call_args[0]
|
||||
assert "verify-sms-code" in verify_url
|
||||
assert verify_payload["data"]["smsCode"] == "654321".encode().hex()
|
||||
@@ -0,0 +1,37 @@
|
||||
"""虎牙文本点选验证码 (dx_auth 填空/菜名/成语) 目标提取与提示图合成测试。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
from core.huya.verification.solver import HuyaVerificationSolver
|
||||
|
||||
|
||||
def test_extract_target_chars_from_dish_question() -> None:
|
||||
question = '请按照菜名"客家盐焗鸡"中汉字的顺序,在给定图片中点选对应的汉字'
|
||||
chars = HuyaVerificationSolver._extract_target_chars_from_question(question)
|
||||
assert chars == ["客", "家", "盐", "焗", "鸡"]
|
||||
|
||||
|
||||
def test_extract_target_chars_from_poem_cloze() -> None:
|
||||
question = '请按照古诗"千_鸟_绝"中汉字被挖空的顺序,在给定图片中点选对应的汉字以填充完整给定古诗'
|
||||
chars = HuyaVerificationSolver._extract_target_chars_from_question(question)
|
||||
assert chars == ["山", "飞"]
|
||||
|
||||
|
||||
def test_extract_target_chars_from_idiom_cloze() -> None:
|
||||
question = '请按照成语"_眸皓_"中汉字被挖空的顺序,在给定图片中点选对应的汉字以填充完整给定成语'
|
||||
chars = HuyaVerificationSolver._extract_target_chars_from_question(question)
|
||||
assert chars == ["明", "齿"]
|
||||
|
||||
|
||||
def test_synthesize_text_tip() -> None:
|
||||
chars = ["客", "家", "盐", "焗", "鸡"]
|
||||
img_bytes = HuyaVerificationSolver._synthesize_text_tip(chars)
|
||||
assert len(img_bytes) > 0
|
||||
arr = np.frombuffer(img_bytes, np.uint8)
|
||||
img = cv2.imdecode(arr, cv2.IMREAD_COLOR)
|
||||
assert img is not None
|
||||
assert img.shape[0] == 44
|
||||
assert img.shape[1] == 220
|
||||
+158
-5
@@ -8,6 +8,8 @@ import threading
|
||||
from datetime import UTC, datetime
|
||||
from typing import cast
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from fastapi import (
|
||||
APIRouter,
|
||||
Depends,
|
||||
@@ -85,6 +87,7 @@ from ..schemas import (
|
||||
HuyaSmsLoginRequest,
|
||||
HuyaTaskBatchRequest,
|
||||
HuyaTaskOut,
|
||||
HuyaTestSmsUrlRequest,
|
||||
HuyaWorkbenchAccountsUpdate,
|
||||
)
|
||||
from ..services.audit_service import record_audit
|
||||
@@ -382,6 +385,7 @@ def _account_out(account: HuyaAccount, include_cookie: bool = True) -> HuyaAccou
|
||||
game_name=account.game_name or "",
|
||||
game_channel=account.game_channel or "",
|
||||
game_phone=account.game_phone or "",
|
||||
sms_url=account.sms_url or "",
|
||||
assigned_to=account.assigned_to,
|
||||
assigned_username=account.assigned_user.username
|
||||
if account.assigned_user
|
||||
@@ -413,6 +417,7 @@ def _account_out_light(
|
||||
game_name=account.game_name or "",
|
||||
game_channel=account.game_channel or "",
|
||||
game_phone=account.game_phone or "",
|
||||
sms_url=account.sms_url or "",
|
||||
assigned_to=account.assigned_to,
|
||||
assigned_username=account.assigned_user.username
|
||||
if account.assigned_user
|
||||
@@ -688,11 +693,18 @@ def import_password_accounts(
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""导入虎牙账号密码,只入库不立即登录。"""
|
||||
"""导入虎牙账号密码,只入库不立即登录(仅允许四段格式)。"""
|
||||
_require_huya_perm(current, "huya:import")
|
||||
count, skipped = import_huya_password_accounts(db, req.text, req.tag)
|
||||
count, skipped = import_huya_password_accounts(
|
||||
db, req.text, req.tag, require_four_segments=True
|
||||
)
|
||||
if count == 0 and skipped > 0:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="导入失败:当前仅允许四段格式(账号----密码----手机号----接码链接)",
|
||||
)
|
||||
return {
|
||||
"message": f"导入/更新 {count} 个虎牙账号,跳过 {skipped} 条",
|
||||
"message": f"成功导入/更新 {count} 个虎牙账号,跳过 {skipped} 条",
|
||||
"success": True,
|
||||
"count": count,
|
||||
"skipped": skipped,
|
||||
@@ -754,11 +766,17 @@ def app_password_login_account(
|
||||
):
|
||||
"""使用 App 协议密码登录虎牙(推荐),自动过滑块并保存 Cookie。"""
|
||||
_require_huya_perm(current, "huya:import")
|
||||
username_clean = req.username.strip()
|
||||
account_db = (
|
||||
db.query(HuyaAccount).filter(HuyaAccount.username == username_clean).first()
|
||||
)
|
||||
sms_url = account_db.sms_url if account_db and account_db.sms_url else ""
|
||||
try:
|
||||
result = login_huya_app_password(
|
||||
username=req.username.strip(),
|
||||
username=username_clean,
|
||||
password=req.password,
|
||||
force_new_device=req.force_new_device,
|
||||
sms_url=sms_url,
|
||||
)
|
||||
except HuyaAppQrAuthRequiredError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
@@ -772,8 +790,14 @@ def app_password_login_account(
|
||||
) from exc
|
||||
|
||||
if not result.success or not result.cookie:
|
||||
logger.warning(
|
||||
f"[huya-app] 账号 {req.username} 密码登录未完成: {result.message} (code={result.code})"
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=502, detail=result.message or "虎牙 App 密码登录失败"
|
||||
status_code=400
|
||||
if result.code in ("SMS_AUTH_REQUIRED", "QR_AUTH_REQUIRED")
|
||||
else 502,
|
||||
detail=result.message or "虎牙 App 密码登录失败",
|
||||
)
|
||||
|
||||
try:
|
||||
@@ -875,6 +899,130 @@ def sms_login_account(
|
||||
}
|
||||
|
||||
|
||||
@router.post("/accounts/test-sms-url")
|
||||
def test_sms_url_endpoint(
|
||||
req: HuyaTestSmsUrlRequest,
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""测试从指定接码链接获取验证码。"""
|
||||
_require_huya_perm(current, "huya:import")
|
||||
url = req.url.strip()
|
||||
if not url:
|
||||
raise HTTPException(status_code=400, detail="接码链接不能为空")
|
||||
from core.sms_provider import SmsLine, SmsProviderClient, detect_provider
|
||||
|
||||
client = SmsProviderClient(timeout=(8, 15))
|
||||
item = SmsLine(
|
||||
phone=req.phone.strip(),
|
||||
url=url,
|
||||
provider=detect_provider(url),
|
||||
raw=f"{req.phone}----{url}",
|
||||
)
|
||||
poll_result = client.poll(item)
|
||||
return {
|
||||
"success": poll_result.has_code,
|
||||
"code": poll_result.code,
|
||||
"status": poll_result.status,
|
||||
"message": poll_result.message
|
||||
or ("成功识别验证码" if poll_result.has_code else "未识别到验证码"),
|
||||
"raw": poll_result.raw[:500],
|
||||
}
|
||||
|
||||
|
||||
@router.post("/accounts/{account_id}/fetch-sms-code")
|
||||
def fetch_account_sms_code(
|
||||
account_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""通过该账号绑定的接码链接获取最新短信验证码。"""
|
||||
_require_huya_perm(current, "huya:import")
|
||||
account = (
|
||||
_visible_huya_accounts_query(db, current)
|
||||
.filter(HuyaAccount.id == account_id)
|
||||
.first()
|
||||
)
|
||||
if account is None:
|
||||
raise HTTPException(status_code=404, detail="虎牙账号不存在或无权操作")
|
||||
if not account.sms_url:
|
||||
raise HTTPException(status_code=400, detail="该账号未配置接码链接 (sms_url)")
|
||||
from core.sms_provider import SmsLine, SmsProviderClient, detect_provider
|
||||
|
||||
client = SmsProviderClient(timeout=(8, 15))
|
||||
item = SmsLine(
|
||||
phone=account.game_phone or "",
|
||||
url=account.sms_url,
|
||||
provider=detect_provider(account.sms_url),
|
||||
raw=f"{account.game_phone}----{account.sms_url}",
|
||||
)
|
||||
poll_result = client.poll(item)
|
||||
return {
|
||||
"account_id": account.id,
|
||||
"username": account.username or "",
|
||||
"phone": account.game_phone or "",
|
||||
"sms_url": account.sms_url,
|
||||
"success": poll_result.has_code,
|
||||
"code": poll_result.code,
|
||||
"status": poll_result.status,
|
||||
"message": poll_result.message
|
||||
or ("成功识别验证码" if poll_result.has_code else "未识别到验证码"),
|
||||
"raw": poll_result.raw[:500],
|
||||
}
|
||||
|
||||
|
||||
@router.post("/accounts/{account_id}/auto-sms-login")
|
||||
def auto_sms_login_account(
|
||||
account_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""使用该账号绑定的手机号与接码链接一键执行虎牙短信登录。"""
|
||||
_require_huya_perm(current, "huya:import")
|
||||
account = (
|
||||
_visible_huya_accounts_query(db, current)
|
||||
.filter(HuyaAccount.id == account_id)
|
||||
.first()
|
||||
)
|
||||
if account is None:
|
||||
raise HTTPException(status_code=404, detail="虎牙账号不存在或无权操作")
|
||||
phone = (account.game_phone or "").strip()
|
||||
sms_url = (account.sms_url or "").strip()
|
||||
if not phone or not sms_url:
|
||||
raise HTTPException(status_code=400, detail="该账号缺少手机号或接码链接")
|
||||
from core.huya.auto_register import auto_register_huya_phone
|
||||
from core.sms_provider import SmsLine, detect_provider
|
||||
|
||||
item = SmsLine(
|
||||
phone=phone,
|
||||
url=sms_url,
|
||||
provider=detect_provider(sms_url),
|
||||
raw=f"{phone}----{sms_url}",
|
||||
)
|
||||
res = auto_register_huya_phone(item, wait_seconds=90, poll_interval=2)
|
||||
if not res.success or not res.cookie:
|
||||
account.status = "login_failed"
|
||||
account.updated_at = datetime.now(UTC)
|
||||
db.commit()
|
||||
raise HTTPException(
|
||||
status_code=502, detail=res.message or "虎牙短信接码登录失败"
|
||||
)
|
||||
saved = save_huya_login_cookie_to_account(
|
||||
db,
|
||||
account,
|
||||
res.cookie,
|
||||
tag=account.tag or "",
|
||||
username_hint=account.username or "",
|
||||
)
|
||||
saved.status = "active"
|
||||
saved.login_channel = "sms"
|
||||
db.commit()
|
||||
return {
|
||||
"message": "短信接码登录成功,Cookie 已保存",
|
||||
"success": True,
|
||||
"account": _account_out(saved),
|
||||
}
|
||||
|
||||
|
||||
@router.post("/register/batches", response_model=HuyaAutoRegisterBatchOut)
|
||||
def create_auto_register_batch(
|
||||
req: HuyaAutoRegisterRequest,
|
||||
@@ -1258,17 +1406,22 @@ def app_password_login_selected_accounts(
|
||||
)
|
||||
continue
|
||||
|
||||
sms_url = (account.sms_url or "").strip()
|
||||
try:
|
||||
result = login_huya_app_password(
|
||||
username=username,
|
||||
password=password,
|
||||
force_new_device=req.force_new_device,
|
||||
sms_url=sms_url,
|
||||
)
|
||||
if not result.success or not result.cookie:
|
||||
account.status = "login_failed"
|
||||
account.updated_at = datetime.now(UTC)
|
||||
db.commit()
|
||||
failed_count += 1
|
||||
logger.warning(
|
||||
f"[huya-app] 账号 {username} 批量登录未完成: {result.message} (code={result.code})"
|
||||
)
|
||||
results.append(
|
||||
{
|
||||
"line": account.id,
|
||||
|
||||
@@ -305,6 +305,13 @@ class HuyaSmsLoginRequest(BaseModel):
|
||||
tag: str = ""
|
||||
|
||||
|
||||
class HuyaTestSmsUrlRequest(BaseModel):
|
||||
"""在线测试从接码链接提取验证码。"""
|
||||
|
||||
url: str = Field(..., min_length=1)
|
||||
phone: str = ""
|
||||
|
||||
|
||||
class HuyaAutoRegisterRequest(BaseModel):
|
||||
"""虎牙自动注册批次。每行格式:手机号----短信查询URL。"""
|
||||
|
||||
@@ -441,6 +448,7 @@ class HuyaAccountOut(BaseModel):
|
||||
game_name: str = ""
|
||||
game_channel: str = ""
|
||||
game_phone: str = ""
|
||||
sms_url: str = ""
|
||||
assigned_to: int | None = None
|
||||
assigned_username: str | None = None
|
||||
created_at: datetime | None = None
|
||||
@@ -463,10 +471,12 @@ class HuyaAccountOut(BaseModel):
|
||||
"tag": self.tag,
|
||||
"remark": self.remark,
|
||||
"status": self.status,
|
||||
"login_channel": self.login_channel,
|
||||
"points": self.points,
|
||||
"game_name": self.game_name,
|
||||
"game_channel": self.game_channel,
|
||||
"game_phone": self.game_phone,
|
||||
"sms_url": self.sms_url,
|
||||
"assigned_to": self.assigned_to,
|
||||
"assigned_username": self.assigned_username,
|
||||
"created_at": _isoformat(self.created_at),
|
||||
|
||||
@@ -136,8 +136,13 @@ def _looks_like_url(value: str) -> bool:
|
||||
return parsed.scheme in {"http", "https"} and bool(parsed.netloc)
|
||||
|
||||
|
||||
def split_huya_password_line(line: str) -> HuyaPasswordLine | None:
|
||||
"""拆分虎牙账号密码行,兼容账号----密码----手机号----验证码链接。"""
|
||||
def split_huya_password_line(
|
||||
line: str, require_four_segments: bool = False
|
||||
) -> HuyaPasswordLine | None:
|
||||
"""拆分虎牙账号密码行,兼容账号----密码----手机号----验证码链接。
|
||||
|
||||
若 require_four_segments 为 True,则严格限制必须包含手机号和有效接码链接。
|
||||
"""
|
||||
raw = (line or "").strip()
|
||||
if not raw or raw.startswith("#"):
|
||||
return None
|
||||
@@ -174,6 +179,10 @@ def split_huya_password_line(line: str) -> HuyaPasswordLine | None:
|
||||
cookie = "----".join(extra)
|
||||
if not username or not password:
|
||||
return None
|
||||
if require_four_segments and (
|
||||
not phone or not sms_url or not _looks_like_url(sms_url)
|
||||
):
|
||||
return None
|
||||
return HuyaPasswordLine(
|
||||
username=username,
|
||||
password=password,
|
||||
@@ -184,15 +193,23 @@ def split_huya_password_line(line: str) -> HuyaPasswordLine | None:
|
||||
|
||||
|
||||
def import_huya_password_accounts(
|
||||
db: Session, text: str, tag: str = ""
|
||||
db: Session,
|
||||
text: str,
|
||||
tag: str = "",
|
||||
require_four_segments: bool = True,
|
||||
) -> tuple[int, int]:
|
||||
"""导入虎牙账号密码,返回 (导入/更新数, 跳过数)。"""
|
||||
"""导入虎牙账号密码,返回 (导入/更新数, 跳过数)。
|
||||
|
||||
默认仅允许四段格式(账号----密码----手机号----接码链接)。
|
||||
"""
|
||||
created_or_updated = 0
|
||||
skipped = 0
|
||||
tag = (tag or "").strip()
|
||||
|
||||
for line in (text or "").splitlines():
|
||||
parsed = split_huya_password_line(line)
|
||||
parsed = split_huya_password_line(
|
||||
line, require_four_segments=require_four_segments
|
||||
)
|
||||
if not parsed:
|
||||
if line.strip():
|
||||
skipped += 1
|
||||
@@ -210,6 +227,7 @@ def import_huya_password_accounts(
|
||||
account_password=password,
|
||||
cookie=normalize_huya_cookie(cookie) if cookie else "",
|
||||
game_phone=parsed.phone,
|
||||
sms_url=parsed.sms_url,
|
||||
tag=tag,
|
||||
status="password_imported",
|
||||
)
|
||||
@@ -220,6 +238,8 @@ def import_huya_password_accounts(
|
||||
account.cookie = normalize_huya_cookie(cookie)
|
||||
if parsed.phone:
|
||||
account.game_phone = parsed.phone
|
||||
if parsed.sms_url:
|
||||
account.sms_url = parsed.sms_url
|
||||
if tag:
|
||||
account.tag = tag
|
||||
if not account.cookie:
|
||||
|
||||
@@ -20,6 +20,7 @@ import type {
|
||||
HuyaAppPasswordLoginRequest,
|
||||
HuyaPasswordLoginResult,
|
||||
HuyaPasswordLoginSelectedRequest,
|
||||
HuyaFetchSmsCodeResult,
|
||||
HuyaRechargeGoodsItem,
|
||||
HuyaRegisterSuccessLog,
|
||||
HuyaSmsCodeRequest,
|
||||
@@ -73,6 +74,12 @@ export const huyaApi = {
|
||||
api.post<HuyaSmsCodeResult, HuyaSmsCodeResult>('/huya/accounts/sms-code', data, { timeout: 120000 }),
|
||||
smsLogin: (data: HuyaSmsLoginRequest) =>
|
||||
api.post<HuyaSmsLoginResult, HuyaSmsLoginResult>('/huya/accounts/sms-login', data, { timeout: 120000 }),
|
||||
testSmsUrl: (sms_url: string) =>
|
||||
api.post<HuyaFetchSmsCodeResult, HuyaFetchSmsCodeResult>('/huya/accounts/test-sms-url', { sms_url }),
|
||||
fetchSmsCode: (accountId: number) =>
|
||||
api.post<HuyaFetchSmsCodeResult, HuyaFetchSmsCodeResult>(`/huya/accounts/${accountId}/fetch-sms-code`),
|
||||
autoSmsLogin: (accountId: number) =>
|
||||
api.post<HuyaPasswordLoginResult, HuyaPasswordLoginResult>(`/huya/accounts/${accountId}/auto-sms-login`),
|
||||
startAutoRegister: (data: HuyaAutoRegisterRequest) =>
|
||||
api.post<HuyaAutoRegisterBatch, HuyaAutoRegisterBatch>('/huya/register/batches', data),
|
||||
listAutoRegisterBatches: (limit = 50) =>
|
||||
|
||||
@@ -458,12 +458,21 @@ export interface HuyaAccountItem {
|
||||
game_name: string;
|
||||
game_channel: string;
|
||||
game_phone: string;
|
||||
sms_url?: string;
|
||||
assigned_to: number | null;
|
||||
assigned_username: string | null;
|
||||
created_at: string | null;
|
||||
updated_at: string | null;
|
||||
}
|
||||
|
||||
export interface HuyaFetchSmsCodeResult {
|
||||
code: string | null;
|
||||
raw: string;
|
||||
success: boolean;
|
||||
message: string;
|
||||
}
|
||||
|
||||
|
||||
/** 工作台账号集合范围: elite=精英宝典 / browser_exchange=浏览器兑换 */
|
||||
export type HuyaWorkbenchScope = 'elite' | 'browser_exchange';
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user