修正活动WSS会话guid一致性

This commit is contained in:
yml2213
2026-09-02 18:07:31 +08:00
parent 92ba6cbc62
commit aaaa568916
9 changed files with 1490 additions and 11 deletions
+7
View File
@@ -94,6 +94,13 @@ class HuyaEliteWssSession:
)
if not initialized:
raise RuntimeError(f"虎牙 {kind} WSS 初始化失败")
if kind == "activity" and self.client._launch_guid:
# wsLaunch 的 sGuid 是当前连接的真实会话值,供后续业务 Cookie
# 与 HTTP baseinfo 继续复用,避免沿用登录时的旧 guid。
self.cookie = self.client._cookie_with_guid(
self.cookie, self.client._launch_guid
)
self.guid = self.client._launch_guid
except Exception:
self.close()
raise
+36 -4
View File
@@ -26,6 +26,7 @@ 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 .taf_protocol import TafInputStream, TafOutputStream, TafStruct, TafType
from .wup_protocol import WupRequest, WupResponse
@@ -192,6 +193,26 @@ class HuyaWssClient:
return normalized
return f"{normalized}; {cookie}"
@staticmethod
def _cookie_with_guid(cookie: str, guid: str) -> str:
"""用当前 WSS 会话的 sGuid 替换 Cookie 中的旧 guid。"""
value = str(guid or "").strip()
if not value:
return cookie or ""
pairs = cookie_pairs(cookie)
replaced = False
normalized: list[tuple[str, str]] = []
for key, item_value in pairs:
if key == "guid":
if not replaced:
normalized.append((key, value))
replaced = True
continue
normalized.append((key, item_value))
if not replaced:
normalized.append(("guid", value))
return normalize_cookie_pairs(normalized)
@staticmethod
def _build_biz_user(uid: int, cookie: str):
"""构造 WSS 业务 UserId(HAR 实证:guid 为空,cookie 不额外加前缀)"""
@@ -389,7 +410,12 @@ class HuyaWssClient:
if launch_rsp is None:
self.logger("[WSS] 活动 wsLaunch 无响应")
return False
await self.send_auth(uid, cookie, sequence=SEQ_ACTIVITY_BIZ)
# wsLaunch 会为当前连接签发新的 sGuid。AUTH、getConfig 和后续业务
# 请求必须使用同一个值;登录 Cookie 里的旧 guid 不能继续复用。
if self._launch_guid:
cookie = self._cookie_with_guid(cookie, self._launch_guid)
guid = self._launch_guid
await self.send_auth(uid, cookie, sequence=SEQ_ACTIVITY_BIZ, guid=guid)
# 9.1 抓包:getConfig 使用 wsLaunch 响应返回的新 guid;业务 UserId 仍为空 guid。
config_rsp = await self.call_get_config_activity(
uid, cookie, guid=self._launch_guid
@@ -570,7 +596,13 @@ class HuyaWssClient:
return body[5 : 5 + wup_len]
return body
async def send_auth(self, uid: int, cookie: str, sequence: int = SEQ_WSLAUNCH):
async def send_auth(
self,
uid: int,
cookie: str,
sequence: int = SEQ_WSLAUNCH,
guid: str = "",
):
"""cmd 0x0a AUTH — 对齐 9.1 抓包(828 活动帧)。
9.1 活动 AUTH 结构(逐字节核验):
@@ -591,8 +623,8 @@ class HuyaWssClient:
os.write_string(1, WSS_COOKIE_UA)
os.write_string(2, HuyaWssClient._normalize_biz_cookie(cookie))
guid_match = re.search(r"(?:^|;\s*)guid=([^;]+)", cookie or "")
guid = (guid_match.group(1).strip() if guid_match else "") or self._launch_guid
os.write_string(3, guid or "")
auth_guid = (guid or "").strip() or (guid_match.group(1).strip() if guid_match else "") or self._launch_guid
os.write_string(3, auth_guid)
os.write_int8(4, 1)
os.write_string(5, "HUYA&ZH&2052")
os.write_string(6, "")