修复 wss 部分错误

This commit is contained in:
yml2213
2026-07-06 18:30:34 +08:00
parent 4235cce4f5
commit 1d15fadfd9
7 changed files with 381 additions and 338 deletions
+113 -86
View File
@@ -1,113 +1,140 @@
package huya
import (
"encoding/binary"
"fmt"
"math/rand"
"huya-go/internal/taf"
)
const (
WSSCommandRPCRequest byte = 0x03
WSSCommandRPCResponse byte = 0x04
WSSCommandAuth byte = 0x0a
WSSCommandHeartbeatSend byte = 0x10
WSSCommandHeartbeatRecv byte = 0x11
WSSCommandConfirmSend byte = 0x17
WSSCommandRegister byte = 0x21
wssSeqHeartbeat uint32 = 0x1d00000d
wssSeqWSLaunch uint32 = 0x1d000078
wssSeqGetConfig uint32 = 0x1d000076
wssSeqRegister uint32 = 0x1d00003c
wssSeqConfirm uint32 = 0x1d000002
wssSeqBusiness uint32 = 0x1d000106
WSSCmdNull int32 = 0
WSSCmdRegisterReq int32 = 1
WSSCmdRegisterRsp int32 = 2
WSSCmdWupReq int32 = 3
WSSCmdWupRsp int32 = 4
WSSCmdHeartBeat int32 = 5
WSSCmdHeartBeatAck int32 = 6
WSSCmdMsgPushReq int32 = 7
WSSCmdDeregisterReq int32 = 8
WSSCmdDeregisterRsp int32 = 9
WSSCmdVerifyCookieReq int32 = 10
WSSCmdVerifyCookieRsp int32 = 11
WSSCmdHeartBeatReq int32 = 20
WSSCmdHeartBeatRsp int32 = 21
WSSCmdMsgPushReqV2 int32 = 22
WSSCmdUpdateUserExpsReq int32 = 23
WSSCmdUpdateUserExpsRsp int32 = 24
WSSCmdUpdateUserInfoReq int32 = 33
WSSCmdUpdateUserInfoRsp int32 = 34
WSSCmdMsgAckReq int32 = 35
WSSCmdMsgAckRsp int32 = 36
WSSCmdS2CRpcReq int32 = 40
WSSCmdC2SRpcRsp int32 = 41
WSSCmdS2CRpcRspRsp int32 = 42
WSSCmdSignalUpgradeReq int32 = 105
WSSCmdSignalUpgradeRsp int32 = 106
defaultWSSTraceEnableValue = 3
)
var (
wssTailBytes = []byte{0x2c, 0x36, 0x00, 0x4c, 0x5c, 0x66, 0x00}
wssBigTailSuffix = []byte{0x4c, 0x5c, 0x66, 0x00}
)
// WSSMessage 是虎牙 WebSocketCommand 的 Go 表达。
// WSSCommand 是浏览器 JS 里的 HUYA.WebSocketCommand。
//
// 外层格式来自浏览器/参考项目:
//
// 1B version(0) + 1B command + 4B sequence + body
type WSSMessage struct {
Command byte
Sequence uint32
Body []byte
// 注意:这不是旧版的 6 字节自定义头。浏览器实际把这个 TAF/JCE
// 结构直接作为 WebSocket binary payload 发送。
type WSSCommand struct {
CmdType int32
Data []byte
RequestID int64
TraceID string
EncryptType int32
Time int64
MD5 string
}
func (m WSSMessage) Encode() []byte {
out := make([]byte, 6+len(m.Body))
out[0] = 0
out[1] = m.Command
binary.BigEndian.PutUint32(out[2:6], m.Sequence)
copy(out[6:], m.Body)
return out
func (c *WSSCommand) WriteTo(w *taf.Writer) error {
if err := w.WriteInt32(0, int64(c.CmdType)); err != nil {
return err
}
if err := w.WriteBytes(1, c.Data); err != nil {
return err
}
if err := w.WriteInt64(2, c.RequestID); err != nil {
return err
}
if err := w.WriteString(3, c.TraceID); err != nil {
return err
}
if err := w.WriteInt32(4, int64(c.EncryptType)); err != nil {
return err
}
if err := w.WriteInt64(5, c.Time); err != nil {
return err
}
return w.WriteString(6, c.MD5)
}
func DecodeWSSMessage(data []byte) (WSSMessage, error) {
if len(data) < 6 {
return WSSMessage{}, fmt.Errorf("wss: 消息太短: %d", len(data))
func (c *WSSCommand) ReadFrom(r *taf.Reader) error {
var err error
if c.CmdType, err = r.ReadInt32(0, false, c.CmdType); err != nil {
return err
}
return WSSMessage{
Command: data[1],
Sequence: binary.BigEndian.Uint32(data[2:6]),
Body: append([]byte(nil), data[6:]...),
}, nil
if c.Data, err = r.ReadBytes(1, false, c.Data); err != nil {
return err
}
if c.RequestID, err = r.ReadInt64(2, false, c.RequestID); err != nil {
return err
}
if c.TraceID, err = r.ReadString(3, false, c.TraceID); err != nil {
return err
}
if c.EncryptType, err = r.ReadInt32(4, false, c.EncryptType); err != nil {
return err
}
if c.Time, err = r.ReadInt64(5, false, c.Time); err != nil {
return err
}
c.MD5, err = r.ReadString(6, false, c.MD5)
return err
}
func encodeWSSRPCBody(wupData []byte) []byte {
if len(wupData) < 4 {
out := append([]byte{}, wupData...)
return append(out, wssTailBytes...)
func (c WSSCommand) Encode() ([]byte, error) {
w := taf.NewWriter()
if err := c.WriteTo(w); err != nil {
return nil, err
}
declaredLen := int(binary.BigEndian.Uint32(wupData[:4]))
wupBody := wupData
if declaredLen == len(wupData) {
wupBody = wupData[4:]
}
if len(wupBody) <= 0xff {
out := append([]byte{}, wupData...)
return append(out, wssTailBytes...)
}
trace := NewWSSBigTailTrace()
packetLen := len(wupData)
out := make([]byte, 0, 1+4+len(wupBody)+len(trace))
out = append(out, byte(packetLen&0xff))
var lenBuf [4]byte
binary.BigEndian.PutUint32(lenBuf[:], uint32(packetLen))
out = append(out, lenBuf[:]...)
out = append(out, wupBody...)
out = append(out, trace...)
return out
return append([]byte(nil), w.Bytes()...), nil
}
func extractWUPFromWSSBody(body []byte) []byte {
if len(body) < 5 {
return body
func DecodeWSSCommand(data []byte) (WSSCommand, error) {
var cmd WSSCommand
if len(data) == 0 {
return cmd, fmt.Errorf("wss: 空消息")
}
// 大包格式: [1B prefix][4B wup_len][裸 WUP body][tail]
wupLen := int(binary.BigEndian.Uint32(body[1:5]))
if 5+wupLen <= len(body) && len(body) >= 7 && body[5] == 0x10 && body[6] == 0x03 {
return body[5 : 5+wupLen]
if err := cmd.ReadFrom(taf.NewReader(data)); err != nil {
return cmd, err
}
// 小包格式: [4B total_len][WUP body][tail]
totalLen := int(binary.BigEndian.Uint32(body[:4]))
if 8 <= totalLen && totalLen <= len(body) && body[4] == 0x10 && body[5] == 0x03 {
return body[:totalLen]
}
if 5+wupLen <= len(body) {
return body[5 : 5+wupLen]
}
return body
return cmd, nil
}
func NewWSSBigTailTrace() []byte {
func EncodeWSSCommand(cmdType int32, data []byte, traceID string) ([]byte, error) {
return WSSCommand{
CmdType: cmdType,
Data: append([]byte(nil), data...),
TraceID: traceID,
}.Encode()
}
func encodeWSSStructCommand(cmdType int32, value taf.Marshaler) ([]byte, error) {
w := taf.NewWriter()
if value != nil {
if err := value.WriteTo(w); err != nil {
return nil, err
}
}
return EncodeWSSCommand(cmdType, w.Bytes(), "")
}
func NewWSSCommandTrace() string {
trace := lowerHex16(rand.Uint64())
out := []byte(",6%" + trace + ":" + trace + ":0:3")
return append(out, wssBigTailSuffix...)
return fmt.Sprintf("%s:%s:0:%d", trace, trace, defaultWSSTraceEnableValue)
}