diff --git a/client/builders.go b/client/builders.go index b7a04ec9..bd2b39fe 100644 --- a/client/builders.go +++ b/client/builders.go @@ -17,7 +17,6 @@ import ( "github.com/Mrs4s/MiraiGo/client/pb/oidb" "github.com/Mrs4s/MiraiGo/client/pb/profilecard" "github.com/Mrs4s/MiraiGo/client/pb/structmsg" - "github.com/Mrs4s/MiraiGo/internal/packets" "github.com/Mrs4s/MiraiGo/internal/proto" "github.com/Mrs4s/MiraiGo/internal/tlv" ) @@ -27,6 +26,23 @@ var ( syncConst2 = rand.Int63() ) +func buildCode2DRequestPacket(seq uint32, j uint64, cmd uint16, bodyFunc func(writer *binary.Writer)) []byte { + return binary.NewWriterF(func(w *binary.Writer) { + w.WriteByte(2) + pos := w.FillUInt16() + w.WriteUInt16(cmd) + w.Write(make([]byte, 21)) + w.WriteByte(3) + w.WriteUInt16(0) + w.WriteUInt16(50) // version + w.WriteUInt32(seq) + w.WriteUInt64(j) + bodyFunc(w) + w.WriteByte(3) + w.WriteUInt16At(pos, uint16(w.Len())) + }) +} + func (c *QQClient) buildLoginRequest() *network.Request { seq := c.nextSeq() req := c.buildOicqRequestPacket(c.Uin, 0x0810, binary.NewWriterF(func(w *binary.Writer) { @@ -130,7 +146,7 @@ func (c *QQClient) buildQRCodeFetchRequest(size, margin, ecLevel uint32) *networ req := c.buildOicqRequestPacket(0, 0x0812, binary.NewWriterF(func(w *binary.Writer) { w.WriteHex(`0001110000001000000072000000`) // trans header w.WriteUInt32(uint32(time.Now().Unix())) - w.Write(packets.BuildCode2DRequestPacket(0, 0, 0x31, func(w *binary.Writer) { + w.Write(buildCode2DRequestPacket(0, 0, 0x31, func(w *binary.Writer) { w.WriteUInt16(0) // const w.WriteUInt32(16) // app id w.WriteUInt64(0) // const @@ -163,7 +179,7 @@ func (c *QQClient) buildQRCodeResultQueryRequest(sig []byte) *network.Request { req := c.buildOicqRequestPacket(0, 0x0812, binary.NewWriterF(func(w *binary.Writer) { w.WriteHex(`0000620000001000000072000000`) // trans header w.WriteUInt32(uint32(time.Now().Unix())) - w.Write(packets.BuildCode2DRequestPacket(1, 0, 0x12, func(w *binary.Writer) { + w.Write(buildCode2DRequestPacket(1, 0, 0x12, func(w *binary.Writer) { w.WriteUInt16(5) // const w.WriteByte(1) // const w.WriteUInt32(8) // product type diff --git a/client/c2c_processor.go b/client/c2c_processor.go index f59a3f9b..f5ec83ae 100644 --- a/client/c2c_processor.go +++ b/client/c2c_processor.go @@ -73,7 +73,7 @@ func (c *QQClient) c2cMessageSyncProcessor(rsp *msg.GetMessageResponse, resp *ne if rsp.GetSyncFlag() != msg.SyncFlag_STOP { c.Debug("continue sync with flag: %v", rsp.SyncFlag) req := c.buildGetMessageRequest(rsp.GetSyncFlag(), time.Now().Unix()) - req.Params = resp.Params + req.Params = resp.Params() _, _ = c.callAndDecode(req) } } @@ -90,7 +90,7 @@ func (c *QQClient) commMsgProcessor(pMsg *msg.Message, resp *network.Response) { return } c.lastC2CMsgTime = int64(pMsg.Head.GetMsgTime()) - if resp.Params.Bool("init") { + if resp.Params().Bool("init") { return } if decoder, _ := peekC2CDecoder(pMsg.Head.GetMsgType()); decoder != nil { @@ -232,7 +232,7 @@ func systemMessageDecoder(c *QQClient, _ *msg.Message, _ *network.Response) { } func troopSystemMessageDecoder(c *QQClient, pMsg *msg.Message, info *network.Response) { - if !info.Params.Bool("used_reg_proxy") && pMsg.Head.GetMsgType() != 85 && pMsg.Head.GetMsgType() != 36 { + if !info.Params().Bool("used_reg_proxy") && pMsg.Head.GetMsgType() != 85 && pMsg.Head.GetMsgType() != 36 { c.exceptAndDispatchGroupSysMsg() } if len(pMsg.Body.GetMsgContent()) == 0 { diff --git a/client/decoders.go b/client/decoders.go index 8445cd18..669a6a4a 100644 --- a/client/decoders.go +++ b/client/decoders.go @@ -267,19 +267,20 @@ func decodeTransEmpResponse(c *QQClient, resp *network.Response) (interface{}, e body.ReadInt32() // app id? code := body.ReadByte() if code != 0 { - if code == 0x30 { - return &QRCodeLoginResponse{State: QRCodeWaitingForScan}, nil + var qrResp QRCodeLoginResponse + switch code { + case 0x30: + qrResp.State = QRCodeWaitingForScan + case 0x35: + qrResp.State = QRCodeWaitingForConfirm + case 0x36: + qrResp.State = QRCodeCanceled + case 0x11: + qrResp.State = QRCodeTimeout + default: + return nil, errors.Errorf("wtlogin.trans_emp sub cmd 0x12 error: %v", code) } - if code == 0x35 { - return &QRCodeLoginResponse{State: QRCodeWaitingForConfirm}, nil - } - if code == 0x36 { - return &QRCodeLoginResponse{State: QRCodeCanceled}, nil - } - if code == 0x11 { - return &QRCodeLoginResponse{State: QRCodeTimeout}, nil - } - return nil, errors.Errorf("wtlogin.trans_emp sub cmd 0x12 error: %v", code) + return qrResp, nil } c.Uin = body.ReadInt64() c.highwaySession.Uin = strconv.FormatInt(c.Uin, 10) diff --git a/client/group_msg.go b/client/group_msg.go index ea91526a..585268b2 100644 --- a/client/group_msg.go +++ b/client/group_msg.go @@ -348,7 +348,7 @@ func decodeGetGroupMsgResponse(c *QQClient, resp *network.Response) (interface{} if m.Head.FromUin == nil { continue } - if m.Content != nil && m.Content.GetPkgNum() > 1 && !resp.Params.Bool("raw") { + if m.Content != nil && m.Content.GetPkgNum() > 1 && !resp.Params().Bool("raw") { if m.Content.GetPkgIndex() == 0 { c.Debug("build fragmented message from history") i := m.Head.GetMsgSeq() - m.Content.GetPkgNum() diff --git a/client/internal/network/response.go b/client/internal/network/response.go index 556e8519..f5d8a737 100644 --- a/client/internal/network/response.go +++ b/client/internal/network/response.go @@ -13,9 +13,15 @@ type Response struct { CommandName string Body []byte - Params Params // Request is the original request that obtained this response. - // Request *Request + Request *Request +} + +func (r *Response) Params() Params { + if r.Request == nil { + return nil + } + return r.Request.Params } var ( diff --git a/client/network.go b/client/network.go index ebcdbd11..b784995c 100644 --- a/client/network.go +++ b/client/network.go @@ -310,7 +310,7 @@ func (c *QQClient) pktProc(req *network.Request, netErr error) { } }() - c.Debug("rev resp: %v seq: %v", req.CommandName, req.SequenceID) + c.Debug("recv pkt: %v seq: %v", req.CommandName, req.SequenceID) c.stat.PacketReceived.Add(1) // snapshot of read call @@ -321,9 +321,9 @@ func (c *QQClient) pktProc(req *network.Request, netErr error) { SequenceID: req.SequenceID, CommandName: req.CommandName, Body: req.Body, - Params: call.Request.Params, - // Request: nil, + Request: call.Request, } + delete(c.pending, req.SequenceID) } c.pendingMu.Unlock() if call != nil && call.Request.CommandName == req.CommandName { diff --git a/internal/packets/global.go b/internal/packets/global.go deleted file mode 100644 index 869a201f..00000000 --- a/internal/packets/global.go +++ /dev/null @@ -1,22 +0,0 @@ -package packets - -import ( - "github.com/Mrs4s/MiraiGo/binary" -) - -func BuildCode2DRequestPacket(seq uint32, j uint64, cmd uint16, bodyFunc func(writer *binary.Writer)) []byte { - return binary.NewWriterF(func(w *binary.Writer) { - w.WriteByte(2) - pos := w.FillUInt16() - w.WriteUInt16(cmd) - w.Write(make([]byte, 21)) - w.WriteByte(3) - w.WriteUInt16(0) - w.WriteUInt16(50) // version - w.WriteUInt32(seq) - w.WriteUInt64(j) - bodyFunc(w) - w.WriteByte(3) - w.WriteUInt16At(pos, uint16(w.Len())) - }) -}