Skip to content

Commit

Permalink
Merge pull request #32 from fumiama/binary
Browse files Browse the repository at this point in the history
optimize: packet builder & string/bytes I/O process
  • Loading branch information
Redmomn authored May 7, 2024
2 parents eae0efe + df1b21d commit bdec9a4
Show file tree
Hide file tree
Showing 24 changed files with 248 additions and 339 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test_and_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: '1.19'
go-version: '1.20'
cache: false
- run: go get
- run: go generate ./...
Expand All @@ -34,7 +34,7 @@ jobs:
uses: actions/setup-go@v5
with:
cache: false
go-version: '1.19'
go-version: '1.20'
- run: go get
- run: go generate ./...
- name: Go vet
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,6 @@ fabric.properties
# Go workspace file
go.work

# Running stub
sig.bin
qrcode.png
31 changes: 16 additions & 15 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/LagrangeDev/LagrangeGo/packets/tlv"

"github.com/LagrangeDev/LagrangeGo/utils/binary"
binary2 "github.com/LagrangeDev/LagrangeGo/utils/binary"

"github.com/LagrangeDev/LagrangeGo/utils"
Expand Down Expand Up @@ -81,11 +82,11 @@ func (c *QQClient) Login(password, qrcodePath string) (bool, error) {
}

func (c *QQClient) FecthQrcode() ([]byte, string, error) {
body := utils.NewPacketBuilder(nil).
body := binary2.NewBuilder(nil).
WriteU16(0).
WriteU64(0).
WriteU8(0).
WriteTlv([][]byte{
WritePacketTlv(
tlv.T16(c.appInfo.AppID, c.appInfo.SubAppID,
utils.MustParseHexStr(c.deviceInfo.Guid), c.appInfo.PTVersion, c.appInfo.PackageName),
tlv.T1b(),
Expand All @@ -94,7 +95,7 @@ func (c *QQClient) FecthQrcode() ([]byte, string, error) {
tlv.T35(c.appInfo.PTOSVersion),
tlv.T66(c.appInfo.PTOSVersion),
tlv.Td1(c.appInfo.OS, c.deviceInfo.DeviceName),
}).WriteU8(3).Pack(-1)
).WriteU8(3).Pack(binary.PackTypeNone)

packet := wtlogin.BuildCode2dPacket(c.Uin, 0x31, c.appInfo, body)
response, err := c.SendUniPacketAndAwait("wtlogin.trans_emp", packet)
Expand All @@ -114,7 +115,7 @@ func (c *QQClient) FecthQrcode() ([]byte, string, error) {
// 这样是不对的,调试后发现应该丢一个字节,然后读下一个字节才是数据的大小
// string(binary2.NewReader(tlvs[209]).ReadBytesWithLength("u16", true))
urlreader.ReadU8()
return tlvs[0x17], string(urlreader.ReadBytesWithLength("u8", false)), nil
return tlvs[0x17], utils.B2S(urlreader.ReadBytesWithLength("u8", false)), nil
}

return nil, "", fmt.Errorf("err qr retcode %d", retCode)
Expand All @@ -126,12 +127,12 @@ func (c *QQClient) GetQrcodeResult() (qrcodeState.State, error) {
return -1, errors.New("no qrsig found, execute fetch_qrcode first")
}

body := utils.NewPacketBuilder(nil).
WriteBytes(c.sig.Qrsig, "u16", false).
body := binary2.NewBuilder(nil).
WritePacketBytes(c.sig.Qrsig, "u16", false).
WriteU64(0).
WriteU32(0).
WriteU8(0).
WriteU8(0x83).Pack(-1)
WriteU8(0x83).Pack(binary.PackTypeNone)

response, err := c.SendUniPacketAndAwait("wtlogin.trans_emp",
wtlogin.BuildCode2dPacket(0, 0x12, c.appInfo, body))
Expand Down Expand Up @@ -172,7 +173,7 @@ func (c *QQClient) KeyExchange() {
}

func (c *QQClient) PasswordLogin(password string) (loginState.State, error) {
md5Password := utils.MD5Digest([]byte(password))
md5Password := utils.MD5Digest(utils.S2B(password))

cr := tlv.T106(
c.appInfo.AppID,
Expand Down Expand Up @@ -226,25 +227,25 @@ func (c *QQClient) QrcodeLogin(refreshInterval int) (bool, error) {

app := c.appInfo
device := c.deviceInfo
body := utils.NewPacketBuilder(nil).
body := binary2.NewBuilder(nil).
WriteU16(0x09).
WriteTlv([][]byte{
utils.NewPacketBuilder(nil).WriteBytes(c.t106, "", true).Pack(0x106),
WritePacketTlv(
binary2.NewBuilder(nil).WritePacketBytes(c.t106, "", true).Pack(0x106),
tlv.T144(c.sig.Tgtgt, app, device),
tlv.T116(app.SubSigmap),
tlv.T142(app.PackageName, 0),
tlv.T145(utils.MustParseHexStr(device.Guid)),
tlv.T18(0, app.AppClientVersion, int(c.Uin), 0, 5, 0),
tlv.T141([]byte("Unknown"), make([]byte, 0)),
tlv.T141([]byte("Unknown"), nil),
tlv.T177(app.WTLoginSDK, 0),
tlv.T191(0),
tlv.T100(5, app.AppID, app.SubAppID, 8001, app.MainSigmap, 0),
tlv.T107(1, 0x0d, 0, 1),
tlv.T318(make([]byte, 0)),
utils.NewPacketBuilder(nil).WriteBytes(c.t16a, "", true).Pack(0x16a),
tlv.T318(nil),
binary2.NewBuilder(nil).WritePacketBytes(c.t16a, "", true).Pack(0x16a),
tlv.T166(5),
tlv.T521(0x13, "basicim"),
}).Pack(-1)
).Pack(binary.PackTypeNone)

response, err := c.SendUniPacketAndAwait(
"wtlogin.login",
Expand Down
3 changes: 2 additions & 1 deletion client/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
msgConverter "github.com/LagrangeDev/LagrangeGo/message"
"github.com/LagrangeDev/LagrangeGo/packets/pb/message"
"github.com/LagrangeDev/LagrangeGo/packets/wtlogin"
"github.com/LagrangeDev/LagrangeGo/utils"
"github.com/LagrangeDev/LagrangeGo/utils/binary"
)

Expand Down Expand Up @@ -63,7 +64,7 @@ func decodeOlPushServicePacket(c *QQClient, pkt *wtlogin.SSOPacket) (any, error)
if err != nil {
return nil, err
}
pb.Operator = []byte(Operator.OperatorField1.OperatorUid)
pb.Operator = utils.S2B(Operator.OperatorField1.OperatorUid)
}
return eventConverter.ParseMemberDecreaseEvent(&pb), nil
case 84: // group request join notice
Expand Down
5 changes: 3 additions & 2 deletions event/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package event

import (
"github.com/LagrangeDev/LagrangeGo/packets/pb/message"
"github.com/LagrangeDev/LagrangeGo/utils"
)

type (
Expand Down Expand Up @@ -96,7 +97,7 @@ func ParseMemberIncreaseEvent(event *message.GroupChange) *GroupMemberIncrease {
GroupUin: event.GroupUin,
},
MemberUid: event.MemberUid,
InvitorUid: string(event.Operator),
InvitorUid: utils.B2S(event.Operator),
JoinType: event.IncreaseType,
}
}
Expand All @@ -107,7 +108,7 @@ func ParseMemberDecreaseEvent(event *message.GroupChange) *GroupMemberDecrease {
GroupUin: event.GroupUin,
},
MemberUid: event.MemberUid,
OperatorUid: string(event.Operator),
OperatorUid: utils.B2S(event.Operator),
ExitType: event.DecreaseType,
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/LagrangeDev/LagrangeGo

go 1.19
go 1.20

require (
github.com/RomiChan/protobuf v0.1.1-0.20230204044148-2ed269a2e54d
Expand Down
4 changes: 2 additions & 2 deletions info/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type DeviceInfo struct {

func NewDeviceInfo(uin int) *DeviceInfo {
return &DeviceInfo{
Guid: fmt.Sprintf("%X", utils.MD5Digest([]byte(strconv.Itoa(uin)))),
DeviceName: fmt.Sprintf("Lagrange-%X", utils.MD5Digest([]byte(strconv.Itoa(uin)))[0:4]),
Guid: fmt.Sprintf("%X", utils.MD5Digest(utils.S2B(strconv.Itoa(uin)))),
DeviceName: fmt.Sprintf("Lagrange-%X", utils.MD5Digest(utils.S2B(strconv.Itoa(uin)))[0:4]),
SystemKernel: fmt.Sprintf("%s %s", platform.GetSystem(), platform.GetVersion()),
KernelVersion: platform.GetVersion(),
}
Expand Down
4 changes: 2 additions & 2 deletions info/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ func Encode(sig *SigInfo) []byte {
return binary.NewBuilder(nil).
WriteBytes(dataHash, true).
WriteBytes(buffer.Bytes(), true).
Pack(-1)
Pack(binary.PackTypeNone)
}

func Decode(buf []byte, verify bool) *SigInfo {
reader := binary.NewReader(buf)
dataHash := reader.ReadBytesWithLength("u16", false)
data := reader.ReadBytesWithLength("u16", false)

if verify && string(dataHash) != string(utils.MD5Digest(data)) {
if verify && !bytes.Equal(dataHash, utils.MD5Digest(data)) {
panic("Data hash does not match")
}
buffer := bytes.NewBuffer(data)
Expand Down
15 changes: 2 additions & 13 deletions info/sig.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,7 @@ type SigInfo struct {

func NewSigInfo(seq int) *SigInfo {
return &SigInfo{
Uid: "",
Sequence: seq,
Tgtgt: make([]byte, 0),
Tgt: make([]byte, 0),
D2: make([]byte, 0),
D2Key: make([]byte, 16),
Qrsig: make([]byte, 0),
ExchangeKey: make([]byte, 0),
KeySig: make([]byte, 0),
Cookies: "",
UnusualSig: make([]byte, 0),
TempPwd: make([]byte, 0),
CaptchaInfo: [3]string{"", "", ""},
Sequence: seq,
D2Key: make([]byte, 16),
}
}
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package main

import (
"fmt"
"github.com/LagrangeDev/LagrangeGo/message"
"os"

"github.com/LagrangeDev/LagrangeGo/client"
"github.com/LagrangeDev/LagrangeGo/info"
"github.com/LagrangeDev/LagrangeGo/message"
)

func main() {
Expand All @@ -23,7 +23,7 @@ func main() {

qqclient.GroupMessageEvent.Subscribe(func(client *client.QQClient, event *message.GroupMessage) {
if event.ToString() == "114514" {
img, _ := os.ReadFile("/Users/wenxuanlin/Desktop/2373259535.png")
img, _ := os.ReadFile("./testgroup.png")
_, err := client.SendGroupMessage(event.GroupCode, []message.IMessageElement{&message.GroupImageElement{Stream: img}})
if err != nil {
return
Expand All @@ -32,7 +32,7 @@ func main() {
})

qqclient.PrivateMessageEvent.Subscribe(func(client *client.QQClient, event *message.PrivateMessage) {
img, _ := os.ReadFile("/Users/wenxuanlin/Desktop/2373259535.png")
img, _ := os.ReadFile("testprivate.png")
_, err := client.SendPrivateMessage(event.Sender.Uin, []message.IMessageElement{&message.FriendImageElement{Stream: img}})
if err != nil {
return
Expand Down
3 changes: 2 additions & 1 deletion message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/LagrangeDev/LagrangeGo/packets/pb/message"
"github.com/LagrangeDev/LagrangeGo/utils"
"github.com/LagrangeDev/LagrangeGo/utils/binary"
"github.com/LagrangeDev/LagrangeGo/utils/proto"
)
Expand Down Expand Up @@ -195,7 +196,7 @@ func parseMessageElements(msg []*message.Elem) []IMessageElement {
return []IMessageElement{
&ShortVideoElement{
Name: elem.VideoFile.FileName,
Uuid: []byte(elem.VideoFile.FileUuid),
Uuid: utils.S2B(elem.VideoFile.FileUuid),
Size: elem.VideoFile.FileSize,
ThumbSize: elem.VideoFile.ThumbFileSize,
Md5: elem.VideoFile.FileMd5,
Expand Down
Loading

0 comments on commit bdec9a4

Please sign in to comment.