diff --git a/client/global.go b/client/global.go index 11144b9e7..1a2c661c9 100644 --- a/client/global.go +++ b/client/global.go @@ -395,8 +395,10 @@ func (info *DeviceInfo) GenNewGuid() { func (info *DeviceInfo) GenNewTgtgtKey() { r := make([]byte, 16) rand.Read(r) - t := md5.Sum(append(r, info.Guid...)) - info.TgtgtKey = t[:] + h := md5.New() + h.Write(r) + h.Write(info.Guid) + info.TgtgtKey = h.Sum(nil) } func (info *DeviceInfo) GenDeviceInfoData() []byte { diff --git a/client/group_info.go b/client/group_info.go index 572662a78..2d9c10a8f 100644 --- a/client/group_info.go +++ b/client/group_info.go @@ -8,14 +8,15 @@ import ( "strings" "sync" + "github.com/pkg/errors" + "google.golang.org/protobuf/proto" + "github.com/Mrs4s/MiraiGo/binary" "github.com/Mrs4s/MiraiGo/binary/jce" "github.com/Mrs4s/MiraiGo/client/pb/oidb" "github.com/Mrs4s/MiraiGo/client/pb/profilecard" "github.com/Mrs4s/MiraiGo/protocol/packets" "github.com/Mrs4s/MiraiGo/utils" - "github.com/pkg/errors" - "google.golang.org/protobuf/proto" ) type ( diff --git a/client/group_msg.go b/client/group_msg.go index 2d31f148a..31e1be8f8 100644 --- a/client/group_msg.go +++ b/client/group_msg.go @@ -350,15 +350,12 @@ func decodeMsgSendResponse(c *QQClient, _ *incomingPacketInfo, payload []byte) ( if err := proto.Unmarshal(payload, &rsp); err != nil { return nil, errors.Wrap(err, "failed to unmarshal protobuf message") } - if rsp.GetResult() != 0 { - switch rsp.GetResult() { - case 55: - c.Error("send msg error: %v Bot has blocked target's content", rsp.GetResult()) - break - default: - c.Error("send msg error: %v %v", rsp.GetResult(), rsp.GetErrMsg()) - break - } + switch rsp.GetResult() { + case 0: // OK. + case 55: + c.Error("send msg error: %v Bot has blocked target's content", rsp.GetResult()) + default: + c.Error("send msg error: %v %v", rsp.GetResult(), rsp.GetErrMsg()) } return nil, nil } diff --git a/client/highway.go b/client/highway.go index a64bf6695..9aa6230e2 100644 --- a/client/highway.go +++ b/client/highway.go @@ -4,10 +4,8 @@ import ( "bytes" "crypto/md5" binary2 "encoding/binary" - "encoding/hex" "fmt" "io" - "io/ioutil" "net" "net/http" "os" @@ -46,10 +44,10 @@ func (c *QQClient) highwayUploadStream(ip uint32, port int, updKey []byte, strea defer conn.Close() offset := 0 reader := binary.NewNetworkReader(conn) - chunk := *binary.Get256KBytes() - defer func() { // 延迟捕获 chunk - binary.Put256KBytes(&chunk) - }() + buf := binary.Get256KBytes() + chunk := *buf + defer binary.Put256KBytes(buf) + w := binary.NewWriter() defer binary.PutWriter(w) for { @@ -131,10 +129,10 @@ func (c *QQClient) highwayUploadByBDH(stream io.Reader, length int64, cmdId int3 return nil, errors.Wrap(err, "echo error") } var rspExt []byte - chunk := *binary.Get256KBytes() - defer func() { // 延迟捕获 chunk - binary.Put256KBytes(&chunk) - }() + buf := binary.Get256KBytes() + chunk := *buf + defer binary.Put256KBytes(buf) + w := binary.NewWriter() defer binary.PutWriter(w) for { @@ -466,7 +464,8 @@ func (c *QQClient) excitingUploadStream(stream io.ReadSeeker, cmdId int32, ticke if err != nil { return nil, errors.Wrap(err, "request error") } - body, _ := ioutil.ReadAll(rsp.Body) + body, _ := io.ReadAll(rsp.Body) + _ = rsp.Body.Close() r := binary.NewReader(body) r.ReadByte() hl := r.ReadInt32() @@ -487,32 +486,6 @@ func (c *QQClient) excitingUploadStream(stream io.ReadSeeker, cmdId int32, ticke return rspExt, nil } -// 只是为了写的跟上面一样长(bushi,当然也应该是最快的玩法 -func (c *QQClient) uploadPtt(ip string, port int32, updKey, fileKey, data, md5 []byte) error { - url := make([]byte, 512)[:0] - url = append(url, "http://"...) - url = append(url, ip...) - url = append(url, ':') - url = strconv.AppendInt(url, int64(port), 10) - url = append(url, "/?ver=4679&ukey="...) - p := len(url) - url = url[:p+len(updKey)*2] - hex.Encode(url[p:], updKey) - url = append(url, "&filekey="...) - p = len(url) - url = url[:p+len(fileKey)*2] - hex.Encode(url[p:], fileKey) - url = append(url, "&filesize="...) - url = strconv.AppendInt(url, int64(len(data)), 10) - url = append(url, "&bmd5="...) - p = len(url) - url = url[:p+32] - hex.Encode(url[p:], md5) - url = append(url, "&mType=pttDu&voice_encodec=1"...) - _, err := utils.HttpPostBytes(string(url), data) - return errors.Wrap(err, "failed to upload ptt") -} - func (c *QQClient) uploadGroupHeadPortrait(groupCode int64, img []byte) error { url := fmt.Sprintf( "http://htdata3.qq.com/cgi-bin/httpconn?htcmd=0x6ff0072&ver=5520&ukey=%v&range=0&uin=%v&seq=23&groupuin=%v&filetype=3&imagetype=5&userdata=0&subcmd=1&subver=101&clip=0_0_0_0&filesize=%v", diff --git a/client/image.go b/client/image.go index 0d43408e7..2b1662db9 100644 --- a/client/image.go +++ b/client/image.go @@ -47,7 +47,7 @@ func (c *QQClient) UploadGroupImage(groupCode int64, img io.ReadSeeker) (*messag } if len(c.srvSsoAddrs) == 0 { for i, addr := range rsp.UploadIp { - c.srvSsoAddrs = append(c.srvSsoAddrs, fmt.Sprintf("%v:%v", binary.UInt32ToIPV4Address(uint32(addr)), rsp.UploadPort[i])) + c.srvSsoAddrs = append(c.srvSsoAddrs, fmt.Sprintf("%v:%v", binary.UInt32ToIPV4Address(addr), rsp.UploadPort[i])) } } if _, err = c.highwayUploadByBDH(img, length, 2, rsp.UploadKey, fh, EmptyBytes, false); err == nil { @@ -74,7 +74,7 @@ func (c *QQClient) UploadGroupImageByFile(groupCode int64, path string) (*messag } defer func() { _ = img.Close() }() fh, length := utils.ComputeMd5AndLength(img) - seq, pkt := c.buildGroupImageStorePacket(groupCode, fh[:], int32(length)) + seq, pkt := c.buildGroupImageStorePacket(groupCode, fh, int32(length)) r, err := c.sendAndWait(seq, pkt) if err != nil { return nil, err @@ -105,7 +105,7 @@ ok: if bytes.Equal(tmp, []byte{0x47, 0x49, 0x46, 0x38}) { imageType = 2000 } - return message.NewGroupImage(binary.CalculateImageResourceId(fh[:]), fh[:], rsp.FileId, int32(length), int32(i.Width), int32(i.Height), imageType), nil + return message.NewGroupImage(binary.CalculateImageResourceId(fh), fh, rsp.FileId, int32(length), int32(i.Width), int32(i.Height), imageType), nil } func (c *QQClient) UploadPrivateImage(target int64, img io.ReadSeeker) (*message.FriendImageElement, error) { diff --git a/client/online_push.go b/client/online_push.go index 4b93f1445..ede6ca3ac 100644 --- a/client/online_push.go +++ b/client/online_push.go @@ -229,35 +229,37 @@ func msgType0x210Sub44Decoder(c *QQClient, protobuf []byte) error { if err := proto.Unmarshal(protobuf, &s44); err != nil { return errors.Wrap(err, "failed to unmarshal protobuf message") } - if s44.GroupSyncMsg != nil { - func() { - groupJoinLock.Lock() - defer groupJoinLock.Unlock() - if s44.GroupSyncMsg.GetGrpCode() != 0 { // member sync - c.Debug("syncing members.") - if group := c.FindGroup(s44.GroupSyncMsg.GetGrpCode()); group != nil { - group.Update(func(_ *GroupInfo) { - var lastJoinTime int64 = 0 - for _, m := range group.Members { - if lastJoinTime < m.JoinTime { - lastJoinTime = m.JoinTime - } - } - if newMem, err := c.GetGroupMembers(group); err == nil { - group.Members = newMem - for _, m := range newMem { - if lastJoinTime < m.JoinTime { - go c.dispatchNewMemberEvent(&MemberJoinGroupEvent{ - Group: group, - Member: m, - }) - } - } - } + if s44.GroupSyncMsg == nil { + return nil + } + groupJoinLock.Lock() + defer groupJoinLock.Unlock() + if s44.GroupSyncMsg.GetGrpCode() != 0 { // member sync + return errors.New("invalid group code") + } + c.Debug("syncing members.") + if group := c.FindGroup(s44.GroupSyncMsg.GetGrpCode()); group != nil { + group.lock.Lock() + defer group.lock.Unlock() + + var lastJoinTime int64 = 0 + for _, m := range group.Members { + if lastJoinTime < m.JoinTime { + lastJoinTime = m.JoinTime + } + } + + if newMem, err := c.GetGroupMembers(group); err == nil { + group.Members = newMem + for _, m := range newMem { + if lastJoinTime < m.JoinTime { + go c.dispatchNewMemberEvent(&MemberJoinGroupEvent{ + Group: group, + Member: m, }) } } - }() + } } return nil } diff --git a/client/ptt.go b/client/ptt.go index ff9eacd29..bb911f481 100644 --- a/client/ptt.go +++ b/client/ptt.go @@ -353,19 +353,3 @@ func decodeGroupShortVideoUploadResponse(_ *QQClient, _ *incomingPacketInfo, pay } return rsp.PttShortVideoUploadRsp, nil } - -func constructPTTExtraInfo(codec, length int32) []byte { - return binary.NewWriterF(func(w *binary.Writer) { - w.WriteByte(3) - w.WriteByte(8) - w.WriteUInt16(4) - w.WriteUInt32(uint32(codec)) - w.WriteByte(9) - w.WriteUInt16(4) - w.WriteUInt32(uint32(14)) // length 时间 - w.WriteByte(10) - info := []byte{0x08, 0x00, 0x28, 0x00, 0x38, 0x00} // todo - w.WriteUInt16(uint16(len(info))) - w.Write(info) - }) -} diff --git a/client/qidian.go b/client/qidian.go index c633f5de1..be8eef545 100644 --- a/client/qidian.go +++ b/client/qidian.go @@ -4,7 +4,7 @@ package client import ( "bytes" "fmt" - "io/ioutil" + "io" "net/http" "github.com/pkg/errors" @@ -144,7 +144,8 @@ func (c *QQClient) bigDataRequest(subCmd uint32, req proto.Message) ([]byte, err if err != nil { return nil, errors.Wrap(err, "request error") } - rspBody, _ := ioutil.ReadAll(rsp.Body) + defer rsp.Body.Close() + rspBody, _ := io.ReadAll(rsp.Body) r := binary.NewReader(rspBody) r.ReadByte() l1 := int(r.ReadInt32()) diff --git a/message/message.go b/message/message.go index a5f9fbc4c..ca60aeb1a 100644 --- a/message/message.go +++ b/message/message.go @@ -430,7 +430,7 @@ func ParseMessageElems(elems []*msg.Elem) []IMessageElement { } if content != "" { if elem.RichMsg.GetServiceId() == 35 { - reg := regexp.MustCompile(`m_resid="(\w+?.*?)"`) + reg := regexp.MustCompile(`m_resid="(.+)"`) sub := reg.FindAllStringSubmatch(content, -1) if len(sub) > 0 && len(sub[0]) > 1 { res = append(res, &ForwardElement{ResId: reg.FindAllStringSubmatch(content, -1)[0][1]}) diff --git a/message/pack.go b/message/pack.go index 36401be32..ac416a43d 100644 --- a/message/pack.go +++ b/message/pack.go @@ -230,7 +230,6 @@ func (e *GroupShowPicElement) Pack() (r []*msg.Elem) { Size: &e.Size, Md5: e.Md5, Flag: []byte{0x11, 0x00, 0x00, 0x00}, - // OldData: imgOld, PbReserve: reserve, }, }) diff --git a/protocol/tlv/tlv.go b/protocol/tlv/tlv.go index 7de80968a..9bed87bdd 100644 --- a/protocol/tlv/tlv.go +++ b/protocol/tlv/tlv.go @@ -1,7 +1,7 @@ package tlv func GuidFlag() uint32 { - var flag uint32 = 0 + var flag uint32 flag |= 1 << 24 & 0xFF000000 flag |= 0 << 8 & 0xFF00 return flag diff --git a/utils/sys.go b/utils/sys.go index f7d587dad..3ebda44c2 100644 --- a/utils/sys.go +++ b/utils/sys.go @@ -31,7 +31,7 @@ func ComputeMd5AndLength(r io.Reader) ([]byte, int64) { h := md5.New() length, _ := io.Copy(h, r) fh := h.Sum(nil) - return fh[:], length + return fh, length } func (r *multiReadSeeker) Read(p []byte) (int, error) {