Skip to content

Commit

Permalink
feat(pikpak&pikpak_share): add download address delay detection (#7136)
Browse files Browse the repository at this point in the history
* feat(pikpak): add download address delay detection

* feat(pikpak_share): add download address delay detection
  • Loading branch information
Three-taile-dragon authored Sep 8, 2024
1 parent c9fa3d7 commit 716d33f
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 56 deletions.
37 changes: 32 additions & 5 deletions drivers/pikpak/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"net/http"
"regexp"
"strconv"
"strings"
)
Expand Down Expand Up @@ -48,6 +49,7 @@ func (d *PikPak) Init(ctx context.Context) (err error) {
d.Common.CaptchaToken = token
op.MustSaveDriverStorage(d)
},
LowLatencyAddr: "",
}
}

Expand All @@ -65,6 +67,13 @@ func (d *PikPak) Init(ctx context.Context) (err error) {
d.PackageName = WebPackageName
d.Algorithms = WebAlgorithms
d.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
} else if d.Platform == "pc" {
d.ClientID = PCClientID
d.ClientSecret = PCClientSecret
d.ClientVersion = PCClientVersion
d.PackageName = PCPackageName
d.Algorithms = PCAlgorithms
d.UserAgent = "MainWindow Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) PikPak/2.5.6.4831 Chrome/100.0.4896.160 Electron/18.3.15 Safari/537.36"
}

if d.Addition.CaptchaToken != "" && d.Addition.RefreshToken == "" {
Expand Down Expand Up @@ -128,6 +137,15 @@ func (d *PikPak) Init(ctx context.Context) (err error) {
// 保存 有效的 RefreshToken
d.Addition.RefreshToken = d.RefreshToken
op.MustSaveDriverStorage(d)

if d.UseLowLatencyAddress && d.Addition.CustomLowLatencyAddress != "" {
d.Common.LowLatencyAddr = d.Addition.CustomLowLatencyAddress
} else if d.UseLowLatencyAddress {
d.Common.LowLatencyAddr = findLowestLatencyAddress(DlAddr)
d.Addition.CustomLowLatencyAddress = d.Common.LowLatencyAddr
op.MustSaveDriverStorage(d)
}

return nil
}

Expand All @@ -147,6 +165,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
var url string
queryParams := map[string]string{
"_magic": "2021",
"usage": "FETCH",
Expand All @@ -162,14 +181,22 @@ func (d *PikPak) Link(ctx context.Context, file model.Obj, args model.LinkArgs)
if err != nil {
return nil, err
}
link := model.Link{
URL: resp.WebContentLink,
}
url = resp.WebContentLink

if !d.DisableMediaLink && len(resp.Medias) > 0 && resp.Medias[0].Link.Url != "" {
log.Debugln("use media link")
link.URL = resp.Medias[0].Link.Url
url = resp.Medias[0].Link.Url
}

if d.UseLowLatencyAddress && d.Common.LowLatencyAddr != "" {
// 替换为加速链接
re := regexp.MustCompile(`https://[^/]+/download/`)
url = re.ReplaceAllString(url, "https://"+d.Common.LowLatencyAddr+"/download/")
}
return &link, nil

return &model.Link{
URL: url,
}, nil
}

func (d *PikPak) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
Expand Down
18 changes: 10 additions & 8 deletions drivers/pikpak/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (

type Addition struct {
driver.RootID
Username string `json:"username" required:"true"`
Password string `json:"password" required:"true"`
Platform string `json:"platform" required:"true" type:"select" options:"android,web"`
RefreshToken string `json:"refresh_token" required:"true" default:""`
RefreshTokenMethod string `json:"refresh_token_method" required:"true" type:"select" options:"oauth2,http"`
CaptchaToken string `json:"captcha_token" default:""`
DeviceID string `json:"device_id" required:"false" default:""`
DisableMediaLink bool `json:"disable_media_link" default:"true"`
Username string `json:"username" required:"true"`
Password string `json:"password" required:"true"`
Platform string `json:"platform" required:"true" type:"select" options:"android,web,pc"`
RefreshToken string `json:"refresh_token" required:"true" default:""`
RefreshTokenMethod string `json:"refresh_token_method" required:"true" type:"select" options:"oauth2,http"`
CaptchaToken string `json:"captcha_token" default:""`
DeviceID string `json:"device_id" required:"false" default:""`
DisableMediaLink bool `json:"disable_media_link" default:"true"`
UseLowLatencyAddress bool `json:"use_low_latency_address" default:"false"`
CustomLowLatencyAddress string `json:"custom_low_latency_address" default:""`
}

var config = driver.Config{
Expand Down
131 changes: 113 additions & 18 deletions drivers/pikpak/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,14 @@ import (
// do others that not defined in Driver interface

var AndroidAlgorithms = []string{
"Gez0T9ijiI9WCeTsKSg3SMlx",
"zQdbalsolyb1R/",
"ftOjr52zt51JD68C3s",
"yeOBMH0JkbQdEFNNwQ0RI9T3wU/v",
"BRJrQZiTQ65WtMvwO",
"je8fqxKPdQVJiy1DM6Bc9Nb1",
"niV",
"9hFCW2R1",
"sHKHpe2i96",
"p7c5E6AcXQ/IJUuAEC9W6",
"",
"aRv9hjc9P+Pbn+u3krN6",
"BzStcgE8qVdqjEH16l4",
"SqgeZvL5j9zoHP95xWHt",
"zVof5yaJkPe3VFpadPof",
"aDhgaSE3MsjROCmpmsWqP1sJdFJ",
"+oaVkqdd8MJuKT+uMr2AYKcd9tdWge3XPEPR2hcePUknd",
"u/sd2GgT2fTytRcKzGicHodhvIltMntA3xKw2SRv7S48OdnaQIS5mn",
"2WZiae2QuqTOxBKaaqCNHCW3olu2UImelkDzBn",
"/vJ3upic39lgmrkX855Qx",
"yNc9ruCVMV7pGV7XvFeuLMOcy1",
"4FPq8mT3JQ1jzcVxMVfwFftLQm33M7i",
"xozoy5e3Ea",
}

var WebAlgorithms = []string{
Expand All @@ -65,6 +58,19 @@ var WebAlgorithms = []string{
"NhXXU9rg4XXdzo7u5o",
}

var PCAlgorithms = []string{
"KHBJ07an7ROXDoK7Db",
"G6n399rSWkl7WcQmw5rpQInurc1DkLmLJqE",
"JZD1A3M4x+jBFN62hkr7VDhkkZxb9g3rWqRZqFAAb",
"fQnw/AmSlbbI91Ik15gpddGgyU7U",
"/Dv9JdPYSj3sHiWjouR95NTQff",
"yGx2zuTjbWENZqecNI+edrQgqmZKP",
"ljrbSzdHLwbqcRn",
"lSHAsqCkGDGxQqqwrVu",
"TsWXI81fD1",
"vk7hBjawK/rOSrSWajtbMk95nfgf3",
}

const (
OSSUserAgent = "aliyun-sdk-android/2.9.13(Linux/Android 14/M2004j7ac;UKQ1.231108.001)"
OssSecurityTokenHeaderName = "X-OSS-Security-Token"
Expand All @@ -74,16 +80,59 @@ const (
const (
AndroidClientID = "YNxT9w7GMdWvEOKa"
AndroidClientSecret = "dbw2OtmVEeuUvIptb1Coyg"
AndroidClientVersion = "1.47.1"
AndroidClientVersion = "1.48.3"
AndroidPackageName = "com.pikcloud.pikpak"
AndroidSdkVersion = "2.0.4.204000"
AndroidSdkVersion = "2.0.4.204101"
WebClientID = "YUMx5nI8ZU8Ap8pm"
WebClientSecret = "dbw2OtmVEeuUvIptb1Coyg"
WebClientVersion = "2.0.0"
WebPackageName = "mypikpak.com"
WebSdkVersion = "8.0.3"
PCClientID = "YvtoWO6GNHiuCl7x"
PCClientSecret = "1NIH5R1IEe2pAxZE3hv3uA"
PCClientVersion = "undefined" // 2.5.6.4831
PCPackageName = "mypikpak.com"
PCSdkVersion = "8.0.3"
)

var DlAddr = []string{
"dl-a10b-0621.mypikpak.com",
"dl-a10b-0622.mypikpak.com",
"dl-a10b-0623.mypikpak.com",
"dl-a10b-0624.mypikpak.com",
"dl-a10b-0625.mypikpak.com",
"dl-a10b-0858.mypikpak.com",
"dl-a10b-0859.mypikpak.com",
"dl-a10b-0860.mypikpak.com",
"dl-a10b-0861.mypikpak.com",
"dl-a10b-0862.mypikpak.com",
"dl-a10b-0863.mypikpak.com",
"dl-a10b-0864.mypikpak.com",
"dl-a10b-0865.mypikpak.com",
"dl-a10b-0866.mypikpak.com",
"dl-a10b-0867.mypikpak.com",
"dl-a10b-0868.mypikpak.com",
"dl-a10b-0869.mypikpak.com",
"dl-a10b-0870.mypikpak.com",
"dl-a10b-0871.mypikpak.com",
"dl-a10b-0872.mypikpak.com",
"dl-a10b-0873.mypikpak.com",
"dl-a10b-0874.mypikpak.com",
"dl-a10b-0875.mypikpak.com",
"dl-a10b-0876.mypikpak.com",
"dl-a10b-0877.mypikpak.com",
"dl-a10b-0878.mypikpak.com",
"dl-a10b-0879.mypikpak.com",
"dl-a10b-0880.mypikpak.com",
"dl-a10b-0881.mypikpak.com",
"dl-a10b-0882.mypikpak.com",
"dl-a10b-0883.mypikpak.com",
"dl-a10b-0884.mypikpak.com",
"dl-a10b-0885.mypikpak.com",
"dl-a10b-0886.mypikpak.com",
"dl-a10b-0887.mypikpak.com",
}

func (d *PikPak) login() error {
url := "https://user.mypikpak.com/v1/auth/signin"
// 使用 用户填写的 CaptchaToken —————— (验证后的captcha_token)
Expand Down Expand Up @@ -180,13 +229,15 @@ func (d *PikPak) request(url string, method string, callback base.ReqCallback, r
"X-Device-ID": d.GetDeviceID(),
"X-Captcha-Token": d.GetCaptchaToken(),
})
if d.oauth2Token != nil {
if d.RefreshTokenMethod == "oauth2" {
// 使用oauth2 获取 access_token
token, err := d.oauth2Token.Token()
if err != nil {
return nil, err
}
req.SetAuthScheme(token.TokenType).SetAuthToken(token.AccessToken)
} else {
req.SetHeader("Authorization", "Bearer "+d.AccessToken)
}

if callback != nil {
Expand Down Expand Up @@ -277,6 +328,7 @@ type Common struct {
UserAgent string
// 验证码token刷新成功回调
RefreshCTokenCk func(token string)
LowLatencyAddr string
}

func generateDeviceSign(deviceID, packageName string) string {
Expand Down Expand Up @@ -667,3 +719,46 @@ func OssOption(params *S3Params) []oss.Option {
}
return options
}

type AddressLatency struct {
Address string
Latency time.Duration
}

func checkLatency(address string, wg *sync.WaitGroup, ch chan<- AddressLatency) {
defer wg.Done()
start := time.Now()
resp, err := http.Get("https://" + address + "/generate_204")
if err != nil {
ch <- AddressLatency{Address: address, Latency: time.Hour} // Set high latency on error
return
}
defer resp.Body.Close()
latency := time.Since(start)
ch <- AddressLatency{Address: address, Latency: latency}
}

func findLowestLatencyAddress(addresses []string) string {
var wg sync.WaitGroup
ch := make(chan AddressLatency, len(addresses))

for _, address := range addresses {
wg.Add(1)
go checkLatency(address, &wg, ch)
}

wg.Wait()
close(ch)

var lowestLatencyAddress string
lowestLatency := time.Hour

for result := range ch {
if result.Latency < lowestLatency {
lowestLatency = result.Latency
lowestLatencyAddress = result.Address
}
}

return lowestLatencyAddress
}
30 changes: 27 additions & 3 deletions drivers/pikpak_share/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"github.com/alist-org/alist/v3/internal/op"
"net/http"
"regexp"
"time"

"github.com/alist-org/alist/v3/internal/driver"
Expand Down Expand Up @@ -36,6 +37,7 @@ func (d *PikPakShare) Init(ctx context.Context) error {
d.Common.CaptchaToken = token
op.MustSaveDriverStorage(d)
},
LowLatencyAddr: "",
}
}

Expand All @@ -60,6 +62,21 @@ func (d *PikPakShare) Init(ctx context.Context) error {
d.PackageName = WebPackageName
d.Algorithms = WebAlgorithms
d.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
} else if d.Platform == "pc" {
d.ClientID = PCClientID
d.ClientSecret = PCClientSecret
d.ClientVersion = PCClientVersion
d.PackageName = PCPackageName
d.Algorithms = PCAlgorithms
d.UserAgent = "MainWindow Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) PikPak/2.5.6.4831 Chrome/100.0.4896.160 Electron/18.3.15 Safari/537.36"
}

if d.UseLowLatencyAddress && d.Addition.CustomLowLatencyAddress != "" {
d.Common.LowLatencyAddr = d.Addition.CustomLowLatencyAddress
} else if d.UseLowLatencyAddress {
d.Common.LowLatencyAddr = findLowestLatencyAddress(DlAddr)
d.Addition.CustomLowLatencyAddress = d.Common.LowLatencyAddr
op.MustSaveDriverStorage(d)
}

// 获取CaptchaToken
Expand All @@ -71,6 +88,7 @@ func (d *PikPakShare) Init(ctx context.Context) error {
if d.SharePwd != "" {
return d.getSharePassToken()
}

return nil
}

Expand Down Expand Up @@ -112,10 +130,16 @@ func (d *PikPakShare) Link(ctx context.Context, file model.Obj, args model.LinkA
}

}
link := model.Link{
URL: downloadUrl,

if d.UseLowLatencyAddress && d.Common.LowLatencyAddr != "" {
// 替换为加速链接
re := regexp.MustCompile(`https://[^/]+/download/`)
downloadUrl = re.ReplaceAllString(downloadUrl, "https://"+d.Common.LowLatencyAddr+"/download/")
}
return &link, nil

return &model.Link{
URL: downloadUrl,
}, nil
}

var _ driver.Driver = (*PikPakShare)(nil)
12 changes: 7 additions & 5 deletions drivers/pikpak_share/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (

type Addition struct {
driver.RootID
ShareId string `json:"share_id" required:"true"`
SharePwd string `json:"share_pwd"`
Platform string `json:"platform" required:"true" type:"select" options:"android,web"`
DeviceID string `json:"device_id" required:"false" default:""`
UseTransCodingAddress bool `json:"use_transcoding_address" required:"true" default:"false"`
ShareId string `json:"share_id" required:"true"`
SharePwd string `json:"share_pwd"`
Platform string `json:"platform" required:"true" type:"select" options:"android,web,pc"`
DeviceID string `json:"device_id" required:"false" default:""`
UseTransCodingAddress bool `json:"use_transcoding_address" required:"true" default:"false"`
UseLowLatencyAddress bool `json:"use_low_latency_address" default:"false"`
CustomLowLatencyAddress string `json:"custom_low_latency_address" default:""`
}

var config = driver.Config{
Expand Down
Loading

0 comments on commit 716d33f

Please sign in to comment.