forked from LagrangeDev/LagrangeGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
highway.go
258 lines (240 loc) · 6.89 KB
/
highway.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package client
import (
"bytes"
binary2 "encoding/binary"
"encoding/hex"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
highway2 "github.com/LagrangeDev/LagrangeGo/packets/highway"
"github.com/LagrangeDev/LagrangeGo/packets/pb/service/highway"
"github.com/LagrangeDev/LagrangeGo/utils"
"github.com/LagrangeDev/LagrangeGo/utils/binary"
"github.com/RomiChan/protobuf/proto"
)
type UpBlock struct {
CommandId int
Uin uint
Sequence uint
FileSize uint64
Offset uint64
Ticket []byte
FileMd5 []byte
Block []byte
ExtendInfo []byte
Timestamp uint64
}
func (c *QQClient) GetServiceServer() ([]byte, map[uint32][]string) {
if c.highwayUri == nil || c.sigSession == nil {
c.highwayUri = make(map[uint32][]string)
packet, err := highway2.BuildHighWayUrlReq(c.sig.Tgt)
if err != nil {
return nil, nil
}
payload, err := c.SendUniPacketAndAwait("HttpConn.0x6ff_501", packet)
if err != nil {
networkLogger.Errorf("Failed to get highway server: %v", err)
return nil, nil
}
resp, err := highway2.ParseHighWayUrlReq(payload.Data)
if err != nil {
networkLogger.Errorf("Failed to parse highway server: %v", err)
return nil, nil
}
for _, info := range resp.HttpConn.ServerInfos {
servicetype := info.ServiceType
for _, addr := range info.ServerAddrs {
ip := make([]byte, 4)
binary2.LittleEndian.PutUint32(ip, addr.IP)
service := c.highwayUri[servicetype]
service = append(service, fmt.Sprintf("http:https://%d.%d.%d.%d:%d/cgi-bin/httpconn?htcmd=0x6FF0087&uin=%d", ip[0], ip[1], ip[2], ip[3], addr.Port, c.sig.Uin))
c.highwayUri[servicetype] = service
}
}
c.sigSession = resp.HttpConn.SigSession
}
return c.sigSession, c.highwayUri
}
func (c *QQClient) UploadSrcByStreamAsync(commonId int, stream io.ReadSeeker, ticket []byte, md5 []byte, extendInfo []byte) bool {
// Get server URL
_, server := c.GetServiceServer()
if server == nil {
networkLogger.Errorln("Failed to get upload server")
return false
}
success := true
var upBlocks []UpBlock
data, err := io.ReadAll(stream)
if err != nil {
networkLogger.Errorln("Failed to read stream")
return false
}
fileSize := uint64(len(data))
offset := uint64(0)
_, err = stream.Seek(0, io.SeekStart)
if err != nil {
networkLogger.Errorln("Failed to seek stream")
return false
}
for offset < fileSize {
var buffersize uint64
if uint64(1024*1024) > fileSize-offset {
buffersize = fileSize - offset
} else {
buffersize = uint64(1024 * 1024)
}
buffer := make([]byte, buffersize)
payload, err := io.ReadFull(stream, buffer)
if err != nil {
networkLogger.Errorln("Failed to read stream")
return false
}
reqBody := UpBlock{
CommandId: commonId,
Uin: uint(c.sig.Uin),
Sequence: uint(c.highwaySequence.Add(1)),
FileSize: fileSize,
Offset: offset,
Ticket: ticket,
FileMd5: md5,
Block: buffer,
ExtendInfo: extendInfo,
}
upBlocks = append(upBlocks, reqBody)
offset += uint64(payload)
// 4 is HighwayConcurrent
if len(upBlocks) >= 4 || offset == fileSize {
for _, block := range upBlocks {
success = success && c.SendUpBlockAsync(block, server[1][0])
if !success {
networkLogger.Errorln("Failed to send block")
return false
}
}
upBlocks = nil
}
}
return success
}
func (c *QQClient) SendUpBlockAsync(block UpBlock, server string) bool {
head := &highway.DataHighwayHead{
Version: 1,
Uin: proto.Some(strconv.Itoa(int(block.Uin))),
Command: proto.Some("PicUp.DataUp"),
Seq: proto.Some(uint32(block.Sequence)),
RetryTimes: proto.Some(uint32(0)),
AppId: uint32(c.appInfo.SubAppID),
DataFlag: 16,
CommandId: uint32(block.CommandId),
}
md5 := utils.Md5Digest(block.Block)
segHead := &highway.SegHead{
ServiceId: proto.Some(uint32(0)),
Filesize: block.FileSize,
DataOffset: proto.Some(block.Offset),
DataLength: uint32(len(block.Block)),
RetCode: proto.Some(uint32(0)),
ServiceTicket: block.Ticket,
Md5: md5,
FileMd5: block.FileMd5,
CacheAddr: proto.Some(uint32(0)),
CachePort: proto.Some(uint32(0)),
}
loginHead := &highway.LoginSigHead{
Uint32LoginSigType: 8,
BytesLoginSig: c.sig.Tgt,
AppId: uint32(c.appInfo.AppID),
}
highwayHead := &highway.ReqDataHighwayHead{
MsgBaseHead: head,
MsgSegHead: segHead,
BytesReqExtendInfo: block.ExtendInfo,
Timestamp: block.Timestamp,
MsgLoginSigHead: loginHead,
}
isEnd := block.Offset+uint64(len(block.Block)) == block.FileSize
packet := &binary.Builder{}
packet.WriteBytes(block.Block, false)
payload, err := SendPacketAsync(highwayHead, packet, server, isEnd)
if err != nil {
networkLogger.Errorln("Failed to send packet ", err)
return false
}
resphead, respbody, err := ParsePacket(payload)
if err != nil {
networkLogger.Errorln("Failed to parse packet ", err)
return false
}
networkLogger.Debugf("Highway Block Result: %d | %d | %x | %v",
resphead.ErrorCode, resphead.MsgSegHead.RetCode.Unwrap(), resphead.BytesRspExtendInfo, respbody)
return resphead.ErrorCode == 0
}
func ParsePacket(data []byte) (head *highway.RespDataHighwayHead, body *binary.Reader, err error) {
reader := binary.NewReader(data)
if reader.ReadBytes(1)[0] == 0x28 {
headlength := reader.ReadU32()
bodylength := reader.ReadU32()
head = &highway.RespDataHighwayHead{}
headraw := reader.ReadBytes(int(int64(headlength)))
err = proto.Unmarshal(headraw, head)
if err != nil {
return nil, nil, err
}
body = binary.NewReader(reader.ReadBytes(int(bodylength)))
if reader.ReadBytes(1)[0] == 0x29 {
return head, body, nil
}
}
return nil, nil, err
}
func SendPacketAsync(packet *highway.ReqDataHighwayHead, buffer *binary.Builder, serverURL string, end bool) ([]byte, error) {
marshal, err := proto.Marshal(packet)
if err != nil {
return nil, err
}
println(hex.EncodeToString(marshal))
writer := binary.NewBuilder(nil).
WriteBytes([]byte{0x28}, false).
WriteU32(uint32(len(marshal))).
WriteU32(uint32(buffer.Len())).
WriteBytes(marshal, false).
WriteBytes(buffer.Data(), false).
WriteBytes([]byte{0x29}, false)
return SendDataAsync(writer.Data(), serverURL, end)
}
func SendDataAsync(packet []byte, serverURL string, end bool) ([]byte, error) {
// Parse server URL
server, err := url.Parse(serverURL)
if err != nil {
return nil, err
}
// Create request
content := bytes.NewBuffer(packet)
req, err := http.NewRequest("POST", server.String(), content)
if err != nil {
return nil, err
}
// Set headers
if end {
req.Header.Set("Connection", "close")
} else {
req.Header.Set("Connection", "keep-alive")
}
// Send request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
// Read response data
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return data, nil
}