Skip to content

Commit

Permalink
fix(pikpak): captcha_token not refreshing correctly (#6788)
Browse files Browse the repository at this point in the history
  • Loading branch information
Three-taile-dragon authored Jul 16, 2024
1 parent a93937f commit 049575b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 59 deletions.
26 changes: 13 additions & 13 deletions drivers/pikpak/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ func (d *PikPak) Init(ctx context.Context) (err error) {
)
}))

// 获取CaptchaToken
_ = d.RefreshCaptchaTokenAtLogin(GetAction(http.MethodGet, "https://api-drive.mypikpak.com/drive/v1/files"), d.Username)

// 获取用户ID
_ = d.GetUserID(ctx)
_ = d.GetUserID()

// 获取CaptchaToken
_ = d.RefreshCaptchaTokenAtLogin(GetAction(http.MethodGet, "https://api-drive.mypikpak.com/drive/v1/files"), d.Common.UserID)
// 更新UserAgent
d.Common.UserAgent = BuildCustomUserAgent(d.Common.DeviceID, ClientID, PackageName, SdkVersion, ClientVersion, PackageName, d.Common.UserID)
return nil
Expand Down Expand Up @@ -320,17 +320,17 @@ func (d *PikPak) DeleteOfflineTasks(ctx context.Context, taskIDs []string, delet
return nil
}

func (d *PikPak) GetUserID(ctx context.Context) error {
url := "https://api-drive.mypikpak.com/vip/v1/vip/info"
var resp VipInfo
_, err := d.requestWithCaptchaToken(url, http.MethodGet, func(req *resty.Request) {
req.SetContext(ctx)
}, &resp)
func (d *PikPak) GetUserID() error {

token, err := d.oauth2Token.Token()
if err != nil {
return fmt.Errorf("failed to get user id : %w", err)
return err
}
if resp.Data.UserID != "" {
d.Common.SetUserID(resp.Data.UserID)

userID := token.Extra("sub").(string)

if userID != "" {
d.Common.SetUserID(userID)
}
return nil
}
Expand Down
40 changes: 0 additions & 40 deletions drivers/pikpak/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ import (
hash_extend "github.com/alist-org/alist/v3/pkg/utils/hash"
)

type RespErr struct {
ErrorCode int `json:"error_code"`
Error string `json:"error"`
}

type Files struct {
Files []File `json:"files"`
NextPageToken string `json:"next_page_token"`
Expand Down Expand Up @@ -174,7 +169,6 @@ type ErrResp struct {
ErrorCode int64 `json:"error_code"`
ErrorMsg string `json:"error"`
ErrorDescription string `json:"error_description"`
// ErrorDetails interface{} `json:"error_details"`
}

func (e *ErrResp) IsError() bool {
Expand All @@ -199,37 +193,3 @@ type CaptchaTokenResponse struct {
ExpiresIn int64 `json:"expires_in"`
Url string `json:"url"`
}

type VipInfo struct {
Data struct {
Expire time.Time `json:"expire"`
ExtUserInfo struct {
UserRegion string `json:"userRegion"`
} `json:"extUserInfo"`
ExtType string `json:"ext_type"`
FeeRecord string `json:"fee_record"`
Restricted struct {
Result bool `json:"result"`
Content struct {
Text string `json:"text"`
Color string `json:"color"`
DeepLink string `json:"deepLink"`
} `json:"content"`
LearnMore struct {
Text string `json:"text"`
Color string `json:"color"`
DeepLink string `json:"deepLink"`
} `json:"learnMore"`
} `json:"restricted"`
Status string `json:"status"`
Type string `json:"type"`
UserID string `json:"user_id"`
VipItem []struct {
Type string `json:"type"`
Description string `json:"description"`
Status string `json:"status"`
Expire time.Time `json:"expire"`
SurplusDay int `json:"surplus_day"`
} `json:"vipItem"`
} `json:"data"`
}
11 changes: 5 additions & 6 deletions drivers/pikpak/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/md5"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"github.com/alist-org/alist/v3/pkg/utils"
"net/http"
Expand Down Expand Up @@ -53,15 +52,15 @@ func (d *PikPak) request(url string, method string, callback base.ReqCallback, r
if resp != nil {
req.SetResult(resp)
}
var e RespErr
var e ErrResp
req.SetError(&e)
res, err := req.Execute(method, url)
if err != nil {
return nil, err
}

if e.ErrorCode != 0 {
return nil, errors.New(e.Error)
if e.IsError() {
return nil, &e
}
return res.Body(), nil
}
Expand Down Expand Up @@ -101,7 +100,7 @@ func (d *PikPak) requestWithCaptchaToken(url string, method string, callback bas
default:
return nil, err
}
return d.request(url, method, callback, resp)
return d.requestWithCaptchaToken(url, method, callback, resp)
}

func (d *PikPak) getFiles(id string) ([]File, error) {
Expand Down Expand Up @@ -264,7 +263,7 @@ func (c *Common) GetCaptchaSign() (timestamp, sign string) {
return
}

// 刷新验证码token
// refreshCaptchaToken 刷新CaptchaToken
func (d *PikPak) refreshCaptchaToken(action string, metas map[string]string) error {
param := CaptchaTokenRequest{
Action: action,
Expand Down

0 comments on commit 049575b

Please sign in to comment.