复刻QIMEI UUID42并默认使用无头浏览器
This commit is contained in:
@@ -96,7 +96,12 @@ class Cdp:
|
||||
return r.get("result", {}).get("value")
|
||||
|
||||
|
||||
def launch(profile: Path, port: int, headless: bool = False) -> tuple[subprocess.Popen, str]:
|
||||
def launch(profile: Path, port: int, headless: bool = True) -> tuple[subprocess.Popen, str]:
|
||||
"""启动用于 CDP 抓取的 Chrome。
|
||||
|
||||
默认使用无头模式,避免登录/抓取流程弹出窗口;传入 ``headless=False``
|
||||
仍可在需要人工观察页面时启用可视化窗口。
|
||||
"""
|
||||
args = [
|
||||
str(CHROME),
|
||||
"--no-first-run", "--no-default-browser-check",
|
||||
@@ -170,9 +175,9 @@ def cookie_names_from_header(headers: dict) -> list[str]:
|
||||
return [part.strip().split("=", 1)[0] for part in raw.split(";") if part.strip()]
|
||||
|
||||
|
||||
async def capture_round(profile: Path, tag: str, port: int):
|
||||
async def capture_round(profile: Path, tag: str, port: int, *, headless: bool = True):
|
||||
"""抓取一轮:导航序列 + 全量 Cookie 快照。"""
|
||||
proc, ws_url = launch(profile, port)
|
||||
proc, ws_url = launch(profile, port, headless=headless)
|
||||
try:
|
||||
async with websockets.connect(ws_url, max_size=64 * 1024 * 1024) as raw_ws:
|
||||
cdp = Cdp(raw_ws)
|
||||
@@ -294,14 +299,14 @@ async def capture_round(profile: Path, tag: str, port: int):
|
||||
proc.kill()
|
||||
|
||||
|
||||
async def probe_anon(profile: Path, port: int):
|
||||
async def probe_anon(profile: Path, port: int, *, headless: bool = True):
|
||||
"""复用已有 profile:复放 udblgn 匿名流程,解密请求/响应体。
|
||||
|
||||
抓取会话内 anonymousLogin 与 web/middle 的 postData 与响应体,
|
||||
然后带全套 Cookie 用 requests 独立重放 anonymousLogin,检查
|
||||
服务端是否在匿名流程中下发 guid/udb_guiddata/匿名 token 等字段。
|
||||
"""
|
||||
proc, ws_url = launch(profile, port)
|
||||
proc, ws_url = launch(profile, port, headless=headless)
|
||||
try:
|
||||
async with websockets.connect(ws_url, max_size=64 * 1024 * 1024) as raw_ws:
|
||||
cdp = Cdp(raw_ws)
|
||||
@@ -562,6 +567,11 @@ def main():
|
||||
ap.add_argument("--profile", default="/tmp/huya-browser-p1")
|
||||
ap.add_argument("--port", type=int, default=9333)
|
||||
ap.add_argument("--out", default=str(OUT_DIR))
|
||||
ap.add_argument(
|
||||
"--headed",
|
||||
action="store_true",
|
||||
help="显示 Chrome 窗口(默认使用无头模式)",
|
||||
)
|
||||
args = ap.parse_args()
|
||||
OUT_DIR = Path(args.out)
|
||||
profile = Path(args.profile)
|
||||
@@ -572,7 +582,7 @@ def main():
|
||||
if args.mode == "round2" and not (profile / "Default").exists():
|
||||
print("round2 需要先运行 round1(同一 profile)")
|
||||
sys.exit(2)
|
||||
asyncio.run(capture_round(profile, args.mode, args.port))
|
||||
asyncio.run(capture_round(profile, args.mode, args.port, headless=not args.headed))
|
||||
elif args.mode == "probe":
|
||||
# 匿名流程只在"无 udb_deviceid 持久态"的全新 profile 触发;
|
||||
# probe 模式内部使用临时全新 profile,跑完自动清理。
|
||||
@@ -581,7 +591,7 @@ def main():
|
||||
|
||||
tmp = tempfile.mkdtemp(prefix="huya-probe-")
|
||||
try:
|
||||
asyncio.run(probe_anon(Path(tmp), args.port))
|
||||
asyncio.run(probe_anon(Path(tmp), args.port, headless=not args.headed))
|
||||
finally:
|
||||
shutil.rmtree(tmp, ignore_errors=True)
|
||||
else:
|
||||
@@ -589,4 +599,4 @@ def main():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
- ``guid/_qimei_uuid42/__yamid_new/game_did`` 由页面 SDK 脚本写 document.cookie,
|
||||
无服务端签发接口。其中 ``guid`` 与会话强绑定:**wsLaunch 响应返回的新 guid 必须
|
||||
回写进 cookie**,否则 getConfig 无响应(2026-09-01 实测);
|
||||
``_qimei_uuid42/__yamid_new/game_did`` 为设备标识,随机值格式对齐即可;
|
||||
``_qimei_uuid42/__yamid_new/game_did`` 为设备标识,已按浏览器脚本算法生成;
|
||||
- 其余统计/会话字段(SoundValue/Hm_*/rep_cnt 等)服务端仅记录,可随机生成。
|
||||
|
||||
2026-09-01 全链路实测:账号 id=58 补齐后 checkUserBindGameAccount 返回
|
||||
@@ -20,7 +20,9 @@ from __future__ import annotations
|
||||
import random
|
||||
import secrets
|
||||
import time
|
||||
import zlib
|
||||
from collections.abc import Mapping
|
||||
from datetime import UTC, datetime
|
||||
|
||||
# 统计/会话字段生成规则;9.1 抓包值为样例,格式保持对齐
|
||||
_FILL_MAP: tuple[tuple[str, str], ...] = (
|
||||
@@ -36,7 +38,7 @@ _FILL_MAP: tuple[tuple[str, str], ...] = (
|
||||
# + v1 版本位 + 时钟序列 + 48 位随机 node)。浏览器 epoch = new Date(1582,10,15,
|
||||
# 0,0,0,0).getTime() 实测 -12216643543000(GMT+8)。CBCC 前缀是时间戳自然结果。
|
||||
# - game_did: vplayerUI.js 35 位随机,字符集 64 去掉末尾 '_'(63 选 1)。
|
||||
# - _qimei_uuid42: QIMEI SDK 生成,cookie 值首字节固定 '1a'(本地存储 44hex 截断)。
|
||||
# - _qimei_uuid42: QIMEI SDK 生成,时间字段 + CRC32 替换段 + 随机半字节段。
|
||||
|
||||
_SDK_EPOCH_1582 = -12216643543000 # 浏览器实测 JS Date(1582,10,15) 本地时区 ms
|
||||
_BASE16 = "0123456789ABCDEF"
|
||||
@@ -263,6 +265,41 @@ def _rand_float() -> str:
|
||||
return f"{random.random():.17f}"
|
||||
|
||||
|
||||
def _qimei_uuid42_generate(
|
||||
now_ns: int | None = None,
|
||||
*,
|
||||
cookie_enabled: bool = True,
|
||||
random_value: float | None = None,
|
||||
) -> str:
|
||||
"""逐字复刻 QIMEI Web SDK 0.6.3 的 42 位 ``h38`` 生成器。
|
||||
|
||||
``now_ns`` 保留旧回退函数的纳秒参数约定;SDK 实际只读取本地时间的
|
||||
年/月/日/时/分/秒,因此毫秒和纳秒不会进入结果。时间前缀的年份和月份
|
||||
使用 SDK 的十六进制编码,最后三位来自 ``navigator.cookieEnabled``。
|
||||
``random_value`` 仅用于固定测试;生产调用沿用 ``Math.random()`` 等价的
|
||||
Python ``random.random()``,并将理论上的 1.0 夹到合法半字节上界 15。
|
||||
"""
|
||||
now_ms = time.time_ns() // 1_000_000 if now_ns is None else now_ns // 1_000_000
|
||||
current = datetime.fromtimestamp(now_ms / 1000, tz=UTC).astimezone()
|
||||
|
||||
# SDK: Number(year % 100).toString(16).padStart(2, "0") +
|
||||
# Number(month + 1).toString(16) + each remaining component padded to 2.
|
||||
year_hex = f"{current.year % 100:x}".rjust(2, "0")
|
||||
month_hex = f"{current.month:x}"
|
||||
date_parts = "".join(
|
||||
f"{value:x}".rjust(2, "0")
|
||||
for value in (current.day, current.hour, current.minute, current.second)
|
||||
)
|
||||
prefix = year_hex + month_hex + date_parts + ("100" if cookie_enabled else "000")
|
||||
|
||||
value = random.random() if random_value is None else float(random_value)
|
||||
nibble = min(15, max(0, int(value * 16)))
|
||||
repeated = f"{nibble:x}" * 20
|
||||
template = prefix + "----" + repeated + "----"
|
||||
replacement = f"{zlib.crc32(template.encode('ascii')) & 0xFFFFFFFF:08x}"
|
||||
return prefix + replacement[:4] + repeated + replacement[4:]
|
||||
|
||||
|
||||
def fill_web_cookie_fields(
|
||||
cookie: str,
|
||||
guid: str,
|
||||
@@ -308,8 +345,8 @@ def fill_web_cookie_fields(
|
||||
elif "guid" not in pairs:
|
||||
# guid 结构: '0a' + 10hex + '956a'/'966a' + 16hex(浏览器实测固定段,见 trace 文档)
|
||||
add("guid", "0a" + secrets.token_hex(5) + "966a" + secrets.token_hex(8))
|
||||
# _qimei_uuid42: QIMEI SDK 产物,cookie 首字节 '1a' + UUID 布局 32hex + 8hex 附加
|
||||
add("_qimei_uuid42", "1a" + secrets.token_hex(20))
|
||||
# _qimei_uuid42: QIMEI SDK 0.6.3 的时间/CRC32/随机半字节组合。
|
||||
add("_qimei_uuid42", _qimei_uuid42_generate())
|
||||
# __yamid_new: room_normal.js generate32 精确复刻(UUID-v1 时间戳,含 CBCC 前缀规律)
|
||||
add("__yamid_new", _yamid_new_generate32())
|
||||
add("game_did", _game_did_generate()) # 35 位随机(真实长度)
|
||||
|
||||
Reference in New Issue
Block a user