diff --git a/client/internal/highway/addr.go b/client/internal/highway/addr.go deleted file mode 100644 index b1031a20..00000000 --- a/client/internal/highway/addr.go +++ /dev/null @@ -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) -} diff --git a/client/internal/highway/bdh.go b/client/internal/highway/bdh.go index 58161f1a..ac688491 100644 --- a/client/internal/highway/bdh.go +++ b/client/internal/highway/bdh.go @@ -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) diff --git a/client/internal/highway/highway.go b/client/internal/highway/highway.go index 96a36feb..cfa363ed 100644 --- a/client/internal/highway/highway.go +++ b/client/internal/highway/highway.go @@ -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) } diff --git a/client/multimsg.go b/client/multimsg.go index c4de9233..eba7270e 100644 --- a/client/multimsg.go +++ b/client/multimsg.go @@ -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) - if err != nil { - continue - } - return &message.ForwardElement{ - FileName: filename, - Content: content, - ResId: rsp.MsgResid, - } + _, err = c.highwaySession.UploadBDH(input) + if err != nil { + return nil + } + return &message.ForwardElement{ + FileName: filename, + Content: content, + ResId: rsp.MsgResid, } - return nil }