Skip to content

Commit

Permalink
Merge branch 'alist-org:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Three-taile-dragon authored Oct 7, 2024
2 parents 040fcab + 5f19d73 commit c11054b
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 35 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
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.3
github.com/hekmon/transmissionrpc/v3 v3.0.0
github.com/hirochachacha/go-smb2 v1.1.0
github.com/ipfs/go-ipfs-api v0.7.0
github.com/jlaffaye/ftp v0.2.0
Expand Down Expand Up @@ -86,6 +87,8 @@ require (
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hekmon/cunits/v2 v2.1.0 // indirect
github.com/ipfs/boxo v0.12.0 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
)
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,17 @@ github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hekmon/cunits/v2 v2.1.0 h1:k6wIjc4PlacNOHwKEMBgWV2/c8jyD4eRMs5mR1BBhI0=
github.com/hekmon/cunits/v2 v2.1.0/go.mod h1:9r1TycXYXaTmEWlAIfFV8JT+Xo59U96yUJAYHxzii2M=
github.com/hekmon/transmissionrpc/v3 v3.0.0 h1:0Fb11qE0IBh4V4GlOwHNYpqpjcYDp5GouolwrpmcUDQ=
github.com/hekmon/transmissionrpc/v3 v3.0.0/go.mod h1:38SlNhFzinVUuY87wGj3acOmRxeYZAZfrj6Re7UgCDg=
github.com/hirochachacha/go-smb2 v1.1.0 h1:b6hs9qKIql9eVXAiN0M2wSFY5xnhbHAQoCwRKbaRTZI=
github.com/hirochachacha/go-smb2 v1.1.0/go.mod h1:8F1A4d5EZzrGu5R7PU163UcMRDJQl4FtcxjBfsY8TZE=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand Down
12 changes: 8 additions & 4 deletions internal/conf/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@ const (
Aria2Uri = "aria2_uri"
Aria2Secret = "aria2_secret"

// transmission
TransmissionUri = "transmission_uri"
TransmissionSeedtime = "transmission_seedtime"

// single
Token = "token"
IndexProgress = "index_progress"

//SSO
// SSO
SSOClientId = "sso_client_id"
SSOClientSecret = "sso_client_secret"
SSOLoginEnabled = "sso_login_enabled"
Expand All @@ -73,7 +77,7 @@ const (
SSODefaultPermission = "sso_default_permission"
SSOCompatibilityMode = "sso_compatibility_mode"

//ldap
// ldap
LdapLoginEnabled = "ldap_login_enabled"
LdapServer = "ldap_server"
LdapManagerDN = "ldap_manager_dn"
Expand All @@ -84,7 +88,7 @@ const (
LdapDefaultDir = "ldap_default_dir"
LdapLoginTips = "ldap_login_tips"

//s3
// s3
S3Buckets = "s3_buckets"
S3AccessKeyId = "s3_access_key_id"
S3SecretAccessKey = "s3_secret_access_key"
Expand All @@ -97,7 +101,7 @@ const (
const (
UNKNOWN = iota
FOLDER
//OFFICE
// OFFICE
VIDEO
AUDIO
TEXT
Expand Down
1 change: 1 addition & 0 deletions internal/offline_download/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ import (
_ "github.com/alist-org/alist/v3/internal/offline_download/http"
_ "github.com/alist-org/alist/v3/internal/offline_download/pikpak"
_ "github.com/alist-org/alist/v3/internal/offline_download/qbit"
_ "github.com/alist-org/alist/v3/internal/offline_download/transmission"
)
13 changes: 13 additions & 0 deletions internal/offline_download/tool/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ outer:
}
}
}

if t.tool.Name() == "transmission" {
// hack for transmission
seedTime := setting.GetInt(conf.TransmissionSeedtime, 0)
if seedTime >= 0 {
t.Status = "offline download completed, waiting for seeding"
<-time.After(time.Minute * time.Duration(seedTime))
err := t.tool.Remove(t)
if err != nil {
log.Errorln(err.Error())
}
}
}
return nil
}

Expand Down
Loading

0 comments on commit c11054b

Please sign in to comment.