diff --git a/binary/pool.go b/binary/pool.go index 76197a5f..08aa6e21 100644 --- a/binary/pool.go +++ b/binary/pool.go @@ -22,8 +22,8 @@ func NewWriter() *Writer { return w } -// PutBuffer 将 Writer 放回池中 -func PutBuffer(w *Writer) { +// PutWriter 将 Writer 放回池中 +func PutWriter(w *Writer) { // See https://golang.org/issue/23199 const maxSize = 1 << 16 if w.Cap() < maxSize { // 对于大Buffer直接丢弃 diff --git a/binary/reader.go b/binary/reader.go index 0e76a8fb..0ddd86af 100644 --- a/binary/reader.go +++ b/binary/reader.go @@ -2,8 +2,11 @@ package binary import ( "bytes" + "encoding/binary" "io" "net" + + "github.com/Mrs4s/MiraiGo/utils" ) type Reader struct { @@ -49,37 +52,33 @@ func (r *Reader) ReadBytesShort() []byte { } func (r *Reader) ReadUInt16() uint16 { - f, _ := r.buf.ReadByte() - s, err := r.buf.ReadByte() - if err != nil { - panic(err) - } - return uint16((int32(f) << 8) + int32(s)) + b := r.ReadBytes(2) + return binary.BigEndian.Uint16(b) } func (r *Reader) ReadInt32() int32 { b := r.ReadBytes(4) - return (int32(b[0]) << 24) | (int32(b[1]) << 16) | (int32(b[2]) << 8) | int32(b[3]) + return int32(binary.BigEndian.Uint32(b)) } func (r *Reader) ReadInt64() int64 { b := r.ReadBytes(8) - return ((int64(b[0]) << 56) | (int64(b[1]) << 48) | (int64(b[2]) << 40) | (int64(b[3]) << 32) | int64(b[4])<<24) | (int64(b[5]) << 16) | (int64(b[6]) << 8) | int64(b[7]) + return int64(binary.BigEndian.Uint64(b)) } func (r *Reader) ReadString() string { data := r.ReadBytes(int(r.ReadInt32() - 4)) - return string(data) + return utils.B2S(data) } func (r *Reader) ReadStringShort() string { data := r.ReadBytes(int(r.ReadUInt16())) - return string(data) + return utils.B2S(data) } func (r *Reader) ReadStringLimit(limit int) string { data := r.ReadBytes(limit) - return string(data) + return utils.B2S(data) } func (r *Reader) ReadAvailable() []byte { @@ -142,13 +141,6 @@ func (r *NetworkReader) ReadByte() (byte, error) { func (r *NetworkReader) ReadBytes(len int) ([]byte, error) { buf := make([]byte, len) _, err := io.ReadFull(r.conn, buf) - // for i := 0; i < len; i++ { - // b, err := r.ReadByte() - // if err != nil { - // return nil, err - // } - // buf[i] = b - // } return buf, err } @@ -157,5 +149,5 @@ func (r *NetworkReader) ReadInt32() (int32, error) { if err != nil { return 0, err } - return (int32(b[0]) << 24) | (int32(b[1]) << 16) | (int32(b[2]) << 8) | int32(b[3]), nil + return int32(binary.BigEndian.Uint32(b)), nil } diff --git a/binary/tea.go b/binary/tea.go index ed64e374..218aebc5 100644 --- a/binary/tea.go +++ b/binary/tea.go @@ -3,14 +3,12 @@ package binary import ( "encoding/binary" "math/rand" - "reflect" "unsafe" ) func xorQ(a, b []byte, c []byte) { // MAGIC - *(*uint64)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&c)).Data)) = - *(*uint64)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&a)).Data)) ^ - *(*uint64)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&b)).Data)) + *(*uint64)(unsafe.Pointer(&c[0])) = + *(*uint64)(unsafe.Pointer(&a[0])) ^ *(*uint64)(unsafe.Pointer(&b[0])) } type TEA [4]uint32 diff --git a/binary/writer.go b/binary/writer.go index dcd53900..aef161a2 100644 --- a/binary/writer.go +++ b/binary/writer.go @@ -4,6 +4,8 @@ import ( "bytes" "encoding/binary" "encoding/hex" + + "github.com/Mrs4s/MiraiGo/utils" ) // Writer 写入 @@ -13,7 +15,7 @@ func NewWriterF(f func(writer *Writer)) []byte { w := NewWriter() f(w) b := append([]byte(nil), w.Bytes()...) - PutBuffer(w) + PutWriter(w) return b } @@ -49,13 +51,13 @@ func (w *Writer) WriteUInt64(v uint64) { } func (w *Writer) WriteString(v string) { - payload := []byte(v) + payload := utils.S2B(v) w.WriteUInt32(uint32(len(payload) + 4)) w.Write(payload) } func (w *Writer) WriteStringShort(v string) { - w.WriteBytesShort([]byte(v)) + w.WriteBytesShort(utils.S2B(v)) } func (w *Writer) WriteBool(b bool) { @@ -94,7 +96,7 @@ func (w *Writer) WriteUniPacket(commandName string, sessionId, extraData, body [ data := w1.Bytes() w.WriteUInt32(uint32(len(data) + 4)) w.Write(data) - PutBuffer(w1) + PutWriter(w1) w.WriteUInt32(uint32(len(body) + 4)) // WriteIntLvPacket w.Write(body) } diff --git a/client/highway.go b/client/highway.go index 3c79c2a2..a64bf669 100644 --- a/client/highway.go +++ b/client/highway.go @@ -51,7 +51,7 @@ func (c *QQClient) highwayUploadStream(ip uint32, port int, updKey []byte, strea binary.Put256KBytes(&chunk) }() w := binary.NewWriter() - defer binary.PutBuffer(w) + defer binary.PutWriter(w) for { chunk = chunk[:chunkSize] rl, err := io.ReadFull(stream, chunk) @@ -136,7 +136,7 @@ func (c *QQClient) highwayUploadByBDH(stream io.Reader, length int64, cmdId int3 binary.Put256KBytes(&chunk) }() w := binary.NewWriter() - defer binary.PutBuffer(w) + defer binary.PutWriter(w) for { chunk = chunk[:chunkSize] rl, err := io.ReadFull(stream, chunk) @@ -384,7 +384,7 @@ func (c *QQClient) highwaySendHeartbreak(conn net.Conn) error { w.Write(head) w.WriteByte(41) _, err := conn.Write(w.Bytes()) - binary.PutBuffer(w) + binary.PutWriter(w) return err } diff --git a/message/pack.go b/message/pack.go index 5b0e6cc8..c646a058 100644 --- a/message/pack.go +++ b/message/pack.go @@ -3,9 +3,10 @@ package message import ( "encoding/hex" + "google.golang.org/protobuf/proto" + "github.com/Mrs4s/MiraiGo/binary" "github.com/Mrs4s/MiraiGo/client/pb/msg" - "google.golang.org/protobuf/proto" ) var imgOld = []byte{ @@ -134,14 +135,6 @@ func (e *ServiceElement) Pack() (r []*msg.Elem) { r = append(r, &msg.Elem{ Text: &msg.Text{Str: &e.ResId}, }) - r = append(r, &msg.Elem{ - RichMsg: &msg.RichMsg{ - Template1: append([]byte{1}, binary.ZlibCompress([]byte(e.Content))...), - ServiceId: &e.Id, - MsgResId: []byte{}, - }, - }) - return } r = append(r, &msg.Elem{ RichMsg: &msg.RichMsg{ diff --git a/protocol/packets/builders.go b/protocol/packets/builders.go index 9d8325cf..a234ed5f 100644 --- a/protocol/packets/builders.go +++ b/protocol/packets/builders.go @@ -39,11 +39,11 @@ func BuildUniPacket(uin int64, seq uint16, commandName string, encryptType byte, w3 := binary.NewWriter() w3.WriteUniPacket(commandName, sessionID, extraData, body) w2.EncryptAndWrite(key, w3.Bytes()) - binary.PutBuffer(w3) + binary.PutWriter(w3) } data := w2.Bytes() w.WriteUInt32(uint32(len(data) + 4)) w.Write(data) - binary.PutBuffer(w2) + binary.PutWriter(w2) }) }