修复 wss 部分错误
This commit is contained in:
+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 {
|
||||
|
||||
Reference in New Issue
Block a user