diff --git a/binary/reader.go b/binary/reader.go index 2e5a6602..6a616930 100644 --- a/binary/reader.go +++ b/binary/reader.go @@ -34,9 +34,11 @@ func (r *Reader) ReadByte() byte { func (r *Reader) ReadBytes(len int) []byte { b := make([]byte, len) - _, err := r.buf.Read(b) - if err != nil { - panic(err) + if len > 0 { + _, err := r.buf.Read(b) + if err != nil { + panic(err) + } } return b } diff --git a/client/client.go b/client/client.go index 4bd9342f..974e1a9b 100644 --- a/client/client.go +++ b/client/client.go @@ -5,14 +5,6 @@ import ( "encoding/hex" "errors" "fmt" - "github.com/Mrs4s/MiraiGo/binary" - "github.com/Mrs4s/MiraiGo/client/pb/longmsg" - "github.com/Mrs4s/MiraiGo/client/pb/msg" - "github.com/Mrs4s/MiraiGo/client/pb/multimsg" - "github.com/Mrs4s/MiraiGo/message" - "github.com/Mrs4s/MiraiGo/protocol/packets" - "github.com/Mrs4s/MiraiGo/utils" - "github.com/golang/protobuf/proto" "io" "log" "math" @@ -21,6 +13,15 @@ import ( "sync" "sync/atomic" "time" + + "github.com/Mrs4s/MiraiGo/binary" + "github.com/Mrs4s/MiraiGo/client/pb/longmsg" + "github.com/Mrs4s/MiraiGo/client/pb/msg" + "github.com/Mrs4s/MiraiGo/client/pb/multimsg" + "github.com/Mrs4s/MiraiGo/message" + "github.com/Mrs4s/MiraiGo/protocol/packets" + "github.com/Mrs4s/MiraiGo/utils" + "github.com/golang/protobuf/proto" ) type QQClient struct { @@ -84,6 +85,9 @@ type loginSigInfo struct { d2Key []byte wtSessionTicketKey []byte deviceToken []byte + + psKeyMap map[string][]byte + pt4TokenMap map[string][]byte } func init() { @@ -656,6 +660,28 @@ func (g *GroupInfo) FindMember(uin int64) *GroupMemberInfo { return nil } +func (c *QQClient) getCookies() string { + return fmt.Sprintf("uin=o%d; skey=%s;", c.Uin, c.sigInfo.sKey) +} + +func (c *QQClient) getCookiesWithDomain(domain string) string { + cookie := c.getCookies() + + if psKey, ok := c.sigInfo.psKeyMap[domain]; ok { + return fmt.Sprintf("%s p_uin=o%d; p_skey=%s;", cookie, c.Uin, psKey) + } else { + return cookie + } +} + +func (c *QQClient) getCSRFToken() int { + accu := 5381 + for _, b := range c.sigInfo.sKey { + accu = accu + (accu << 5) + int(b) + } + return 2147483647 & accu +} + func (c *QQClient) editMemberCard(groupCode, memberUin int64, card string) { _, _ = c.sendAndWait(c.buildEditGroupTagPacket(groupCode, memberUin, card)) } diff --git a/client/tlv_decoders.go b/client/tlv_decoders.go index 264cc58b..1671af56 100644 --- a/client/tlv_decoders.go +++ b/client/tlv_decoders.go @@ -2,8 +2,9 @@ package client import ( "fmt" - "github.com/Mrs4s/MiraiGo/binary" "time" + + "github.com/Mrs4s/MiraiGo/binary" ) // --- tlv decoders for qq client --- @@ -52,6 +53,8 @@ func (c *QQClient) decodeT119(data []byte) { //noPicSig []byte //ctime = time.Now().Unix() //etime = ctime + 2160000 + psKeyMap map[string][]byte + pt4TokenMap map[string][]byte ) if _, ok := m[0x125]; ok { @@ -69,9 +72,9 @@ func (c *QQClient) decodeT119(data []byte) { if _, ok := m[0x200]; ok { //pf, pfkey = readT200(t200) } - if _, ok := m[0x512]; ok { - - } // ζš‚δΈε€„η†, Http api cookie + if t512, ok := m[0x512]; ok { + psKeyMap, pt4TokenMap = readT512(t512) + } if _, ok := m[0x531]; ok { //a1, noPicSig = readT531(t531) } @@ -87,6 +90,9 @@ func (c *QQClient) decodeT119(data []byte) { d2Key: m[0x305], wtSessionTicketKey: m[0x134], deviceToken: m[0x322], + + psKeyMap: psKeyMap, + pt4TokenMap: pt4TokenMap, } c.Nickname = nick c.Age = age @@ -142,6 +148,30 @@ func readT200(data []byte) (pf, pfKey []byte) { return } +func readT512(data []byte) (psKeyMap map[string][]byte, pt4TokenMap map[string][]byte) { + reader := binary.NewReader(data) + length := int(reader.ReadUInt16()) + + psKeyMap = make(map[string][]byte, length) + pt4TokenMap = make(map[string][]byte, length) + + for i := 0; i < length; i++ { + domain := reader.ReadStringShort() + psKey := reader.ReadBytesShort() + pt4Token := reader.ReadBytesShort() + + if len(psKey) > 0 { + psKeyMap[domain] = psKey + } + + if len(pt4Token) > 0 { + pt4TokenMap[domain] = pt4Token + } + } + + return +} + func readT531(data []byte) (a1, noPicSig []byte) { reader := binary.NewReader(data) m := reader.ReadTlvMap(2)