8000 feat(123_share): add access token (#6357) · anwen-anyi/alist@32ddab9 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 32ddab9

Browse files
author
Xiaoran Studio
authored
feat(123_share): add access token (AlistGo#6357)
1 parent 0c9dcec commit 32ddab9

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

drivers/123_share/driver.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import (
44
"context"
55
"encoding/base64"
66
"fmt"
7+
"golang.org/x/time/rate"
78
"net/http"
89
"net/url"
10+
"sync"
11+
"time"
912

1013
"github.com/alist-org/alist/v3/drivers/base"
1114
"github.com/alist-org/alist/v3/internal/driver"
@@ -19,6 +22,7 @@ import (
1922
type Pan123Share struct {
2023
model.Storage
2124
Addition
25+
apiRateLimit sync.Map
2226
}
2327

2428
func (d *Pan123Share) Config() driver.Config {
@@ -146,4 +150,11 @@ func (d *Pan123Share) Put(ctx context.Context, dstDir model.Obj, stream model.Fi
146150
// return nil, errs.NotSupport
147151
//}
148152

153+
func (d *Pan123Share) APIRateLimit(api string) bool {
154+
limiter, _ := d.apiRateLimit.LoadOrStore(api,
155+
rate.NewLimiter(rate.Every(time.Millisecond*700), 1))
156+
ins := limiter.(*rate.Limiter)
157+
return ins.Allow()
158+
}
159+
149160
var _ driver.Driver = (*Pan123Share)(nil)

drivers/123_share/meta.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77

88
type Addition struct {
99
ShareKey string `json:"sharekey" required:"true"`
10-
SharePwd string `json:"sharepassword" required:"true"`
10+
SharePwd string `json:"sharepassword"`
1111
driver.RootID
1212
OrderBy string `json:"order_by" type:"select" options:"file_name,size,update_at" default:"file_name"`
1313
OrderDirection string `json:"order_direction" type:"select" options:"asc,desc" default:"asc"`
14+
AccessToken string `json:"accesstoken" type:"text"`
1415
}
1516

1617
var config = driver.Config{

drivers/123_share/util.go

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@ package _123Share
22

33
import (
44
"errors"
5+
"fmt"
6+
"hash/crc32"
7+
"math"
8+
"math/rand"
59
"net/http"
10+
"net/url"
611
"strconv"
12+
"strings"
13+
"time"
714

815
"github.com/alist-org/alist/v3/drivers/base"
916
"github.com/alist-org/alist/v3/pkg/utils"
@@ -15,28 +22,53 @@ const (
1522
Api = "https://www.123pan.com/api"
1623
AApi = "https://www.123pan.com/a/api"
1724
BApi = "https://www.123pan.com/b/api"
18-
MainApi = Api
25+
MainApi = BApi
1926
FileList = MainApi + "/share/get"
2027
DownloadInfo = MainApi + "/share/download/info"
2128
//AuthKeySalt = "8-8D$sL8gPjom7bk#cY"
2229
)
2330

31+
func signPath(path string, os string, version string) (k string, v string) {
32+
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'}
33+
random := fmt.Sprintf("%.f", math.Round(1e7*rand.Float64()))
34+
now := time.Now().In(time.FixedZone("CST", 8*3600))
35+
timestamp := fmt.Sprint(now.Unix())
36+
nowStr := []byte(now.Format("200601021504"))
37+
for i := 0; i < len(nowStr); i++ {
38+
nowStr[i] = table[nowStr[i]-48]
39+
}
40+
timeSign := fmt.Sprint(crc32.ChecksumIEEE(nowStr))
41+
data := strings.Join([]string{timestamp, random, path, os, version, timeSign}, "|")
42+
dataSign := fmt.Sprint(crc32.ChecksumIEEE([]byte(data)))
43+
return timeSign, strings.Join([]string{timestamp, random, dataSign}, "-")
44+
}
45+
46+
func GetApi(rawUrl string) string {
47+
u, _ := url.Parse(rawUrl)
48+
query := u.Query()
49+
query.Add(signPath(u.Path, "web", "3"))
50+
u.RawQuery = query.Encode()
51+
return u.String()
52+
}
53+
2454
func (d *Pan123Share) request(url string, method string, callback base.ReqCallback, resp interface{}) ([]byte, error) {
2555
req := base.RestyClient.R()
2656
req.SetHeaders(map[string]string{
27-
"origin": "https://www.123pan.com",
28-
"referer": "https://www.123pan.com/",
29-
"user-agent": "Dart/2.19(dart:io)",
30-
"platform": "android",
31-
"app-version": "36",
57+
"origin": "https://www.123pan.com",
58+
"referer": "https://www.123pan.com/",
59+
"authorization": "Bearer " + d.AccessToken,
60+
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) alist-client",
61+
"platform": "web",
62+
"app-version": "3",
63+
//"user-agent": base.UserAgent,
3264
})
3365
if callback != nil {
3466
callback(req)
3567
}
3668
if resp != nil {
3769
req.SetResult(resp)
3870
}
39-
res, err := req.Execute(method, url)
71+
res, err := req.Execute(method, GetApi(url))
4072
if err != nil {
4173
return nil, err
4274
}
@@ -52,6 +84,10 @@ func (d *Pan123Share) getFiles(parentId string) ([]File, error) {
5284
page := 1
5385
res := make([]File, 0)
5486
for {
87+
if !d.APIRateLimit(FileList) {
88+
time.Sleep(time.Millisecond * 200)
89+
continue
90+
}
5591
var resp Files
5692
query := map[string]string{
5793
"limit": "100",

0 commit comments

Comments
 (0)
0