修复 wss 部分错误
This commit is contained in:
+1
-1
@@ -114,7 +114,7 @@ func (a *App) Shutdown(ctx context.Context) {
|
||||
|
||||
func (a *App) DefaultConfig() Config {
|
||||
return Config{
|
||||
Transport: "wss",
|
||||
Transport: "http",
|
||||
WSSHost: huya.DefaultWSSHost,
|
||||
SID: 2203,
|
||||
PID: 12889,
|
||||
|
||||
+185
-189
@@ -48,7 +48,8 @@ type WSSSession struct {
|
||||
|
||||
writeMu sync.Mutex
|
||||
pendingMu sync.Mutex
|
||||
pending []chan rpcResult
|
||||
pending map[int32]chan rpcResult
|
||||
requestMu sync.Mutex
|
||||
closed chan struct{}
|
||||
closeOnce sync.Once
|
||||
closeErr error
|
||||
@@ -59,7 +60,7 @@ type WSSSession struct {
|
||||
}
|
||||
|
||||
type rpcResult struct {
|
||||
body []byte
|
||||
resp *wup.Response
|
||||
err error
|
||||
}
|
||||
|
||||
@@ -173,7 +174,8 @@ func (c *WSSClient) dialSession(ctx context.Context, uid int64, cookie string) (
|
||||
guid: guid,
|
||||
conn: conn,
|
||||
closed: make(chan struct{}),
|
||||
nextWUPRequestID: 8,
|
||||
pending: map[int32]chan rpcResult{},
|
||||
nextWUPRequestID: 1,
|
||||
}
|
||||
go session.readLoop()
|
||||
go session.heartbeatLoop()
|
||||
@@ -214,18 +216,25 @@ func (s *WSSSession) readLoop() {
|
||||
if messageType != websocket.BinaryMessage {
|
||||
continue
|
||||
}
|
||||
msg, err := DecodeWSSMessage(raw)
|
||||
cmd, err := DecodeWSSCommand(raw)
|
||||
if err != nil {
|
||||
s.client.logf("[WSS] 解码消息失败: %v", err)
|
||||
continue
|
||||
}
|
||||
switch msg.Command {
|
||||
case WSSCommandRPCResponse:
|
||||
s.resolveNext(rpcResult{body: msg.Body})
|
||||
case WSSCommandHeartbeatRecv:
|
||||
switch cmd.CmdType {
|
||||
case WSSCmdWupRsp:
|
||||
var resp wup.Response
|
||||
if err := resp.Decode(cmd.Data); err != nil {
|
||||
s.client.logf("[WSS] 解码 WUP 响应失败: %v", err)
|
||||
continue
|
||||
}
|
||||
s.resolveRequest(resp.RequestID, rpcResult{resp: &resp})
|
||||
case WSSCmdHeartBeatAck, WSSCmdHeartBeatRsp:
|
||||
// 心跳响应无需处理。
|
||||
case WSSCmdVerifyCookieRsp, WSSCmdUpdateUserInfoRsp, WSSCmdUpdateUserExpsRsp:
|
||||
s.client.logf("[WSS] 收到控制响应 cmd=%d data=%dB", cmd.CmdType, len(cmd.Data))
|
||||
default:
|
||||
s.client.logf("[WSS] 收到 cmd=0x%02x seq=0x%08x body=%dB", msg.Command, msg.Sequence, len(msg.Body))
|
||||
s.client.logf("[WSS] 收到 cmd=%d data=%dB requestID=%d", cmd.CmdType, len(cmd.Data), cmd.RequestID)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -257,70 +266,40 @@ func (s *WSSSession) closeWithError(err error) {
|
||||
func (s *WSSSession) failPending(err error) {
|
||||
s.pendingMu.Lock()
|
||||
pending := s.pending
|
||||
s.pending = nil
|
||||
s.pending = map[int32]chan rpcResult{}
|
||||
s.pendingMu.Unlock()
|
||||
for _, ch := range pending {
|
||||
ch <- rpcResult{err: err}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *WSSSession) resolveNext(result rpcResult) {
|
||||
func (s *WSSSession) resolveRequest(requestID int32, result rpcResult) {
|
||||
s.pendingMu.Lock()
|
||||
if len(s.pending) == 0 {
|
||||
ch := s.pending[requestID]
|
||||
if ch == nil {
|
||||
s.pendingMu.Unlock()
|
||||
s.client.logf("[WSS] 未匹配响应 requestID=%d", requestID)
|
||||
return
|
||||
}
|
||||
ch := s.pending[0]
|
||||
copy(s.pending, s.pending[1:])
|
||||
s.pending = s.pending[:len(s.pending)-1]
|
||||
delete(s.pending, requestID)
|
||||
s.pendingMu.Unlock()
|
||||
ch <- result
|
||||
}
|
||||
|
||||
func (s *WSSSession) removePending(target chan rpcResult) {
|
||||
func (s *WSSSession) removePending(requestID int32) {
|
||||
s.pendingMu.Lock()
|
||||
defer s.pendingMu.Unlock()
|
||||
for i, ch := range s.pending {
|
||||
if ch == target {
|
||||
copy(s.pending[i:], s.pending[i+1:])
|
||||
s.pending = s.pending[:len(s.pending)-1]
|
||||
return
|
||||
}
|
||||
}
|
||||
delete(s.pending, requestID)
|
||||
}
|
||||
|
||||
func (s *WSSSession) writeMessage(ctx context.Context, msg WSSMessage) error {
|
||||
func (s *WSSSession) writePayload(ctx context.Context, payload []byte) error {
|
||||
s.writeMu.Lock()
|
||||
defer s.writeMu.Unlock()
|
||||
if deadline, ok := ctx.Deadline(); ok {
|
||||
_ = s.conn.SetWriteDeadline(deadline)
|
||||
defer s.conn.SetWriteDeadline(time.Time{})
|
||||
}
|
||||
return s.conn.WriteMessage(websocket.BinaryMessage, msg.Encode())
|
||||
}
|
||||
|
||||
func (s *WSSSession) callRaw(ctx context.Context, msg WSSMessage) ([]byte, error) {
|
||||
ch := make(chan rpcResult, 1)
|
||||
s.pendingMu.Lock()
|
||||
s.pending = append(s.pending, ch)
|
||||
s.pendingMu.Unlock()
|
||||
|
||||
if err := s.writeMessage(ctx, msg); err != nil {
|
||||
s.removePending(ch)
|
||||
return nil, err
|
||||
}
|
||||
select {
|
||||
case result := <-ch:
|
||||
return result.body, result.err
|
||||
case <-ctx.Done():
|
||||
s.removePending(ch)
|
||||
return nil, ctx.Err()
|
||||
case <-s.closed:
|
||||
if s.closeErr != nil {
|
||||
return nil, s.closeErr
|
||||
}
|
||||
return nil, errors.New("wss closed")
|
||||
}
|
||||
return s.conn.WriteMessage(websocket.BinaryMessage, payload)
|
||||
}
|
||||
|
||||
func (s *WSSSession) initialize(ctx context.Context) error {
|
||||
@@ -330,15 +309,18 @@ func (s *WSSSession) initialize(ctx context.Context) error {
|
||||
if err := s.callWSLaunch(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.callGetConfig(ctx); err != nil {
|
||||
s.client.logf("[WSS] getConfig 失败但继续: %v", err)
|
||||
}
|
||||
if s.launchGUID != "" {
|
||||
if err := s.sendRegister(ctx, s.launchGUID); err != nil {
|
||||
if err := s.sendUpdateUserInfo(ctx, s.launchGUID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return s.sendConfirm(ctx)
|
||||
if err := s.sendVerifyCookie(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.callGetConfig(ctx); err != nil {
|
||||
s.client.logf("[WSS] getConfig 失败但继续: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *WSSSession) withTimeout(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
@@ -349,48 +331,19 @@ func (s *WSSSession) withTimeout(ctx context.Context) (context.Context, context.
|
||||
}
|
||||
|
||||
func (s *WSSSession) sendHeartbeat(ctx context.Context) error {
|
||||
w := taf.NewWriter()
|
||||
if err := w.WriteHead(0, taf.TypeList); err != nil {
|
||||
payload, err := EncodeWSSCommand(WSSCmdHeartBeatReq, nil, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteInt32(0, 1); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteString(0, "live:0"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteString(1, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
body := append(append([]byte{}, w.Bytes()...), wssTailBytes...)
|
||||
return s.writeMessage(ctx, WSSMessage{Command: WSSCommandHeartbeatSend, Sequence: wssSeqHeartbeat, Body: body})
|
||||
return s.writePayload(ctx, payload)
|
||||
}
|
||||
|
||||
func (s *WSSSession) callWSLaunch(ctx context.Context) error {
|
||||
req := &wsLaunchReq{}
|
||||
wupReq := wup.NewRequest("launch", "wsLaunch", -1)
|
||||
wupReq.Timeout = 0
|
||||
if err := wupReq.WriteStruct("tReq", req); err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := wupReq.Encode()
|
||||
req := &wsLaunchReq{UID: s.uid, GUID: s.guid, UA: WSSInitUA}
|
||||
resp, err := s.callWUP(ctx, "launch", "wsLaunch", req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
body, err := s.callRaw(ctx, WSSMessage{
|
||||
Command: WSSCommandRPCRequest,
|
||||
Sequence: wssSeqWSLaunch,
|
||||
Body: encodeWSSRPCBody(data),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var resp wup.Response
|
||||
if err := resp.Decode(extractWUPFromWSSBody(body)); err != nil {
|
||||
return err
|
||||
}
|
||||
var launch wsLaunchResp
|
||||
ok, err := resp.ReadStruct("tRsp", &launch)
|
||||
if err != nil {
|
||||
@@ -406,82 +359,51 @@ func (s *WSSSession) callWSLaunch(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (s *WSSSession) callGetConfig(ctx context.Context) error {
|
||||
req := &getConfigReq{}
|
||||
wupReq := wup.NewRequest("mobileui", "getConfig", 7)
|
||||
wupReq.Timeout = 0
|
||||
if err := wupReq.WriteStruct("tReq", req); err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := wupReq.Encode()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
_, err = s.callRaw(ctx, WSSMessage{
|
||||
Command: WSSCommandRPCRequest,
|
||||
Sequence: wssSeqGetConfig,
|
||||
Body: encodeWSSRPCBody(data),
|
||||
})
|
||||
req := &getConfigReq{UID: s.uid, GUID: s.launchGUID, Cookie: s.cookie}
|
||||
_, err := s.callWUP(ctx, "mobileui", "getConfig", req)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *WSSSession) sendRegister(ctx context.Context, launchGUID string) error {
|
||||
w := taf.NewWriter()
|
||||
if err := w.WriteString(0, "HUYA&ZH&2052"); err != nil {
|
||||
func (s *WSSSession) sendUpdateUserInfo(ctx context.Context, launchGUID string) error {
|
||||
req := &wsUpdateUserInfoReq{AppSrc: "HUYA&ZH&2052", GUID: launchGUID}
|
||||
payload, err := encodeWSSStructCommand(WSSCmdUpdateUserInfoReq, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteString(1, launchGUID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteInt32(2, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteInt32(3, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteStructBegin(6); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteMapStringString(0, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteMapStringString(1, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteInt32(3, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteStructEnd(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteMapStringString(7, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteInt32(8, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
body := append(append([]byte{}, w.Bytes()...), wssTailBytes...)
|
||||
return s.writeMessage(ctx, WSSMessage{Command: WSSCommandRegister, Sequence: wssSeqRegister, Body: body})
|
||||
return s.writePayload(ctx, payload)
|
||||
}
|
||||
|
||||
func (s *WSSSession) sendConfirm(ctx context.Context) error {
|
||||
w := taf.NewWriter()
|
||||
if err := w.WriteMapStringString(0, nil); err != nil {
|
||||
func (s *WSSSession) sendVerifyCookie(ctx context.Context) error {
|
||||
guid := s.launchGUID
|
||||
if guid == "" {
|
||||
guid = s.guid
|
||||
}
|
||||
req := &wsVerifyCookieReq{
|
||||
UID: s.uid,
|
||||
UA: WSSInitUA,
|
||||
Cookie: NormalizeWSSCookie(s.cookie),
|
||||
GUID: guid,
|
||||
AutoRegisterUID: 1,
|
||||
AppSrc: "HUYA&ZH&2052",
|
||||
}
|
||||
payload, err := encodeWSSStructCommand(WSSCmdVerifyCookieReq, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
body := append(append([]byte{}, w.Bytes()...), wssTailBytes...)
|
||||
return s.writeMessage(ctx, WSSMessage{Command: WSSCommandConfirmSend, Sequence: wssSeqConfirm, Body: body})
|
||||
return s.writePayload(ctx, payload)
|
||||
}
|
||||
|
||||
func (s *WSSSession) nextRequestID() int32 {
|
||||
s.requestMu.Lock()
|
||||
defer s.requestMu.Unlock()
|
||||
id := s.nextWUPRequestID
|
||||
s.nextWUPRequestID++
|
||||
return id
|
||||
}
|
||||
|
||||
func (s *WSSSession) callRPC(ctx context.Context, service, method string, reqStruct taf.Marshaler, respStruct taf.Unmarshaler) (*wup.Response, error) {
|
||||
wupReq := wup.NewRequest(service, method, s.nextRequestID())
|
||||
func (s *WSSSession) callWUP(ctx context.Context, service, method string, reqStruct taf.Marshaler) (*wup.Response, error) {
|
||||
requestID := s.nextRequestID()
|
||||
wupReq := wup.NewRequest(service, method, requestID)
|
||||
wupReq.Timeout = 0
|
||||
if err := wupReq.WriteStruct("tReq", reqStruct); err != nil {
|
||||
return nil, err
|
||||
@@ -492,17 +414,37 @@ func (s *WSSSession) callRPC(ctx context.Context, service, method string, reqStr
|
||||
}
|
||||
ctx, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
s.client.logf("[WSS] RPC %s.%s body=%dB", service, method, len(data))
|
||||
body, err := s.callRaw(ctx, WSSMessage{
|
||||
Command: WSSCommandRPCRequest,
|
||||
Sequence: wssSeqBusiness,
|
||||
Body: encodeWSSRPCBody(data),
|
||||
})
|
||||
payload, err := EncodeWSSCommand(WSSCmdWupReq, data, NewWSSCommandTrace())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var resp wup.Response
|
||||
if err := resp.Decode(extractWUPFromWSSBody(body)); err != nil {
|
||||
ch := make(chan rpcResult, 1)
|
||||
s.pendingMu.Lock()
|
||||
s.pending[requestID] = ch
|
||||
s.pendingMu.Unlock()
|
||||
s.client.logf("[WSS] WUP %s.%s requestID=%d body=%dB", service, method, requestID, len(data))
|
||||
if err := s.writePayload(ctx, payload); err != nil {
|
||||
s.removePending(requestID)
|
||||
return nil, err
|
||||
}
|
||||
select {
|
||||
case result := <-ch:
|
||||
return result.resp, result.err
|
||||
case <-ctx.Done():
|
||||
s.removePending(requestID)
|
||||
return nil, ctx.Err()
|
||||
case <-s.closed:
|
||||
s.removePending(requestID)
|
||||
if s.closeErr != nil {
|
||||
return nil, s.closeErr
|
||||
}
|
||||
return nil, errors.New("wss closed")
|
||||
}
|
||||
}
|
||||
|
||||
func (s *WSSSession) callRPC(ctx context.Context, service, method string, reqStruct taf.Marshaler, respStruct taf.Unmarshaler) (*wup.Response, error) {
|
||||
resp, err := s.callWUP(ctx, service, method, reqStruct)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if respStruct != nil {
|
||||
@@ -514,7 +456,7 @@ func (s *WSSSession) callRPC(ctx context.Context, service, method string, reqStr
|
||||
return nil, fmt.Errorf("%s.%s 响应缺少 tRsp/tResp", service, method)
|
||||
}
|
||||
}
|
||||
return &resp, nil
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *WSSClient) buildUser(uid int64, cookie string) ActivityUserID {
|
||||
@@ -588,16 +530,24 @@ func NormalizeWSSCookie(cookie string) string {
|
||||
return strings.Join(out, "; ")
|
||||
}
|
||||
|
||||
type wsLaunchReq struct{}
|
||||
type wsLaunchReq struct {
|
||||
UID int64
|
||||
GUID string
|
||||
UA string
|
||||
}
|
||||
|
||||
func (r *wsLaunchReq) WriteTo(w *taf.Writer) error {
|
||||
if err := w.WriteInt64(0, 0); err != nil {
|
||||
if err := w.WriteInt64(0, r.UID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteString(1, ""); err != nil {
|
||||
if err := w.WriteString(1, r.GUID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteString(2, WSSInitUA); err != nil {
|
||||
ua := r.UA
|
||||
if ua == "" {
|
||||
ua = WSSInitUA
|
||||
}
|
||||
if err := w.WriteString(2, ua); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteString(3, "HUYA&ZH&2052"); err != nil {
|
||||
@@ -614,6 +564,73 @@ func (r *wsLaunchReq) WriteTo(w *taf.Writer) error {
|
||||
return w.WriteStructEnd()
|
||||
}
|
||||
|
||||
type wsUpdateUserInfoReq struct {
|
||||
AppSrc string
|
||||
GUID string
|
||||
}
|
||||
|
||||
func (r *wsUpdateUserInfoReq) WriteTo(w *taf.Writer) error {
|
||||
if err := w.WriteString(0, r.AppSrc); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteString(1, r.GUID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteInt32(2, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteInt32(3, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteStructBegin(6); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteMapStringString(0, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteMapStringString(1, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteInt32(2, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteStructEnd(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteMapStringString(7, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
return w.WriteInt32(8, 0)
|
||||
}
|
||||
|
||||
type wsVerifyCookieReq struct {
|
||||
UID int64
|
||||
UA string
|
||||
Cookie string
|
||||
GUID string
|
||||
AutoRegisterUID int32
|
||||
AppSrc string
|
||||
}
|
||||
|
||||
func (r *wsVerifyCookieReq) WriteTo(w *taf.Writer) error {
|
||||
if err := w.WriteInt64(0, r.UID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteString(1, r.UA); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteString(2, r.Cookie); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteString(3, r.GUID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteInt32(4, int64(r.AutoRegisterUID)); err != nil {
|
||||
return err
|
||||
}
|
||||
return w.WriteString(5, r.AppSrc)
|
||||
}
|
||||
|
||||
type wsLaunchResp struct {
|
||||
GUID string
|
||||
ClientIP string
|
||||
@@ -628,37 +645,16 @@ func (r *wsLaunchResp) ReadFrom(rd *taf.Reader) error {
|
||||
return err
|
||||
}
|
||||
|
||||
type getConfigReq struct{}
|
||||
type getConfigReq struct {
|
||||
UID int64
|
||||
GUID string
|
||||
Cookie string
|
||||
}
|
||||
|
||||
func (r *getConfigReq) WriteTo(w *taf.Writer) error {
|
||||
if err := w.WriteStructBegin(0); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteInt64(0, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteString(1, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteString(2, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteString(3, WSSInitUA); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteString(4, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteInt32(5, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteString(6, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteString(7, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteStructEnd(); err != nil {
|
||||
user := NewActivityUserID(r.UID, NormalizeWSSCookie(r.Cookie), WSSInitUA)
|
||||
user.GUID = r.GUID
|
||||
if err := w.WriteStruct(0, &user); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteMapStringString(1, nil); err != nil {
|
||||
|
||||
+113
-86
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user