继续补全 wss
This commit is contained in:
@@ -99,3 +99,42 @@ func GenerateHTTPBaseinfo(uid int64, guid, cookie string) (string, error) {
|
||||
}
|
||||
return url.QueryEscape(base64.StdEncoding.EncodeToString(w.Bytes())), nil
|
||||
}
|
||||
|
||||
// GenerateWSSBaseinfo 生成浏览器 WSS 握手里的 baseinfo。
|
||||
//
|
||||
// 手动抓包里 WSS 握手只带 uid/guid/ua/appSrc 等连接信息,cookie
|
||||
// 通过后续 VerifyCookie 命令发送。
|
||||
func GenerateWSSBaseinfo(uid int64, guid string) (string, error) {
|
||||
w := taf.NewWriter()
|
||||
if err := w.WriteInt64(0, uid); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := w.WriteString(1, guid); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := w.WriteString(2, HTTPHuyaUA); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := w.WriteString(3, "HUYA&ZH&2052"); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := w.WriteString(4, ""); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := w.WriteString(5, ""); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := w.WriteInt32(6, 0); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := w.WriteString(7, ""); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := w.WriteString(8, ""); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := w.WriteString(9, ""); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return url.QueryEscape(base64.StdEncoding.EncodeToString(w.Bytes())), nil
|
||||
}
|
||||
|
||||
+64
-13
@@ -22,8 +22,9 @@ const (
|
||||
DefaultWSSOrigin = "https://zt.huya.com"
|
||||
|
||||
WSSCookieUA = "webh5&0.0.1&websocket&&diypc_52775"
|
||||
WSSBizUA = "web&1.0.0&huya"
|
||||
WSSInitUA = "webh5&0.0.1&websocket&&h5_/index.html"
|
||||
WSSBizUA = WSSCookieUA
|
||||
WSSInitUA = WSSCookieUA
|
||||
WSSGroupID = "live:0"
|
||||
)
|
||||
|
||||
type WSSClient struct {
|
||||
@@ -136,7 +137,7 @@ func (c *WSSClient) dialSession(ctx context.Context, uid int64, cookie string) (
|
||||
}
|
||||
|
||||
guid := CookieValue(cookie, "guid")
|
||||
baseinfo, err := GenerateHTTPBaseinfo(uid, guid, NormalizeWSSCookie(cookie))
|
||||
baseinfo, err := GenerateWSSBaseinfo(uid, guid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -231,7 +232,7 @@ func (s *WSSSession) readLoop() {
|
||||
s.resolveRequest(resp.RequestID, rpcResult{resp: &resp})
|
||||
case WSSCmdHeartBeatAck, WSSCmdHeartBeatRsp:
|
||||
// 心跳响应无需处理。
|
||||
case WSSCmdVerifyCookieRsp, WSSCmdUpdateUserInfoRsp, WSSCmdUpdateUserExpsRsp:
|
||||
case WSSCmdRegisterGroupRsp, WSSCmdVerifyCookieRsp, WSSCmdUpdateUserInfoRsp, WSSCmdUpdateUserExpsRsp:
|
||||
s.client.logf("[WSS] 收到控制响应 cmd=%d data=%dB", cmd.CmdType, len(cmd.Data))
|
||||
default:
|
||||
s.client.logf("[WSS] 收到 cmd=%d data=%dB requestID=%d", cmd.CmdType, len(cmd.Data), cmd.RequestID)
|
||||
@@ -303,23 +304,27 @@ func (s *WSSSession) writePayload(ctx context.Context, payload []byte) error {
|
||||
}
|
||||
|
||||
func (s *WSSSession) initialize(ctx context.Context) error {
|
||||
if err := s.sendHeartbeat(ctx); err != nil {
|
||||
if err := s.sendRegisterGroup(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.callWSLaunch(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if s.launchGUID != "" {
|
||||
if err := s.sendUpdateUserInfo(ctx, s.launchGUID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := s.sendVerifyCookie(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.callGetConfig(ctx); err != nil {
|
||||
s.client.logf("[WSS] getConfig 失败但继续: %v", err)
|
||||
}
|
||||
if err := s.sendUpdateUserExps(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.sendRegisterGroup(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.sendHeartbeat(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -338,9 +343,18 @@ func (s *WSSSession) sendHeartbeat(ctx context.Context) error {
|
||||
return s.writePayload(ctx, payload)
|
||||
}
|
||||
|
||||
func (s *WSSSession) sendRegisterGroup(ctx context.Context) error {
|
||||
req := &wsRegisterGroupReq{GroupIDs: []string{WSSGroupID}}
|
||||
payload, err := encodeWSSStructCommand(WSSCmdRegisterGroupReq, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.writePayload(ctx, payload)
|
||||
}
|
||||
|
||||
func (s *WSSSession) callWSLaunch(ctx context.Context) error {
|
||||
req := &wsLaunchReq{UID: s.uid, GUID: s.guid, UA: WSSInitUA}
|
||||
resp, err := s.callWUP(ctx, "launch", "wsLaunch", req)
|
||||
resp, err := s.callWUPWithRequestID(ctx, "launch", "wsLaunch", req, -1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -373,6 +387,14 @@ func (s *WSSSession) sendUpdateUserInfo(ctx context.Context, launchGUID string)
|
||||
return s.writePayload(ctx, payload)
|
||||
}
|
||||
|
||||
func (s *WSSSession) sendUpdateUserExps(ctx context.Context) error {
|
||||
payload, err := encodeWSSStructCommand(WSSCmdUpdateUserExpsReq, &wsUpdateUserExpsReq{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.writePayload(ctx, payload)
|
||||
}
|
||||
|
||||
func (s *WSSSession) sendVerifyCookie(ctx context.Context) error {
|
||||
guid := s.launchGUID
|
||||
if guid == "" {
|
||||
@@ -402,7 +424,10 @@ func (s *WSSSession) nextRequestID() int32 {
|
||||
}
|
||||
|
||||
func (s *WSSSession) callWUP(ctx context.Context, service, method string, reqStruct taf.Marshaler) (*wup.Response, error) {
|
||||
requestID := s.nextRequestID()
|
||||
return s.callWUPWithRequestID(ctx, service, method, reqStruct, s.nextRequestID())
|
||||
}
|
||||
|
||||
func (s *WSSSession) callWUPWithRequestID(ctx context.Context, service, method string, reqStruct taf.Marshaler, requestID int32) (*wup.Response, error) {
|
||||
wupReq := wup.NewRequest(service, method, requestID)
|
||||
wupReq.Timeout = 0
|
||||
if err := wupReq.WriteStruct("tReq", reqStruct); err != nil {
|
||||
@@ -460,7 +485,7 @@ func (s *WSSSession) callRPC(ctx context.Context, service, method string, reqStr
|
||||
}
|
||||
|
||||
func (c *WSSClient) buildUser(uid int64, cookie string) ActivityUserID {
|
||||
user := NewActivityUserID(uid, NormalizeWSSCookie(cookie), WSSBizUA)
|
||||
user := NewActivityUserID(uid, NormalizeCookie(cookie), WSSBizUA)
|
||||
user.GUID = ""
|
||||
user.Token = ""
|
||||
user.DeviceInfo = ""
|
||||
@@ -530,6 +555,26 @@ func NormalizeWSSCookie(cookie string) string {
|
||||
return strings.Join(out, "; ")
|
||||
}
|
||||
|
||||
type wsRegisterGroupReq struct {
|
||||
GroupIDs []string
|
||||
Token string
|
||||
}
|
||||
|
||||
func (r *wsRegisterGroupReq) WriteTo(w *taf.Writer) error {
|
||||
if err := w.WriteHead(0, taf.TypeList); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteInt32(0, int64(len(r.GroupIDs))); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, groupID := range r.GroupIDs {
|
||||
if err := w.WriteString(0, groupID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return w.WriteString(1, r.Token)
|
||||
}
|
||||
|
||||
type wsLaunchReq struct {
|
||||
UID int64
|
||||
GUID string
|
||||
@@ -603,6 +648,12 @@ func (r *wsUpdateUserInfoReq) WriteTo(w *taf.Writer) error {
|
||||
return w.WriteInt32(8, 0)
|
||||
}
|
||||
|
||||
type wsUpdateUserExpsReq struct{}
|
||||
|
||||
func (r *wsUpdateUserExpsReq) WriteTo(w *taf.Writer) error {
|
||||
return w.WriteMapStringString(0, nil)
|
||||
}
|
||||
|
||||
type wsVerifyCookieReq struct {
|
||||
UID int64
|
||||
UA string
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package huya
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"huya-go/internal/taf"
|
||||
)
|
||||
|
||||
func TestWSRegisterGroupReqEncoding(t *testing.T) {
|
||||
w := taf.NewWriter()
|
||||
req := &wsRegisterGroupReq{GroupIDs: []string{WSSGroupID}}
|
||||
if err := req.WriteTo(w); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := hex.EncodeToString(w.Bytes())
|
||||
want := "09000106066c6976653a301600"
|
||||
if got != want {
|
||||
t.Fatalf("register group bytes = %s, want %s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWSUpdateUserExpsReqEncoding(t *testing.T) {
|
||||
w := taf.NewWriter()
|
||||
if err := (&wsUpdateUserExpsReq{}).WriteTo(w); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := hex.EncodeToString(w.Bytes())
|
||||
want := "080c"
|
||||
if got != want {
|
||||
t.Fatalf("update user exps bytes = %s, want %s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateWSSBaseinfoMatchesBrowserShape(t *testing.T) {
|
||||
encoded, err := GenerateWSSBaseinfo(123, "guid-value")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
unescaped, err := url.QueryUnescape(encoded)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
raw, err := base64.StdEncoding.DecodeString(unescaped)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r := taf.NewReader(raw)
|
||||
uid, err := r.ReadInt64(0, true, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
guid, err := r.ReadString(1, true, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ua, err := r.ReadString(2, true, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
appSrc, err := r.ReadString(3, true, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cookie, err := r.ReadString(8, false, "not-empty")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
traceID, err := r.ReadString(9, false, "not-empty")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if uid != 123 || guid != "guid-value" || ua != WSSCookieUA || appSrc != "HUYA&ZH&2052" {
|
||||
t.Fatalf("unexpected baseinfo: uid=%d guid=%q ua=%q appSrc=%q", uid, guid, ua, appSrc)
|
||||
}
|
||||
if cookie != "" || traceID != "" {
|
||||
t.Fatalf("wss baseinfo should not carry cookie/trace: cookie=%q trace=%q", cookie, traceID)
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,14 @@ const (
|
||||
WSSCmdDeregisterRsp int32 = 9
|
||||
WSSCmdVerifyCookieReq int32 = 10
|
||||
WSSCmdVerifyCookieRsp int32 = 11
|
||||
WSSCmdVerifyHuyaTokenReq int32 = 12
|
||||
WSSCmdVerifyHuyaTokenRsp int32 = 13
|
||||
WSSCmdUnverifyReq int32 = 14
|
||||
WSSCmdUnverifyRsp int32 = 15
|
||||
WSSCmdRegisterGroupReq int32 = 16
|
||||
WSSCmdRegisterGroupRsp int32 = 17
|
||||
WSSCmdUnregisterGroupReq int32 = 18
|
||||
WSSCmdUnregisterGroupRsp int32 = 19
|
||||
WSSCmdHeartBeatReq int32 = 20
|
||||
WSSCmdHeartBeatRsp int32 = 21
|
||||
WSSCmdMsgPushReqV2 int32 = 22
|
||||
|
||||
Reference in New Issue
Block a user