diff --git a/drivers/terabox/driver.go b/drivers/terabox/driver.go index f163c898c8e..b5287f5a7f8 100644 --- a/drivers/terabox/driver.go +++ b/drivers/terabox/driver.go @@ -1,4 +1,4 @@ -package terbox +package terabox import ( "bytes" @@ -23,6 +23,7 @@ import ( type Terabox struct { model.Storage Addition + JsToken string } func (d *Terabox) Config() driver.Config { @@ -167,7 +168,7 @@ func (d *Terabox) Put(ctx context.Context, dstDir model.Obj, stream model.FileSt } log.Debugf("%+v", precreateResp) if precreateResp.Errno != 0 { - return fmt.Errorf("[terabox] failed to precreate file, errno: %s", precreateResp.Errno) + return fmt.Errorf("[terabox] failed to precreate file, errno: %d", precreateResp.Errno) } if precreateResp.ReturnType == 2 { return nil diff --git a/drivers/terabox/meta.go b/drivers/terabox/meta.go index fd2a59e001e..63ae5856471 100644 --- a/drivers/terabox/meta.go +++ b/drivers/terabox/meta.go @@ -1,4 +1,4 @@ -package terbox +package terabox import ( "github.com/alist-org/alist/v3/internal/driver" @@ -7,8 +7,8 @@ import ( type Addition struct { driver.RootPath - Cookie string `json:"cookie" required:"true"` - JsToken string `json:"js_token" type:"string" required:"true"` + Cookie string `json:"cookie" required:"true"` + //JsToken string `json:"js_token" type:"string" required:"true"` DownloadAPI string `json:"download_api" type:"select" options:"official,crack" default:"official"` OrderBy string `json:"order_by" type:"select" options:"name,time,size" default:"name"` OrderDirection string `json:"order_direction" type:"select" options:"asc,desc" default:"asc"` diff --git a/drivers/terabox/types.go b/drivers/terabox/types.go index 25bd99c698f..890d53056ea 100644 --- a/drivers/terabox/types.go +++ b/drivers/terabox/types.go @@ -1,9 +1,10 @@ -package terbox +package terabox import ( - "github.com/alist-org/alist/v3/internal/model" "strconv" "time" + + "github.com/alist-org/alist/v3/internal/model" ) type File struct { diff --git a/drivers/terabox/util.go b/drivers/terabox/util.go index 825a2ee706d..c162773ecd4 100644 --- a/drivers/terabox/util.go +++ b/drivers/terabox/util.go @@ -1,10 +1,11 @@ -package terbox +package terabox import ( "encoding/base64" "fmt" "net/http" "net/url" + "regexp" "strconv" "strings" "time" @@ -15,7 +16,39 @@ import ( "github.com/go-resty/resty/v2" ) -func (d *Terabox) request(furl string, method string, callback base.ReqCallback, resp interface{}) ([]byte, error) { +func getStrBetween(raw, start, end string) string { + regexPattern := fmt.Sprintf(`%s(.*?)%s`, regexp.QuoteMeta(start), regexp.QuoteMeta(end)) + regex := regexp.MustCompile(regexPattern) + matches := regex.FindStringSubmatch(raw) + if len(matches) < 2 { + return "" + } + mid := matches[1] + return mid +} + +func (d *Terabox) resetJsToken() error { + u := "https://www.terabox.com/main" + res, err := base.RestyClient.R().SetHeaders(map[string]string{ + "Cookie": d.Cookie, + "Accept": "application/json, text/plain, */*", + "Referer": "https://www.terabox.com/", + "User-Agent": base.UserAgent, + "X-Requested-With": "XMLHttpRequest", + }).Get(u) + if err != nil { + return err + } + html := res.String() + jsToken := getStrBetween(html, "`function%20fn%28a%29%7Bwindow.jsToken%20%3D%20a%7D%3Bfn%28%22", "%22%29`") + if jsToken == "" { + return fmt.Errorf("jsToken not found, html: %s", html) + } + d.JsToken = jsToken + return nil +} + +func (d *Terabox) request(furl string, method string, callback base.ReqCallback, resp interface{}, noRetry ...bool) ([]byte, error) { req := base.RestyClient.R() req.SetHeaders(map[string]string{ "Cookie": d.Cookie, @@ -41,6 +74,17 @@ func (d *Terabox) request(furl string, method string, callback base.ReqCallback, if err != nil { return nil, err } + errno := utils.Json.Get(res.Body(), "errno").ToInt() + if errno == 4000023 { + // reget jsToken + err = d.resetJsToken() + if err != nil { + return nil, err + } + if !utils.IsBool(noRetry...) { + return d.request(furl, method, callback, resp, true) + } + } return res.Body(), nil }