@@ -4,6 +4,7 @@ package download
44import (
55 "bufio"
66 "compress/gzip"
7+ "crypto/tls"
78 "fmt"
89 "io"
910 "net/http"
@@ -22,15 +23,28 @@ import (
2223
2324var client = & http.Client {
2425 Transport : & http.Transport {
25- Proxy : func (request * http.Request ) (u * url.URL , e error ) {
26+ Proxy : func (request * http.Request ) (* url.URL , error ) {
2627 if base .Proxy == "" {
2728 return http .ProxyFromEnvironment (request )
2829 }
2930 return url .Parse (base .Proxy )
3031 },
31- ForceAttemptHTTP2 : false ,
32- MaxConnsPerHost : 0 ,
33- MaxIdleConns : 0 ,
32+ // Disable http2
33+ TLSNextProto : map [string ]func (authority string , c * tls.Conn ) http.RoundTripper {},
34+ MaxIdleConnsPerHost : 999 ,
35+ },
36+ Timeout : time .Second * 5 ,
37+ }
38+
39+ var clienth2 = & http.Client {
40+ Transport : & http.Transport {
41+ Proxy : func (request * http.Request ) (* url.URL , error ) {
42+ if base .Proxy == "" {
43+ return http .ProxyFromEnvironment (request )
44+ }
45+ return url .Parse (base .Proxy )
46+ },
47+ ForceAttemptHTTP2 : true ,
3448 MaxIdleConnsPerHost : 999 ,
3549 },
3650 Timeout : time .Second * 5 ,
@@ -42,11 +56,13 @@ var ErrOverSize = errors.New("oversize")
4256// UserAgent HTTP请求时使用的UA
4357const 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"
4458
45- func SetGlobalTimeout (t time.Duration ) {
59+ // SetTimeout set internal/download client timeout
60+ func SetTimeout (t time.Duration ) {
4661 if t == 0 {
4762 t = time .Second * 10
4863 }
4964 client .Timeout = t
65+ clienth2 .Timeout = t
5066}
5167
5268// Request is a file download request
@@ -58,6 +74,13 @@ type Request struct {
5874 Body io.Reader
5975}
6076
77+ func (r Request ) client () * http.Client {
78+ if strings .Contains (r .URL , "go-cqhttp.org" ) {
79+ return clienth2
80+ }
81+ return client
82+ }
83+
6184func (r Request ) do () (* http.Response , error ) {
6285 if r .Method == "" {
6386 r .Method = http .MethodGet
@@ -72,7 +95,7 @@ func (r Request) do() (*http.Response, error) {
7295 req .Header .Set (k , v )
7396 }
7497
75- return client .Do (req )
98+ return r . client () .Do (req )
7699}
77100
78101func (r Request ) body () (io.ReadCloser , error ) {
0 commit comments