8000 feat(139): refresh token periodically (#6146) · anwen-anyi/alist@195c869 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 195c869

Browse files
authored
feat(139): refresh token periodically (AlistGo#6146)
* 139定时刷新token * fix build fail
1 parent bdfc159 commit 195c869

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

drivers/139/driver.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@ import (
88
"net/http"
99
"strconv"
1010
"strings"
11+
"time"
1112

1213
"github.com/alist-org/alist/v3/drivers/base"
1314
"github.com/alist-org/alist/v3/internal/driver"
1415
"github.com/alist-org/alist/v3/internal/errs"
1516
"github.com/alist-org/alist/v3/internal/model"
1617
"github.com/alist-org/alist/v3/pkg/utils"
18+
"github.com/alist-org/alist/v3/pkg/cron"
1719
log "github.com/sirupsen/logrus"
1820
)
1921

2022
type Yun139 struct {
2123
model.Storage
2224
Addition
25+
cron *cron.Cron
2326
Account string
2427
}
2528

@@ -35,6 +38,13 @@ func (d *Yun139) Init(ctx context.Context) error {
3538
if d.Authorization == "" {
3639
return fmt.Errorf("authorization is empty")
3740
}
41+
d.cron = cron.NewCron(time.Hour * 24 * 7)
42+
d.cron.Do(func() {
43+
err := d.refreshToken()
44+
if err != nil {
45+
log.Errorf("%+v", err)
46+
}
47+
})
3848
switch d.Addition.Type {
3949
case MetaPersonalNew:
4050
if len(d.Addition.RootFolderID) == 0 {
@@ -72,6 +82,9 @@ func (d *Yun139) Init(ctx context.Context) error {
7282
}
7383

7484
func (d *Yun139) Drop(ctx context.Context) error {
85+
if d.cron != nil {
86+
d.cron.Stop()
87+
}
7588
return nil
7689
}
7790

drivers/139/types.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package _139
22

3+
import (
4+
"encoding/xml"
5+
)
6+
37
const (
48
MetaPersonal string = "personal"
59
MetaFamily string = "family"
@@ -230,3 +234,12 @@ type PersonalUploadResp struct {
230234
UploadId string `json:"uploadId"`
231235
}
232236
}
237+
238+
type RefreshTokenResp struct {
239+
XMLName xml.Name `xml:"root"`
240+
Return string `xml:"return"`
241+
Token string `xml:"token"`
242+
Expiretime int32 `xml:"expiretime"`
243+
AccessToken string `xml:"accessToken"`
244+
Desc string `xml:"desc"`
245+
}

drivers/139/util.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/alist-org/alist/v3/internal/model"
1616
"github.com/alist-org/alist/v3/pkg/utils"
1717
"github.com/alist-org/alist/v3/pkg/utils/random"
18+
"github.com/alist-org/alist/v3/internal/op"
1819
"github.com/go-resty/resty/v2"
1920
jsoniter "github.com/json-iterator/go"
2021
log "github.com/sirupsen/logrus"
@@ -52,6 +53,32 @@ func getTime(t string) time.Time {
5253
return stamp
5354
}
5455

56+
func (d *Yun139) refreshToken() error {
57+
url := "https://aas.caiyun.feixin.10086.cn:443/tellin/authTokenRefresh.do"
58+
var resp RefreshTokenResp
59+
decode, err := base64.StdEncoding.DecodeString(d.Authorization)
60+
if err != nil {
61+
return err
62+
}
63+
decodeStr := string(decode)
64+
splits := strings.Split(decodeStr, ":")
65+
reqBody := "<root><token>" + splits[2] + "</token><account>" + splits[1] + "</account><clienttype>656</clienttype></root>"
66+
_, err = base.RestyClient.R().
67+
//ForceContentType("application/json").
68+
SetBody(reqBody).
69+
SetResult(&resp).
70+
Post(url)
71+
if err != nil {
72+
return err
73+
}
74+
if resp.Return != "0" {
75+
return fmt.Errorf("failed to refresh token: %s", resp.Desc)
76+
}
77+
d.Authorization = base64.StdEncoding.EncodeToString([]byte(splits[0] + splits[1] + ":" + resp.Token))
78+
op.MustSaveDriverStorage(d)
79+
return nil
80+
}
81+
5582
func (d *Yun139) request(pathname string, method string, callback base.ReqCallback, resp interface{}) ([]byte, error) {
5683
url := "https://yun.139.com" + pathname
5784
req := base.RestyClient.R()

0 commit comments

Comments
 (0)
0