close #535 request set timeout

pull/548/head
Xhofe 2022-02-14 14:59:00 +08:00
parent fb05a6ca48
commit e90b979d15
5 changed files with 14 additions and 15 deletions

View File

@ -107,6 +107,7 @@ func (driver Cloud189) Login(account *model.Account) error {
//cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List}) //cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
client = resty.New() client = resty.New()
//client.SetCookieJar(cookieJar) //client.SetCookieJar(cookieJar)
client.SetTimeout(base.DefaultTimeout)
client.SetRetryCount(3) client.SetRetryCount(3)
client.SetHeader("Referer", "https://cloud.189.cn/") client.SetHeader("Referer", "https://cloud.189.cn/")
} }

View File

@ -236,6 +236,7 @@ func (driver AliDrive) batch(srcId, dstId string, account *model.Account) error
func init() { func init() {
base.RegisterDriver(&AliDrive{}) base.RegisterDriver(&AliDrive{})
aliClient. aliClient.
SetTimeout(base.DefaultTimeout).
SetRetryCount(3). SetRetryCount(3).
SetHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"). SetHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36").
SetHeader("content-type", "application/json"). SetHeader("content-type", "application/json").

View File

@ -5,6 +5,7 @@ import (
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"net/http" "net/http"
"time"
) )
type DriverConfig struct { type DriverConfig struct {
@ -168,6 +169,7 @@ var NoRedirectClient *resty.Client
var RestyClient = resty.New() var RestyClient = resty.New()
var HttpClient = &http.Client{} var HttpClient = &http.Client{}
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" 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"
var DefaultTimeout = time.Second * 20
func init() { func init() {
NoRedirectClient = resty.New().SetRedirectPolicy( NoRedirectClient = resty.New().SetRedirectPolicy(
@ -178,4 +180,5 @@ func init() {
NoRedirectClient.SetHeader("user-agent", UserAgent) NoRedirectClient.SetHeader("user-agent", UserAgent)
RestyClient.SetHeader("user-agent", UserAgent) RestyClient.SetHeader("user-agent", UserAgent)
RestyClient.SetRetryCount(3) RestyClient.SetRetryCount(3)
RestyClient.SetTimeout(DefaultTimeout)
} }

View File

@ -6,7 +6,6 @@ import (
"github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"net/url" "net/url"
"path/filepath" "path/filepath"
@ -15,8 +14,6 @@ import (
"time" "time"
) )
var lanzouClient = resty.New()
type LanZouFile struct { type LanZouFile struct {
Name string `json:"name"` Name string `json:"name"`
NameAll string `json:"name_all"` NameAll string `json:"name_all"`
@ -58,7 +55,7 @@ func (driver *Lanzou) GetFiles(folderId string, account *model.Account) ([]LanZo
files := make([]LanZouFile, 0) files := make([]LanZouFile, 0)
var resp LanZouFilesResp var resp LanZouFilesResp
// folders // folders
res, err := lanzouClient.R().SetResult(&resp).SetHeader("Cookie", account.AccessToken). res, err := base.RestyClient.R().SetResult(&resp).SetHeader("Cookie", account.AccessToken).
SetFormData(map[string]string{ SetFormData(map[string]string{
"task": "47", "task": "47",
"folder_id": folderId, "folder_id": folderId,
@ -77,7 +74,7 @@ func (driver *Lanzou) GetFiles(folderId string, account *model.Account) ([]LanZo
// files // files
pg := 1 pg := 1
for { for {
_, err = lanzouClient.R().SetResult(&resp).SetHeader("Cookie", account.AccessToken). _, err = base.RestyClient.R().SetResult(&resp).SetHeader("Cookie", account.AccessToken).
SetFormData(map[string]string{ SetFormData(map[string]string{
"task": "5", "task": "5",
"folder_id": folderId, "folder_id": folderId,
@ -108,7 +105,7 @@ func (driver *Lanzou) GetFilesByUrl(account *model.Account) ([]LanZouFile, error
if err != nil { if err != nil {
return nil, err return nil, err
} }
res, err := lanzouClient.R().Get(shareUrl) res, err := base.RestyClient.R().Get(shareUrl)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -129,7 +126,7 @@ func (driver *Lanzou) GetFilesByUrl(account *model.Account) ([]LanZouFile, error
pg := 1 pg := 1
for { for {
var resp LanZouFilesResp var resp LanZouFilesResp
res, err = lanzouClient.R().SetResult(&resp).SetFormData(map[string]string{ res, err = base.RestyClient.R().SetResult(&resp).SetFormData(map[string]string{
"lx": lx, "lx": lx,
"fid": fid, "fid": fid,
"uid": uid, "uid": uid,
@ -166,7 +163,7 @@ func (driver *Lanzou) GetFilesByUrl(account *model.Account) ([]LanZouFile, error
// GetDownPageId 获取下载页面的ID // GetDownPageId 获取下载页面的ID
func (driver *Lanzou) GetDownPageId(fileId string, account *model.Account) (string, error) { func (driver *Lanzou) GetDownPageId(fileId string, account *model.Account) (string, error) {
var resp LanZouFilesResp var resp LanZouFilesResp
res, err := lanzouClient.R().SetResult(&resp).SetHeader("Cookie", account.AccessToken). res, err := base.RestyClient.R().SetResult(&resp).SetHeader("Cookie", account.AccessToken).
SetFormData(map[string]string{ SetFormData(map[string]string{
"task": "22", "task": "22",
"file_id": fileId, "file_id": fileId,
@ -201,7 +198,7 @@ func (driver *Lanzou) GetLink(downId string, account *model.Account) (string, er
if err != nil { if err != nil {
return "", err return "", err
} }
res, err := lanzouClient.R().Get(fmt.Sprintf("https://%s/%s", u.Host, downId)) res, err := base.RestyClient.R().Get(fmt.Sprintf("https://%s/%s", u.Host, downId))
if err != nil { if err != nil {
return "", err return "", err
} }
@ -210,7 +207,7 @@ func (driver *Lanzou) GetLink(downId string, account *model.Account) (string, er
return "", fmt.Errorf("get down empty page") return "", fmt.Errorf("get down empty page")
} }
iframeUrl := fmt.Sprintf("https://%s%s", u.Host, iframe[1]) iframeUrl := fmt.Sprintf("https://%s%s", u.Host, iframe[1])
res, err = lanzouClient.R().Get(iframeUrl) res, err = base.RestyClient.R().Get(iframeUrl)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -237,7 +234,7 @@ func (driver *Lanzou) GetLink(downId string, account *model.Account) (string, er
"websignkey": websignkey, "websignkey": websignkey,
} }
log.Debugf("form: %+v", form) log.Debugf("form: %+v", form)
res, err = lanzouClient.R().SetResult(&resp). res, err = base.RestyClient.R().SetResult(&resp).
SetHeader("origin", "https://"+u.Host). SetHeader("origin", "https://"+u.Host).
SetHeader("referer", iframeUrl). SetHeader("referer", iframeUrl).
SetFormData(form).Post(fmt.Sprintf("https://%s/ajaxm.php", u.Host)) SetFormData(form).Post(fmt.Sprintf("https://%s/ajaxm.php", u.Host))
@ -253,7 +250,4 @@ func (driver *Lanzou) GetLink(downId string, account *model.Account) (string, er
func init() { func init() {
base.RegisterDriver(&Lanzou{}) base.RegisterDriver(&Lanzou{})
lanzouClient.
SetRetryCount(3).
SetHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
} }

View File

@ -67,7 +67,7 @@ var client *resty.Client
func init() { func init() {
client = resty.New() client = resty.New()
client.SetRetryCount(3) client.SetRetryCount(3).SetTimeout(base.DefaultTimeout)
} }
func Text(c *gin.Context, link *base.Link) { func Text(c *gin.Context, link *base.Link) {