2022-08-30 21:52:06 +08:00
|
|
|
package base
|
|
|
|
|
|
|
|
import (
|
2023-05-11 18:48:16 +08:00
|
|
|
"crypto/tls"
|
2022-08-30 21:52:06 +08:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2025-07-01 09:54:50 +08:00
|
|
|
"github.com/OpenListTeam/OpenList/v4/internal/conf"
|
|
|
|
"github.com/OpenListTeam/OpenList/v4/internal/net"
|
2022-08-30 21:52:06 +08:00
|
|
|
"github.com/go-resty/resty/v2"
|
|
|
|
)
|
|
|
|
|
2023-05-14 17:05:47 +08:00
|
|
|
var (
|
|
|
|
NoRedirectClient *resty.Client
|
|
|
|
RestyClient *resty.Client
|
|
|
|
HttpClient *http.Client
|
|
|
|
)
|
2025-06-30 10:35:52 +08:00
|
|
|
var UserAgent = "Mozilla/5.0 (Macintosh; Apple macOS 15_5) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36 Chrome/138.0.0.0"
|
2022-11-08 20:37:42 +08:00
|
|
|
var DefaultTimeout = time.Second * 30
|
2022-08-30 21:52:06 +08:00
|
|
|
|
2023-05-14 17:05:47 +08:00
|
|
|
func InitClient() {
|
2022-08-30 21:52:06 +08:00
|
|
|
NoRedirectClient = resty.New().SetRedirectPolicy(
|
|
|
|
resty.RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
}),
|
2023-05-14 17:05:47 +08:00
|
|
|
).SetTLSClientConfig(&tls.Config{InsecureSkipVerify: conf.Conf.TlsInsecureSkipVerify})
|
2022-08-30 21:52:06 +08:00
|
|
|
NoRedirectClient.SetHeader("user-agent", UserAgent)
|
2023-05-14 17:05:47 +08:00
|
|
|
|
|
|
|
RestyClient = NewRestyClient()
|
2025-02-16 12:22:11 +08:00
|
|
|
HttpClient = net.NewHttpClient()
|
2022-09-08 15:00:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewRestyClient() *resty.Client {
|
2023-02-08 22:01:26 +08:00
|
|
|
client := resty.New().
|
2022-09-08 15:00:57 +08:00
|
|
|
SetHeader("user-agent", UserAgent).
|
|
|
|
SetRetryCount(3).
|
2023-12-14 21:31:36 +08:00
|
|
|
SetRetryResetReaders(true).
|
2023-05-14 17:05:47 +08:00
|
|
|
SetTimeout(DefaultTimeout).
|
|
|
|
SetTLSClientConfig(&tls.Config{InsecureSkipVerify: conf.Conf.TlsInsecureSkipVerify})
|
2023-02-08 22:01:26 +08:00
|
|
|
return client
|
2022-08-30 21:52:06 +08:00
|
|
|
}
|