1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 19:17:38 +08:00

use binary package in reader.

This commit is contained in:
wdvxdr 2021-07-13 10:17:36 +08:00
parent 061067bf95
commit b7dee2aeb2
No known key found for this signature in database
GPG Key ID: 55FF1414A69CEBA6
7 changed files with 28 additions and 43 deletions

View File

@ -22,8 +22,8 @@ func NewWriter() *Writer {
return w return w
} }
// PutBuffer 将 Writer 放回池中 // PutWriter 将 Writer 放回池中
func PutBuffer(w *Writer) { func PutWriter(w *Writer) {
// See https://golang.org/issue/23199 // See https://golang.org/issue/23199
const maxSize = 1 << 16 const maxSize = 1 << 16
if w.Cap() < maxSize { // 对于大Buffer直接丢弃 if w.Cap() < maxSize { // 对于大Buffer直接丢弃

View File

@ -2,8 +2,11 @@ package binary
import ( import (
"bytes" "bytes"
"encoding/binary"
"io" "io"
"net" "net"
"github.com/Mrs4s/MiraiGo/utils"
) )
type Reader struct { type Reader struct {
@ -49,37 +52,33 @@ func (r *Reader) ReadBytesShort() []byte {
} }
func (r *Reader) ReadUInt16() uint16 { func (r *Reader) ReadUInt16() uint16 {
f, _ := r.buf.ReadByte() b := r.ReadBytes(2)
s, err := r.buf.ReadByte() return binary.BigEndian.Uint16(b)
if err != nil {
panic(err)
}
return uint16((int32(f) << 8) + int32(s))
} }
func (r *Reader) ReadInt32() int32 { func (r *Reader) ReadInt32() int32 {
b := r.ReadBytes(4) 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 { func (r *Reader) ReadInt64() int64 {
b := r.ReadBytes(8) 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 { func (r *Reader) ReadString() string {
data := r.ReadBytes(int(r.ReadInt32() - 4)) data := r.ReadBytes(int(r.ReadInt32() - 4))
return string(data) return utils.B2S(data)
} }
func (r *Reader) ReadStringShort() string { func (r *Reader) ReadStringShort() string {
data := r.ReadBytes(int(r.ReadUInt16())) data := r.ReadBytes(int(r.ReadUInt16()))
return string(data) return utils.B2S(data)
} }
func (r *Reader) ReadStringLimit(limit int) string { func (r *Reader) ReadStringLimit(limit int) string {
data := r.ReadBytes(limit) data := r.ReadBytes(limit)
return string(data) return utils.B2S(data)
} }
func (r *Reader) ReadAvailable() []byte { func (r *Reader) ReadAvailable() []byte {
@ -142,13 +141,6 @@ func (r *NetworkReader) ReadByte() (byte, error) {
func (r *NetworkReader) ReadBytes(len int) ([]byte, error) { func (r *NetworkReader) ReadBytes(len int) ([]byte, error) {
buf := make([]byte, len) buf := make([]byte, len)
_, err := io.ReadFull(r.conn, buf) _, 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 return buf, err
} }
@ -157,5 +149,5 @@ func (r *NetworkReader) ReadInt32() (int32, error) {
if err != nil { if err != nil {
return 0, err 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
} }

View File

@ -3,14 +3,12 @@ package binary
import ( import (
"encoding/binary" "encoding/binary"
"math/rand" "math/rand"
"reflect"
"unsafe" "unsafe"
) )
func xorQ(a, b []byte, c []byte) { // MAGIC func xorQ(a, b []byte, c []byte) { // MAGIC
*(*uint64)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&c)).Data)) = *(*uint64)(unsafe.Pointer(&c[0])) =
*(*uint64)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&a)).Data)) ^ *(*uint64)(unsafe.Pointer(&a[0])) ^ *(*uint64)(unsafe.Pointer(&b[0]))
*(*uint64)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&b)).Data))
} }
type TEA [4]uint32 type TEA [4]uint32

View File

@ -4,6 +4,8 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
"github.com/Mrs4s/MiraiGo/utils"
) )
// Writer 写入 // Writer 写入
@ -13,7 +15,7 @@ func NewWriterF(f func(writer *Writer)) []byte {
w := NewWriter() w := NewWriter()
f(w) f(w)
b := append([]byte(nil), w.Bytes()...) b := append([]byte(nil), w.Bytes()...)
PutBuffer(w) PutWriter(w)
return b return b
} }
@ -49,13 +51,13 @@ func (w *Writer) WriteUInt64(v uint64) {
} }
func (w *Writer) WriteString(v string) { func (w *Writer) WriteString(v string) {
payload := []byte(v) payload := utils.S2B(v)
w.WriteUInt32(uint32(len(payload) + 4)) w.WriteUInt32(uint32(len(payload) + 4))
w.Write(payload) w.Write(payload)
} }
func (w *Writer) WriteStringShort(v string) { func (w *Writer) WriteStringShort(v string) {
w.WriteBytesShort([]byte(v)) w.WriteBytesShort(utils.S2B(v))
} }
func (w *Writer) WriteBool(b bool) { func (w *Writer) WriteBool(b bool) {
@ -94,7 +96,7 @@ func (w *Writer) WriteUniPacket(commandName string, sessionId, extraData, body [
data := w1.Bytes() data := w1.Bytes()
w.WriteUInt32(uint32(len(data) + 4)) w.WriteUInt32(uint32(len(data) + 4))
w.Write(data) w.Write(data)
PutBuffer(w1) PutWriter(w1)
w.WriteUInt32(uint32(len(body) + 4)) // WriteIntLvPacket w.WriteUInt32(uint32(len(body) + 4)) // WriteIntLvPacket
w.Write(body) w.Write(body)
} }

View File

@ -51,7 +51,7 @@ func (c *QQClient) highwayUploadStream(ip uint32, port int, updKey []byte, strea
binary.Put256KBytes(&chunk) binary.Put256KBytes(&chunk)
}() }()
w := binary.NewWriter() w := binary.NewWriter()
defer binary.PutBuffer(w) defer binary.PutWriter(w)
for { for {
chunk = chunk[:chunkSize] chunk = chunk[:chunkSize]
rl, err := io.ReadFull(stream, chunk) rl, err := io.ReadFull(stream, chunk)
@ -136,7 +136,7 @@ func (c *QQClient) highwayUploadByBDH(stream io.Reader, length int64, cmdId int3
binary.Put256KBytes(&chunk) binary.Put256KBytes(&chunk)
}() }()
w := binary.NewWriter() w := binary.NewWriter()
defer binary.PutBuffer(w) defer binary.PutWriter(w)
for { for {
chunk = chunk[:chunkSize] chunk = chunk[:chunkSize]
rl, err := io.ReadFull(stream, chunk) rl, err := io.ReadFull(stream, chunk)
@ -384,7 +384,7 @@ func (c *QQClient) highwaySendHeartbreak(conn net.Conn) error {
w.Write(head) w.Write(head)
w.WriteByte(41) w.WriteByte(41)
_, err := conn.Write(w.Bytes()) _, err := conn.Write(w.Bytes())
binary.PutBuffer(w) binary.PutWriter(w)
return err return err
} }

View File

@ -3,9 +3,10 @@ package message
import ( import (
"encoding/hex" "encoding/hex"
"google.golang.org/protobuf/proto"
"github.com/Mrs4s/MiraiGo/binary" "github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/client/pb/msg" "github.com/Mrs4s/MiraiGo/client/pb/msg"
"google.golang.org/protobuf/proto"
) )
var imgOld = []byte{ var imgOld = []byte{
@ -134,14 +135,6 @@ func (e *ServiceElement) Pack() (r []*msg.Elem) {
r = append(r, &msg.Elem{ r = append(r, &msg.Elem{
Text: &msg.Text{Str: &e.ResId}, 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{ r = append(r, &msg.Elem{
RichMsg: &msg.RichMsg{ RichMsg: &msg.RichMsg{

View File

@ -39,11 +39,11 @@ func BuildUniPacket(uin int64, seq uint16, commandName string, encryptType byte,
w3 := binary.NewWriter() w3 := binary.NewWriter()
w3.WriteUniPacket(commandName, sessionID, extraData, body) w3.WriteUniPacket(commandName, sessionID, extraData, body)
w2.EncryptAndWrite(key, w3.Bytes()) w2.EncryptAndWrite(key, w3.Bytes())
binary.PutBuffer(w3) binary.PutWriter(w3)
} }
data := w2.Bytes() data := w2.Bytes()
w.WriteUInt32(uint32(len(data) + 4)) w.WriteUInt32(uint32(len(data) + 4))
w.Write(data) w.Write(data)
binary.PutBuffer(w2) binary.PutWriter(w2)
}) })
} }