Skip to content

Commit

Permalink
internal/download: disable http when not visiting go-cqhttp.org
Browse files Browse the repository at this point in the history
  • Loading branch information
wdvxdr1123 committed Apr 9, 2023
1 parent 1ed675d commit 42606a8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/gocq/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func LoginInteract() {
})
saveToken()
cli.AllowSlider = true
download.SetGlobalTimeout(time.Duration(base.HTTPTimeout) * time.Second) // 在登录完成后设置, 防止在堵塞协议更新
download.SetTimeout(time.Duration(base.HTTPTimeout) * time.Second) // 在登录完成后设置, 防止在堵塞协议更新
log.Infof("登录成功 欢迎使用: %v", cli.Nickname)
log.Info("开始加载好友列表...")
global.Check(cli.ReloadFriendList(), true)
Expand Down
35 changes: 29 additions & 6 deletions internal/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package download
import (
"bufio"
"compress/gzip"
"crypto/tls"
"fmt"
"io"
"net/http"
Expand All @@ -22,15 +23,28 @@ import (

var client = &http.Client{
Transport: &http.Transport{
Proxy: func(request *http.Request) (u *url.URL, e error) {
Proxy: func(request *http.Request) (*url.URL, error) {
if base.Proxy == "" {
return http.ProxyFromEnvironment(request)
}
return url.Parse(base.Proxy)
},
ForceAttemptHTTP2: false,
MaxConnsPerHost: 0,
MaxIdleConns: 0,
// Disable http2
TLSNextProto: map[string]func(authority string, c *tls.Conn) http.RoundTripper{},
MaxIdleConnsPerHost: 999,
},
Timeout: time.Second * 5,
}

var clienth2 = &http.Client{
Transport: &http.Transport{
Proxy: func(request *http.Request) (*url.URL, error) {
if base.Proxy == "" {
return http.ProxyFromEnvironment(request)
}
return url.Parse(base.Proxy)
},
ForceAttemptHTTP2: true,
MaxIdleConnsPerHost: 999,
},
Timeout: time.Second * 5,
Expand All @@ -42,11 +56,13 @@ var ErrOverSize = errors.New("oversize")
// UserAgent HTTP请求时使用的UA
const 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"

func SetGlobalTimeout(t time.Duration) {
// SetTimeout set internal/download client timeout
func SetTimeout(t time.Duration) {
if t == 0 {
t = time.Second * 10
}
client.Timeout = t
clienth2.Timeout = t
}

// Request is a file download request
Expand All @@ -58,6 +74,13 @@ type Request struct {
Body io.Reader
}

func (r Request) client() *http.Client {
if strings.Contains(r.URL, "go-cqhttp.org") {
return clienth2
}
return client
}

func (r Request) do() (*http.Response, error) {
if r.Method == "" {
r.Method = http.MethodGet
Expand All @@ -72,7 +95,7 @@ func (r Request) do() (*http.Response, error) {
req.Header.Set(k, v)
}

return client.Do(req)
return r.client().Do(req)
}

func (r Request) body() (io.ReadCloser, error) {
Expand Down
1 change: 1 addition & 0 deletions internal/encryption/t544/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func sub_e(*[256][6]byte, []byte)
//go:noescape
func sub_f(*[16]byte, *[15]uint32, *[16][16]byte) (w [44]uint32)

//go:noescape
func sub_aa(int, *[16][2][16][16]byte, *[16]byte, []byte) byte

// transformInner see com/tencent/mobileqq/dt/model/FEBound
Expand Down
1 change: 1 addition & 0 deletions pkg/onebot/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type (
// Kind is the kind of Value.
type Kind int

// Kind
const (
KindAny Kind = iota
KindBool
Expand Down

0 comments on commit 42606a8

Please sign in to comment.