Skip to content

Commit

Permalink
fix Terabox ( AlistGo#6961 AlistGo#6983 )
Browse files Browse the repository at this point in the history
  • Loading branch information
URenko committed Sep 28, 2024
1 parent 6106a2d commit 8ae8e51
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 27 deletions.
66 changes: 56 additions & 10 deletions drivers/terabox/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
stdpath "path"
"strconv"
"strings"
"time"

"github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/pkg/utils"
Expand All @@ -23,7 +24,9 @@ import (
type Terabox struct {
model.Storage
Addition
JsToken string
JsToken string
url_domain_prefix string
base_url string
}

func (d *Terabox) Config() driver.Config {
Expand All @@ -36,6 +39,8 @@ func (d *Terabox) GetAddition() driver.Additional {

func (d *Terabox) Init(ctx context.Context) error {
var resp CheckLoginResp
d.base_url = "https://www.terabox.com"
d.url_domain_prefix = "jp"
_, err := d.get("/api/check/login", nil, &resp)
if err != nil {
return err
Expand Down Expand Up @@ -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 {
_, 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
}

Expand Down Expand Up @@ -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 {
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()
if err != nil {
return err
Expand Down Expand Up @@ -157,23 +185,28 @@ func (d *Terabox) Put(ctx context.Context, dstDir model.Obj, stream model.FileSt
rawPath := stdpath.Join(dstDir.GetPath(), stream.GetName())
path := encodeURIComponent(rawPath)
block_list_str := fmt.Sprintf("[%s]", strings.Join(block_list, ","))
data := fmt.Sprintf("path=%s&size=%d&isdir=0&autoinit=1&block_list=%s",
path, stream.GetSize(),
block_list_str)
params := map[string]string{}
data := map[string]string{
"path": rawPath,
"autoinit": "1",
"target_path": dstDir.GetPath(),
"block_list": block_list_str,
"local_mtime": strconv.FormatInt(time.Now().Unix(), 10),
}
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 {
return err
}
log.Debugf("%+v", precreateResp)
if precreateResp.Errno != 0 {
log.Debugln(string(res))
return fmt.Errorf("[terabox] failed to precreate file, errno: %d", precreateResp.Errno)
}
if precreateResp.ReturnType == 2 {
return nil
}
params = map[string]string{
params := map[string]string{
"method": "upload",
"path": path,
"uploadid": precreateResp.Uploadid,
Expand All @@ -200,7 +233,7 @@ func (d *Terabox) Put(ctx context.Context, dstDir model.Obj, stream model.FileSt
if err != nil {
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)
res, err := base.RestyClient.R().
SetContext(ctx).
Expand All @@ -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)))
}
}
_, 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
}

Expand Down
4 changes: 4 additions & 0 deletions drivers/terabox/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,7 @@ type PrecreateResp struct {
type CheckLoginResp struct {
Errno int `json:"errno"`
}

type LocateUploadResp struct {
Host string `json:"host"`
}
41 changes: 24 additions & 17 deletions drivers/terabox/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
)

func getStrBetween(raw, start, end string) string {
Expand All @@ -28,11 +29,11 @@ func getStrBetween(raw, start, end string) string {
}

func (d *Terabox) resetJsToken() error {
u := "https://www.terabox.com/main"
u := d.base_url
res, err := base.RestyClient.R().SetHeaders(map[string]string{
"Cookie": d.Cookie,
"Accept": "application/json, text/plain, */*",
"Referer": "https://www.terabox.com/",
"Referer": d.base_url,
"User-Agent": base.UserAgent,
"X-Requested-With": "XMLHttpRequest",
}).Get(u)
Expand All @@ -48,12 +49,12 @@ func (d *Terabox) resetJsToken() error {
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.SetHeaders(map[string]string{
"Cookie": d.Cookie,
"Accept": "application/json, text/plain, */*",
"Referer": "https://www.terabox.com/",
"Referer": d.base_url,
"User-Agent": base.UserAgent,
"X-Requested-With": "XMLHttpRequest",
})
Expand All @@ -70,7 +71,7 @@ func (d *Terabox) request(furl string, method string, callback base.ReqCallback,
if resp != nil {
req.SetResult(resp)
}
res, err := req.Execute(method, furl)
res, err := req.Execute(method, d.base_url+rurl)
if err != nil {
return nil, err
}
Expand All @@ -82,29 +83,44 @@ func (d *Terabox) request(furl string, method string, callback base.ReqCallback,
return nil, err
}
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
}

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 {
req.SetQueryParams(params)
}
}, resp)
}

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 {
req.SetQueryParams(params)
}
req.SetBody(data)
}, 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) {
page := 1
num := 100
Expand Down Expand Up @@ -237,15 +253,6 @@ func (d *Terabox) manage(opera string, filelist interface{}) ([]byte, error) {
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 {
r := url.QueryEscape(str)
r = strings.ReplaceAll(r, "+", "%20")
Expand Down

0 comments on commit 8ae8e51

Please sign in to comment.