fix: Terabox ( close #6961 close #6983 in #7279)

This commit is contained in:
URenko 2024-10-04 07:46:10 +00:00 committed by GitHub
parent bdf4b52885
commit 5f19d73fcc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 84 additions and 27 deletions

View File

@ -11,6 +11,7 @@ import (
stdpath "path" stdpath "path"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/alist-org/alist/v3/drivers/base" "github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/pkg/utils" "github.com/alist-org/alist/v3/pkg/utils"
@ -23,7 +24,9 @@ import (
type Terabox struct { type Terabox struct {
model.Storage model.Storage
Addition Addition
JsToken string JsToken string
url_domain_prefix string
base_url string
} }
func (d *Terabox) Config() driver.Config { func (d *Terabox) Config() driver.Config {
@ -36,6 +39,8 @@ func (d *Terabox) GetAddition() driver.Additional {
func (d *Terabox) Init(ctx context.Context) error { func (d *Terabox) Init(ctx context.Context) error {
var resp CheckLoginResp var resp CheckLoginResp
d.base_url = "https://www.terabox.com"
d.url_domain_prefix = "jp"
_, err := d.get("/api/check/login", nil, &resp) _, err := d.get("/api/check/login", nil, &resp)
if err != nil { if err != nil {
return err return err
@ -71,7 +76,16 @@ func (d *Terabox) Link(ctx context.Context, file model.Obj, args model.LinkArgs)
} }
func (d *Terabox) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error { func (d *Terabox) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
_, err := d.create(stdpath.Join(parentDir.GetPath(), dirName), 0, 1, "", "") params := map[string]string{
"a": "commit",
}
data := map[string]string{
"path": stdpath.Join(parentDir.GetPath(), dirName),
"isdir": "1",
"block_list": "[]",
}
res, err := d.post_form("/api/create", params, data, nil)
log.Debugln(string(res))
return err return err
} }
@ -117,6 +131,20 @@ func (d *Terabox) Remove(ctx context.Context, obj model.Obj) error {
} }
func (d *Terabox) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error { func (d *Terabox) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
resp, err := base.RestyClient.R().
SetContext(ctx).
Get("https://" + d.url_domain_prefix + "-data.terabox.com/rest/2.0/pcs/file?method=locateupload")
if err != nil {
return err
}
var locateupload_resp LocateUploadResp
err = utils.Json.Unmarshal(resp.Body(), &locateupload_resp)
if err != nil {
log.Debugln(resp)
return err
}
log.Debugln(locateupload_resp)
tempFile, err := stream.CacheFullInTempFile() tempFile, err := stream.CacheFullInTempFile()
if err != nil { if err != nil {
return err return err
@ -157,23 +185,28 @@ func (d *Terabox) Put(ctx context.Context, dstDir model.Obj, stream model.FileSt
rawPath := stdpath.Join(dstDir.GetPath(), stream.GetName()) rawPath := stdpath.Join(dstDir.GetPath(), stream.GetName())
path := encodeURIComponent(rawPath) path := encodeURIComponent(rawPath)
block_list_str := fmt.Sprintf("[%s]", strings.Join(block_list, ",")) block_list_str := fmt.Sprintf("[%s]", strings.Join(block_list, ","))
data := fmt.Sprintf("path=%s&size=%d&isdir=0&autoinit=1&block_list=%s", data := map[string]string{
path, stream.GetSize(), "path": rawPath,
block_list_str) "autoinit": "1",
params := map[string]string{} "target_path": dstDir.GetPath(),
"block_list": block_list_str,
"local_mtime": strconv.FormatInt(time.Now().Unix(), 10),
}
var precreateResp PrecreateResp var precreateResp PrecreateResp
_, err = d.post("/api/precreate", params, data, &precreateResp) log.Debugln(data)
res, err := d.post_form("/api/precreate", nil, data, &precreateResp)
if err != nil { if err != nil {
return err return err
} }
log.Debugf("%+v", precreateResp) log.Debugf("%+v", precreateResp)
if precreateResp.Errno != 0 { if precreateResp.Errno != 0 {
log.Debugln(string(res))
return fmt.Errorf("[terabox] failed to precreate file, errno: %d", precreateResp.Errno) return fmt.Errorf("[terabox] failed to precreate file, errno: %d", precreateResp.Errno)
} }
if precreateResp.ReturnType == 2 { if precreateResp.ReturnType == 2 {
return nil return nil
} }
params = map[string]string{ params := map[string]string{
"method": "upload", "method": "upload",
"path": path, "path": path,
"uploadid": precreateResp.Uploadid, "uploadid": precreateResp.Uploadid,
@ -200,7 +233,7 @@ func (d *Terabox) Put(ctx context.Context, dstDir model.Obj, stream model.FileSt
if err != nil { if err != nil {
return err return err
} }
u := "https://c-jp.terabox.com/rest/2.0/pcs/superfile2" u := "https://" + locateupload_resp.Host + "/rest/2.0/pcs/superfile2"
params["partseq"] = strconv.Itoa(partseq) params["partseq"] = strconv.Itoa(partseq)
res, err := base.RestyClient.R(). res, err := base.RestyClient.R().
SetContext(ctx). SetContext(ctx).
@ -216,7 +249,20 @@ func (d *Terabox) Put(ctx context.Context, dstDir model.Obj, stream model.FileSt
up(float64(i) * 100 / float64(len(precreateResp.BlockList))) up(float64(i) * 100 / float64(len(precreateResp.BlockList)))
} }
} }
_, err = d.create(rawPath, stream.GetSize(), 0, precreateResp.Uploadid, block_list_str) params = map[string]string{
"isdir": "0",
"rtype": "1",
}
data = map[string]string{
"path": rawPath,
"size": strconv.FormatInt(stream.GetSize(), 10),
"uploadid": precreateResp.Uploadid,
"target_path": dstDir.GetPath(),
"block_list": block_list_str,
"local_mtime": strconv.FormatInt(time.Now().Unix(), 10),
}
res, err = d.post_form("/api/create", params, data, nil)
log.Debugln(string(res))
return err return err
} }

View File

@ -95,3 +95,7 @@ type PrecreateResp struct {
type CheckLoginResp struct { type CheckLoginResp struct {
Errno int `json:"errno"` Errno int `json:"errno"`
} }
type LocateUploadResp struct {
Host string `json:"host"`
}

View File

@ -14,6 +14,7 @@ import (
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils" "github.com/alist-org/alist/v3/pkg/utils"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
) )
func getStrBetween(raw, start, end string) string { func getStrBetween(raw, start, end string) string {
@ -28,11 +29,11 @@ func getStrBetween(raw, start, end string) string {
} }
func (d *Terabox) resetJsToken() error { func (d *Terabox) resetJsToken() error {
u := "https://www.terabox.com/main" u := d.base_url
res, err := base.RestyClient.R().SetHeaders(map[string]string{ res, err := base.RestyClient.R().SetHeaders(map[string]string{
"Cookie": d.Cookie, "Cookie": d.Cookie,
"Accept": "application/json, text/plain, */*", "Accept": "application/json, text/plain, */*",
"Referer": "https://www.terabox.com/", "Referer": d.base_url,
"User-Agent": base.UserAgent, "User-Agent": base.UserAgent,
"X-Requested-With": "XMLHttpRequest", "X-Requested-With": "XMLHttpRequest",
}).Get(u) }).Get(u)
@ -48,12 +49,12 @@ func (d *Terabox) resetJsToken() error {
return nil return nil
} }
func (d *Terabox) request(furl string, method string, callback base.ReqCallback, resp interface{}, noRetry ...bool) ([]byte, error) { func (d *Terabox) request(rurl string, method string, callback base.ReqCallback, resp interface{}, noRetry ...bool) ([]byte, error) {
req := base.RestyClient.R() req := base.RestyClient.R()
req.SetHeaders(map[string]string{ req.SetHeaders(map[string]string{
"Cookie": d.Cookie, "Cookie": d.Cookie,
"Accept": "application/json, text/plain, */*", "Accept": "application/json, text/plain, */*",
"Referer": "https://www.terabox.com/", "Referer": d.base_url,
"User-Agent": base.UserAgent, "User-Agent": base.UserAgent,
"X-Requested-With": "XMLHttpRequest", "X-Requested-With": "XMLHttpRequest",
}) })
@ -70,7 +71,7 @@ func (d *Terabox) request(furl string, method string, callback base.ReqCallback,
if resp != nil { if resp != nil {
req.SetResult(resp) req.SetResult(resp)
} }
res, err := req.Execute(method, furl) res, err := req.Execute(method, d.base_url+rurl)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -82,14 +83,20 @@ func (d *Terabox) request(furl string, method string, callback base.ReqCallback,
return nil, err return nil, err
} }
if !utils.IsBool(noRetry...) { if !utils.IsBool(noRetry...) {
return d.request(furl, method, callback, resp, true) return d.request(rurl, method, callback, resp, true)
} }
} else if errno == -6 {
log.Debugln(res.Header())
d.url_domain_prefix = res.Header()["Url-Domain-Prefix"][0]
d.base_url = "https://" + d.url_domain_prefix + ".terabox.com"
log.Debugln("Redirect base_url to", d.base_url)
return d.request(rurl, method, callback, resp, noRetry...)
} }
return res.Body(), nil return res.Body(), nil
} }
func (d *Terabox) get(pathname string, params map[string]string, resp interface{}) ([]byte, error) { func (d *Terabox) get(pathname string, params map[string]string, resp interface{}) ([]byte, error) {
return d.request("https://www.terabox.com"+pathname, http.MethodGet, func(req *resty.Request) { return d.request(pathname, http.MethodGet, func(req *resty.Request) {
if params != nil { if params != nil {
req.SetQueryParams(params) req.SetQueryParams(params)
} }
@ -97,7 +104,7 @@ func (d *Terabox) get(pathname string, params map[string]string, resp interface{
} }
func (d *Terabox) post(pathname string, params map[string]string, data interface{}, resp interface{}) ([]byte, error) { func (d *Terabox) post(pathname string, params map[string]string, data interface{}, resp interface{}) ([]byte, error) {
return d.request("https://www.terabox.com"+pathname, http.MethodPost, func(req *resty.Request) { return d.request(pathname, http.MethodPost, func(req *resty.Request) {
if params != nil { if params != nil {
req.SetQueryParams(params) req.SetQueryParams(params)
} }
@ -105,6 +112,15 @@ func (d *Terabox) post(pathname string, params map[string]string, data interface
}, resp) }, resp)
} }
func (d *Terabox) post_form(pathname string, params map[string]string, data map[string]string, resp interface{}) ([]byte, error) {
return d.request(pathname, http.MethodPost, func(req *resty.Request) {
if params != nil {
req.SetQueryParams(params)
}
req.SetFormData(data)
}, resp)
}
func (d *Terabox) getFiles(dir string) ([]File, error) { func (d *Terabox) getFiles(dir string) ([]File, error) {
page := 1 page := 1
num := 100 num := 100
@ -237,15 +253,6 @@ func (d *Terabox) manage(opera string, filelist interface{}) ([]byte, error) {
return d.post("/api/filemanager", params, data, nil) return d.post("/api/filemanager", params, data, nil)
} }
func (d *Terabox) create(path string, size int64, isdir int, uploadid, block_list string) ([]byte, error) {
params := map[string]string{}
data := fmt.Sprintf("path=%s&size=%d&isdir=%d", encodeURIComponent(path), size, isdir)
if uploadid != "" {
data += fmt.Sprintf("&uploadid=%s&block_list=%s", uploadid, block_list)
}
return d.post("/api/create", params, data, nil)
}
func encodeURIComponent(str string) string { func encodeURIComponent(str string) string {
r := url.QueryEscape(str) r := url.QueryEscape(str)
r = strings.ReplaceAll(r, "+", "%20") r = strings.ReplaceAll(r, "+", "%20")