Skip to content

Commit

Permalink
fix(terabox): auto refresh JsToken (close #5277)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Sep 25, 2023
1 parent a64dd48 commit 6b67a36
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
5 changes: 3 additions & 2 deletions drivers/terabox/driver.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package terbox
package terabox

import (
"bytes"
Expand All @@ -23,6 +23,7 @@ import (
type Terabox struct {
model.Storage
Addition
JsToken string
}

func (d *Terabox) Config() driver.Config {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions drivers/terabox/meta.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package terbox
package terabox

import (
"github.com/alist-org/alist/v3/internal/driver"
Expand All @@ -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"`
Expand Down
5 changes: 3 additions & 2 deletions drivers/terabox/types.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
48 changes: 46 additions & 2 deletions drivers/terabox/util.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package terbox
package terabox

import (
"encoding/base64"
"fmt"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"time"
Expand All @@ -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,
Expand All @@ -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
}

Expand Down

3 comments on commit 6b67a36

@anwen-anyi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

提示如下错误:任何文件都是这样
image

Log

�[37mDEBU�[0m[2023-09-26 09:25:39]/source/internal/op/path.go:25 github.com/alist-org/alist/v3/internal/op.GetStorageAndActualPath() use storage:  /Terabox                       
�[37mDEBU�[0m[2023-09-26 09:25:39]/source/internal/op/fs.go:167 github.com/alist-org/alist/v3/internal/op.Get() op.Get /新建文本文档 (7).txt                       
�[37mDEBU�[0m[2023-09-26 09:25:39]/source/internal/op/fs.go:108 github.com/alist-org/alist/v3/internal/op.List() op.List /                                    
�[37mDEBU�[0m[2023-09-26 09:25:39]/source/internal/op/fs.go:112 github.com/alist-org/alist/v3/internal/op.List() use cache when list /                        
�[37mDEBU�[0m[2023-09-26 09:25:39]/source/internal/op/path.go:25 github.com/alist-org/alist/v3/internal/op.GetStorageAndActualPath() use storage:  /Terabox                       
�[37mDEBU�[0m[2023-09-26 09:25:39]/source/internal/op/path.go:25 github.com/alist-org/alist/v3/internal/op.GetStorageAndActualPath() use storage:  /Terabox                       
�[37mDEBU�[0m[2023-09-26 09:25:39]/source/internal/op/fs.go:167 github.com/alist-org/alist/v3/internal/op.Get() op.Get /新建文本文档 (7).txt                       
�[37mDEBU�[0m[2023-09-26 09:25:39]/source/internal/op/fs.go:108 github.com/alist-org/alist/v3/internal/op.List() op.List /                                    
�[37mDEBU�[0m[2023-09-26 09:25:39]/source/internal/op/fs.go:112 github.com/alist-org/alist/v3/internal/op.List() use cache when list /                        
�[31mERRO�[0m[2023-09-26 09:25:41]/source/internal/fs/fs.go:49 github.com/alist-org/alist/v3/internal/fs.Link() failed link /Terabox/新建文本文档 (7).txt: fid 680805135623529 no dlink found, errno: 113
failed get link
github.com/alist-org/alist/v3/internal/op.Link.func1
	/source/internal/op/fs.go:260
github.com/alist-org/alist/v3/pkg/singleflight.(*Group[...]).doCall.func2
	/source/pkg/singleflight/singleflight.go:193
github.com/alist-org/alist/v3/pkg/singleflight.(*Group[...]).doCall
	/source/pkg/singleflight/singleflight.go:195
github.com/alist-org/alist/v3/pkg/singleflight.(*Group[...]).Do
	/source/pkg/singleflight/singleflight.go:108
github.com/alist-org/alist/v3/internal/op.Link
	/source/internal/op/fs.go:270
github.com/alist-org/alist/v3/internal/fs.link
	/source/internal/fs/link.go:19
github.com/alist-org/alist/v3/internal/fs.Link
	/source/internal/fs/fs.go:47
github.com/alist-org/alist/v3/server/handles.FsGet
	/source/server/handles/fsread.go:295
github.com/gin-gonic/gin.(*Context).Next
	/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174
github.com/alist-org/alist/v3/server/middlewares.Auth
	/source/server/middlewares/auth.go:67
github.com/gin-gonic/gin.(*Context).Next
	/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174
github.com/alist-org/alist/v3/server/middlewares.StoragesLoaded
	/source/server/middlewares/check.go:14
github.com/gin-gonic/gin.(*Context).Next
	/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174
github.com/gin-gonic/gin.CustomRecoveryWithWriter.func1
	/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/recovery.go:102
github.com/gin-gonic/gin.(*Context).Next
	/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174
github.com/gin-gonic/gin.LoggerWithConfig.func1
	/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/logger.go:240
github.com/gin-gonic/gin.(*Context).Next
	/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174
github.com/gin-gonic/gin.(*Engine).handleHTTPRequest
	/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:620
github.com/gin-gonic/gin.(*Engine).ServeHTTP
	/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:576
net/http.serverHandler.ServeHTTP
	/usr/local/go/src/net/http/server.go:2938
net/http.(*conn).serve
	/usr/local/go/src/net/http/server.go:2009
runtime.goexit
	/usr/local/go/src/runtime/asm_amd64.s:1650
failed link 

@xhofe
Copy link
Member Author

@xhofe xhofe commented on 6b67a36 Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

提示如下错误:任何文件都是这样 image

Log

�[37mDEBU�[0m[2023-09-26 09:25:39]/source/internal/op/path.go:25 github.com/alist-org/alist/v3/internal/op.GetStorageAndActualPath() use storage:  /Terabox                       
�[37mDEBU�[0m[2023-09-26 09:25:39]/source/internal/op/fs.go:167 github.com/alist-org/alist/v3/internal/op.Get() op.Get /新建文本文档 (7).txt                       
�[37mDEBU�[0m[2023-09-26 09:25:39]/source/internal/op/fs.go:108 github.com/alist-org/alist/v3/internal/op.List() op.List /                                    
�[37mDEBU�[0m[2023-09-26 09:25:39]/source/internal/op/fs.go:112 github.com/alist-org/alist/v3/internal/op.List() use cache when list /                        
�[37mDEBU�[0m[2023-09-26 09:25:39]/source/internal/op/path.go:25 github.com/alist-org/alist/v3/internal/op.GetStorageAndActualPath() use storage:  /Terabox                       
�[37mDEBU�[0m[2023-09-26 09:25:39]/source/internal/op/path.go:25 github.com/alist-org/alist/v3/internal/op.GetStorageAndActualPath() use storage:  /Terabox                       
�[37mDEBU�[0m[2023-09-26 09:25:39]/source/internal/op/fs.go:167 github.com/alist-org/alist/v3/internal/op.Get() op.Get /新建文本文档 (7).txt                       
�[37mDEBU�[0m[2023-09-26 09:25:39]/source/internal/op/fs.go:108 github.com/alist-org/alist/v3/internal/op.List() op.List /                                    
�[37mDEBU�[0m[2023-09-26 09:25:39]/source/internal/op/fs.go:112 github.com/alist-org/alist/v3/internal/op.List() use cache when list /                        
�[31mERRO�[0m[2023-09-26 09:25:41]/source/internal/fs/fs.go:49 github.com/alist-org/alist/v3/internal/fs.Link() failed link /Terabox/新建文本文档 (7).txt: fid 680805135623529 no dlink found, errno: 113
failed get link
github.com/alist-org/alist/v3/internal/op.Link.func1
	/source/internal/op/fs.go:260
github.com/alist-org/alist/v3/pkg/singleflight.(*Group[...]).doCall.func2
	/source/pkg/singleflight/singleflight.go:193
github.com/alist-org/alist/v3/pkg/singleflight.(*Group[...]).doCall
	/source/pkg/singleflight/singleflight.go:195
github.com/alist-org/alist/v3/pkg/singleflight.(*Group[...]).Do
	/source/pkg/singleflight/singleflight.go:108
github.com/alist-org/alist/v3/internal/op.Link
	/source/internal/op/fs.go:270
github.com/alist-org/alist/v3/internal/fs.link
	/source/internal/fs/link.go:19
github.com/alist-org/alist/v3/internal/fs.Link
	/source/internal/fs/fs.go:47
github.com/alist-org/alist/v3/server/handles.FsGet
	/source/server/handles/fsread.go:295
github.com/gin-gonic/gin.(*Context).Next
	/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174
github.com/alist-org/alist/v3/server/middlewares.Auth
	/source/server/middlewares/auth.go:67
github.com/gin-gonic/gin.(*Context).Next
	/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174
github.com/alist-org/alist/v3/server/middlewares.StoragesLoaded
	/source/server/middlewares/check.go:14
github.com/gin-gonic/gin.(*Context).Next
	/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174
github.com/gin-gonic/gin.CustomRecoveryWithWriter.func1
	/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/recovery.go:102
github.com/gin-gonic/gin.(*Context).Next
	/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174
github.com/gin-gonic/gin.LoggerWithConfig.func1
	/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/logger.go:240
github.com/gin-gonic/gin.(*Context).Next
	/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174
github.com/gin-gonic/gin.(*Engine).handleHTTPRequest
	/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:620
github.com/gin-gonic/gin.(*Engine).ServeHTTP
	/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:576
net/http.serverHandler.ServeHTTP
	/usr/local/go/src/net/http/server.go:2938
net/http.(*conn).serve
	/usr/local/go/src/net/http/server.go:2009
runtime.goexit
	/usr/local/go/src/runtime/asm_amd64.s:1650
failed link 

用 crack

@anwen-anyi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

选择crack下载 使用IDM下载会提示如下
image


使用浏览器下载,没反应QAQ,我这里没反应 代理应该是没问题的
image
image


推送到 Aria2 下载也错误~
image


使用Google Chrome倒是可以下载 很奇怪 😹
image

Please sign in to comment.