mirror of
https://github.com/Mrs4s/MiraiGo.git
synced 2025-05-04 19:17:38 +08:00
Merge branch 'master' of github.com:/Mrs4s/MiraiGo
This commit is contained in:
commit
64c8b0583b
@ -90,38 +90,3 @@ func releaseZlibWriter(w *zlibWriter) {
|
|||||||
zlibPool.Put(w)
|
zlibPool.Put(w)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const size128k = 128 * 1024
|
|
||||||
|
|
||||||
var b128kPool = sync.Pool{
|
|
||||||
New: func() interface{} {
|
|
||||||
return make128kSlicePointer()
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get128KBytes 获取一个128k大小 []byte
|
|
||||||
func Get128KBytes() *[]byte {
|
|
||||||
buf := b128kPool.Get().(*[]byte)
|
|
||||||
if buf == nil {
|
|
||||||
return make128kSlicePointer()
|
|
||||||
}
|
|
||||||
if cap(*buf) < size128k {
|
|
||||||
return make128kSlicePointer()
|
|
||||||
}
|
|
||||||
*buf = (*buf)[:size128k]
|
|
||||||
return buf
|
|
||||||
}
|
|
||||||
|
|
||||||
// Put128KBytes 放回一个128k大小 []byte
|
|
||||||
func Put128KBytes(b *[]byte) {
|
|
||||||
if cap(*b) < size128k || cap(*b) > 2*size128k { // 太大或太小的 []byte 不要放入
|
|
||||||
return
|
|
||||||
}
|
|
||||||
*b = (*b)[:cap(*b)]
|
|
||||||
b128kPool.Put(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func make128kSlicePointer() *[]byte {
|
|
||||||
data := make([]byte, size128k)
|
|
||||||
return &data
|
|
||||||
}
|
|
||||||
|
@ -3,14 +3,12 @@ package binary
|
|||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"reflect"
|
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
func xorQ(a, b []byte, c []byte) { // MAGIC
|
func xorQ(a, b []byte, c []byte) { // MAGIC
|
||||||
*(*uint64)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&c)).Data)) =
|
*(*uint64)(unsafe.Pointer(&c[0])) =
|
||||||
*(*uint64)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&a)).Data)) ^
|
*(*uint64)(unsafe.Pointer(&a[0])) ^ *(*uint64)(unsafe.Pointer(&b[0]))
|
||||||
*(*uint64)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&b)).Data))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type TEA [4]uint32
|
type TEA [4]uint32
|
||||||
|
@ -37,31 +37,13 @@ func (c *QQClient) highwayUploadStream(ip uint32, port int, updKey []byte, strea
|
|||||||
h := md5.New()
|
h := md5.New()
|
||||||
length, _ := io.Copy(h, stream)
|
length, _ := io.Copy(h, stream)
|
||||||
fh := h.Sum(nil)
|
fh := h.Sum(nil)
|
||||||
const chunkSize = 8192 * 8
|
|
||||||
_, _ = stream.Seek(0, io.SeekStart)
|
_, _ = stream.Seek(0, io.SeekStart)
|
||||||
conn, err := net.DialTCP("tcp", nil, &addr)
|
conn, err := net.DialTCP("tcp", nil, &addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "connect error")
|
return errors.Wrap(err, "connect error")
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
offset := 0
|
|
||||||
reader := binary.NewNetworkReader(conn)
|
reader := binary.NewNetworkReader(conn)
|
||||||
chunk := *binary.Get128KBytes()
|
|
||||||
defer func() { // 延迟捕获 chunk
|
|
||||||
binary.Put128KBytes(&chunk)
|
|
||||||
}()
|
|
||||||
w := binary.NewWriter()
|
|
||||||
defer binary.PutBuffer(w)
|
|
||||||
for {
|
|
||||||
chunk = chunk[:chunkSize]
|
|
||||||
rl, err := io.ReadFull(stream, 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{
|
head, _ := proto.Marshal(&pb.ReqDataHighwayHead{
|
||||||
MsgBasehead: &pb.DataHighwayHead{
|
MsgBasehead: &pb.DataHighwayHead{
|
||||||
Version: 1,
|
Version: 1,
|
||||||
@ -75,23 +57,23 @@ func (c *QQClient) highwayUploadStream(ip uint32, port int, updKey []byte, strea
|
|||||||
},
|
},
|
||||||
MsgSeghead: &pb.SegHead{
|
MsgSeghead: &pb.SegHead{
|
||||||
Filesize: length,
|
Filesize: length,
|
||||||
Dataoffset: int64(offset),
|
Dataoffset: int64(0),
|
||||||
Datalength: int32(rl),
|
Datalength: int32(length),
|
||||||
Serviceticket: updKey,
|
Serviceticket: updKey,
|
||||||
Md5: ch[:],
|
Md5: fh,
|
||||||
FileMd5: fh,
|
FileMd5: fh,
|
||||||
},
|
},
|
||||||
ReqExtendinfo: EmptyBytes,
|
ReqExtendinfo: EmptyBytes,
|
||||||
})
|
})
|
||||||
offset += rl
|
w := binary.NewWriter()
|
||||||
w.Reset()
|
defer binary.PutBuffer(w)
|
||||||
w.WriteByte(40)
|
w.WriteByte(40)
|
||||||
w.WriteUInt32(uint32(len(head)))
|
w.WriteUInt32(uint32(len(head)))
|
||||||
w.WriteUInt32(uint32(len(chunk)))
|
w.WriteUInt32(uint32(length))
|
||||||
w.Write(head)
|
w.Write(head)
|
||||||
w.Write(chunk)
|
_, _ = conn.Write(w.Bytes())
|
||||||
w.WriteByte(41)
|
_, _ = conn.ReadFrom(stream)
|
||||||
_, err = conn.Write(w.Bytes())
|
_, err = conn.Write([]byte{41})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "write conn error")
|
return errors.Wrap(err, "write conn error")
|
||||||
}
|
}
|
||||||
@ -102,7 +84,6 @@ func (c *QQClient) highwayUploadStream(ip uint32, port int, updKey []byte, strea
|
|||||||
if rspHead.ErrorCode != 0 {
|
if rspHead.ErrorCode != 0 {
|
||||||
return errors.New("upload failed")
|
return errors.New("upload failed")
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,13 +97,11 @@ func (c *QQClient) highwayUploadByBDH(stream io.Reader, length int64, cmdId int3
|
|||||||
}
|
}
|
||||||
ext = binary.NewTeaCipher(c.bigDataSession.SessionKey).Encrypt(ext)
|
ext = binary.NewTeaCipher(c.bigDataSession.SessionKey).Encrypt(ext)
|
||||||
}
|
}
|
||||||
const chunkSize = 8192 * 16
|
|
||||||
conn, err := net.DialTimeout("tcp", c.srvSsoAddrs[0], time.Second*20)
|
conn, err := net.DialTimeout("tcp", c.srvSsoAddrs[0], time.Second*20)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "connect error")
|
return nil, errors.Wrap(err, "connect error")
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
offset := 0
|
|
||||||
reader := binary.NewNetworkReader(conn)
|
reader := binary.NewNetworkReader(conn)
|
||||||
if err = c.highwaySendHeartbreak(conn); err != nil {
|
if err = c.highwaySendHeartbreak(conn); err != nil {
|
||||||
return nil, errors.Wrap(err, "echo error")
|
return nil, errors.Wrap(err, "echo error")
|
||||||
@ -131,22 +110,6 @@ func (c *QQClient) highwayUploadByBDH(stream io.Reader, length int64, cmdId int3
|
|||||||
return nil, errors.Wrap(err, "echo error")
|
return nil, errors.Wrap(err, "echo error")
|
||||||
}
|
}
|
||||||
var rspExt []byte
|
var rspExt []byte
|
||||||
chunk := *binary.Get128KBytes()
|
|
||||||
defer func() { // 延迟捕获 chunk
|
|
||||||
binary.Put128KBytes(&chunk)
|
|
||||||
}()
|
|
||||||
w := binary.NewWriter()
|
|
||||||
defer binary.PutBuffer(w)
|
|
||||||
for {
|
|
||||||
chunk = chunk[:chunkSize]
|
|
||||||
rl, err := io.ReadFull(stream, 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{
|
head, _ := proto.Marshal(&pb.ReqDataHighwayHead{
|
||||||
MsgBasehead: &pb.DataHighwayHead{
|
MsgBasehead: &pb.DataHighwayHead{
|
||||||
Version: 1,
|
Version: 1,
|
||||||
@ -160,23 +123,24 @@ func (c *QQClient) highwayUploadByBDH(stream io.Reader, length int64, cmdId int3
|
|||||||
},
|
},
|
||||||
MsgSeghead: &pb.SegHead{
|
MsgSeghead: &pb.SegHead{
|
||||||
Filesize: length,
|
Filesize: length,
|
||||||
Dataoffset: int64(offset),
|
Dataoffset: int64(0),
|
||||||
Datalength: int32(rl),
|
Datalength: int32(length),
|
||||||
Serviceticket: ticket,
|
Serviceticket: ticket,
|
||||||
Md5: ch[:],
|
Md5: sum,
|
||||||
FileMd5: sum,
|
FileMd5: sum,
|
||||||
},
|
},
|
||||||
ReqExtendinfo: ext,
|
ReqExtendinfo: ext,
|
||||||
})
|
})
|
||||||
offset += rl
|
w := binary.NewWriter()
|
||||||
|
defer binary.PutBuffer(w)
|
||||||
w.Reset()
|
w.Reset()
|
||||||
w.WriteByte(40)
|
w.WriteByte(40)
|
||||||
w.WriteUInt32(uint32(len(head)))
|
w.WriteUInt32(uint32(len(head)))
|
||||||
w.WriteUInt32(uint32(len(chunk)))
|
w.WriteUInt32(uint32(length))
|
||||||
w.Write(head)
|
w.Write(head)
|
||||||
w.Write(chunk)
|
_, _ = conn.Write(w.Bytes())
|
||||||
w.WriteByte(41)
|
_, _ = io.Copy(conn, stream)
|
||||||
_, err = conn.Write(w.Bytes())
|
_, err = conn.Write([]byte{41})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "write conn error")
|
return nil, errors.Wrap(err, "write conn error")
|
||||||
}
|
}
|
||||||
@ -193,7 +157,6 @@ func (c *QQClient) highwayUploadByBDH(stream io.Reader, length int64, cmdId int3
|
|||||||
if rspHead.MsgSeghead != nil && rspHead.MsgSeghead.Serviceticket != nil {
|
if rspHead.MsgSeghead != nil && rspHead.MsgSeghead.Serviceticket != nil {
|
||||||
ticket = rspHead.MsgSeghead.Serviceticket
|
ticket = rspHead.MsgSeghead.Serviceticket
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return rspExt, nil
|
return rspExt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ func (c *QQClient) UploadGroupImage(groupCode int64, img io.ReadSeeker) (*messag
|
|||||||
c.srvSsoAddrs = append(c.srvSsoAddrs, fmt.Sprintf("%v:%v", binary.UInt32ToIPV4Address(uint32(addr)), rsp.UploadPort[i]))
|
c.srvSsoAddrs = append(c.srvSsoAddrs, fmt.Sprintf("%v:%v", binary.UInt32ToIPV4Address(uint32(addr)), rsp.UploadPort[i]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if _, err = c.highwayUploadByBDH(img, length, 2, rsp.UploadKey, EmptyBytes, fh, false); err == nil {
|
if _, err = c.highwayUploadByBDH(img, length, 2, rsp.UploadKey, fh, EmptyBytes, false); err == nil {
|
||||||
goto ok
|
goto ok
|
||||||
}
|
}
|
||||||
return nil, errors.Wrap(err, "upload failed")
|
return nil, errors.Wrap(err, "upload failed")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user