2021-12-06 07:55:05 +00:00
|
|
|
package _89
|
2021-11-15 10:00:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
|
|
|
"github.com/Xhofe/alist/conf"
|
2021-12-06 07:55:05 +00:00
|
|
|
"github.com/Xhofe/alist/drivers/base"
|
2021-11-15 10:00:43 +00:00
|
|
|
"github.com/Xhofe/alist/model"
|
|
|
|
"github.com/Xhofe/alist/utils"
|
|
|
|
"github.com/go-resty/resty/v2"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
mathRand "math/rand"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var client189Map map[string]*resty.Client
|
|
|
|
|
2021-11-27 11:40:36 +00:00
|
|
|
func (driver Cloud189) FormatFile(file *Cloud189File) *model.File {
|
2021-11-15 10:00:43 +00:00
|
|
|
f := &model.File{
|
2021-11-27 10:55:09 +00:00
|
|
|
Id: strconv.FormatInt(file.Id, 10),
|
2021-11-15 10:00:43 +00:00
|
|
|
Name: file.Name,
|
|
|
|
Size: file.Size,
|
2021-11-30 01:37:51 +00:00
|
|
|
Driver: driver.Config().Name,
|
2021-11-15 10:00:43 +00:00
|
|
|
UpdatedAt: nil,
|
|
|
|
Thumbnail: file.Icon.SmallUrl,
|
|
|
|
Url: file.Url,
|
|
|
|
}
|
2021-11-15 12:31:44 +00:00
|
|
|
loc, _ := time.LoadLocation("Local")
|
|
|
|
lastOpTime, err := time.ParseInLocation("2006-01-02 15:04:05", file.LastOpTime, loc)
|
|
|
|
if err == nil {
|
|
|
|
f.UpdatedAt = &lastOpTime
|
|
|
|
}
|
2021-11-15 10:00:43 +00:00
|
|
|
if file.Size == -1 {
|
|
|
|
f.Type = conf.FOLDER
|
2021-11-15 12:31:44 +00:00
|
|
|
f.Size = 0
|
2021-11-15 10:00:43 +00:00
|
|
|
} else {
|
|
|
|
f.Type = utils.GetFileType(filepath.Ext(file.Name))
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2021-11-27 10:55:09 +00:00
|
|
|
//func (c Cloud189) GetFile(path string, account *model.Account) (*Cloud189File, error) {
|
|
|
|
// dir, name := filepath.Split(path)
|
|
|
|
// dir = utils.ParsePath(dir)
|
2021-12-06 09:49:20 +00:00
|
|
|
// _, _, err := c.ParentPath(dir, account)
|
2021-11-27 10:55:09 +00:00
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
// parentFiles_, _ := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, dir))
|
|
|
|
// parentFiles, _ := parentFiles_.([]Cloud189File)
|
|
|
|
// for _, file := range parentFiles {
|
|
|
|
// if file.Name == name {
|
|
|
|
// if file.Size != -1 {
|
|
|
|
// return &file, err
|
|
|
|
// } else {
|
2021-12-05 07:22:19 +00:00
|
|
|
// return nil, ErrNotFile
|
2021-11-27 10:55:09 +00:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2021-12-05 07:22:19 +00:00
|
|
|
// return nil, ErrPathNotFound
|
2021-11-27 10:55:09 +00:00
|
|
|
//}
|
|
|
|
|
2021-11-15 10:00:43 +00:00
|
|
|
type Cloud189Down struct {
|
|
|
|
ResCode int `json:"res_code"`
|
|
|
|
ResMessage string `json:"res_message"`
|
|
|
|
FileDownloadUrl string `json:"fileDownloadUrl"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type LoginResp struct {
|
|
|
|
Msg string `json:"msg"`
|
|
|
|
Result int `json:"result"`
|
|
|
|
ToUrl string `json:"toUrl"`
|
|
|
|
}
|
|
|
|
|
2021-11-15 12:15:25 +00:00
|
|
|
// Login refer to PanIndex
|
2021-11-27 11:40:36 +00:00
|
|
|
func (driver Cloud189) Login(account *model.Account) error {
|
2021-11-15 10:00:43 +00:00
|
|
|
client, ok := client189Map[account.Name]
|
|
|
|
if !ok {
|
2021-11-15 11:06:44 +00:00
|
|
|
//cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
|
2021-11-15 10:00:43 +00:00
|
|
|
client = resty.New()
|
2021-11-15 11:06:44 +00:00
|
|
|
//client.SetCookieJar(cookieJar)
|
2021-11-15 10:00:43 +00:00
|
|
|
client.SetRetryCount(3)
|
|
|
|
}
|
|
|
|
url := "https://cloud.189.cn/api/portal/loginUrl.action?redirectURL=https%3A%2F%2Fcloud.189.cn%2Fmain.action"
|
2021-11-23 07:46:10 +00:00
|
|
|
b := ""
|
2021-11-15 10:00:43 +00:00
|
|
|
lt := ""
|
|
|
|
ltText := regexp.MustCompile(`lt = "(.+?)"`)
|
2021-11-23 07:46:10 +00:00
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
res, err := client.R().Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b = res.String()
|
|
|
|
ltTextArr := ltText.FindStringSubmatch(b)
|
|
|
|
if len(ltTextArr) > 0 {
|
|
|
|
lt = ltTextArr[1]
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
<-time.After(time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if lt == "" {
|
|
|
|
return fmt.Errorf("get empty login page")
|
2021-11-15 10:00:43 +00:00
|
|
|
}
|
|
|
|
captchaToken := regexp.MustCompile(`captchaToken' value='(.+?)'`).FindStringSubmatch(b)[1]
|
|
|
|
returnUrl := regexp.MustCompile(`returnUrl = '(.+?)'`).FindStringSubmatch(b)[1]
|
|
|
|
paramId := regexp.MustCompile(`paramId = "(.+?)"`).FindStringSubmatch(b)[1]
|
|
|
|
//reqId := regexp.MustCompile(`reqId = "(.+?)"`).FindStringSubmatch(b)[1]
|
|
|
|
jRsakey := regexp.MustCompile(`j_rsaKey" value="(\S+)"`).FindStringSubmatch(b)[1]
|
|
|
|
vCodeID := regexp.MustCompile(`picCaptcha\.do\?token\=([A-Za-z0-9\&\=]+)`).FindStringSubmatch(b)[1]
|
|
|
|
vCodeRS := ""
|
|
|
|
if vCodeID != "" {
|
|
|
|
// need ValidateCode
|
|
|
|
}
|
|
|
|
userRsa := RsaEncode([]byte(account.Username), jRsakey)
|
|
|
|
passwordRsa := RsaEncode([]byte(account.Password), jRsakey)
|
|
|
|
url = "https://open.e.189.cn/api/logbox/oauth2/loginSubmit.do"
|
|
|
|
var loginResp LoginResp
|
2021-11-23 07:46:10 +00:00
|
|
|
res, err := client.R().
|
2021-11-15 10:00:43 +00:00
|
|
|
SetHeaders(map[string]string{
|
2021-11-15 12:15:25 +00:00
|
|
|
"lt": lt,
|
|
|
|
"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",
|
|
|
|
"Referer": "https://open.e.189.cn/",
|
|
|
|
"accept": "application/json;charset=UTF-8",
|
|
|
|
}).SetFormData(map[string]string{
|
2021-11-15 10:00:43 +00:00
|
|
|
"appKey": "cloud",
|
|
|
|
"accountType": "01",
|
|
|
|
"userName": "{RSA}" + userRsa,
|
|
|
|
"password": "{RSA}" + passwordRsa,
|
|
|
|
"validateCode": vCodeRS,
|
|
|
|
"captchaToken": captchaToken,
|
|
|
|
"returnUrl": returnUrl,
|
|
|
|
"mailSuffix": "@pan.cn",
|
|
|
|
"paramId": paramId,
|
|
|
|
"clientType": "10010",
|
|
|
|
"dynamicCheck": "FALSE",
|
|
|
|
"cb_SaveName": "1",
|
|
|
|
"isOauth2": "false",
|
|
|
|
}).Post(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(res.Body(), &loginResp)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if loginResp.Result != 0 {
|
|
|
|
return fmt.Errorf(loginResp.Msg)
|
|
|
|
}
|
|
|
|
_, err = client.R().Get(loginResp.ToUrl)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
client189Map[account.Name] = client
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Cloud189Error struct {
|
|
|
|
ErrorCode string `json:"errorCode"`
|
|
|
|
ErrorMsg string `json:"errorMsg"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Cloud189File struct {
|
|
|
|
Id int64 `json:"id"`
|
|
|
|
LastOpTime string `json:"lastOpTime"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
Icon struct {
|
|
|
|
SmallUrl string `json:"smallUrl"`
|
|
|
|
//LargeUrl string `json:"largeUrl"`
|
|
|
|
} `json:"icon"`
|
|
|
|
Url string `json:"url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Cloud189Folder struct {
|
|
|
|
Id int64 `json:"id"`
|
|
|
|
LastOpTime string `json:"lastOpTime"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Cloud189Files struct {
|
|
|
|
ResCode int `json:"res_code"`
|
|
|
|
ResMessage string `json:"res_message"`
|
|
|
|
FileListAO struct {
|
|
|
|
Count int `json:"count"`
|
|
|
|
FileList []Cloud189File `json:"fileList"`
|
|
|
|
FolderList []Cloud189Folder `json:"folderList"`
|
|
|
|
} `json:"fileListAO"`
|
|
|
|
}
|
|
|
|
|
2021-11-27 11:40:36 +00:00
|
|
|
func (driver Cloud189) GetFiles(fileId string, account *model.Account) ([]Cloud189File, error) {
|
2021-11-15 10:00:43 +00:00
|
|
|
client, ok := client189Map[account.Name]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("can't find [%s] client", account.Name)
|
|
|
|
}
|
|
|
|
res := make([]Cloud189File, 0)
|
|
|
|
pageNum := 1
|
|
|
|
for {
|
|
|
|
var e Cloud189Error
|
|
|
|
var resp Cloud189Files
|
|
|
|
_, err := client.R().SetResult(&resp).SetError(&e).
|
2021-11-15 12:15:25 +00:00
|
|
|
SetHeader("Accept", "application/json;charset=UTF-8").
|
2021-11-15 10:00:43 +00:00
|
|
|
SetQueryParams(map[string]string{
|
2021-11-15 12:15:25 +00:00
|
|
|
"noCache": random(),
|
|
|
|
"pageSize": "60",
|
|
|
|
"pageNum": strconv.Itoa(pageNum),
|
|
|
|
"mediaType": "0",
|
|
|
|
"folderId": fileId,
|
|
|
|
"iconOption": "5",
|
|
|
|
"orderBy": account.OrderBy,
|
|
|
|
"descending": account.OrderDirection,
|
|
|
|
}).Get("https://cloud.189.cn/api/open/file/listFiles.action")
|
2021-11-15 10:00:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if e.ErrorCode != "" {
|
|
|
|
if e.ErrorCode == "InvalidSessionKey" {
|
2021-11-27 11:40:36 +00:00
|
|
|
err = driver.Login(account)
|
2021-11-15 10:00:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-27 11:40:36 +00:00
|
|
|
return driver.GetFiles(fileId, account)
|
2021-11-15 10:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if resp.ResCode != 0 {
|
|
|
|
return nil, fmt.Errorf(resp.ResMessage)
|
|
|
|
}
|
|
|
|
if resp.FileListAO.Count == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
res = append(res, resp.FileListAO.FileList...)
|
|
|
|
for _, folder := range resp.FileListAO.FolderList {
|
|
|
|
res = append(res, Cloud189File{
|
|
|
|
Id: folder.Id,
|
|
|
|
LastOpTime: folder.LastOpTime,
|
|
|
|
Name: folder.Name,
|
|
|
|
Size: -1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
pageNum++
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func random() string {
|
|
|
|
return fmt.Sprintf("0.%17v", mathRand.New(mathRand.NewSource(time.Now().UnixNano())).Int63n(100000000000000000))
|
|
|
|
}
|
|
|
|
|
|
|
|
func RsaEncode(origData []byte, j_rsakey string) string {
|
|
|
|
publicKey := []byte("-----BEGIN PUBLIC KEY-----\n" + j_rsakey + "\n-----END PUBLIC KEY-----")
|
|
|
|
block, _ := pem.Decode(publicKey)
|
|
|
|
pubInterface, _ := x509.ParsePKIXPublicKey(block.Bytes)
|
|
|
|
pub := pubInterface.(*rsa.PublicKey)
|
|
|
|
b, err := rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("err: %s", err.Error())
|
|
|
|
}
|
|
|
|
return b64tohex(base64.StdEncoding.EncodeToString(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
var b64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
|
|
|
|
|
|
var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz"
|
|
|
|
|
|
|
|
func int2char(a int) string {
|
|
|
|
return strings.Split(BI_RM, "")[a]
|
|
|
|
}
|
|
|
|
|
|
|
|
func b64tohex(a string) string {
|
|
|
|
d := ""
|
|
|
|
e := 0
|
|
|
|
c := 0
|
|
|
|
for i := 0; i < len(a); i++ {
|
|
|
|
m := strings.Split(a, "")[i]
|
|
|
|
if m != "=" {
|
|
|
|
v := strings.Index(b64map, m)
|
|
|
|
if 0 == e {
|
|
|
|
e = 1
|
|
|
|
d += int2char(v >> 2)
|
|
|
|
c = 3 & v
|
|
|
|
} else if 1 == e {
|
|
|
|
e = 2
|
|
|
|
d += int2char(c<<2 | v>>4)
|
|
|
|
c = 15 & v
|
|
|
|
} else if 2 == e {
|
|
|
|
e = 3
|
|
|
|
d += int2char(c)
|
|
|
|
d += int2char(v >> 2)
|
|
|
|
c = 3 & v
|
|
|
|
} else {
|
|
|
|
e = 0
|
|
|
|
d += int2char(c<<2 | v>>4)
|
|
|
|
d += int2char(15 & v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if e == 1 {
|
|
|
|
d += int2char(c << 2)
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
2021-11-27 12:20:14 +00:00
|
|
|
|
|
|
|
func init() {
|
2021-12-06 07:55:05 +00:00
|
|
|
base.RegisterDriver(&Cloud189{})
|
2021-11-27 12:20:14 +00:00
|
|
|
client189Map = make(map[string]*resty.Client, 0)
|
|
|
|
}
|