Skip to content

Commit

Permalink
fix some issue with golangci-lint.
Browse files Browse the repository at this point in the history
  • Loading branch information
wdvxdr1123 committed Jul 15, 2021
1 parent 7797ecd commit 7fe0b87
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 101 deletions.
6 changes: 4 additions & 2 deletions client/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions client/group_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
15 changes: 6 additions & 9 deletions client/group_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
47 changes: 10 additions & 37 deletions client/highway.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"bytes"
"crypto/md5"
binary2 "encoding/binary"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions client/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
54 changes: 28 additions & 26 deletions client/online_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
16 changes: 0 additions & 16 deletions client/ptt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
5 changes: 3 additions & 2 deletions client/qidian.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package client
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/pkg/errors"
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]})
Expand Down
1 change: 0 additions & 1 deletion message/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
})
Expand Down
2 changes: 1 addition & 1 deletion protocol/tlv/tlv.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion utils/sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7fe0b87

Please sign in to comment.