fix(terabox): auto refresh `JsToken` (close #5277)

pull/5308/head
Andy Hsu 2023-09-25 16:38:05 +08:00
parent a64dd4885e
commit 6b67a36d63
4 changed files with 55 additions and 9 deletions

View File

@ -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

View File

@ -1,4 +1,4 @@
package terbox
package terabox
import (
"github.com/alist-org/alist/v3/internal/driver"
@ -8,7 +8,7 @@ import (
type Addition struct {
driver.RootPath
Cookie string `json:"cookie" required:"true"`
JsToken string `json:"js_token" type:"string" 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"`

View File

@ -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 {

View File

@ -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
}