diff --git a/drivers/139/driver.go b/drivers/139/driver.go index 9d8cbd52..d33c3d77 100644 --- a/drivers/139/driver.go +++ b/drivers/139/driver.go @@ -8,18 +8,21 @@ import ( "net/http" "strconv" "strings" + "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/alist-org/alist/v3/pkg/cron" log "github.com/sirupsen/logrus" ) type Yun139 struct { model.Storage Addition + cron *cron.Cron Account string } @@ -35,6 +38,13 @@ func (d *Yun139) Init(ctx context.Context) error { if d.Authorization == "" { return fmt.Errorf("authorization is empty") } + d.cron = cron.NewCron(time.Hour * 24 * 7) + d.cron.Do(func() { + err := d.refreshToken() + if err != nil { + log.Errorf("%+v", err) + } + }) switch d.Addition.Type { case MetaPersonalNew: if len(d.Addition.RootFolderID) == 0 { @@ -72,6 +82,9 @@ func (d *Yun139) Init(ctx context.Context) error { } func (d *Yun139) Drop(ctx context.Context) error { + if d.cron != nil { + d.cron.Stop() + } return nil } diff --git a/drivers/139/types.go b/drivers/139/types.go index 841aa9d3..f7971966 100644 --- a/drivers/139/types.go +++ b/drivers/139/types.go @@ -1,5 +1,9 @@ package _139 +import ( + "encoding/xml" +) + const ( MetaPersonal string = "personal" MetaFamily string = "family" @@ -230,3 +234,12 @@ type PersonalUploadResp struct { UploadId string `json:"uploadId"` } } + +type RefreshTokenResp struct { + XMLName xml.Name `xml:"root"` + Return string `xml:"return"` + Token string `xml:"token"` + Expiretime int32 `xml:"expiretime"` + AccessToken string `xml:"accessToken"` + Desc string `xml:"desc"` +} diff --git a/drivers/139/util.go b/drivers/139/util.go index a3627b6c..3cd2966e 100644 --- a/drivers/139/util.go +++ b/drivers/139/util.go @@ -15,6 +15,7 @@ import ( "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/pkg/utils" "github.com/alist-org/alist/v3/pkg/utils/random" + "github.com/alist-org/alist/v3/internal/op" "github.com/go-resty/resty/v2" jsoniter "github.com/json-iterator/go" log "github.com/sirupsen/logrus" @@ -52,6 +53,32 @@ func getTime(t string) time.Time { return stamp } +func (d *Yun139) refreshToken() error { + url := "https://aas.caiyun.feixin.10086.cn:443/tellin/authTokenRefresh.do" + var resp RefreshTokenResp + decode, err := base64.StdEncoding.DecodeString(d.Authorization) + if err != nil { + return err + } + decodeStr := string(decode) + splits := strings.Split(decodeStr, ":") + reqBody := "" + splits[2] + "" + splits[1] + "656" + _, err = base.RestyClient.R(). + //ForceContentType("application/json"). + SetBody(reqBody). + SetResult(&resp). + Post(url) + if err != nil { + return err + } + if resp.Return != "0" { + return fmt.Errorf("failed to refresh token: %s", resp.Desc) + } + d.Authorization = base64.StdEncoding.EncodeToString([]byte(splits[0] + splits[1] + ":" + resp.Token)) + op.MustSaveDriverStorage(d) + return nil +} + func (d *Yun139) request(pathname string, method string, callback base.ReqCallback, resp interface{}) ([]byte, error) { url := "https://yun.139.com" + pathname req := base.RestyClient.R()