1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 11:07:40 +08:00

client: use UploadBDH for uploading forward message

This commit is contained in:
wdvxdr 2023-02-16 21:31:04 +08:00
parent ed15fbca26
commit f630205782
4 changed files with 23 additions and 90 deletions

View File

@ -1,21 +0,0 @@
package highway
import (
"fmt"
"net"
"github.com/Mrs4s/MiraiGo/binary"
)
type Addr struct {
IP uint32
Port int
}
func (a Addr) AsNetIP() net.IP {
return net.IPv4(byte(a.IP>>24), byte(a.IP>>16), byte(a.IP>>8), byte(a.IP))
}
func (a Addr) String() string {
return fmt.Sprintf("%v:%v", binary.UInt32ToIPV4Address(a.IP), a.Port)
}

View File

@ -74,13 +74,9 @@ func uploadBDH(s *Session, addr Addr, trans *Transaction) ([]byte, error) {
}
const chunkSize = 256 * 1024
var rspExt, chunk []byte
var rspExt []byte
offset := 0
if trans.Size > chunkSize {
chunk = make([]byte, chunkSize)
} else {
chunk = make([]byte, trans.Size)
}
chunk := make([]byte, chunkSize)
for {
chunk = chunk[:cap(chunk)]
rl, err := io.ReadFull(trans.Body, chunk)

View File

@ -7,7 +7,6 @@ import (
"net"
"net/http"
"sync/atomic"
"time"
"github.com/pkg/errors"
@ -22,6 +21,19 @@ const (
_REQ_CMD_HEART_BREAK = "PicUp.Echo"
)
type Addr struct {
IP uint32
Port int
}
func (a Addr) AsNetIP() net.IP {
return net.IPv4(byte(a.IP>>24), byte(a.IP>>16), byte(a.IP>>8), byte(a.IP))
}
func (a Addr) String() string {
return fmt.Sprintf("%v:%v", binary.UInt32ToIPV4Address(a.IP), a.Port)
}
type Session struct {
Uin string
AppID int32
@ -46,56 +58,6 @@ func (s *Session) AppendAddr(ip, port uint32) {
s.SsoAddr = append(s.SsoAddr, addr)
}
func (s *Session) Upload(addr Addr, trans Transaction) error {
conn, err := net.DialTimeout("tcp", addr.String(), time.Second*3)
if err != nil {
return errors.Wrap(err, "connect error")
}
defer conn.Close()
const chunkSize = 8192 * 8
chunk := make([]byte, chunkSize)
offset := 0
reader := binary.NewNetworkReader(conn)
for {
chunk = chunk[:chunkSize]
rl, err := io.ReadFull(trans.Body, chunk)
if errors.Is(err, io.EOF) {
break
}
if errors.Is(err, io.ErrUnexpectedEOF) {
chunk = chunk[:rl]
}
ch := md5.Sum(chunk)
head, _ := proto.Marshal(&pb.ReqDataHighwayHead{
MsgBasehead: s.dataHighwayHead(_REQ_CMD_DATA, 4096, trans.CommandID, 2052),
MsgSeghead: &pb.SegHead{
Filesize: trans.Size,
Dataoffset: int64(offset),
Datalength: int32(rl),
Serviceticket: trans.Ticket,
Md5: ch[:],
FileMd5: trans.Sum,
},
ReqExtendinfo: []byte{},
})
offset += rl
frame := newFrame(head, chunk)
_, err = frame.WriteTo(conn)
if err != nil {
return errors.Wrap(err, "write conn error")
}
rspHead, err := readResponse(reader)
if err != nil {
return errors.Wrap(err, "highway upload error")
}
if rspHead.ErrorCode != 0 {
return errors.New("upload failed")
}
}
return nil
}
func (s *Session) UploadExciting(trans Transaction) ([]byte, error) {
return s.retry(uploadExciting, &trans)
}

View File

@ -313,17 +313,13 @@ func (builder *ForwardMessageBuilder) Main(m *message.ForwardMessage) *message.F
Sum: bodyHash[:],
Size: int64(len(body)),
}
for i, ip := range rsp.Uint32UpIp {
addr := highway.Addr{IP: uint32(ip), Port: int(rsp.Uint32UpPort[i])}
err := c.highwaySession.Upload(addr, input)
_, err = c.highwaySession.UploadBDH(input)
if err != nil {
continue
return nil
}
return &message.ForwardElement{
FileName: filename,
Content: content,
ResId: rsp.MsgResid,
}
}
return nil
}