From 7d1034c569a8d70630b87538fc18d35558807b69 Mon Sep 17 00:00:00 2001 From: foxxorcat <95907542+foxxorcat@users.noreply.github.com> Date: Thu, 16 Feb 2023 22:12:19 +0800 Subject: [PATCH] fix(aliyundrive): error occurred when running multiple instances at the same time (#3448) * fix(aliyundrive):an error occurred when running multiple instances at the same time * Update util.go fix(aliyunpan):clear retry count --- drivers/aliyundrive/driver.go | 4 +-- drivers/aliyundrive/global.go | 2 +- drivers/aliyundrive/util.go | 54 ++++++++++++----------------------- 3 files changed, 21 insertions(+), 39 deletions(-) diff --git a/drivers/aliyundrive/driver.go b/drivers/aliyundrive/driver.go index 63cc9a02..46e65525 100644 --- a/drivers/aliyundrive/driver.go +++ b/drivers/aliyundrive/driver.go @@ -71,14 +71,14 @@ func (d *AliDrive) Init(ctx context.Context) error { // init privateKey privateKey, _ := NewPrivateKeyFromHex(deviceID) state := State{ - nonce: -1, privateKey: privateKey, deviceID: deviceID, } // store state global.Store(d.UserID, &state) // init signature - return d.reSign() + d.sign() + return nil } func (d *AliDrive) Drop(ctx context.Context) error { diff --git a/drivers/aliyundrive/global.go b/drivers/aliyundrive/global.go index 31045d38..a0929780 100644 --- a/drivers/aliyundrive/global.go +++ b/drivers/aliyundrive/global.go @@ -9,7 +9,7 @@ import ( type State struct { deviceID string signature string - nonce int + retry int privateKey *ecdsa.PrivateKey } diff --git a/drivers/aliyundrive/util.go b/drivers/aliyundrive/util.go index 2376f8a7..b36900fb 100644 --- a/drivers/aliyundrive/util.go +++ b/drivers/aliyundrive/util.go @@ -13,7 +13,6 @@ import ( "github.com/dustinxie/ecc" "github.com/go-resty/resty/v2" "github.com/google/uuid" - log "github.com/sirupsen/logrus" ) func (d *AliDrive) createSession() error { @@ -21,56 +20,39 @@ func (d *AliDrive) createSession() error { if !ok { return fmt.Errorf("can't load user state, user_id: %s", d.UserID) } + d.sign() + state.retry++ + if state.retry > 3 { + state.retry = 0 + return fmt.Errorf("createSession failed after three retries") + } _, err, _ := d.request("https://api.aliyundrive.com/users/v1/users/device/create_session", http.MethodPost, func(req *resty.Request) { req.SetBody(base.Json{ "deviceName": "samsung", "modelName": "SM-G9810", - "nonce": state.nonce, + "nonce": 0, "pubKey": PublicKeyToHex(&state.privateKey.PublicKey), "refreshToken": d.RefreshToken, }) }, nil) + if err == nil{ + state.retry = 0 + } return err } -func (d *AliDrive) renewSession() error { - _, err, _ := d.request("https://api.aliyundrive.com/users/v1/users/device/renew_session", http.MethodPost, nil, nil) - return err -} +// func (d *AliDrive) renewSession() error { +// _, err, _ := d.request("https://api.aliyundrive.com/users/v1/users/device/renew_session", http.MethodPost, nil, nil) +// return err +// } func (d *AliDrive) sign() { - state, ok := global.Load(d.UserID) - if !ok { - log.Errorf("can't load user state, user_id: %s", d.UserID) - return - } + state, _ := global.Load(d.UserID) secpAppID := "5dde4e1bdf9e4966b387ba58f4b3fdc3" - singdata := fmt.Sprintf("%s:%s:%s:%d", secpAppID, state.deviceID, d.UserID, state.nonce) + singdata := fmt.Sprintf("%s:%s:%s:%d", secpAppID, state.deviceID, d.UserID, 0) hash := sha256.Sum256([]byte(singdata)) data, _ := ecc.SignBytes(state.privateKey, hash[:], ecc.RecID|ecc.LowerS) - state.signature = hex.EncodeToString(data) -} - -func (d *AliDrive) reSign() error { - state, ok := global.Load(d.UserID) - if !ok { - return fmt.Errorf("can't load user state, user_id: %s", d.UserID) - } - state.nonce++ - if state.nonce >= 1073741823 { - state.nonce = 0 - } - d.sign() - if state.nonce == 0 { - return d.createSession() - } - err := d.renewSession() - if err != nil && err.Error() == "device session signature error" { - state.nonce = 0 - d.sign() - return d.createSession() - } - return nil + state.signature = hex.EncodeToString(data) //strconv.Itoa(state.nonce) } // do others that not defined in Driver interface @@ -141,7 +123,7 @@ func (d *AliDrive) request(url, method string, callback base.ReqCallback, resp i return nil, err, e } case "DeviceSessionSignatureInvalid": - err = d.reSign() + err = d.createSession() if err != nil { return nil, err, e }