Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize(builder): split WriteLenBytes and WriteBytes #39

Merged
merged 3 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions client/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ func (c *QQClient) OnMessage(msgLen int) {
fetcher.AddResult(packet.Seq, packet)
}
} else { // server pushed
if _, ok := listeners[packet.Cmd]; ok {
if fn, ok := listeners[packet.Cmd]; ok {
networkLogger.Debugf("Server Push(%d) <- %s, extra: %s", packet.RetCode, packet.Cmd, packet.Extra)
msg, err := listeners[packet.Cmd](c, packet)
msg, err := fn(c, packet)
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (c *QQClient) QrcodeLogin(refreshInterval int) error {
body := binary.NewBuilder(nil).
WriteU16(0x09).
WriteTlv(
binary.NewBuilder(nil).WriteBytes(c.t106, false).Pack(0x106),
binary.NewBuilder(nil).WriteBytes(c.t106).Pack(0x106),
tlv.T144(c.sig.Tgtgt, app, device),
tlv.T116(app.SubSigmap),
tlv.T142(app.PackageName, 0),
Expand All @@ -268,7 +268,7 @@ func (c *QQClient) QrcodeLogin(refreshInterval int) error {
tlv.T100(5, app.AppID, app.SubAppID, 8001, app.MainSigmap, 0),
tlv.T107(1, 0x0d, 0, 1),
tlv.T318(nil),
binary.NewBuilder(nil).WriteBytes(c.t16a, false).Pack(0x16a),
binary.NewBuilder(nil).WriteBytes(c.t16a).Pack(0x16a),
tlv.T166(5),
tlv.T521(0x13, "basicim"),
).ToBytes()
Expand Down
4 changes: 2 additions & 2 deletions client/highway.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ func sendHighwayPacket(packet *highway.ReqDataHighwayHead, buffer io.Reader, buf
}

writer := binary.NewBuilder(nil).
WriteBytes([]byte{0x28}, false).
WriteBytes([]byte{0x28}).
WriteU32(uint32(len(marshal))).
WriteU32(bufferSize).
WriteBytes(marshal, false)
WriteBytes(marshal)
_, _ = io.Copy(writer, buffer)
_, _ = writer.Write([]byte{0x29})

Expand Down
4 changes: 2 additions & 2 deletions info/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func Encode(sig *SigInfo) ([]byte, error) {
dataHash := crypto.MD5Digest(buffer.Bytes())

return binary.NewBuilder(nil).
WriteBytes(dataHash, true).
WriteBytes(buffer.Bytes(), true).
WriteLenBytes(dataHash).
WriteLenBytes(buffer.Bytes()).
ToBytes(), nil
}

Expand Down
18 changes: 9 additions & 9 deletions packets/tlv/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func T106(appId, appClientVersion, uin int, guid string, passwordMd5, tgtgtKey,
uint32(appClientVersion),
uint64(uin)).
WriteU32(uint32(utils.TimeStamp())).
WriteBytes(ip, false).
WriteBytes(ip).
WriteBool(savePassword).
WriteBytes(passwordMd5, false).
WriteBytes(tgtgtKey, false).
WriteBytes(passwordMd5).
WriteBytes(tgtgtKey).
WriteU32(0).
WriteBool(true).
WriteBytes(utils.MustParseHexStr(guid), false).
WriteBytes(utils.MustParseHexStr(guid)).
WriteU32(0).
WriteU32(1).
WritePacketString(strconv.Itoa(uin), "u16", false).
Expand Down Expand Up @@ -87,7 +87,7 @@ func T116(subSigmap int) []byte {

func T124() []byte {
return binary.NewBuilder(nil).
WriteBytes(make([]byte, 12), false).
WriteBytes(make([]byte, 12)).
Pack(0x124)
}

Expand Down Expand Up @@ -134,7 +134,7 @@ func T144(tgtgtKey []byte, appInfo *info.AppInfo, device *info.DeviceInfo) []byt

func T145(guid []byte) []byte {
return binary.NewBuilder(nil).
WriteBytes(guid, false).
WriteBytes(guid).
Pack(0x145)
}

Expand All @@ -154,13 +154,13 @@ func T166(imageType int) []byte {

func T16a(noPicSig []byte) []byte {
return binary.NewBuilder(nil).
WriteBytes(noPicSig, false).
WriteBytes(noPicSig).
Pack(0x16a)
}

func T16e(deviceName string) []byte {
return binary.NewBuilder(nil).
WriteBytes(utils.S2B(deviceName), false).
WriteBytes(utils.S2B(deviceName)).
Pack(0x16e)
}

Expand All @@ -182,7 +182,7 @@ func T191(canWebVerify int) []byte {
// T318 默认参数 tgtQr = []byte{0}
func T318(tgtQr []byte) []byte {
return binary.NewBuilder(nil).
WriteBytes(tgtQr, false).
WriteBytes(tgtQr).
Pack(0x318)
}

Expand Down
8 changes: 4 additions & 4 deletions packets/tlv/qrcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func T11(unusualSign []byte) []byte {
return binary.NewBuilder(nil).
WriteBytes(unusualSign, false).
WriteBytes(unusualSign).
Pack(0x11)
}

Expand All @@ -16,7 +16,7 @@ func T16(appid, subAppid int, guid []byte, ptVersion, packageName string) []byte
WriteU32(0).
WriteU32(uint32(appid)).
WriteU32(uint32(subAppid)).
WriteBytes(guid, false).
WriteBytes(guid).
WritePacketString(packageName, "u16", false).
WritePacketString(ptVersion, "u16", false).
WritePacketString(packageName, "u16", false).
Expand All @@ -39,7 +39,7 @@ func T1d(miscBitmap int) []byte {
}

func T33(guid []byte) []byte {
return binary.NewBuilder(nil).WriteBytes(guid, false).Pack(0x33)
return binary.NewBuilder(nil).WriteBytes(guid).Pack(0x33)
}

func T35(PtOSVersion int) []byte {
Expand All @@ -60,6 +60,6 @@ func Td1(AppOS, DeviceName string) []byte {
4: proto.DynamicMessage{
6: 1,
},
}.Encode(), false).
}.Encode()).
Pack(0xd1)
}
4 changes: 2 additions & 2 deletions packets/wtlogin/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func BuildKexExchangeRequest(uin uint32, guid string) ([]byte, error) {

p2Hash := crypto.SHA256Digest(
binary.NewBuilder(nil).
WriteBytes(ecdh.P256().PublicKey(), false).
WriteBytes(ecdh.P256().PublicKey()).
WriteU32(1).
WriteBytes(encl, false).
WriteBytes(encl).
WriteU32(0).
WriteU32(uint32(utils.TimeStamp())).
ToBytes(),
Expand Down
24 changes: 12 additions & 12 deletions packets/wtlogin/oicq.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ func BuildCode2dPacket(uin uint32, cmdID int, appInfo *info.AppInfo, body []byte
WriteU16(uint16(len(body))+53).
WriteU32(uint32(appInfo.AppID)).
WriteU32(0x72).
WriteBytes(make([]byte, 3), false).
WriteBytes(make([]byte, 3)).
WriteU32(uint32(utils.TimeStamp())).
WriteU8(2).
WriteU16(uint16(len(body)+49)).
WriteU16(uint16(cmdID)).
WriteBytes(make([]byte, 21), false).
WriteBytes(make([]byte, 21)).
WriteU8(3).
WriteU32(50).
WriteBytes(make([]byte, 14), false).
WriteBytes(make([]byte, 14)).
WriteU32(uint32(appInfo.AppID)).
WriteBytes(body, false).
WriteBytes(body).
ToBytes(),
)
}
Expand Down Expand Up @@ -69,18 +69,18 @@ func BuildLoginPacket(uin uint32, cmd string, appinfo *info.AppInfo, body []byte
WriteU32(0).
WriteU8(1).
WriteU8(1).
WriteBytes(make([]byte, 16), false).
WriteBytes(make([]byte, 16)).
WriteU16(0x102).
WriteU16(uint16(len(pk))).
WriteBytes(pk, false).
WriteBytes(encBody, false).
WriteBytes(pk).
WriteBytes(encBody).
WriteU8(3).
ToBytes()

frame := binary.NewBuilder(nil).
WriteU8(2).
WriteU16(uint16(len(frameBody))+3). // + 2 + 1
WriteBytes(frameBody, false).
WriteU16(uint16(len(frameBody)) + 3). // + 2 + 1
WriteBytes(frameBody).
ToBytes()

return frame
Expand All @@ -107,8 +107,8 @@ func BuildUniPacket(uin int, seq uint32, cmd string, sign map[string]string,
ssoHeader := binary.NewBuilder(nil).
WriteU32(uint32(seq)).
WriteU32(uint32(appInfo.SubAppID)).
WriteU32(2052). // locate id
WriteBytes(append([]byte{0x02}, make([]byte, 11)...), false). //020000000000000000000000
WriteU32(2052). // locate id
WriteBytes(append([]byte{0x02}, make([]byte, 11)...)). //020000000000000000000000
WritePacketBytes(sigInfo.Tgt, "u32", true).
WritePacketString(cmd, "u32", true).
WritePacketBytes(nil, "u32", true).
Expand Down Expand Up @@ -138,7 +138,7 @@ func BuildUniPacket(uin int, seq uint32, cmd string, sign map[string]string,
WritePacketBytes(sigInfo.D2, "u32", true).
WriteU8(0).
WritePacketString(strconv.Itoa(uin), "u32", true).
WriteBytes(encrypted, false).
WriteBytes(encrypted).
ToBytes()

return binary.NewBuilder(nil).WritePacketBytes(service, "u32", true).ToBytes()
Expand Down
17 changes: 10 additions & 7 deletions utils/binary/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,19 @@ func (b *Builder) ReadFrom(r io.Reader) (n int64, err error) {
return io.Copy(b.buffer, r)
}

func (b *Builder) WriteBytes(v []byte, withLength bool) *Builder {
if withLength {
b.WriteU16(uint16(len(v)))
}
func (b *Builder) WriteLenBytes(v []byte) *Builder {
b.WriteU16(uint16(len(v)))
b.append(v)
return b
}

func (b *Builder) WriteBytes(v []byte) *Builder {
b.append(v)
return b
}

func (b *Builder) WriteString(v string) *Builder {
return b.WriteBytes(utils.S2B(v), true)
func (b *Builder) WriteLenString(v string) *Builder {
return b.WriteLenBytes(utils.S2B(v))
}

func (b *Builder) WriteStruct(data ...any) *Builder {
Expand Down Expand Up @@ -195,7 +198,7 @@ func (b *Builder) WriteDouble(v float64) *Builder {
func (b *Builder) WriteTlv(tlvs ...[]byte) *Builder {
b.WriteU16(uint16(len(tlvs)))
for _, tlv := range tlvs {
b.WriteBytes(tlv, false)
b.WriteBytes(tlv)
}
return b
}
4 changes: 2 additions & 2 deletions utils/binary/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (

func TestBuilder(t *testing.T) {
r := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}
data := NewBuilder(nil).WriteBytes(r, true).ToBytes()
data := NewBuilder(nil).WriteLenBytes(r).ToBytes()
fmt.Printf("%x\n", data)
}

func build() []byte {
r := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}
return NewBuilder(nil).WriteBytes(r, true).WriteBytes(r, true).ToBytes()
return NewBuilder(nil).WriteLenBytes(r).WriteLenBytes(r).ToBytes()
}

func TestReader(t *testing.T) {
Expand Down
Loading