From aa6ed0fc3488451f1c9155f8e624bc52142fbe73 Mon Sep 17 00:00:00 2001 From: Lin <767763591@qq.com> Date: Fri, 22 Jan 2021 00:14:01 +0800 Subject: [PATCH] SpeedUP Rand --- .github/workflows/go.yml | 22 +++++++------- README.md | 4 +-- binary/tea.go | 4 ++- binary/tea_test.go | 52 ++++++++++++++++++++++++++++++++++ client/builders.go | 7 +++-- client/client.go | 16 +++++------ client/global.go | 8 +++--- client/group_msg.go | 6 ++-- client/pb/oidb/oidb0xb77.proto | 2 +- client/richmsg.go | 2 +- go.mod | 1 + go.sum | 2 ++ protocol/tlv/t1.go | 2 +- protocol/tlv/t106.go | 2 +- 14 files changed, 94 insertions(+), 36 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index c7b0db0f..a51b4496 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -13,20 +13,20 @@ jobs: runs-on: ubuntu-latest steps: - - name: Set up Go 1.x - uses: actions/setup-go@v2 - with: - go-version: ^1.13 + - name: Set up Go 1.x + uses: actions/setup-go@v2 + with: + go-version: ^1.13 - - name: Check out code into the Go module directory - uses: actions/checkout@v2 + - name: Check out code into the Go module directory + uses: actions/checkout@v2 - - name: Get dependencies - run: | - go get -v -t -d ./... + - name: Get dependencies + run: | + go get -v -t -d ./... - - name: Build - run: go build -v ./... + - name: Build + run: go build -v ./... # - name: Test # run: go test -v . diff --git a/README.md b/README.md index f87f4b14..1e21e992 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ qq-android协议的golang实现 移植于mirai - [x] 登录号加群 - [x] 登录号退群(包含T出) - [x] 新成员进群/退群 -- [x] 群/好友消息撤回 +- [x] 群/好友消息撤回 - [x] 群禁言 - [x] 群成员权限变更 - [x] 收到邀请进群通知 @@ -44,7 +44,7 @@ qq-android协议的golang实现 移植于mirai - [x] 新好友 - [x] 新好友请求 - [x] 客户端离线 -- [x] 群提示 (戳一戳/运气王等) +- [x] 群提示 (戳一戳/运气王等) #### 主动操作 > 为防止滥用,将不支持主动邀请新成员进群 diff --git a/binary/tea.go b/binary/tea.go index 005f4ca1..70b7cfaa 100644 --- a/binary/tea.go +++ b/binary/tea.go @@ -2,9 +2,11 @@ package binary import ( "encoding/binary" - "math/rand" + //"math/rand" "reflect" "unsafe" + + rand "github.com/LXY1226/fastrand" ) func xorQ(a, b []byte, c []byte) { // MAGIC diff --git a/binary/tea_test.go b/binary/tea_test.go index 8bf4a07b..3f7b8a99 100644 --- a/binary/tea_test.go +++ b/binary/tea_test.go @@ -99,6 +99,58 @@ func BenchmarkTEAde16(b *testing.B) { } } +func BenchmarkTEAen200(b *testing.B) { + data := make([]byte, 200) + _, err := rand.Read(data) + if err != nil { + panic(err) + } + b.ResetTimer() + for i := 0; i < b.N; i++ { + testTEA.Encrypt(data) + } +} + +func BenchmarkTEAde200(b *testing.B) { + data := make([]byte, 200) + _, err := rand.Read(data) + if err != nil { + panic(err) + } + data = testTEA.Encrypt(data) + b.ResetTimer() + for i := 0; i < b.N; i++ { + testTEA.Decrypt(data) + } +} + +func BenchmarkTEAen200M(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + data := make([]byte, 200) + _, err := rand.Read(data) + if err != nil { + panic(err) + } + for pb.Next() { + testTEA.Encrypt(data) + } + }) +} + +func BenchmarkTEAde200M(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + data := make([]byte, 200) + _, err := rand.Read(data) + if err != nil { + panic(err) + } + data = testTEA.Encrypt(data) + for pb.Next() { + testTEA.Decrypt(data) + } + }) +} + func BenchmarkTEAen256(b *testing.B) { data := make([]byte, 256) _, err := rand.Read(data) diff --git a/client/builders.go b/client/builders.go index b8595112..fc9f68b7 100644 --- a/client/builders.go +++ b/client/builders.go @@ -4,11 +4,10 @@ import ( "crypto/md5" "encoding/hex" "fmt" - "github.com/Mrs4s/MiraiGo/client/pb/profilecard" - "github.com/Mrs4s/MiraiGo/client/pb/qweb" - "math/rand" "time" + rand "github.com/LXY1226/fastrand" + "github.com/golang/protobuf/proto" "github.com/Mrs4s/MiraiGo/binary" @@ -17,6 +16,8 @@ import ( "github.com/Mrs4s/MiraiGo/client/pb/cmd0x352" "github.com/Mrs4s/MiraiGo/client/pb/msg" "github.com/Mrs4s/MiraiGo/client/pb/oidb" + "github.com/Mrs4s/MiraiGo/client/pb/profilecard" + "github.com/Mrs4s/MiraiGo/client/pb/qweb" "github.com/Mrs4s/MiraiGo/client/pb/structmsg" "github.com/Mrs4s/MiraiGo/message" "github.com/Mrs4s/MiraiGo/protocol/crypto" diff --git a/client/client.go b/client/client.go index 28f654fb..f0ee2044 100644 --- a/client/client.go +++ b/client/client.go @@ -4,11 +4,11 @@ import ( "bytes" "crypto/md5" "fmt" + rand "github.com/LXY1226/fastrand" "github.com/Mrs4s/MiraiGo/binary/jce" jsoniter "github.com/json-iterator/go" "io" "math" - "math/rand" "net" "runtime/debug" "sort" @@ -145,9 +145,9 @@ var decoders = map[string]func(*QQClient, uint16, []byte) (interface{}, error){ "PttCenterSvr.pb_pttCenter_CMD_REQ_APPLY_UPLOAD-500": decodePrivatePttStoreResponse, } -func init() { - rand.Seed(time.Now().UTC().UnixNano()) -} +//func init() { +// rand.Seed(time.Now().UTC().UnixNano()) +//} // NewClient create new qq client func NewClient(uin int64, password string) *QQClient { @@ -163,7 +163,7 @@ func NewClientMd5(uin int64, passwordMd5 [16]byte) *QQClient { OutGoingPacketSessionId: []byte{0x02, 0xB0, 0x5B, 0x8B}, sigInfo: &loginSigInfo{}, requestPacketRequestId: 1921334513, - groupSeq: int32(rand.Intn(20000)), + groupSeq: rand.Int31n(20000), friendSeq: 22911, highwayApplyUpSeq: 77918, ksid: []byte(fmt.Sprintf("|%s|A8.2.7.27f6ea96", SystemDeviceInfo.IMEI)), @@ -427,7 +427,7 @@ func (c *QQClient) GetFriendList() (*FriendListResponse, error) { } func (c *QQClient) SendPrivateMessage(target int64, m *message.SendingMessage) *message.PrivateMessage { - mr := int32(rand.Uint32()) + mr := rand.Int31() seq := c.nextFriendSeq() t := time.Now().Unix() imgCount := m.Count(func(e message.IMessageElement) bool { return e.Type() == message.Image }) @@ -436,7 +436,7 @@ func (c *QQClient) SendPrivateMessage(target int64, m *message.SendingMessage) * return nil } if msgLen > 300 || imgCount > 2 { - div := int32(rand.Uint32()) + div := rand.Int31() fragmented := m.ToFragmented() for i, elems := range fragmented { _, pkt := c.buildFriendSendingPacket(target, c.nextFriendSeq(), mr, int32(len(fragmented)), int32(i), div, t, elems) @@ -476,7 +476,7 @@ func (c *QQClient) SendTempMessage(groupCode, target int64, m *message.SendingMe Elements: m.Elements, } } - mr := int32(rand.Uint32()) + mr := rand.Int31() seq := c.nextFriendSeq() t := time.Now().Unix() _, pkt := c.buildTempSendingPacket(group.Uin, target, seq, mr, t, m) diff --git a/client/global.go b/client/global.go index e994ed66..73fb21ee 100644 --- a/client/global.go +++ b/client/global.go @@ -7,13 +7,13 @@ import ( "encoding/hex" "encoding/xml" "fmt" - "math/rand" "net" "sort" "strconv" "strings" "time" + rand "github.com/LXY1226/fastrand" "github.com/Mrs4s/MiraiGo/binary" "github.com/Mrs4s/MiraiGo/binary/jce" devinfo "github.com/Mrs4s/MiraiGo/client/pb" @@ -368,11 +368,11 @@ func GenIMEI() string { sum := 0 // the control sum of digits var final strings.Builder - randSrc := rand.NewSource(time.Now().UnixNano()) - randGen := rand.New(randSrc) + //randSrc := rand.NewSource(time.Now().UnixNano()) + //randGen := rand.New(randSrc) for i := 0; i < 14; i++ { // generating all the base digits - toAdd := randGen.Intn(10) + toAdd := rand.Intn(10) if (i+1)%2 == 0 { // special proc for every 2nd one toAdd *= 2 if toAdd >= 10 { diff --git a/client/group_msg.go b/client/group_msg.go index 15f96d8e..330e86a9 100644 --- a/client/group_msg.go +++ b/client/group_msg.go @@ -2,6 +2,7 @@ package client import ( "fmt" + rand "github.com/LXY1226/fastrand" "github.com/Mrs4s/MiraiGo/client/pb/longmsg" "github.com/Mrs4s/MiraiGo/client/pb/msg" "github.com/Mrs4s/MiraiGo/client/pb/multimsg" @@ -12,7 +13,6 @@ import ( "github.com/pkg/errors" "google.golang.org/protobuf/proto" "math" - "math/rand" "time" ) @@ -85,7 +85,7 @@ func (c *QQClient) GetAtAllRemain(groupCode int64) (*AtAllRemainInfo, error) { func (c *QQClient) sendGroupMessage(groupCode int64, forward bool, m *message.SendingMessage) *message.GroupMessage { eid := utils.RandomString(6) - mr := int32(rand.Uint32()) + mr := rand.Int31() ch := make(chan int32) c.onGroupMessageReceipt(eid, func(c *QQClient, e *groupMessageReceiptEvent) { if e.Rand == mr && !utils.IsChanClosed(ch) { @@ -105,7 +105,7 @@ func (c *QQClient) sendGroupMessage(groupCode int64, forward bool, m *message.Se } return ok || ok2 || ok3 }) { - div := int32(rand.Uint32()) + div := rand.Int31() fragmented := m.ToFragmented() for i, elems := range fragmented { _, pkt := c.buildGroupSendingPacket(groupCode, mr, int32(len(fragmented)), int32(i), div, forward, elems) diff --git a/client/pb/oidb/oidb0xb77.proto b/client/pb/oidb/oidb0xb77.proto index 240a8422..8ea654d3 100644 --- a/client/pb/oidb/oidb0xb77.proto +++ b/client/pb/oidb/oidb0xb77.proto @@ -34,7 +34,7 @@ message DB77ExtInfo { message DB77RichMsgBody { string title = 10; - string summary = 11; + string summary = 11; string brief = 12; string url = 13; string pictureUrl = 14; diff --git a/client/richmsg.go b/client/richmsg.go index 2a56a92e..2b266b39 100644 --- a/client/richmsg.go +++ b/client/richmsg.go @@ -1,9 +1,9 @@ package client import ( - "math/rand" "time" + rand "github.com/LXY1226/fastrand" "github.com/Mrs4s/MiraiGo/client/pb/oidb" "github.com/Mrs4s/MiraiGo/message" "github.com/Mrs4s/MiraiGo/protocol/packets" diff --git a/go.mod b/go.mod index 54a080cb..c02e66ab 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/Mrs4s/MiraiGo go 1.15 require ( + github.com/LXY1226/fastrand v0.0.0-20210121160840-7a3db3e79031 github.com/golang/protobuf v1.4.3 github.com/json-iterator/go v1.1.10 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index 0f710ceb..34336636 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/LXY1226/fastrand v0.0.0-20210121160840-7a3db3e79031 h1:DnoCySrXUFvtngW2kSkuBeZoPfvOgctJXjTulCn7eV0= +github.com/LXY1226/fastrand v0.0.0-20210121160840-7a3db3e79031/go.mod h1:mEFi4jHUsE2sqQGSJ7eQfXnO8esMzEYcftiCGG+L/OE= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/protocol/tlv/t1.go b/protocol/tlv/t1.go index 3e8875d2..4af317b7 100644 --- a/protocol/tlv/t1.go +++ b/protocol/tlv/t1.go @@ -1,8 +1,8 @@ package tlv import ( + rand "github.com/LXY1226/fastrand" "github.com/Mrs4s/MiraiGo/binary" - "math/rand" "time" ) diff --git a/protocol/tlv/t106.go b/protocol/tlv/t106.go index 699f166b..2383d316 100644 --- a/protocol/tlv/t106.go +++ b/protocol/tlv/t106.go @@ -3,8 +3,8 @@ package tlv import ( "crypto/md5" binary2 "encoding/binary" + rand "github.com/LXY1226/fastrand" "github.com/Mrs4s/MiraiGo/binary" - "math/rand" "strconv" "time" )