2022-08-30 13:52:06 +00:00
|
|
|
package base
|
|
|
|
|
|
|
|
import (
|
2023-05-11 10:48:16 +00:00
|
|
|
"crypto/tls"
|
2022-08-30 13:52:06 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2023-05-11 10:48:16 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/conf"
|
2022-08-30 13:52:06 +00:00
|
|
|
"github.com/go-resty/resty/v2"
|
|
|
|
)
|
|
|
|
|
2023-05-14 09:05:47 +00:00
|
|
|
var (
|
|
|
|
NoRedirectClient *resty.Client
|
|
|
|
RestyClient *resty.Client
|
|
|
|
HttpClient *http.Client
|
|
|
|
)
|
2022-08-30 13:52:06 +00:00
|
|
|
var UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
|
2022-11-08 12:37:42 +00:00
|
|
|
var DefaultTimeout = time.Second * 30
|
2022-08-30 13:52:06 +00:00
|
|
|
|
2023-05-14 09:05:47 +00:00
|
|
|
func InitClient() {
|
2022-08-30 13:52:06 +00:00
|
|
|
NoRedirectClient = resty.New().SetRedirectPolicy(
|
|
|
|
resty.RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
}),
|
2023-05-14 09:05:47 +00:00
|
|
|
).SetTLSClientConfig(&tls.Config{InsecureSkipVerify: conf.Conf.TlsInsecureSkipVerify})
|
2022-08-30 13:52:06 +00:00
|
|
|
NoRedirectClient.SetHeader("user-agent", UserAgent)
|
2023-05-14 09:05:47 +00:00
|
|
|
|
|
|
|
RestyClient = NewRestyClient()
|
|
|
|
HttpClient = NewHttpClient()
|
2022-09-08 07:00:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewRestyClient() *resty.Client {
|
2023-02-08 14:01:26 +00:00
|
|
|
client := resty.New().
|
2022-09-08 07:00:57 +00:00
|
|
|
SetHeader("user-agent", UserAgent).
|
|
|
|
SetRetryCount(3).
|
2023-12-14 13:31:36 +00:00
|
|
|
SetRetryResetReaders(true).
|
2023-05-14 09:05:47 +00:00
|
|
|
SetTimeout(DefaultTimeout).
|
|
|
|
SetTLSClientConfig(&tls.Config{InsecureSkipVerify: conf.Conf.TlsInsecureSkipVerify})
|
2023-02-08 14:01:26 +00:00
|
|
|
return client
|
2022-08-30 13:52:06 +00:00
|
|
|
}
|
2023-05-14 09:05:47 +00:00
|
|
|
|
|
|
|
func NewHttpClient() *http.Client {
|
|
|
|
return &http.Client{
|
2023-05-18 15:32:05 +00:00
|
|
|
Timeout: time.Hour * 48,
|
2023-05-14 09:05:47 +00:00
|
|
|
Transport: &http.Transport{
|
2023-06-09 14:08:54 +00:00
|
|
|
Proxy: http.ProxyFromEnvironment,
|
2023-05-14 09:05:47 +00:00
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: conf.Conf.TlsInsecureSkipVerify},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|