Skip to content

Commit

Permalink
fix(115): use latest appVer for upload (close AlistGo#7315)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Oct 11, 2024
1 parent 5f19d73 commit c3e43ff
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 25 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

0 comments on commit c3e43ff

Please sign in to comment.