Skip to content

Commit

Permalink
fix(pikpak): add captcha_token generation function (#6775)
Browse files Browse the repository at this point in the history
closes #6752 
closes #6760
  • Loading branch information
Three-taile-dragon authored Jul 14, 2024
1 parent 488ebaa commit a93937f
Show file tree
Hide file tree
Showing 3 changed files with 342 additions and 2 deletions.
42 changes: 40 additions & 2 deletions drivers/pikpak/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/alist-org/alist/v3/internal/op"
"net/http"
"strconv"
"strings"
Expand All @@ -25,7 +26,7 @@ import (
type PikPak struct {
model.Storage
Addition

*Common
oauth2Token oauth2.TokenSource
}

Expand All @@ -43,6 +44,20 @@ func (d *PikPak) Init(ctx context.Context) (err error) {
d.ClientSecret = "dbw2OtmVEeuUvIptb1Coyg"
}

if d.Common == nil {
d.Common = &Common{
client: base.NewRestyClient(),
CaptchaToken: "",
UserID: "",
DeviceID: utils.GetMD5EncodeStr(d.Username + d.Password),
UserAgent: BuildCustomUserAgent(utils.GetMD5EncodeStr(d.Username+d.Password), ClientID, PackageName, SdkVersion, ClientVersion, PackageName, ""),
RefreshCTokenCk: func(token string) {
d.Common.CaptchaToken = token
op.MustSaveDriverStorage(d)
},
}
}

oauth2Config := &oauth2.Config{
ClientID: d.ClientID,
ClientSecret: d.ClientSecret,
Expand All @@ -60,6 +75,14 @@ func (d *PikPak) Init(ctx context.Context) (err error) {
d.Password,
)
}))

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

// 获取用户ID
_ = d.GetUserID(ctx)
// 更新UserAgent
d.Common.UserAgent = BuildCustomUserAgent(d.Common.DeviceID, ClientID, PackageName, SdkVersion, ClientVersion, PackageName, d.Common.UserID)
return nil
}

Expand All @@ -79,7 +102,7 @@ func (d *PikPak) List(ctx context.Context, dir model.Obj, args model.ListArgs) (

func (d *PikPak) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
var resp File
_, err := d.request(fmt.Sprintf("https://api-drive.mypikpak.com/drive/v1/files/%s?_magic=2021&thumbnail_size=SIZE_LARGE", file.GetID()),
_, err := d.requestWithCaptchaToken(fmt.Sprintf("https://api-drive.mypikpak.com/drive/v1/files/%s?_magic=2021&thumbnail_size=SIZE_LARGE", file.GetID()),
http.MethodGet, nil, &resp)
if err != nil {
return nil, err
Expand Down Expand Up @@ -297,4 +320,19 @@ 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)
if err != nil {
return fmt.Errorf("failed to get user id : %w", err)
}
if resp.Data.UserID != "" {
d.Common.SetUserID(resp.Data.UserID)
}
return nil
}

var _ driver.Driver = (*PikPak)(nil)
65 changes: 65 additions & 0 deletions drivers/pikpak/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pikpak

import (
"fmt"
"strconv"
"time"

Expand Down Expand Up @@ -168,3 +169,67 @@ type ReferenceResource struct {
Tags []string `json:"tags"`
ThumbnailLink string `json:"thumbnail_link"`
}

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 {
return e.ErrorCode != 0 || e.ErrorMsg != "" || e.ErrorDescription != ""
}

func (e *ErrResp) Error() string {
return fmt.Sprintf("ErrorCode: %d ,Error: %s ,ErrorDescription: %s ", e.ErrorCode, e.ErrorMsg, e.ErrorDescription)
}

type CaptchaTokenRequest struct {
Action string `json:"action"`
CaptchaToken string `json:"captcha_token"`
ClientID string `json:"client_id"`
DeviceID string `json:"device_id"`
Meta map[string]string `json:"meta"`
RedirectUri string `json:"redirect_uri"`
}

type CaptchaTokenResponse struct {
CaptchaToken string `json:"captcha_token"`
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"`
}
Loading

0 comments on commit a93937f

Please sign in to comment.