mirror of https://github.com/Xhofe/alist
🎨 abstract 123pan request
parent
99d06c7449
commit
f5b8815a84
|
@ -11,14 +11,13 @@ import (
|
||||||
"github.com/Xhofe/alist/utils"
|
"github.com/Xhofe/alist/utils"
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var pan123Client = resty.New()
|
|
||||||
|
|
||||||
type BaseResp struct {
|
type BaseResp struct {
|
||||||
Code int `json:"code"`
|
Code int `json:"code"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
|
@ -43,7 +42,7 @@ type Pan123File struct {
|
||||||
|
|
||||||
type Pan123Files struct {
|
type Pan123Files struct {
|
||||||
BaseResp
|
BaseResp
|
||||||
Data struct {
|
Data struct {
|
||||||
InfoList []Pan123File `json:"InfoList"`
|
InfoList []Pan123File `json:"InfoList"`
|
||||||
Next string `json:"Next"`
|
Next string `json:"Next"`
|
||||||
} `json:"data"`
|
} `json:"data"`
|
||||||
|
@ -51,19 +50,23 @@ type Pan123Files struct {
|
||||||
|
|
||||||
type Pan123DownResp struct {
|
type Pan123DownResp struct {
|
||||||
BaseResp
|
BaseResp
|
||||||
Data struct {
|
Data struct {
|
||||||
DownloadUrl string `json:"DownloadUrl"`
|
DownloadUrl string `json:"DownloadUrl"`
|
||||||
} `json:"data"`
|
} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (driver Pan123) Login(account *model.Account) error {
|
func (driver Pan123) Login(account *model.Account) error {
|
||||||
|
url := "https://www.123pan.com/api/user/sign_in"
|
||||||
|
if account.APIProxyUrl != "" {
|
||||||
|
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
||||||
|
}
|
||||||
var resp Pan123TokenResp
|
var resp Pan123TokenResp
|
||||||
_, err := pan123Client.R().
|
_, err := base.RestyClient.R().
|
||||||
SetResult(&resp).
|
SetResult(&resp).
|
||||||
SetBody(base.Json{
|
SetBody(base.Json{
|
||||||
"passport": account.Username,
|
"passport": account.Username,
|
||||||
"password": account.Password,
|
"password": account.Password,
|
||||||
}).Post("https://www.123pan.com/api/user/sign_in")
|
}).Post(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -99,50 +102,86 @@ func (driver Pan123) GetFiles(parentId string, account *model.Account) ([]Pan123
|
||||||
res := make([]Pan123File, 0)
|
res := make([]Pan123File, 0)
|
||||||
for next != "-1" {
|
for next != "-1" {
|
||||||
var resp Pan123Files
|
var resp Pan123Files
|
||||||
_, err := pan123Client.R().SetResult(&resp).
|
query := map[string]string{
|
||||||
SetHeader("authorization", "Bearer "+account.AccessToken).
|
"driveId": "0",
|
||||||
SetQueryParams(map[string]string{
|
"limit": "100",
|
||||||
"driveId": "0",
|
"next": next,
|
||||||
"limit": "100",
|
"orderBy": account.OrderBy,
|
||||||
"next": next,
|
"orderDirection": account.OrderDirection,
|
||||||
"orderBy": account.OrderBy,
|
"parentFileId": parentId,
|
||||||
"orderDirection": account.OrderDirection,
|
"trashed": "false",
|
||||||
"parentFileId": parentId,
|
}
|
||||||
"trashed": "false",
|
_, err := driver.Request("https://www.123pan.com/api/file/list",
|
||||||
}).Get("https://www.123pan.com/api/file/list")
|
base.Get, query, nil, &resp, false, account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if resp.Code != 0 {
|
|
||||||
if resp.Code == 401 {
|
|
||||||
err := driver.Login(account)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return driver.GetFiles(parentId, account)
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf(resp.Message)
|
|
||||||
}
|
|
||||||
next = resp.Data.Next
|
next = resp.Data.Next
|
||||||
res = append(res, resp.Data.InfoList...)
|
res = append(res, resp.Data.InfoList...)
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (driver Pan123) Post(url string, data base.Json, account *model.Account) ([]byte, error) {
|
func (driver Pan123) Request(url string, method int, query map[string]string, data *base.Json, resp interface{}, proxy bool, account *model.Account) ([]byte, error) {
|
||||||
res, err := pan123Client.R().
|
rawUrl := url
|
||||||
SetHeader("authorization", "Bearer "+account.AccessToken).
|
if account.APIProxyUrl != "" {
|
||||||
SetBody(data).Post(url)
|
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
||||||
|
}
|
||||||
|
log.Debugf("request: %s", url)
|
||||||
|
req := base.RestyClient.R()
|
||||||
|
req.SetHeader("Authorization", "Bearer "+account.AccessToken)
|
||||||
|
if query != nil {
|
||||||
|
req.SetQueryParams(query)
|
||||||
|
}
|
||||||
|
if data != nil {
|
||||||
|
req.SetBody(data)
|
||||||
|
}
|
||||||
|
if resp != nil {
|
||||||
|
req.SetResult(resp)
|
||||||
|
}
|
||||||
|
var res *resty.Response
|
||||||
|
var err error
|
||||||
|
switch method {
|
||||||
|
case base.Get:
|
||||||
|
res, err = req.Get(url)
|
||||||
|
case base.Post:
|
||||||
|
res, err = req.Post(url)
|
||||||
|
default:
|
||||||
|
return nil, base.ErrNotSupport
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
log.Debug(res.String())
|
||||||
body := res.Body()
|
body := res.Body()
|
||||||
if jsoniter.Get(body, "code").ToInt() != 0 {
|
code := jsoniter.Get(body, "code").ToInt()
|
||||||
|
if code != 0 {
|
||||||
|
if code == 401 {
|
||||||
|
err := driver.Login(account)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return driver.Request(rawUrl, method, query, data, resp, proxy, account)
|
||||||
|
}
|
||||||
return nil, errors.New(jsoniter.Get(body, "message").ToString())
|
return nil, errors.New(jsoniter.Get(body, "message").ToString())
|
||||||
}
|
}
|
||||||
return body, nil
|
return body, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//func (driver Pan123) Post(url string, data base.Json, account *model.Account) ([]byte, error) {
|
||||||
|
// res, err := pan123Client.R().
|
||||||
|
// SetHeader("authorization", "Bearer "+account.AccessToken).
|
||||||
|
// SetBody(data).Post(url)
|
||||||
|
// if err != nil {
|
||||||
|
// return nil, err
|
||||||
|
// }
|
||||||
|
// body := res.Body()
|
||||||
|
// if jsoniter.Get(body, "code").ToInt() != 0 {
|
||||||
|
// return nil, errors.New(jsoniter.Get(body, "message").ToString())
|
||||||
|
// }
|
||||||
|
// return body, nil
|
||||||
|
//}
|
||||||
|
|
||||||
func (driver Pan123) GetFile(path string, account *model.Account) (*Pan123File, error) {
|
func (driver Pan123) GetFile(path string, account *model.Account) (*Pan123File, error) {
|
||||||
dir, name := filepath.Split(path)
|
dir, name := filepath.Split(path)
|
||||||
dir = utils.ParsePath(dir)
|
dir = utils.ParsePath(dir)
|
||||||
|
@ -168,7 +207,7 @@ func RandStr(length int) string {
|
||||||
str := "123456789abcdefghijklmnopqrstuvwxyz"
|
str := "123456789abcdefghijklmnopqrstuvwxyz"
|
||||||
bytes := []byte(str)
|
bytes := []byte(str)
|
||||||
var result []byte
|
var result []byte
|
||||||
rand.Seed(time.Now().UnixNano()+ int64(rand.Intn(100)))
|
rand.Seed(time.Now().UnixNano() + int64(rand.Intn(100)))
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
result = append(result, bytes[rand.Intn(len(bytes))])
|
result = append(result, bytes[rand.Intn(len(bytes))])
|
||||||
}
|
}
|
||||||
|
@ -188,5 +227,4 @@ func HMAC(message string, secret string) string {
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
base.RegisterDriver(&Pan123{})
|
base.RegisterDriver(&Pan123{})
|
||||||
pan123Client.SetRetryCount(3)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -131,29 +131,22 @@ func (driver Pan123) Link(path string, account *model.Account) (*base.Link, erro
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var resp Pan123DownResp
|
var resp Pan123DownResp
|
||||||
_, err = pan123Client.R().SetResult(&resp).SetHeader("authorization", "Bearer "+account.AccessToken).
|
data := base.Json{
|
||||||
SetBody(base.Json{
|
"driveId": 0,
|
||||||
"driveId": 0,
|
"etag": file.Etag,
|
||||||
"etag": file.Etag,
|
"fileId": file.FileId,
|
||||||
"fileId": file.FileId,
|
"fileName": file.FileName,
|
||||||
"fileName": file.FileName,
|
"s3keyFlag": file.S3KeyFlag,
|
||||||
"s3keyFlag": file.S3KeyFlag,
|
"size": file.Size,
|
||||||
"size": file.Size,
|
"type": file.Type,
|
||||||
"type": file.Type,
|
}
|
||||||
}).Post("https://www.123pan.com/api/file/download_info")
|
_, err = driver.Request("https://www.123pan.com/api/file/download_info",
|
||||||
|
base.Post, nil, &data, &resp, true, account)
|
||||||
|
//_, err = pan123Client.R().SetResult(&resp).SetHeader("authorization", "Bearer "+account.AccessToken).
|
||||||
|
// SetBody().Post("https://www.123pan.com/api/file/download_info")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if resp.Code != 0 {
|
|
||||||
if resp.Code == 401 {
|
|
||||||
err := driver.Login(account)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return driver.Link(path, account)
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf(resp.Message)
|
|
||||||
}
|
|
||||||
u, err := url.Parse(resp.Data.DownloadUrl)
|
u, err := url.Parse(resp.Data.DownloadUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -164,12 +157,12 @@ func (driver Pan123) Link(path string, account *model.Account) (*base.Link, erro
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
log.Debug(res.String())
|
log.Debug(res.String())
|
||||||
link := base.Link{}
|
link := base.Link{
|
||||||
if res.StatusCode() == 302 {
|
Url: resp.Data.DownloadUrl,
|
||||||
link.Url = res.Header().Get("location")
|
|
||||||
} else {
|
|
||||||
link.Url = resp.Data.DownloadUrl
|
|
||||||
}
|
}
|
||||||
|
//if res.StatusCode() == 302 {
|
||||||
|
// link.Url = res.Header().Get("location")
|
||||||
|
//}
|
||||||
return &link, nil
|
return &link, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -221,7 +214,9 @@ func (driver Pan123) MakeDir(path string, account *model.Account) error {
|
||||||
"size": 0,
|
"size": 0,
|
||||||
"type": 1,
|
"type": 1,
|
||||||
}
|
}
|
||||||
_, err = driver.Post("https://www.123pan.com/api/file/upload_request", data, account)
|
_, err = driver.Request("https://www.123pan.com/api/file/upload_request",
|
||||||
|
base.Post, nil, &data, nil, false, account)
|
||||||
|
//_, err = driver.Post("https://www.123pan.com/api/file/upload_request", data, account)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
_ = base.DeleteCache(dir, account)
|
_ = base.DeleteCache(dir, account)
|
||||||
}
|
}
|
||||||
|
@ -243,7 +238,9 @@ func (driver Pan123) Move(src string, dst string, account *model.Account) error
|
||||||
"fileId": fileId,
|
"fileId": fileId,
|
||||||
"fileName": dstName,
|
"fileName": dstName,
|
||||||
}
|
}
|
||||||
_, err = driver.Post("https://www.123pan.com/api/file/rename", data, account)
|
_, err = driver.Request("https://www.123pan.com/api/file/rename",
|
||||||
|
base.Post, nil, &data, nil, false, account)
|
||||||
|
//_, err = driver.Post("https://www.123pan.com/api/file/rename", data, account)
|
||||||
} else {
|
} else {
|
||||||
// move
|
// move
|
||||||
dstDirFile, err := driver.File(dstDir, account)
|
dstDirFile, err := driver.File(dstDir, account)
|
||||||
|
@ -255,7 +252,9 @@ func (driver Pan123) Move(src string, dst string, account *model.Account) error
|
||||||
"fileId": fileId,
|
"fileId": fileId,
|
||||||
"parentFileId": parentFileId,
|
"parentFileId": parentFileId,
|
||||||
}
|
}
|
||||||
_, err = driver.Post("https://www.123pan.com/api/file/mod_pid", data, account)
|
_, err = driver.Request("https://www.123pan.com/api/file/mod_pid",
|
||||||
|
base.Post, nil, &data, nil, false, account)
|
||||||
|
//_, err = driver.Post("https://www.123pan.com/api/file/mod_pid", data, account)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = base.DeleteCache(srcDir, account)
|
_ = base.DeleteCache(srcDir, account)
|
||||||
|
@ -278,7 +277,9 @@ func (driver Pan123) Delete(path string, account *model.Account) error {
|
||||||
"operation": true,
|
"operation": true,
|
||||||
"fileTrashInfoList": file,
|
"fileTrashInfoList": file,
|
||||||
}
|
}
|
||||||
_, err = driver.Post("https://www.123pan.com/api/file/trash", data, account)
|
_, err = driver.Request("https://www.123pan.com/api/file/trash",
|
||||||
|
base.Post, nil, &data, nil, false, account)
|
||||||
|
//_, err = driver.Post("https://www.123pan.com/api/file/trash", data, account)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
_ = base.DeleteCache(utils.Dir(path), account)
|
_ = base.DeleteCache(utils.Dir(path), account)
|
||||||
}
|
}
|
||||||
|
@ -311,7 +312,9 @@ func (driver Pan123) Upload(file *model.FileStream, account *model.Account) erro
|
||||||
"size": file.GetSize(),
|
"size": file.GetSize(),
|
||||||
"type": 0,
|
"type": 0,
|
||||||
}
|
}
|
||||||
res, err := driver.Post("https://www.123pan.com/api/file/upload_request", data, account)
|
res, err := driver.Request("https://www.123pan.com/api/file/upload_request",
|
||||||
|
base.Post, nil, &data, nil, false, account)
|
||||||
|
//res, err := driver.Post("https://www.123pan.com/api/file/upload_request", data, account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -319,19 +322,19 @@ func (driver Pan123) Upload(file *model.FileStream, account *model.Account) erro
|
||||||
var resp UploadResp
|
var resp UploadResp
|
||||||
kSecret := jsoniter.Get(res, "data.SecretAccessKey").ToString()
|
kSecret := jsoniter.Get(res, "data.SecretAccessKey").ToString()
|
||||||
nowTimeStr := time.Now().String()
|
nowTimeStr := time.Now().String()
|
||||||
Date := strings.ReplaceAll(strings.Split(nowTimeStr, "T")[0],"-","")
|
Date := strings.ReplaceAll(strings.Split(nowTimeStr, "T")[0], "-", "")
|
||||||
|
|
||||||
StringToSign := fmt.Sprintf("%s\n%s\n%s\n%s",
|
StringToSign := fmt.Sprintf("%s\n%s\n%s\n%s",
|
||||||
"AWS4-HMAC-SHA256",
|
"AWS4-HMAC-SHA256",
|
||||||
nowTimeStr,
|
nowTimeStr,
|
||||||
fmt.Sprintf("%s/us-east-1/s3/aws4_request", Date),
|
fmt.Sprintf("%s/us-east-1/s3/aws4_request", Date),
|
||||||
)
|
)
|
||||||
|
|
||||||
kDate := HMAC("AWS4"+kSecret, Date)
|
kDate := HMAC("AWS4"+kSecret, Date)
|
||||||
kRegion := HMAC(kDate, "us-east-1")
|
kRegion := HMAC(kDate, "us-east-1")
|
||||||
kService := HMAC(kRegion, "s3")
|
kService := HMAC(kRegion, "s3")
|
||||||
kSigning := HMAC(kService, "aws4_request")
|
kSigning := HMAC(kService, "aws4_request")
|
||||||
_, err = pan123Client.R().SetResult(&resp).SetHeaders(map[string]string{
|
_, err = base.RestyClient.R().SetResult(&resp).SetHeaders(map[string]string{
|
||||||
"Authorization": fmt.Sprintf("AWS4-HMAC-SHA256 Credential=%s/%s/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-security-token;x-amz-user-agent, Signature=%s",
|
"Authorization": fmt.Sprintf("AWS4-HMAC-SHA256 Credential=%s/%s/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-security-token;x-amz-user-agent, Signature=%s",
|
||||||
jsoniter.Get(res, "data.AccessKeyId"),
|
jsoniter.Get(res, "data.AccessKeyId"),
|
||||||
Date,
|
Date,
|
||||||
|
|
|
@ -110,6 +110,8 @@ func init() {
|
||||||
return http.ErrUseLastResponse
|
return http.ErrUseLastResponse
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
NoRedirectClient.SetHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
|
userAgent := "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
|
||||||
RestyClient.SetHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
|
NoRedirectClient.SetHeader("user-agent", userAgent)
|
||||||
|
RestyClient.SetHeader("user-agent", userAgent)
|
||||||
|
RestyClient.SetRetryCount(3)
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,6 +79,7 @@ func (driver PikPak) RefreshToken(account *model.Account) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (driver PikPak) Request(url string, method int, query map[string]string, data *base.Json, resp interface{}, account *model.Account) ([]byte, error) {
|
func (driver PikPak) Request(url string, method int, query map[string]string, data *base.Json, resp interface{}, account *model.Account) ([]byte, error) {
|
||||||
|
rawUrl := url
|
||||||
if account.APIProxyUrl != "" {
|
if account.APIProxyUrl != "" {
|
||||||
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
||||||
}
|
}
|
||||||
|
@ -119,7 +120,7 @@ func (driver PikPak) Request(url string, method int, query map[string]string, da
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
_ = model.SaveAccount(account)
|
_ = model.SaveAccount(account)
|
||||||
return driver.Request(url, method, query, data, resp, account)
|
return driver.Request(rawUrl, method, query, data, resp, account)
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New(e.Error)
|
return nil, errors.New(e.Error)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue