Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/drivers/123_share/util.go
1987 views
1
package _123Share
2
3
import (
4
"context"
5
"errors"
6
"fmt"
7
"hash/crc32"
8
"math"
9
"math/rand"
10
"net/http"
11
"net/url"
12
"strconv"
13
"strings"
14
"time"
15
16
"github.com/alist-org/alist/v3/drivers/base"
17
"github.com/alist-org/alist/v3/pkg/utils"
18
"github.com/go-resty/resty/v2"
19
jsoniter "github.com/json-iterator/go"
20
)
21
22
const (
23
Api = "https://www.123pan.com/api"
24
AApi = "https://www.123pan.com/a/api"
25
BApi = "https://www.123pan.com/b/api"
26
MainApi = BApi
27
FileList = MainApi + "/share/get"
28
DownloadInfo = MainApi + "/share/download/info"
29
//AuthKeySalt = "8-8D$sL8gPjom7bk#cY"
30
)
31
32
func signPath(path string, os string, version string) (k string, v string) {
33
table := []byte{'a', 'd', 'e', 'f', 'g', 'h', 'l', 'm', 'y', 'i', 'j', 'n', 'o', 'p', 'k', 'q', 'r', 's', 't', 'u', 'b', 'c', 'v', 'w', 's', 'z'}
34
random := fmt.Sprintf("%.f", math.Round(1e7*rand.Float64()))
35
now := time.Now().In(time.FixedZone("CST", 8*3600))
36
timestamp := fmt.Sprint(now.Unix())
37
nowStr := []byte(now.Format("200601021504"))
38
for i := 0; i < len(nowStr); i++ {
39
nowStr[i] = table[nowStr[i]-48]
40
}
41
timeSign := fmt.Sprint(crc32.ChecksumIEEE(nowStr))
42
data := strings.Join([]string{timestamp, random, path, os, version, timeSign}, "|")
43
dataSign := fmt.Sprint(crc32.ChecksumIEEE([]byte(data)))
44
return timeSign, strings.Join([]string{timestamp, random, dataSign}, "-")
45
}
46
47
func GetApi(rawUrl string) string {
48
u, _ := url.Parse(rawUrl)
49
query := u.Query()
50
query.Add(signPath(u.Path, "web", "3"))
51
u.RawQuery = query.Encode()
52
return u.String()
53
}
54
55
func (d *Pan123Share) request(url string, method string, callback base.ReqCallback, resp interface{}) ([]byte, error) {
56
if d.ref != nil {
57
return d.ref.Request(url, method, callback, resp)
58
}
59
req := base.RestyClient.R()
60
req.SetHeaders(map[string]string{
61
"origin": "https://www.123pan.com",
62
"referer": "https://www.123pan.com/",
63
"authorization": "Bearer " + d.AccessToken,
64
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) alist-client",
65
"platform": "web",
66
"app-version": "3",
67
//"user-agent": base.UserAgent,
68
})
69
if callback != nil {
70
callback(req)
71
}
72
if resp != nil {
73
req.SetResult(resp)
74
}
75
res, err := req.Execute(method, GetApi(url))
76
if err != nil {
77
return nil, err
78
}
79
body := res.Body()
80
code := utils.Json.Get(body, "code").ToInt()
81
if code != 0 {
82
return nil, errors.New(jsoniter.Get(body, "message").ToString())
83
}
84
return body, nil
85
}
86
87
func (d *Pan123Share) getFiles(ctx context.Context, parentId string) ([]File, error) {
88
page := 1
89
res := make([]File, 0)
90
for {
91
if err := d.APIRateLimit(ctx, FileList); err != nil {
92
return nil, err
93
}
94
var resp Files
95
query := map[string]string{
96
"limit": "100",
97
"next": "0",
98
"orderBy": "file_id",
99
"orderDirection": "desc",
100
"parentFileId": parentId,
101
"Page": strconv.Itoa(page),
102
"shareKey": d.ShareKey,
103
"SharePwd": d.SharePwd,
104
}
105
_, err := d.request(FileList, http.MethodGet, func(req *resty.Request) {
106
req.SetQueryParams(query)
107
}, &resp)
108
if err != nil {
109
return nil, err
110
}
111
page++
112
res = append(res, resp.Data.InfoList...)
113
if len(resp.Data.InfoList) == 0 || resp.Data.Next == "-1" {
114
break
115
}
116
}
117
return res, nil
118
}
119
120
// do others that not defined in Driver interface
121
122