feat(huya): 对齐积分中心开通链路(2026-10-13 抓包)
按 10.13「开通精英宝典/购买幸运币」抓包(m-shop.huya.com/points.html)对齐商城链路: - WSS host 改为 launch.queryHttpDns 动态解析(失败回退抓包实测 host);握手 baseinfo 只带 uid/guid/页面 UA/exp,Origin 与页面同源 - wsLaunch 改为带真实 lUid/sGuid;初始化补上 AUTH(0x0A) → getConfig → UpdateUserExps(0x17),并把 getConfig 下发的版本 map 原样回传(不回传则业务 RPC 无响应) - 补齐新 RPC:getSupplierInfoV5 / acceptHyProtocolV5 / calcOrderPromotion / PlayMallUI.getMyPromotion|queryAllocation / wupui.* / huyauserui.getUserProfile - 下单参数对齐:shopAppInfo.scene=7 与订单 src/orderScene=4 分离、orderType=6、 bizType=5、gameCategoryId=507、extraParam.bizParams 标记 jifencenter、env 仅空点击 序列、支付回调指向 m-shop.huya.com/points.html - 开通链路抽到 services/huya_shop_flow.py,充值任务改为调用它 验证:新增 tests/test_huya_points_shop_protocol.py 与抓包逐字节比对(心跳、版本回传、 wsLaunch/getConfig/下单/支付帧与新 RPC 参数,共 10 条);按 AGENTS.md 做了反向验证 (把 jifencenter 改成 0 后该用例变红,还原后恢复)。ruff format --check 与全量 pytest 通过。
This commit is contained in:
+153
-3
@@ -11,6 +11,8 @@ import base64
|
||||
import hashlib
|
||||
import json
|
||||
import random
|
||||
import re
|
||||
import time
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
from collections.abc import Callable
|
||||
@@ -121,6 +123,32 @@ def generate_http_baseinfo(
|
||||
return urllib.parse.quote(b64, safe="")
|
||||
|
||||
|
||||
def generate_ws_baseinfo(
|
||||
uid: int,
|
||||
guid: str,
|
||||
ua: str,
|
||||
exp: str = "",
|
||||
) -> str:
|
||||
"""构造 WSS 连接 URL 的 baseinfo(返回未转义的 base64)。
|
||||
|
||||
2026-10-13 积分中心抓包实证:握手 baseinfo 只有
|
||||
lUid/sGuid/sUA/sAppSrc/sExp 有值,cookie 与 traceId 都是空的;
|
||||
``sExp`` 是 getConfig 下发的版本串(实测 ``58664.107263``)。
|
||||
"""
|
||||
info = WSConnectParaInfo()
|
||||
info.lUid = int(uid or 0)
|
||||
info.sGuid = guid or ""
|
||||
info.sUA = ua
|
||||
info.sAppSrc = "HUYA&ZH&2052"
|
||||
info.sExp = exp or ""
|
||||
info.sCookie = ""
|
||||
info.sTraceId = ""
|
||||
|
||||
os = TafOutputStream()
|
||||
info.write_to(os)
|
||||
return base64.b64encode(os.get_bytes()).decode("ascii")
|
||||
|
||||
|
||||
class HuyaHttpClient:
|
||||
"""虎牙 HTTP POST RPC 客户端"""
|
||||
|
||||
@@ -261,6 +289,9 @@ class HuyaHttpClient:
|
||||
guid: str = "",
|
||||
cookie: str = "",
|
||||
timeout: float = 15.0,
|
||||
request_id: int = 1,
|
||||
origin: str = "https://zt.huya.com",
|
||||
referer: str = "https://zt.huya.com/",
|
||||
):
|
||||
"""
|
||||
HTTP POST RPC 调用
|
||||
@@ -271,12 +302,14 @@ class HuyaHttpClient:
|
||||
req_struct: 请求结构体 (TafStruct)
|
||||
rsp_class: 响应类
|
||||
uid/guid/cookie: 用于构造 baseinfo,业务请求体也会按各接口需要携带
|
||||
request_id: WUP iRequestId(launch.queryHttpDns 固定 -1)
|
||||
origin/referer: 部分接口(积分中心页面)要求与页面同源
|
||||
"""
|
||||
# 1. 构造 Wup 请求体
|
||||
wup = WupRequest()
|
||||
wup.setServant(service)
|
||||
wup.setFunc(method)
|
||||
wup.setRequestId(1)
|
||||
wup.setRequestId(request_id)
|
||||
wup.writeStruct("tReq", req_struct)
|
||||
wup_data = wup.encode() # [4字节长度][wup body]
|
||||
|
||||
@@ -297,8 +330,8 @@ class HuyaHttpClient:
|
||||
method="POST",
|
||||
headers={
|
||||
"User-Agent": PC_UA,
|
||||
"Origin": "https://zt.huya.com",
|
||||
"Referer": "https://zt.huya.com/",
|
||||
"Origin": origin,
|
||||
"Referer": referer,
|
||||
"Content-Type": "application/octet-stream",
|
||||
"Accept-Language": "zh-CN,zh;q=0.9",
|
||||
},
|
||||
@@ -348,6 +381,39 @@ class HuyaHttpClient:
|
||||
|
||||
# ---------- 业务便捷方法 ----------
|
||||
|
||||
def query_http_dns(
|
||||
self,
|
||||
uid: int,
|
||||
cookie: str,
|
||||
guid: str = "",
|
||||
ua: str = HTTP_HUYA_UA,
|
||||
domains: list[str] | None = None,
|
||||
timeout: float = 10.0,
|
||||
) -> list[str]:
|
||||
"""launch.queryHttpDns — 换取 WSS host 列表(失败返回空列表)。"""
|
||||
req = QueryHttpDnsReq()
|
||||
req.uid = int(uid or 0)
|
||||
req.ua = ua
|
||||
req.domains = list(domains or [HTTP_DNS_DOMAIN])
|
||||
resp = self.call_rpc(
|
||||
"launch",
|
||||
"queryHttpDns",
|
||||
req,
|
||||
None,
|
||||
uid=uid,
|
||||
guid=guid,
|
||||
cookie=cookie,
|
||||
timeout=timeout,
|
||||
request_id=-1,
|
||||
origin="https://m-shop.huya.com",
|
||||
referer="https://m-shop.huya.com/points.html",
|
||||
)
|
||||
if resp is None:
|
||||
return []
|
||||
newdata = getattr(resp, "newdata", None) or {}
|
||||
payload = newdata.get("tRsp") or newdata.get("tResp") or b""
|
||||
return _extract_ws_hosts(payload)
|
||||
|
||||
def get_act_info(
|
||||
self,
|
||||
act_id: int,
|
||||
@@ -1189,5 +1255,89 @@ def _self_check():
|
||||
assert empty_user.sCookie == ""
|
||||
|
||||
|
||||
# ============================================================
|
||||
# launch.queryHttpDns:动态解析 WSS host
|
||||
#
|
||||
# 2026-10-13「精英宝典」抓包确认:m-shop.huya.com 页面先用 HTTP cdnws 调
|
||||
# launch.queryHttpDns(iRequestId 固定 -1)换取 host 列表,再连 WSS。
|
||||
# 因此 host 不应长期写死;解析失败时回退到抓包实测 host。
|
||||
# ============================================================
|
||||
|
||||
HTTP_DNS_DOMAIN = "cdnws.api.huya.com"
|
||||
_WSS_HOST_RE = re.compile(rb"[a-z0-9]{6,}-ws\.va\.huya\.com")
|
||||
#: 解析成功后的缓存时长(响应实测 ttl 1296000s,这里保守缓存)。
|
||||
DNS_CACHE_TTL = 600.0
|
||||
#: 解析失败后的冷却时间,避免每个任务都打一次无用的 DNS 请求。
|
||||
DNS_FAILURE_COOLDOWN = 30.0
|
||||
_dns_cache: dict[str, tuple[float, list[str]]] = {}
|
||||
|
||||
|
||||
class QueryHttpDnsReq(TafStruct):
|
||||
"""launch.queryHttpDns 请求(2026-10-13 抓包逐字段)。
|
||||
|
||||
tag0 uid, tag1 UA, tag2 域名列表, tag3 appSrc, tag4 "", tag5 0, tag6 1
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.uid: int = 0 # tag 0
|
||||
self.ua: str = "" # tag 1
|
||||
self.domains: list[str] = [] # tag 2
|
||||
self.appSrc: str = "HUYA&ZH&2052" # tag 3
|
||||
self.blank4: str = "" # tag 4
|
||||
self.flag5: int = 0 # tag 5
|
||||
self.flag6: int = 1 # tag 6
|
||||
|
||||
def write_to(self, os: TafOutputStream):
|
||||
os.write_int64(0, self.uid)
|
||||
os.write_string(1, self.ua)
|
||||
os.write_list(2, self.domains)
|
||||
os.write_string(3, self.appSrc)
|
||||
os.write_string(4, self.blank4)
|
||||
os.write_int32(5, self.flag5)
|
||||
os.write_int32(6, self.flag6)
|
||||
|
||||
def read_from(self, ins: TafInputStream):
|
||||
pass
|
||||
|
||||
|
||||
def _extract_ws_hosts(payload: bytes) -> list[str]:
|
||||
"""从 queryHttpDns 响应里按出现顺序取出 WSS host(去重)。"""
|
||||
hosts: list[str] = []
|
||||
for match in _WSS_HOST_RE.findall(bytes(payload or b"")):
|
||||
host = match.decode("ascii", errors="ignore")
|
||||
if host and host not in hosts:
|
||||
hosts.append(host)
|
||||
return hosts
|
||||
|
||||
|
||||
def resolve_wss_host(
|
||||
uid: int,
|
||||
cookie: str,
|
||||
guid: str = "",
|
||||
logger: Callable[[str], None] | None = None,
|
||||
) -> str:
|
||||
"""解析积分中心通道的 WSS host:queryHttpDns + TTL 缓存 + 抓包 host 兜底。"""
|
||||
from .wss_client import SHOP_POINTS_WS_HOST
|
||||
|
||||
log = logger or (lambda _message: None)
|
||||
now = time.monotonic()
|
||||
cached = _dns_cache.get(HTTP_DNS_DOMAIN)
|
||||
if cached and now < cached[0]:
|
||||
hosts = cached[1]
|
||||
else:
|
||||
hosts = []
|
||||
try:
|
||||
hosts = HuyaHttpClient(logger=log).query_http_dns(uid, cookie, guid=guid)
|
||||
except Exception: # noqa: BLE001 外部接口失败一律回退默认 host
|
||||
hosts = []
|
||||
ttl = DNS_CACHE_TTL if hosts else DNS_FAILURE_COOLDOWN
|
||||
_dns_cache[HTTP_DNS_DOMAIN] = (now + ttl, hosts)
|
||||
if not hosts:
|
||||
log("[WSS] queryHttpDns 无结果,使用抓包实测 host")
|
||||
return SHOP_POINTS_WS_HOST
|
||||
log(f"[WSS] queryHttpDns 解析 host={hosts[0]}(候选 {len(hosts)} 个)")
|
||||
return hosts[0]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
_self_check()
|
||||
|
||||
Reference in New Issue
Block a user