Skip to content

Commit

Permalink
chore: Merge pull request AlistGo#1020 from foxxorcat/dev
Browse files Browse the repository at this point in the history
fix(xunlei):download link speed limit
  • Loading branch information
xhofe authored May 1, 2022
2 parents 75c9842 + 11830bb commit 427ae56
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 101 deletions.
79 changes: 74 additions & 5 deletions drivers/xunlei/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/Xhofe/alist/conf"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/Xhofe/alist/utils"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/go-resty/resty/v2"
"github.com/google/uuid"
)

type XunLeiCloud struct{}
Expand Down Expand Up @@ -46,22 +48,84 @@ func (driver XunLeiCloud) Items() []base.Item {
Required: true,
Description: "account password",
},
{
Name: "captcha_token",
Label: "verified captcha token",
Type: base.TypeString,
},
{
Name: "root_folder",
Label: "root folder file_id",
Type: base.TypeString,
},
{
Name: "client_version",
Label: "client version",
Default: "7.43.0.7998",
Type: base.TypeString,
Required: true,
},
{
Name: "client_id",
Label: "client id",
Default: "Xp6vsxz_7IYVw2BB",
Type: base.TypeString,
Required: true,
},
{
Name: "client_secret",
Label: "client secret",
Default: "Xp6vsy4tN9toTVdMSpomVdXpRmES",
Type: base.TypeString,
Required: true,
},
{
Name: "algorithms",
Label: "algorithms",
Default: "hrVPGbeqYPs+CIscj05VpAtjalzY5yjpvlMS8bEo,DrI0uTP,HHK0VXyMgY0xk2K0o,BBaXsExvL3GadmIacjWv7ISUJp3ifAwqbJumu,5toJ7ejB+bh1,5LsZTFAFjgvFvIl1URBgOAJ,QcJ5Ry+,hYgZVz8r7REROaCYfd9,zw6gXgkk/8TtGrmx6EGfekPESLnbZfDFwqR,gtSwLnMBa8h12nF3DU6+LwEQPHxd,fMG8TvtAYbCkxuEbIm0Xi/Lb7Z",
Type: base.TypeString,
Required: true,
},
{
Name: "package_name",
Label: "package name",
Default: "com.xunlei.downloadprovider",
Type: base.TypeString,
Required: true,
},
{
Name: "user_agent",
Label: "user agent",
Default: "ANDROID-com.xunlei.downloadprovider/7.43.0.7998 netWorkType/WIFI appid/40 deviceName/Samsung_Sm-g9810 deviceModel/SM-G9810 OSVersion/7.1.2 protocolVersion/301 platformVersion/10 sdkVersion/220200 Oauth2Client/0.9 (Linux 4_0_9+) (JAVA 0)",
Type: base.TypeString,
Required: false,
},
{
Name: "device_id",
Label: "device id",
Default: utils.GetMD5Encode(uuid.NewString()),
Type: base.TypeString,
Required: false,
},
}
}

func (driver XunLeiCloud) Save(account *model.Account, old *model.Account) error {
if account == nil {
return nil
}

client := GetClient(account)
// 指定验证通过的captchaToken
if client.captchaToken != "" {
client.captchaToken = account.CaptchaToken
account.CaptchaToken = ""
}

if client.token == "" {
return client.Login(account)
}

account.Status = "work"
model.SaveAccount(account)
return nil
Expand Down Expand Up @@ -105,6 +169,7 @@ func (driver XunLeiCloud) Files(path string, account *model.Account) ([]model.Fi
return nil, err
}

time.Sleep(time.Millisecond * 400)
files := make([]model.File, 0)
for {
var fileList FileList
Expand Down Expand Up @@ -161,7 +226,9 @@ func (driver XunLeiCloud) Link(args base.Args, account *model.Account) (*base.Li
return nil, base.ErrNotFile
}
var lFile Files
_, err = GetClient(account).Request("GET", FILE_API_URL+"/"+file.Id, func(r *resty.Request) {
clinet := GetClient(account)
_, err = clinet.Request("GET", FILE_API_URL+"/{fileID}", func(r *resty.Request) {
r.SetPathParam("fileID", file.Id)
r.SetQueryParam("with_audit", "true")
r.SetResult(&lFile)
}, account)
Expand All @@ -170,7 +237,7 @@ func (driver XunLeiCloud) Link(args base.Args, account *model.Account) (*base.Li
}
return &base.Link{
Headers: []base.Header{
{Name: "User-Agent", Value: base.UserAgent},
{Name: "User-Agent", Value: clinet.userAgent},
},
Url: lFile.WebContentLink,
}, nil
Expand Down Expand Up @@ -201,7 +268,8 @@ func (driver XunLeiCloud) Rename(src string, dst string, account *model.Account)
if err != nil {
return err
}
_, err = GetClient(account).Request("PATCH", FILE_API_URL+"/"+srcFile.Id, func(r *resty.Request) {
_, err = GetClient(account).Request("PATCH", FILE_API_URL+"/{fileID}", func(r *resty.Request) {
r.SetPathParam("fileID", srcFile.Id)
r.SetBody(&base.Json{"name": filepath.Base(dst)})
}, account)
return err
Expand Down Expand Up @@ -270,7 +338,8 @@ func (driver XunLeiCloud) Delete(path string, account *model.Account) error {
if err != nil {
return err
}
_, err = GetClient(account).Request("PATCH", FILE_API_URL+"/"+srcFile.Id+"/trash", func(r *resty.Request) {
_, err = GetClient(account).Request("PATCH", FILE_API_URL+"/{fileID}/trash", func(r *resty.Request) {
r.SetPathParam("fileID", srcFile.Id)
r.SetBody(&base.Json{})
}, account)
return err
Expand Down Expand Up @@ -318,6 +387,7 @@ func (driver XunLeiCloud) Upload(file *model.FileStream, account *model.Account)

param := resp.Resumable.Params
if resp.UploadType == UPLOAD_TYPE_RESUMABLE {
param.Endpoint = strings.TrimLeft(param.Endpoint, param.Bucket+".")
client, err := oss.New(param.Endpoint, param.AccessKeyID, param.AccessKeySecret, oss.SecurityToken(param.SecurityToken), oss.EnableMD5(true))
if err != nil {
return err
Expand All @@ -331,7 +401,6 @@ func (driver XunLeiCloud) Upload(file *model.FileStream, account *model.Account)
return err
}
}
time.Sleep(time.Millisecond * 200)
return nil
}

Expand Down
12 changes: 5 additions & 7 deletions drivers/xunlei/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ type Erron struct {
// ErrorDetails interface{} `json:"error_details"`
}

func (e *Erron) HasError() bool {
return e.ErrorCode != 0 || e.ErrorMsg != "" || e.ErrorDescription != ""
}

func (e *Erron) Error() string {
return fmt.Sprintf("ErrorCode: %d ,Error: %s ,ErrorDescription: %s ", e.ErrorCode, e.ErrorMsg, e.ErrorDescription)
}
Expand All @@ -25,7 +29,7 @@ type CaptchaTokenRequest struct {
ClientID string `json:"client_id"`
DeviceID string `json:"device_id"`
Meta map[string]string `json:"meta"`
//RedirectUri string `json:"redirect_uri"`
RedirectUri string `json:"redirect_uri"`
}

type CaptchaTokenResponse struct {
Expand Down Expand Up @@ -173,9 +177,3 @@ type UploadTaskResponse struct {

File Files `json:"file"`
}

type Tasks struct {
Tasks []interface{}
NextPageToken string `json:"next_page_token"`
//ExpiresIn int64 `json:"expires_in"`
}
57 changes: 3 additions & 54 deletions drivers/xunlei/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,19 @@ package xunlei
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"net"
"net/url"

"github.com/Xhofe/alist/utils"
)

const (
// 小米浏览器
CLIENT_ID = "X7MtiU0Gb5YqWv-6"
CLIENT_SECRET = "84MYEih3Eeu2HF4RrGce3Q"
CLIENT_VERSION = "5.1.0.51045"

ALG_VERSION = "1"
PACKAGE_NAME = "com.xunlei.xcloud.lib"
)

var Algorithms = []string{
"",
"BXza40wm+P4zw8rEFpHA",
"UfZLfKfYRmKTA0",
"OMBGVt/9Wcaln1XaBz",
"Jn217F4rk5FPPWyhoeV",
"w5OwkGo0pGpb0Xe/XZ5T3",
"5guM3DNiY4F78x49zQ97q75",
"QXwn4D2j884wJgrYXjGClM/IVrJX",
"NXBRosYvbHIm6w8vEB",
"2kZ8Ie1yW2ib4O2iAkNpJobP",
"11CoVJJQEc",
"xf3QWysVwnVsNv5DCxU+cgNT1rK",
"9eEfKkrqkfw",
"T78dnANexYRbiZy",
}

const (
API_URL = "https://api-pan.xunlei.com/drive/v1"
FILE_API_URL = API_URL + "/files"
XLUSER_API_URL = "https://xluser-ssl.xunlei.com/v1"
)

const (
FOLDER = "drive#folder"
FILE = "drive#file"

FOLDER = "drive#folder"
FILE = "drive#file"
RESUMABLE = "drive#resumable"
)

Expand All @@ -58,18 +26,9 @@ const (
UPLOAD_TYPE_URL = "UPLOAD_TYPE_URL"
)

// 验证码签名
func captchaSign(driverID string, time int64) string {
str := fmt.Sprint(CLIENT_ID, CLIENT_VERSION, PACKAGE_NAME, driverID, time)
for _, algorithm := range Algorithms {
str = utils.GetMD5Encode(str + algorithm)
}
return ALG_VERSION + "." + str
}

func getAction(method string, u string) string {
c, _ := url.Parse(u)
return fmt.Sprint(method, ":", c.Path)
return method + ":" + c.Path
}

// 计算文件Gcid
Expand Down Expand Up @@ -102,13 +61,3 @@ func getGcid(r io.Reader, size int64) (string, error) {
}
return hex.EncodeToString(hash1.Sum(nil)), nil
}

// 获取driverID
func getDriverID(username string) string {
interfaces, _ := net.Interfaces()
str := username
for _, inter := range interfaces {
str += inter.HardwareAddr.String()
}
return utils.GetMD5Encode(str)
}
Loading

0 comments on commit 427ae56

Please sign in to comment.