Skip to content

Commit

Permalink
close #535 request set timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Feb 14, 2022
1 parent fb05a6c commit e90b979
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
1 change: 1 addition & 0 deletions drivers/189/189.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (driver Cloud189) Login(account *model.Account) error {
//cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
client = resty.New()
//client.SetCookieJar(cookieJar)
client.SetTimeout(base.DefaultTimeout)
client.SetRetryCount(3)
client.SetHeader("Referer", "https://cloud.189.cn/")
}
Expand Down
1 change: 1 addition & 0 deletions drivers/alidrive/alidrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ func (driver AliDrive) batch(srcId, dstId string, account *model.Account) error
func init() {
base.RegisterDriver(&AliDrive{})
aliClient.
SetTimeout(base.DefaultTimeout).
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("content-type", "application/json").
Expand Down
3 changes: 3 additions & 0 deletions drivers/base/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
"net/http"
"time"
)

type DriverConfig struct {
Expand Down Expand Up @@ -168,6 +169,7 @@ var NoRedirectClient *resty.Client
var RestyClient = resty.New()
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 DefaultTimeout = time.Second * 20

func init() {
NoRedirectClient = resty.New().SetRedirectPolicy(
Expand All @@ -178,4 +180,5 @@ func init() {
NoRedirectClient.SetHeader("user-agent", UserAgent)
RestyClient.SetHeader("user-agent", UserAgent)
RestyClient.SetRetryCount(3)
RestyClient.SetTimeout(DefaultTimeout)
}
22 changes: 8 additions & 14 deletions drivers/lanzou/lanzou.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
"net/url"
"path/filepath"
Expand All @@ -15,8 +14,6 @@ import (
"time"
)

var lanzouClient = resty.New()

type LanZouFile struct {
Name string `json:"name"`
NameAll string `json:"name_all"`
Expand Down Expand Up @@ -58,7 +55,7 @@ func (driver *Lanzou) GetFiles(folderId string, account *model.Account) ([]LanZo
files := make([]LanZouFile, 0)
var resp LanZouFilesResp
// 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{
"task": "47",
"folder_id": folderId,
Expand All @@ -77,7 +74,7 @@ func (driver *Lanzou) GetFiles(folderId string, account *model.Account) ([]LanZo
// files
pg := 1
for {
_, err = lanzouClient.R().SetResult(&resp).SetHeader("Cookie", account.AccessToken).
_, err = base.RestyClient.R().SetResult(&resp).SetHeader("Cookie", account.AccessToken).
SetFormData(map[string]string{
"task": "5",
"folder_id": folderId,
Expand Down Expand Up @@ -108,7 +105,7 @@ func (driver *Lanzou) GetFilesByUrl(account *model.Account) ([]LanZouFile, error
if err != nil {
return nil, err
}
res, err := lanzouClient.R().Get(shareUrl)
res, err := base.RestyClient.R().Get(shareUrl)
if err != nil {
return nil, err
}
Expand All @@ -129,7 +126,7 @@ func (driver *Lanzou) GetFilesByUrl(account *model.Account) ([]LanZouFile, error
pg := 1
for {
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,
"fid": fid,
"uid": uid,
Expand Down Expand Up @@ -166,7 +163,7 @@ func (driver *Lanzou) GetFilesByUrl(account *model.Account) ([]LanZouFile, error
// GetDownPageId 获取下载页面的ID
func (driver *Lanzou) GetDownPageId(fileId string, account *model.Account) (string, error) {
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{
"task": "22",
"file_id": fileId,
Expand Down Expand Up @@ -201,7 +198,7 @@ func (driver *Lanzou) GetLink(downId string, account *model.Account) (string, er
if err != nil {
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 {
return "", err
}
Expand All @@ -210,7 +207,7 @@ func (driver *Lanzou) GetLink(downId string, account *model.Account) (string, er
return "", fmt.Errorf("get down empty page")
}
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 {
return "", err
}
Expand All @@ -237,7 +234,7 @@ func (driver *Lanzou) GetLink(downId string, account *model.Account) (string, er
"websignkey": websignkey,
}
log.Debugf("form: %+v", form)
res, err = lanzouClient.R().SetResult(&resp).
res, err = base.RestyClient.R().SetResult(&resp).
SetHeader("origin", "https://"+u.Host).
SetHeader("referer", iframeUrl).
SetFormData(form).Post(fmt.Sprintf("https://%s/ajaxm.php", u.Host))
Expand All @@ -253,7 +250,4 @@ func (driver *Lanzou) GetLink(downId string, account *model.Account) (string, er

func init() {
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")
}
2 changes: 1 addition & 1 deletion server/controllers/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ var client *resty.Client

func init() {
client = resty.New()
client.SetRetryCount(3)
client.SetRetryCount(3).SetTimeout(base.DefaultTimeout)
}

func Text(c *gin.Context, link *base.Link) {
Expand Down

0 comments on commit e90b979

Please sign in to comment.