Skip to content

Commit

Permalink
highway: rename BdhInput and Input to Transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
wdvxdr1123 committed Mar 28, 2022
1 parent 9422ce4 commit 33590e4
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 25 deletions.
8 changes: 6 additions & 2 deletions client/group_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"bytes"
"crypto/md5"
"encoding/base64"
"encoding/json"
"math"
Expand Down Expand Up @@ -170,10 +171,13 @@ func (c *QQClient) uploadGroupLongMessage(groupCode int64, m *message.ForwardMes
}
for i, ip := range rsp.Uint32UpIp {
addr := highway.Addr{IP: uint32(ip), Port: int(rsp.Uint32UpPort[i])}
input := highway.Input{
hash := md5.Sum(body)
input := highway.Transaction{
CommandID: 27,
Key: rsp.MsgSig,
Ticket: rsp.MsgSig,
Body: bytes.NewReader(body),
Size: int64(len(body)),
Sum: hash[:],
}
err := c.highwaySession.Upload(addr, input)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions client/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (c *QQClient) uploadGroupOrGuildImage(target message.Source, img io.ReadSee

var r any
var err error
var input highway.BdhInput
var input highway.Transaction
switch target.SourceType {
case message.SourceGroup:
r, err = c.sendAndWait(c.buildGroupImageStorePacket(target.PrimaryID, fh, int32(length)))
Expand All @@ -115,7 +115,7 @@ func (c *QQClient) uploadGroupOrGuildImage(target message.Source, img io.ReadSee
}
}

input = highway.BdhInput{
input = highway.Transaction{
CommandID: cmd,
Body: img,
Size: length,
Expand Down Expand Up @@ -322,7 +322,7 @@ func (c *QQClient) uploadOcrImage(img io.Reader, size int32, sum []byte) (string
Uuid: binary.GenUUID(r),
})

rsp, err := c.highwaySession.UploadBDH(highway.BdhInput{
rsp, err := c.highwaySession.UploadBDH(highway.Transaction{
CommandID: 76,
Body: img,
Size: int64(size),
Expand Down
8 changes: 4 additions & 4 deletions client/internal/highway/bdh.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/Mrs4s/MiraiGo/internal/proto"
)

type BdhInput struct {
type Transaction struct {
CommandID int32
Body io.Reader
Sum []byte // md5 sum of body
Expand All @@ -26,7 +26,7 @@ type BdhInput struct {
Encrypt bool
}

func (bdh *BdhInput) encrypt(key []byte) error {
func (bdh *Transaction) encrypt(key []byte) error {
if bdh.Encrypt {
if len(key) == 0 {
return errors.New("session key not found. maybe miss some packet?")
Expand All @@ -36,7 +36,7 @@ func (bdh *BdhInput) encrypt(key []byte) error {
return nil
}

func (s *Session) UploadBDH(input BdhInput) ([]byte, error) {
func (s *Session) UploadBDH(input Transaction) ([]byte, error) {
if len(s.SsoAddr) == 0 {
return nil, errors.New("srv addrs not found. maybe miss some packet?")
}
Expand Down Expand Up @@ -108,7 +108,7 @@ func (s *Session) UploadBDH(input BdhInput) ([]byte, error) {
return rspExt, nil
}

func (s *Session) UploadBDHMultiThread(input BdhInput, threadCount int) ([]byte, error) {
func (s *Session) UploadBDHMultiThread(input Transaction, threadCount int) ([]byte, error) {
// for small file and small thread count,
// use UploadBDH instead of UploadBDHMultiThread
if input.Size < 1024*1024*3 || threadCount < 2 {
Expand Down
16 changes: 4 additions & 12 deletions client/internal/highway/highway.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,7 @@ func (s *Session) AppendAddr(ip, port uint32) {
s.SsoAddr = append(s.SsoAddr, addr)
}

type Input struct {
CommandID int32
Key []byte
Body io.ReadSeeker
}

func (s *Session) Upload(addr Addr, input Input) error {
fh, length := utils.ComputeMd5AndLength(input.Body)
_, _ = input.Body.Seek(0, io.SeekStart)
func (s *Session) Upload(addr Addr, input Transaction) error {
conn, err := net.DialTimeout("tcp", addr.String(), time.Second*3)
if err != nil {
return errors.Wrap(err, "connect error")
Expand All @@ -79,12 +71,12 @@ func (s *Session) Upload(addr Addr, input Input) error {
head, _ := proto.Marshal(&pb.ReqDataHighwayHead{
MsgBasehead: s.dataHighwayHead(_REQ_CMD_DATA, 4096, input.CommandID, 2052),
MsgSeghead: &pb.SegHead{
Filesize: length,
Filesize: input.Size,
Dataoffset: int64(offset),
Datalength: int32(rl),
Serviceticket: input.Key,
Serviceticket: input.Ticket,
Md5: ch[:],
FileMd5: fh,
FileMd5: input.Sum,
},
ReqExtendinfo: []byte{},
})
Expand Down
7 changes: 5 additions & 2 deletions client/multimsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,13 @@ func (builder *ForwardMessageBuilder) Main(m *message.ForwardMessage) *message.F
content := forwardDisplay(rsp.MsgResid, filename, m.Preview(), fmt.Sprintf("查看 %d 条转发消息", m.Length()))
for i, ip := range rsp.Uint32UpIp {
addr := highway.Addr{IP: uint32(ip), Port: int(rsp.Uint32UpPort[i])}
input := highway.Input{
hash := md5.Sum(body)
input := highway.Transaction{
CommandID: 27,
Key: rsp.MsgSig,
Ticket: rsp.MsgSig,
Body: bytes.NewReader(body),
Sum: hash[:],
Size: int64(len(body)),
}
err := c.highwaySession.Upload(addr, input)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions client/ptt.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (c *QQClient) UploadVoice(target message.Source, voice io.ReadSeeker) (*mes
ext = c.buildGroupPttStoreBDHExt(target.PrimaryID, fh, int32(length), 0, int32(length))
}
// multi-thread upload is no need
rsp, err := c.highwaySession.UploadBDH(highway.BdhInput{
rsp, err := c.highwaySession.UploadBDH(highway.Transaction{
CommandID: cmd,
Body: voice,
Sum: fh,
Expand Down Expand Up @@ -157,7 +157,7 @@ func (c *QQClient) UploadShortVideo(target message.Source, video, thumb io.ReadS
}
ext, _ := proto.Marshal(c.buildPttShortVideoProto(target, videoSum, thumbSum, videoLen, thumbLen).PttShortVideoUploadReq)
combined := utils.MultiReadSeeker(thumb, video)
input := highway.BdhInput{
input := highway.Transaction{
CommandID: cmd,
Body: combined,
Size: videoLen + thumbLen,
Expand Down

0 comments on commit 33590e4

Please sign in to comment.