Skip to content

Commit

Permalink
feat: add lanzou driver
Browse files Browse the repository at this point in the history
  • Loading branch information
foxxorcat committed Sep 20, 2022
1 parent 57686d9 commit 1ab73e0
Show file tree
Hide file tree
Showing 6 changed files with 915 additions and 0 deletions.
1 change: 1 addition & 0 deletions drivers/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
_ "github.com/alist-org/alist/v3/drivers/baidu_photo"
_ "github.com/alist-org/alist/v3/drivers/ftp"
_ "github.com/alist-org/alist/v3/drivers/google_drive"
_ "github.com/alist-org/alist/v3/drivers/lanzou"
_ "github.com/alist-org/alist/v3/drivers/local"
_ "github.com/alist-org/alist/v3/drivers/mediatrack"
_ "github.com/alist-org/alist/v3/drivers/onedrive"
Expand Down
171 changes: 171 additions & 0 deletions drivers/lanzou/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package lanzou

import (
"context"
"net/http"
"time"

"github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/go-resty/resty/v2"
)

var upClient = base.NewRestyClient().SetTimeout(120 * time.Second)

type LanZou struct {
Addition
model.Storage
}

func (d *LanZou) Config() driver.Config {
return config
}

func (d *LanZou) GetAddition() driver.Additional {
return d.Addition
}

func (d *LanZou) Init(ctx context.Context, storage model.Storage) error {
d.Storage = storage
err := utils.Json.UnmarshalFromString(d.Storage.Addition, &d.Addition)
if err != nil {
return err
}
if d.IsCookie() {
if d.RootFolderID == "" {
d.RootFolderID = "-1"
}
}
return nil
}

func (d *LanZou) Drop(ctx context.Context) error {
return nil
}

// 获取的大小和时间不准确
func (d *LanZou) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
if d.IsCookie() {
return d.GetFiles(ctx, dir.GetID())
} else {
return d.GetFileOrFolderByShareUrl(ctx, dir.GetID(), d.SharePassword)
}
}

func (d *LanZou) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
downID := file.GetID()
pwd := d.SharePassword
if d.IsCookie() {
share, err := d.getFileShareUrlByID(ctx, file.GetID())
if err != nil {
return nil, err
}
downID = share.FID
pwd = share.Pwd
}
fileInfo, err := d.getFilesByShareUrl(ctx, downID, pwd, nil)
if err != nil {
return nil, err
}

return &model.Link{
URL: fileInfo.Url,
Header: http.Header{
"User-Agent": []string{base.UserAgent},
},
}, nil
}

func (d *LanZou) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
if d.IsCookie() {
_, err := d.post(d.BaseUrl+"/doupload.php", func(req *resty.Request) {
req.SetContext(ctx)
req.SetFormData(map[string]string{
"task": "2",
"parent_id": parentDir.GetID(),
"folder_name": dirName,
"folder_description": "",
})
}, nil)
return err
}
return errs.NotImplement
}

func (d *LanZou) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
if d.IsCookie() {
if !srcObj.IsDir() {
_, err := d.post(d.BaseUrl+"/doupload.php", func(req *resty.Request) {
req.SetContext(ctx)
req.SetFormData(map[string]string{
"task": "20",
"folder_id": dstDir.GetID(),
"file_id": srcObj.GetID(),
})
}, nil)
return err
}
}
return errs.NotImplement
}

func (d *LanZou) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
if d.IsCookie() {
if !srcObj.IsDir() {
_, err := d.post(d.BaseUrl+"/doupload.php", func(req *resty.Request) {
req.SetContext(ctx)
req.SetFormData(map[string]string{
"task": "46",
"file_id": srcObj.GetID(),
"file_name": newName,
"type": "2",
})
}, nil)
return err
}
}
return errs.NotImplement
}

func (d *LanZou) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
return errs.NotImplement
}

func (d *LanZou) Remove(ctx context.Context, obj model.Obj) error {
if d.IsCookie() {
_, err := d.post(d.BaseUrl+"/doupload.php", func(req *resty.Request) {
req.SetContext(ctx)
if obj.IsDir() {
req.SetFormData(map[string]string{
"task": "3",
"folder_id": obj.GetID(),
})
} else {
req.SetFormData(map[string]string{
"task": "6",
"file_id": obj.GetID(),
})
}
}, nil)
return err
}
return errs.NotImplement
}

func (d *LanZou) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
if d.IsCookie() {
_, err := d._post(d.BaseUrl+"/fileup.php", func(req *resty.Request) {
req.SetFormData(map[string]string{
"task": "1",
"id": "WU_FILE_0",
"name": stream.GetName(),
"folder_id": dstDir.GetID(),
}).SetFileReader("upload_file", stream.GetName(), stream)
}, nil, true)
return err
}
return errs.NotImplement
}
165 changes: 165 additions & 0 deletions drivers/lanzou/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package lanzou

import (
"bytes"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"unicode"
)

const DAY time.Duration = 84600000000000

var timeSplitReg = regexp.MustCompile("([0-9.]*)\\s*([\u4e00-\u9fa5]+)")

func MustParseTime(str string) time.Time {
lastOpTime, err := time.ParseInLocation("2006-01-02 -07", str+" +08", time.Local)
if err != nil {
strs := timeSplitReg.FindStringSubmatch(str)
lastOpTime = time.Now()
if len(strs) == 3 {
i, _ := strconv.ParseInt(strs[1], 10, 64)
ti := time.Duration(-i)
switch strs[2] {
case "秒前":
lastOpTime = lastOpTime.Add(time.Second * ti)
case "分钟前":
lastOpTime = lastOpTime.Add(time.Minute * ti)
case "小时前":
lastOpTime = lastOpTime.Add(time.Hour * ti)
case "天前":
lastOpTime = lastOpTime.Add(DAY * ti)
case "昨天":
lastOpTime = lastOpTime.Add(-DAY)
case "前天":
lastOpTime = lastOpTime.Add(-DAY * 2)
}
}
}
return lastOpTime
}

var sizeSplitReg = regexp.MustCompile(`(?i)([0-9.]+)\s*([bkm]+)`)

func SizeStrToInt64(size string) int64 {
strs := sizeSplitReg.FindStringSubmatch(size)
if len(strs) < 3 {
return 0
}

s, _ := strconv.ParseFloat(strs[1], 64)
switch strings.ToUpper(strs[2]) {
case "B":
return int64(s)
case "K":
return int64(s * (1 << 10))
case "M":
return int64(s * (1 << 20))
}
return 0
}

// 移除注释
func RemoveNotes(html []byte) []byte {
return regexp.MustCompile(`<!--.*?-->|//.*|/\*.*?\*/`).ReplaceAll(html, []byte{})
}

var findAcwScV2Reg = regexp.MustCompile(`arg1='([0-9A-Z]+)'`)

// 在页面被过多访问或其他情况下,有时候会先返回一个加密的页面,其执行计算出一个acw_sc__v2后放入页面后再重新访问页面才能获得正常页面
// 若该页面进行了js加密,则进行解密,计算acw_sc__v2,并加入cookie
func CalcAcwScV2(html string) (string, error) {
acwScV2s := findAcwScV2Reg.FindStringSubmatch(html)
if len(acwScV2s) != 2 {
return "", fmt.Errorf("无法匹配acw_sc__v2")
}
return HexXor(Unbox(acwScV2s[1]), "3000176000856006061501533003690027800375"), nil
}

func Unbox(hex string) string {
var box = []int{6, 28, 34, 31, 33, 18, 30, 23, 9, 8, 19, 38, 17, 24, 0, 5, 32, 21, 10, 22, 25, 14, 15, 3, 16, 27, 13, 35, 2, 29, 11, 26, 4, 36, 1, 39, 37, 7, 20, 12}
var newBox = make([]byte, len(hex))
for i := 0; i < len(box); i++ {
j := box[i]
if len(newBox) > j {
newBox[j] = hex[i]
}
}
return string(newBox)
}

func HexXor(hex1, hex2 string) string {
out := bytes.NewBuffer(make([]byte, len(hex1)))
for i := 0; i < len(hex1) && i < len(hex2); i += 2 {
v1, _ := strconv.ParseInt(hex1[i:i+2], 16, 64)
v2, _ := strconv.ParseInt(hex2[i:i+2], 16, 64)
out.WriteString(strconv.FormatInt(v1^v2, 16))
}
return out.String()
}

var findDataReg = regexp.MustCompile(`data[:\s]+({[^}]+})`) // 查找json
var findKVReg = regexp.MustCompile(`'(.+?)':('?([^' },]*)'?)`) // 拆分kv

// 根据key查询js变量
func findJSVarFunc(key, data string) string {
values := regexp.MustCompile(`var ` + key + ` = '(.+?)';`).FindStringSubmatch(data)
if len(values) == 0 {
return ""
}
return values[1]
}

// 解析html中的JSON
func htmlJsonToMap(html string) (map[string]string, error) {
datas := findDataReg.FindStringSubmatch(html)
if len(datas) != 2 {
return nil, fmt.Errorf("not find data")
}
return jsonToMap(datas[1], html), nil
}

func jsonToMap(data, html string) map[string]string {
var param = make(map[string]string)
kvs := findKVReg.FindAllStringSubmatch(data, -1)
for _, kv := range kvs {
k, v := kv[1], kv[3]
if v == "" || strings.Contains(kv[2], "'") || IsNumber(kv[2]) {
param[k] = v
} else {
param[k] = findJSVarFunc(v, html)
}
}
return param
}

func IsNumber(str string) bool {
for _, s := range str {
if !unicode.IsDigit(s) {
return false
}
}
return true
}

var findFromReg = regexp.MustCompile(`data : '(.+?)'`) // 查找from字符串

// 解析html中的from
func htmlFormToMap(html string) (map[string]string, error) {
froms := findFromReg.FindStringSubmatch(html)
if len(froms) != 2 {
return nil, fmt.Errorf("not find file sgin")
}
return fromToMap(froms[1]), nil
}

func fromToMap(from string) map[string]string {
var param = make(map[string]string)
for _, kv := range strings.Split(from, "&") {
kv := strings.SplitN(kv, "=", 2)[:2]
param[kv[0]] = kv[1]
}
return param
}
31 changes: 31 additions & 0 deletions drivers/lanzou/meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package lanzou

import (
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/op"
)

type Addition struct {
Type string `json:"type" type:"select" options:"cookie,url" default:"cookie"`
Cookie string `json:"cookie" required:"true" help:"about 15 days valid"`
driver.RootID
SharePassword string `json:"share_password"`
BaseUrl string `json:"baseUrl" required:"true" default:"https://pc.woozooo.com"`
ShareUrl string `json:"shareUrl" required:"true" default:"https://pan.lanzouo.com"`
}

func (a *Addition) IsCookie() bool {
return a.Type == "cookie"
}

var config = driver.Config{
Name: "Lanzou",
LocalSort: true,
DefaultRoot: "-1",
}

func init() {
op.RegisterDriver(config, func() driver.Driver {
return &LanZou{}
})
}
Loading

0 comments on commit 1ab73e0

Please sign in to comment.