Skip to content

Commit

Permalink
Merge pull request #25 from loognsss/tv-1
Browse files Browse the repository at this point in the history
feat(aliyundrive_open): Try to use multi-threaded download-tv interface-native proxy
  • Loading branch information
Three-taile-dragon authored Oct 13, 2024
2 parents b33bb50 + 9d3fc3a commit 93db7f3
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 33 deletions.
43 changes: 43 additions & 0 deletions drivers/115/appver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package _115

import (
driver115 "github.com/SheltonZhu/115driver/pkg/driver"
"github.com/alist-org/alist/v3/drivers/base"
log "github.com/sirupsen/logrus"
)

var (
md5Salt = "Qclm8MGWUv59TnrR0XPg"
appVer = "27.0.5.7"
)

func (d *Pan115) getAppVersion() ([]driver115.AppVersion, error) {
result := driver115.VersionResp{}
resp, err := base.RestyClient.R().Get(driver115.ApiGetVersion)

err = driver115.CheckErr(err, &result, resp)
if err != nil {
return nil, err
}

return result.Data.GetAppVersions(), nil
}

func (d *Pan115) getAppVer() string {
// todo add some cache?
vers, err := d.getAppVersion()
if err != nil {
log.Warnf("[115] get app version failed: %v", err)
return appVer
}
for _, ver := range vers {
if ver.AppName == "win" {
return ver.Version
}
}
return appVer
}

func (d *Pan115) initAppVer() {
appVer = d.getAppVer()
}
7 changes: 5 additions & 2 deletions drivers/115/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package _115
import (
"context"
"strings"
"sync"

driver115 "github.com/SheltonZhu/115driver/pkg/driver"
"github.com/alist-org/alist/v3/internal/driver"
Expand All @@ -16,8 +17,9 @@ import (
type Pan115 struct {
model.Storage
Addition
client *driver115.Pan115Client
limiter *rate.Limiter
client *driver115.Pan115Client
limiter *rate.Limiter
appVerOnce sync.Once
}

func (d *Pan115) Config() driver.Config {
Expand All @@ -29,6 +31,7 @@ func (d *Pan115) GetAddition() driver.Additional {
}

func (d *Pan115) Init(ctx context.Context) error {
d.appVerOnce.Do(d.initAppVer)
if d.LimitRate > 0 {
d.limiter = rate.NewLimiter(rate.Limit(d.LimitRate), 1)
}
Expand Down
41 changes: 18 additions & 23 deletions drivers/115/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package _115

import (
"bytes"
"crypto/md5"
"crypto/tls"
"encoding/hex"
"encoding/json"
"fmt"
"io"
Expand All @@ -26,12 +28,12 @@ import (
"github.com/pkg/errors"
)

var UserAgent = driver115.UA115Browser
//var UserAgent = driver115.UA115Browser

func (d *Pan115) login() error {
var err error
opts := []driver115.Option{
driver115.UA(UserAgent),
driver115.UA(d.getUA()),
func(c *driver115.Pan115Client) {
c.Client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: conf.Conf.TlsInsecureSkipVerify})
},
Expand Down Expand Up @@ -73,25 +75,11 @@ func (d *Pan115) getFiles(fileId string) ([]FileObj, error) {
return res, nil
}

const (
appVer = "27.0.3.7"
)

func (c *Pan115) getAppVer() string {
// todo add some cache?
vers, err := c.client.GetAppVersion()
if err != nil {
return appVer
}
for _, ver := range vers {
if ver.AppName == "win" {
return ver.Version
}
}
return appVer
func (d *Pan115) getUA() string {
return fmt.Sprintf("Mozilla/5.0 115Browser/%s", appVer)
}

func (c *Pan115) DownloadWithUA(pickCode, ua string) (*driver115.DownloadInfo, error) {
func (d *Pan115) DownloadWithUA(pickCode, ua string) (*driver115.DownloadInfo, error) {
key := crypto.GenerateKey()
result := driver115.DownloadResp{}
params, err := utils.Json.Marshal(map[string]string{"pickcode": pickCode})
Expand All @@ -105,10 +93,10 @@ func (c *Pan115) DownloadWithUA(pickCode, ua string) (*driver115.DownloadInfo, e
reqUrl := fmt.Sprintf("%s?t=%s", driver115.ApiDownloadGetUrl, driver115.Now().String())
req, _ := http.NewRequest(http.MethodPost, reqUrl, bodyReader)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Cookie", c.Cookie)
req.Header.Set("Cookie", d.Cookie)
req.Header.Set("User-Agent", ua)

resp, err := c.client.Client.GetClient().Do(req)
resp, err := d.client.Client.GetClient().Do(req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -146,6 +134,13 @@ func (c *Pan115) DownloadWithUA(pickCode, ua string) (*driver115.DownloadInfo, e
return nil, driver115.ErrUnexpected
}

func (c *Pan115) GenerateToken(fileID, preID, timeStamp, fileSize, signKey, signVal string) string {
userID := strconv.FormatInt(c.client.UserID, 10)
userIDMd5 := md5.Sum([]byte(userID))
tokenMd5 := md5.Sum([]byte(md5Salt + fileID + fileSize + signKey + signVal + userID + timeStamp + hex.EncodeToString(userIDMd5[:]) + appVer))
return hex.EncodeToString(tokenMd5[:])
}

func (d *Pan115) rapidUpload(fileSize int64, fileName, dirID, preID, fileID string, stream model.FileStreamer) (*driver115.UploadInitResp, error) {
var (
ecdhCipher *cipher.EcdhCipher
Expand All @@ -165,7 +160,7 @@ func (d *Pan115) rapidUpload(fileSize int64, fileName, dirID, preID, fileID stri
userID := strconv.FormatInt(d.client.UserID, 10)
form := url.Values{}
form.Set("appid", "0")
form.Set("appversion", d.getAppVer())
form.Set("appversion", appVer)
form.Set("userid", userID)
form.Set("filename", fileName)
form.Set("filesize", fileSizeStr)
Expand All @@ -186,7 +181,7 @@ func (d *Pan115) rapidUpload(fileSize int64, fileName, dirID, preID, fileID stri
}

form.Set("t", t.String())
form.Set("token", d.client.GenerateToken(fileID, preID, t.String(), fileSizeStr, signKey, signVal))
form.Set("token", d.GenerateToken(fileID, preID, t.String(), fileSizeStr, signKey, signVal))
if signKey != "" && signVal != "" {
form.Set("sign_key", signKey)
form.Set("sign_val", signVal)
Expand Down
4 changes: 2 additions & 2 deletions drivers/123_share/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (d *Pan123Share) Link(ctx context.Context, file model.Obj, args model.LinkA
}
u_ := u.String()
log.Debug("download url: ", u_)
res, err := base.NoRedirectClient.R().SetHeader("Referer", "https://www.123pan.com/").Get(u_)
res, err := base.NoRedirectClient.R().SetHeader("Referer", "https://www.123865.com/").Get(u_)
if err != nil {
return nil, err
}
Expand All @@ -134,7 +134,7 @@ func (d *Pan123Share) Link(ctx context.Context, file model.Obj, args model.LinkA
link.URL = utils.Json.Get(res.Body(), "data", "redirect_url").ToString()
}
link.Header = http.Header{
"Referer": []string{"https://www.123pan.com/"},
"Referer": []string{"https://www.123865.com/"},
}
return &link, nil
}
Expand Down
6 changes: 4 additions & 2 deletions drivers/aliyundrive_open/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ func (d *AliyundriveOpen) link(ctx context.Context, file model.Obj) (*model.Link
}
exp := time.Minute
return &model.Link{
URL: url,
Expiration: &exp,
URL: url,
Expiration: &exp,
Concurrency: 5,
PartSize: 10 * utils.MB,
}, nil
}

Expand Down
9 changes: 5 additions & 4 deletions internal/net/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,12 @@ func (d *downloader) finishBuf(id int) (isLast bool, buf *Buf) {
if id >= len(d.chunks)-1 {
return true, nil
}
if d.nextChunk > id+1 {
return false, d.getBuf(id + 1)

if d.nextChunk < len(d.chunks) {
d.sendChunkTask()
}
ch := d.sendChunkTask()
return false, ch.buf

return false, d.getBuf(id + 1)
}

// downloadPart is an individual goroutine worker reading from the ch channel
Expand Down

0 comments on commit 93db7f3

Please sign in to comment.