fix(huya): 结构体尾部字段容错解析(服务端新增 ZERO 字段不再打挂整个 RPC)

真机现象(2026-10-15,外部 CK 导入后查询角色):checkUserBindGameAccount 响应结构
尾部多了 tag6(ZERO 类型 0x6c),共享的 read_struct 读到非 STRUCT_END 直接抛
「期望 STRUCT_END, 实际 0x0c」,整个 RPC 返回 None —— 工作台「查询角色」永远失败。

- taf_protocol:read_struct 收尾改为按 TAF 语义跳过未建模的尾部字段(上限 32 个,
  防止解析错位时无限吞字节);新增 skip_to_struct_end()
- frame_decoder / shop_structs:抽出 decode_struct_bytes 与 ShopRawRsp.from_payload,
  WSS 与 HTTP 两条通道共用一份通用解码逻辑(wss_client 同步改用)

验证:tests/test_huya_struct_parse_resilience.py 用真机响应字节回归(含 tag6=ZERO 的
响应能解析出角色「泪雨全是谎言」);按 AGENTS.md 反向验证 —— 注掉
skip_to_struct_end() 即复现线上报错「期望 STRUCT_END, 实际 0x0c」,还原后恢复。
全量 pytest 通过。
This commit is contained in:
yml2213
2026-10-15 15:21:34 +08:00
parent 4ce30e5c26
commit a9d711a7e9
5 changed files with 133 additions and 29 deletions
+8 -28
View File
@@ -26,7 +26,12 @@ from typing import Any, cast
import websockets
from .cookie_utils import cookie_pairs, normalize_cookie_pairs
from .frame_decoder import _decode_taf_struct, _truncate, format_wss_log
from .frame_decoder import (
_decode_taf_struct,
_truncate,
decode_struct_bytes,
format_wss_log,
)
from .taf_protocol import TafInputStream, TafOutputStream, TafStruct, TafType
from .wup_protocol import WupRequest, WupResponse
@@ -817,39 +822,14 @@ class HuyaWssClient:
except Exception: # noqa: BLE001 响应解析失败按空结构处理
return {}
data = wup_resp.newdata.get("tRsp") or wup_resp.newdata.get("tResp")
if not isinstance(data, bytes) or not data:
return {}
ins = TafInputStream(data)
try:
_tag, dtype = ins.peek_head()
if dtype != TafType.STRUCT_BEGIN:
return {}
ins.read_head()
return _decode_taf_struct(ins)
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
return {}
return decode_struct_bytes(data) if isinstance(data, bytes) else {}
@classmethod
def _raw_rsp_from_body(cls, body: bytes):
"""响应 → ShopRawRsp(code/message + 通用解码结构)。"""
from .shop_structs import ShopRawRsp
payload = cls._decode_rsp_dict(body)
code = 0
message = ""
head = payload.get("tag0")
if isinstance(head, int):
# 扁平响应(acceptHyProtocolV5 / createOrderV5 等)
code = int(head)
if isinstance(payload.get("tag1"), str):
message = payload["tag1"]
elif isinstance(head, dict):
# 嵌套响应(PlayMallUI.getMyPromotion 的 tag0.tag0/tag0.tag1)
if isinstance(head.get("tag0"), int):
code = int(head["tag0"])
if isinstance(head.get("tag1"), str):
message = head["tag1"]
return ShopRawRsp(code=code, message=message, payload=payload)
return ShopRawRsp.from_payload(cls._decode_rsp_dict(body))
async def call_rpc_raw(
self,