forked from Mrs4s/go-cqhttp
/
net.go
329 lines (305 loc) · 8.22 KB
/
net.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package global
import (
"bufio"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/guonaihong/gout"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
)
var (
client = &http.Client{
Transport: &http.Transport{
Proxy: func(request *http.Request) (u *url.URL, e error) {
if Proxy == "" {
return http.ProxyFromEnvironment(request)
}
return url.Parse(Proxy)
},
ForceAttemptHTTP2: true,
MaxConnsPerHost: 0,
MaxIdleConns: 0,
MaxIdleConnsPerHost: 999,
},
}
// Proxy 存储Config.proxy_rewrite,用于设置代理
Proxy string
// ErrOverSize 响应主体过大时返回此错误
ErrOverSize = errors.New("oversize")
// UserAgent HTTP请求时使用的UA
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.66"
)
// GetBytes 对给定URL发送Get请求,返回响应主体
func GetBytes(url string) ([]byte, error) {
reader, err := HTTPGetReadCloser(url)
if err != nil {
return nil, err
}
defer func() {
_ = reader.Close()
}()
return ioutil.ReadAll(reader)
}
// DownloadFile 将给定URL对应的文件下载至给定Path
func DownloadFile(url, path string, limit int64, headers map[string]string) error {
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0o666)
if err != nil {
return err
}
defer file.Close()
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
for k, v := range headers {
req.Header.Set(k, v)
}
if _, ok := headers["User-Agent"]; !ok {
req.Header["User-Agent"] = []string{UserAgent}
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if limit > 0 && resp.ContentLength > limit {
return ErrOverSize
}
_, err = io.Copy(file, resp.Body)
if err != nil {
return err
}
return nil
}
// DownloadFileMultiThreading 使用threadCount个线程将给定URL对应的文件下载至给定Path
func DownloadFileMultiThreading(url, path string, limit int64, threadCount int, headers map[string]string) error {
if threadCount < 2 {
return DownloadFile(url, path, limit, headers)
}
type BlockMetaData struct {
BeginOffset int64
EndOffset int64
DownloadedSize int64
}
var blocks []*BlockMetaData
var contentLength int64
errUnsupportedMultiThreading := errors.New("unsupported multi-threading")
// 初始化分块或直接下载
initOrDownload := func() error {
copyStream := func(s io.ReadCloser) error {
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0o666)
if err != nil {
return err
}
defer file.Close()
if _, err = io.Copy(file, s); err != nil {
return err
}
return errUnsupportedMultiThreading
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
for k, v := range headers {
req.Header.Set(k, v)
}
if _, ok := headers["User-Agent"]; !ok {
req.Header["User-Agent"] = []string{UserAgent}
}
req.Header.Set("range", "bytes=0-")
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return errors.New("response status unsuccessful: " + strconv.FormatInt(int64(resp.StatusCode), 10))
}
if resp.StatusCode == 200 {
if limit > 0 && resp.ContentLength > limit {
return ErrOverSize
}
return copyStream(resp.Body)
}
if resp.StatusCode == 206 {
contentLength = resp.ContentLength
if limit > 0 && resp.ContentLength > limit {
return ErrOverSize
}
blockSize := func() int64 {
if contentLength > 1024*1024 {
return (contentLength / int64(threadCount)) - 10
}
return contentLength
}()
if blockSize == contentLength {
return copyStream(resp.Body)
}
var tmp int64
for tmp+blockSize < contentLength {
blocks = append(blocks, &BlockMetaData{
BeginOffset: tmp,
EndOffset: tmp + blockSize - 1,
})
tmp += blockSize
}
blocks = append(blocks, &BlockMetaData{
BeginOffset: tmp,
EndOffset: contentLength - 1,
})
return nil
}
return errors.New("unknown status code")
}
// 下载分块
downloadBlock := func(block *BlockMetaData) error {
req, _ := http.NewRequest("GET", url, nil)
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0o666)
if err != nil {
return err
}
defer file.Close()
_, _ = file.Seek(block.BeginOffset, io.SeekStart)
writer := bufio.NewWriter(file)
defer writer.Flush()
for k, v := range headers {
req.Header.Set(k, v)
}
if _, ok := headers["User-Agent"]; ok {
req.Header["User-Agent"] = []string{UserAgent}
}
req.Header.Set("range", "bytes="+strconv.FormatInt(block.BeginOffset, 10)+"-"+strconv.FormatInt(block.EndOffset, 10))
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return errors.New("response status unsuccessful: " + strconv.FormatInt(int64(resp.StatusCode), 10))
}
buffer := make([]byte, 1024)
i, err := resp.Body.Read(buffer)
for {
if err != nil && err != io.EOF {
return err
}
i64 := int64(len(buffer[:i]))
needSize := block.EndOffset + 1 - block.BeginOffset
if i64 > needSize {
i64 = needSize
err = io.EOF
}
_, e := writer.Write(buffer[:i64])
if e != nil {
return e
}
block.BeginOffset += i64
block.DownloadedSize += i64
if err == io.EOF || block.BeginOffset > block.EndOffset {
break
}
i, err = resp.Body.Read(buffer)
}
return nil
}
if err := initOrDownload(); err != nil {
if err == errUnsupportedMultiThreading {
return nil
}
return err
}
wg := sync.WaitGroup{}
wg.Add(len(blocks))
var lastErr error
for i := range blocks {
go func(b *BlockMetaData) {
defer wg.Done()
if err := downloadBlock(b); err != nil {
lastErr = err
}
}(blocks[i])
}
wg.Wait()
return lastErr
}
// GetSliderTicket 通过给定的验证链接raw和id,获取验证结果Ticket
func GetSliderTicket(raw, id string) (string, error) {
var rsp string
if err := gout.POST("https://api.shkong.com/gocqhttpapi/task").SetJSON(gout.H{
"id": id,
"url": raw,
}).SetTimeout(time.Second * 35).BindBody(&rsp).Do(); err != nil {
return "", err
}
g := gjson.Parse(rsp)
if g.Get("error").Str != "" {
return "", errors.New(g.Get("error").Str)
}
return g.Get("ticket").Str, nil
}
// QQMusicSongInfo 通过给定id在QQ音乐上查找曲目信息
func QQMusicSongInfo(id string) (gjson.Result, error) {
d, err := GetBytes(`https://u.y.qq.com/cgi-bin/musicu.fcg?format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq.json&needNewCode=0&data={%22comm%22:{%22ct%22:24,%22cv%22:0},%22songinfo%22:{%22method%22:%22get_song_detail_yqq%22,%22param%22:{%22song_type%22:0,%22song_mid%22:%22%22,%22song_id%22:` + id + `},%22module%22:%22music.pf_song_detail_svr%22}}`)
if err != nil {
return gjson.Result{}, err
}
return gjson.ParseBytes(d).Get("songinfo.data"), nil
}
// NeteaseMusicSongInfo 通过给定id在wdd音乐上查找曲目信息
func NeteaseMusicSongInfo(id string) (gjson.Result, error) {
d, err := GetBytes(fmt.Sprintf("http://music.163.com/api/song/detail/?id=%s&ids=%%5B%s%%5D", id, id))
if err != nil {
return gjson.Result{}, err
}
return gjson.ParseBytes(d).Get("songs.0"), nil
}
type gzipCloser struct {
f io.Closer
r *gzip.Reader
}
// NewGzipReadCloser 从 io.ReadCloser 创建 gunzip io.ReadCloser
func NewGzipReadCloser(reader io.ReadCloser) (io.ReadCloser, error) {
gzipReader, err := gzip.NewReader(reader)
if err != nil {
return nil, err
}
return &gzipCloser{
f: reader,
r: gzipReader,
}, nil
}
// Read impls io.Reader
func (g *gzipCloser) Read(p []byte) (n int, err error) {
return g.r.Read(p)
}
// Close impls io.Closer
func (g *gzipCloser) Close() error {
_ = g.f.Close()
return g.r.Close()
}
// HTTPGetReadCloser 从 Http url 获取 io.ReadCloser
func HTTPGetReadCloser(url string) (io.ReadCloser, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header["User-Agent"] = []string{UserAgent}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if strings.Contains(resp.Header.Get("Content-Encoding"), "gzip") {
return NewGzipReadCloser(resp.Body)
}
return resp.Body, err
}