虎牙网页Cookie来源驱动合并与WSS传输门禁

This commit is contained in:
yml2213
2026-09-01 19:17:32 +08:00
parent 8e3c0e69bc
commit 48c37bb8d7
16 changed files with 840 additions and 57 deletions
+18 -6
View File
@@ -22,6 +22,7 @@ from .device_profile import canonical_account_key, mobile_user_agent
FINGERPRINT_DIR = Path(__file__).parent / "fingerprint"
RUNNER_JS = FINGERPRINT_DIR / "runner.js"
SDID_PREFIX = "__SDID__"
COOKIE_PREFIX = "__COOKIES__"
# 每账号独立的 hydevice localStorage 状态目录 (一号一设备的关键):
# 默认全局临时目录会让所有账号共用同一份设备采集状态 → sdid/40hex hdid 同源,
@@ -78,6 +79,9 @@ class HuyaSdidResult:
hdid: str = "" # df/collect 同响应下发的 40hex 设备ID; 与 WUP 登录的 32hex hdid 非同一体系(见 docs/HUYA_APP_OVERVIEW.md §二)
source: str = ""
message: str = ""
# Cookie values actually written by the hydevice runtime. This is kept
# separate from sdid/hdid because the latter are response values.
cookies: dict[str, str] | None = None
HDID_PREFIX = "__HDID__"
@@ -85,8 +89,8 @@ HDID_PREFIX = "__HDID__"
def _run_node_runner(
state_dir: Path, app_id: str, timeout: tuple[float, float]
) -> tuple[str, str]:
"""调用 node runner,返回 (sdid, hdid)。
) -> tuple[str, str, dict[str, str]]:
"""调用 node runner,返回 (sdid, hdid, cookies)。
若 state_dir/device.json 存在 (账号画像派生的设备覆盖参数), runner 会以该
设备身份运行 hydevice → 不同账号的 sdid/40hex hdid 不再同源 (一号一设备)。
@@ -109,15 +113,21 @@ def _run_node_runner(
except subprocess.TimeoutExpired as exc:
raise HuyaFingerprintError(f"hydevice runner 超时({total_timeout}s)") from exc
sdid, hdid = "", ""
sdid, hdid, cookies = "", "", {}
for line in (proc.stdout or "").splitlines():
line = line.strip()
if line.startswith(SDID_PREFIX) and len(line) > len(SDID_PREFIX) + 20:
sdid = line[len(SDID_PREFIX) :]
if line.startswith(HDID_PREFIX) and len(line) > len(HDID_PREFIX) + 20:
hdid = line[len(HDID_PREFIX) :]
if line.startswith(COOKIE_PREFIX):
raw = line[len(COOKIE_PREFIX) :]
for item in raw.split(";"):
key, sep, value = item.strip().partition("=")
if sep and key and value:
cookies[key] = value
if sdid:
return sdid, hdid
return sdid, hdid, cookies
stderr_tail = (proc.stderr or "").strip().splitlines()
detail = stderr_tail[-1][:120] if stderr_tail else f"exit={proc.returncode}"
raise HuyaFingerprintError(f"hydevice runner 未返回 sdid: {detail}")
@@ -195,12 +205,14 @@ def get_huya_sdid(
# 一号一设备: 账号画像 → hydevice 设备覆盖 (UA/屏幕/机型)
write_device_hint(state_dir, device_hint)
try:
sdid, hdid = _run_node_runner(state_dir, app_id, timeout)
sdid, hdid, cookies = _run_node_runner(state_dir, app_id, timeout)
if sdid:
logger.debug(
"虎牙设备指纹成功(node): sdid={}... hdid={}...", sdid[:24], hdid[:10]
)
return HuyaSdidResult(sdid=sdid, hdid=hdid, source="fingerprint")
return HuyaSdidResult(
sdid=sdid, hdid=hdid, source="fingerprint", cookies=cookies
)
except HuyaFingerprintError as exc:
logger.warning("虎牙 hydevice 指纹失败: {}", exc)
if not allow_fallback: