按 10.13 抓包对齐 WSS:host 自动解析、exp 回传、pingSignal 与兑换记录字段
- 新增 launch.queryHttpDns:类型定义 + HTTPClient.QueryHttpDns + WSSClient.resolveHost(TTL 缓存、多 host 轮询、失败回退默认 host);Host 留空或等于历史默认值即视为自动解析 - WSS 握手 baseinfo 补 tag5=exp 与 tag10 空 customHeaders;UpdateUserExpsReq(cmd23) 改为回传 getConfig 的版本 map(缓存供重连复用) - 新增 launch.pingSignal:HTTP 预热改走 pingSignal,WSS 预热在建连后探测业务通道(失败仅记日志) - UserPrizeInfo 补 tag12 orderId/tag14 图标/tag16 prizeId/tag17 shopItemId/tag19 itemId/tag20 ext/tag21 exchangeDate,前端复盘表加订单号列 - taf 补 WriteListString/ReadListString/ReadMapStringInt64/ReadMapStringStruct - 新增 internal/huya/launch_test.go:用抓包真实报文做字节级夹具(baseinfo 122B、queryHttpDns 请求 93B、记录 308B 等) - CLI -wss-host 默认改为留空;GUI 设置支持留空并给出占位提示;README/WSS_NOTES 同步协议细节
This commit is contained in:
+156
-17
@@ -28,7 +28,13 @@ const (
|
||||
)
|
||||
|
||||
type WSSClient struct {
|
||||
Host string
|
||||
// Host 为空时(默认)自动通过 launch.queryHttpDns 解析 WSS host;
|
||||
// 显式赋值则固定使用该 host。
|
||||
Host string
|
||||
// AutoResolveHost 为 false 时不做 DNS 解析,直接回退 DefaultWSSHost。
|
||||
AutoResolveHost bool
|
||||
// DNSEndpoint 是 queryHttpDns 使用的 cdnws 入口,为空时用 DefaultCDNWSEndpoint。
|
||||
DNSEndpoint string
|
||||
Origin string
|
||||
UserAgent string
|
||||
Dialer *websocket.Dialer
|
||||
@@ -42,6 +48,12 @@ type WSSClient struct {
|
||||
sessions map[string]*WSSSession
|
||||
// dialing 记录正在建连的 uid,避免并发调用重复握手。
|
||||
dialing map[string]chan struct{}
|
||||
// exps 缓存 uid -> getConfig 版本串,供后续握手 baseinfo.exp 使用。
|
||||
exps map[string]string
|
||||
// dnsHosts 缓存 queryHttpDns 结果,按顺序轮询使用。
|
||||
dnsHosts []string
|
||||
dnsIndex int
|
||||
dnsExpireAt time.Time
|
||||
}
|
||||
|
||||
type WSSSession struct {
|
||||
@@ -63,6 +75,8 @@ type WSSSession struct {
|
||||
nextWUPRequestID int32
|
||||
launchGUID string
|
||||
launchIP string
|
||||
// configExps 是 getConfig 下发的版本 map,UpdateUserExpsReq 要回传它。
|
||||
configExps map[string]string
|
||||
}
|
||||
|
||||
type rpcResult struct {
|
||||
@@ -72,13 +86,14 @@ type rpcResult struct {
|
||||
|
||||
func NewWSSClient(logger func(string)) *WSSClient {
|
||||
return &WSSClient{
|
||||
Host: DefaultWSSHost,
|
||||
Origin: DefaultWSSOrigin,
|
||||
UserAgent: PCUserAgent,
|
||||
Logger: logger,
|
||||
CallTimeout: 10 * time.Second,
|
||||
sessions: map[string]*WSSSession{},
|
||||
dialing: map[string]chan struct{}{},
|
||||
AutoResolveHost: true,
|
||||
Origin: DefaultWSSOrigin,
|
||||
UserAgent: PCUserAgent,
|
||||
Logger: logger,
|
||||
CallTimeout: 10 * time.Second,
|
||||
sessions: map[string]*WSSSession{},
|
||||
dialing: map[string]chan struct{}{},
|
||||
exps: map[string]string{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,8 +122,111 @@ func (c *WSSClient) Close() error {
|
||||
}
|
||||
|
||||
func (c *WSSClient) PrewarmActivity(ctx context.Context, uid int64, cookie string, sid int32) error {
|
||||
_, err := c.session(ctx, uid, cookie)
|
||||
return err
|
||||
if _, err := c.session(ctx, uid, cookie); err != nil {
|
||||
return err
|
||||
}
|
||||
// pingSignal 只为确认业务通道可用,失败不影响抢购,仅记录日志。
|
||||
if _, err := c.PingSignal(ctx, uid, cookie); err != nil {
|
||||
c.logf("[WSS] pingSignal 失败但连接可用: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PingSignal 通过 WSS 通道调用 launch.pingSignal,用于预热后的延迟探测。
|
||||
func (c *WSSClient) PingSignal(ctx context.Context, uid int64, cookie string) (*PingSignalResp, error) {
|
||||
session, err := c.session(ctx, uid, cookie)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req := &PingSignalReq{Extra: map[string]string{}}
|
||||
var resp PingSignalResp
|
||||
start := time.Now()
|
||||
if _, err := session.callRPC(ctx, ServantLaunch, FuncPingSignal, req, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.logf("[WSS] pingSignal 往返=%s 服务端=%s", time.Since(start).Round(time.Millisecond), resp.ServerElapsed())
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
// resolveHost 返回本次建连使用的 host。
|
||||
//
|
||||
// 优先级:显式配置的 Host > queryHttpDns 结果(TTL 内缓存、多 host 轮询)> DefaultWSSHost。
|
||||
func (c *WSSClient) resolveHost(ctx context.Context, uid int64, cookie string) string {
|
||||
if host := strings.TrimSpace(c.Host); host != "" {
|
||||
return host
|
||||
}
|
||||
if !c.AutoResolveHost {
|
||||
return DefaultWSSHost
|
||||
}
|
||||
if host := c.nextCachedDNSHost(); host != "" {
|
||||
return host
|
||||
}
|
||||
resp, err := c.queryHttpDns(ctx, uid, cookie)
|
||||
if err != nil {
|
||||
c.logf("[WSS] queryHttpDns 失败,回退 %s: %v", DefaultWSSHost, err)
|
||||
return DefaultWSSHost
|
||||
}
|
||||
hosts := resp.WSSHosts(HttpDnsDomain)
|
||||
if len(hosts) == 0 {
|
||||
c.logf("[WSS] queryHttpDns 未返回 host,回退 %s", DefaultWSSHost)
|
||||
return DefaultWSSHost
|
||||
}
|
||||
c.storeDNSHosts(hosts, resp.TTL(HttpDnsDomain))
|
||||
return c.nextCachedDNSHost()
|
||||
}
|
||||
|
||||
func (c *WSSClient) queryHttpDns(ctx context.Context, uid int64, cookie string) (*QueryHttpDnsResp, error) {
|
||||
endpoint := strings.TrimSpace(c.DNSEndpoint)
|
||||
if endpoint == "" {
|
||||
endpoint = DefaultCDNWSEndpoint
|
||||
}
|
||||
client := &HTTPClient{
|
||||
Endpoint: endpoint,
|
||||
Client: &http.Client{Timeout: 5 * time.Second},
|
||||
Logger: c.Logger,
|
||||
}
|
||||
return client.QueryHttpDns(ctx, uid, cookie, []string{HttpDnsDomain})
|
||||
}
|
||||
|
||||
func (c *WSSClient) nextCachedDNSHost() string {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if len(c.dnsHosts) == 0 || time.Now().After(c.dnsExpireAt) {
|
||||
return ""
|
||||
}
|
||||
host := c.dnsHosts[c.dnsIndex%len(c.dnsHosts)]
|
||||
c.dnsIndex++
|
||||
return host
|
||||
}
|
||||
|
||||
func (c *WSSClient) storeDNSHosts(hosts []string, ttl time.Duration) {
|
||||
if ttl <= 0 {
|
||||
ttl = DefaultDNSTTL
|
||||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.dnsHosts = append([]string(nil), hosts...)
|
||||
c.dnsIndex = 0
|
||||
c.dnsExpireAt = time.Now().Add(ttl)
|
||||
c.logf("[WSS] queryHttpDns 缓存 %d 个 host(ttl=%s)", len(hosts), ttl)
|
||||
}
|
||||
|
||||
func (c *WSSClient) configExp(uid int64) string {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
return c.exps[strconv.FormatInt(uid, 10)]
|
||||
}
|
||||
|
||||
func (c *WSSClient) setConfigExp(uid int64, exp string) {
|
||||
if exp == "" {
|
||||
return
|
||||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if c.exps == nil {
|
||||
c.exps = map[string]string{}
|
||||
}
|
||||
c.exps[strconv.FormatInt(uid, 10)] = exp
|
||||
}
|
||||
|
||||
// session 返回 uid 对应的连接,必要时新建。
|
||||
@@ -162,7 +280,7 @@ func (c *WSSClient) dial(ctx context.Context, uid int64, cookie string) (*WSSSes
|
||||
}
|
||||
|
||||
func (c *WSSClient) dialSession(ctx context.Context, uid int64, cookie string) (*WSSSession, error) {
|
||||
host := c.Host
|
||||
host := c.resolveHost(ctx, uid, cookie)
|
||||
if host == "" {
|
||||
host = DefaultWSSHost
|
||||
}
|
||||
@@ -176,7 +294,7 @@ func (c *WSSClient) dialSession(ctx context.Context, uid int64, cookie string) (
|
||||
}
|
||||
|
||||
guid := CookieValue(cookie, "guid")
|
||||
baseinfo, err := GenerateWSSBaseinfo(uid, guid)
|
||||
baseinfo, err := GenerateWSSBaseinfo(uid, guid, c.configExp(uid))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -413,12 +531,29 @@ func (s *WSSSession) callWSLaunch(ctx context.Context) error {
|
||||
|
||||
func (s *WSSSession) callGetConfig(ctx context.Context) error {
|
||||
req := &getConfigReq{UID: s.uid, GUID: s.launchGUID, Cookie: s.cookie}
|
||||
_, err := s.callWUP(ctx, "mobileui", "getConfig", req)
|
||||
return err
|
||||
resp, err := s.callWUP(ctx, "mobileui", FuncGetConfig, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var cfg GetConfigResp
|
||||
ok, err := resp.ReadStruct("tRsp", &cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !ok {
|
||||
return errors.New("getConfig 响应缺少 tRsp")
|
||||
}
|
||||
s.configExps = cfg.Exps
|
||||
exp := cfg.Exp()
|
||||
s.client.setConfigExp(s.uid, exp)
|
||||
if exp != "" {
|
||||
s.client.logf("[WSS] getConfig 版本=%s 开关=%d 项", exp, len(cfg.Flags))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *WSSSession) sendUpdateUserExps(ctx context.Context) error {
|
||||
payload, err := encodeWSSStructCommand(WSSCmdUpdateUserExpsReq, &wsUpdateUserExpsReq{})
|
||||
payload, err := encodeWSSStructCommand(WSSCmdUpdateUserExpsReq, &wsUpdateUserExpsReq{Exps: s.configExps})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -656,10 +791,14 @@ func (r *wsLaunchReq) WriteTo(w *taf.Writer) error {
|
||||
return w.WriteStructEnd()
|
||||
}
|
||||
|
||||
type wsUpdateUserExpsReq struct{}
|
||||
// wsUpdateUserExpsReq 对应 EWSCmdC2S_UpdateUserExpsReq(23):
|
||||
// 回传 getConfig 下发的平台版本 map(实测 {"58664":"107263"})。
|
||||
type wsUpdateUserExpsReq struct {
|
||||
Exps map[string]string
|
||||
}
|
||||
|
||||
func (r *wsUpdateUserExpsReq) WriteTo(w *taf.Writer) error {
|
||||
return w.WriteMapStringString(0, nil)
|
||||
return w.WriteMapStringString(0, r.Exps)
|
||||
}
|
||||
|
||||
type wsVerifyCookieReq struct {
|
||||
|
||||
Reference in New Issue
Block a user