Skip to content

Commit

Permalink
feat(baidu_netdisk,baidu_photo): add and fix hashinfo (#7469)
Browse files Browse the repository at this point in the history
  • Loading branch information
foxxorcat authored Nov 8, 2024
1 parent f58de99 commit 67c93ee
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 19 deletions.
3 changes: 2 additions & 1 deletion drivers/baidu_netdisk/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
)

type TokenErrResp struct {
Expand Down Expand Up @@ -72,7 +73,7 @@ func fileToObj(f File) *model.ObjThumb {
IsFolder: f.Isdir == 1,

// 直接获取的MD5是错误的
// HashInfo: utils.NewHashInfo(utils.MD5, f.Md5),
HashInfo: utils.NewHashInfo(utils.MD5, DecryptMd5(f.Md5)),
},
Thumbnail: model.Thumbnail{Thumbnail: f.Thumbs.Url3},
}
Expand Down
57 changes: 40 additions & 17 deletions drivers/baidu_netdisk/util.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package baidu_netdisk

import (
"encoding/hex"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"unicode"

"github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/internal/errs"
Expand Down Expand Up @@ -153,8 +156,6 @@ func (d *BaiduNetdisk) linkOfficial(file model.Obj, args model.LinkArgs) (*model
u = res.Header().Get("location")
//}

updateObjMd5(file, "pan.baidu.com", u)

return &model.Link{
URL: u,
Header: http.Header{
Expand All @@ -178,8 +179,6 @@ func (d *BaiduNetdisk) linkCrack(file model.Obj, args model.LinkArgs) (*model.Li
return nil, err
}

updateObjMd5(file, d.CustomCrackUA, resp.Info[0].Dlink)

return &model.Link{
URL: resp.Info[0].Dlink,
Header: http.Header{
Expand Down Expand Up @@ -229,19 +228,6 @@ func joinTime(form map[string]string, ctime, mtime int64) {
form["local_ctime"] = strconv.FormatInt(ctime, 10)
}

func updateObjMd5(obj model.Obj, userAgent, u string) {
object := model.GetRawObject(obj)
if object != nil {
req, _ := http.NewRequest(http.MethodHead, u, nil)
req.Header.Add("User-Agent", userAgent)
resp, _ := base.HttpClient.Do(req)
if resp != nil {
contentMd5 := resp.Header.Get("Content-Md5")
object.HashInfo = utils.NewHashInfo(utils.MD5, contentMd5)
}
}
}

const (
DefaultSliceSize int64 = 4 * utils.MB
VipSliceSize = 16 * utils.MB
Expand All @@ -267,3 +253,40 @@ func (d *BaiduNetdisk) getSliceSize() int64 {
// r = strings.ReplaceAll(r, "+", "%20")
// return r
// }

func DecryptMd5(encryptMd5 string) string {
if _, err := hex.DecodeString(encryptMd5); err == nil {
return encryptMd5
}

var out strings.Builder
out.Grow(len(encryptMd5))
for i, n := 0, int64(0); i < len(encryptMd5); i++ {
if i == 9 {
n = int64(unicode.ToLower(rune(encryptMd5[i])) - 'g')
} else {
n, _ = strconv.ParseInt(encryptMd5[i:i+1], 16, 64)
}
out.WriteString(strconv.FormatInt(n^int64(15&i), 16))
}

encryptMd5 = out.String()
return encryptMd5[8:16] + encryptMd5[:8] + encryptMd5[24:32] + encryptMd5[16:24]
}

func EncryptMd5(originalMd5 string) string {
reversed := originalMd5[8:16] + originalMd5[:8] + originalMd5[24:32] + originalMd5[16:24]

var out strings.Builder
out.Grow(len(reversed))
for i, n := 0, int64(0); i < len(reversed); i++ {
n, _ = strconv.ParseInt(reversed[i:i+1], 16, 64)
n ^= int64(15 & i)
if i == 9 {
out.WriteRune(rune(n) + 'g')
} else {
out.WriteString(strconv.FormatInt(n, 16))
}
}
return out.String()
}
2 changes: 1 addition & 1 deletion drivers/baidu_photo/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (c *File) Thumb() string {
}

func (c *File) GetHash() utils.HashInfo {
return utils.NewHashInfo(utils.MD5, c.Md5)
return utils.NewHashInfo(utils.MD5, DecryptMd5(c.Md5))
}

/*相册部分*/
Expand Down
41 changes: 41 additions & 0 deletions drivers/baidu_photo/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package baiduphoto

import (
"context"
"encoding/hex"
"fmt"
"net/http"
"strconv"
"strings"
"unicode"

"github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/internal/errs"
Expand Down Expand Up @@ -476,3 +480,40 @@ func (d *BaiduPhoto) uInfo() (*UInfo, error) {
}
return &info, nil
}

func DecryptMd5(encryptMd5 string) string {
if _, err := hex.DecodeString(encryptMd5); err == nil {
return encryptMd5
}

var out strings.Builder
out.Grow(len(encryptMd5))
for i, n := 0, int64(0); i < len(encryptMd5); i++ {
if i == 9 {
n = int64(unicode.ToLower(rune(encryptMd5[i])) - 'g')
} else {
n, _ = strconv.ParseInt(encryptMd5[i:i+1], 16, 64)
}
out.WriteString(strconv.FormatInt(n^int64(15&i), 16))
}

encryptMd5 = out.String()
return encryptMd5[8:16] + encryptMd5[:8] + encryptMd5[24:32] + encryptMd5[16:24]
}

func EncryptMd5(originalMd5 string) string {
reversed := originalMd5[8:16] + originalMd5[:8] + originalMd5[24:32] + originalMd5[16:24]

var out strings.Builder
out.Grow(len(reversed))
for i, n := 0, int64(0); i < len(reversed); i++ {
n, _ = strconv.ParseInt(reversed[i:i+1], 16, 64)
n ^= int64(15 & i)
if i == 9 {
out.WriteRune(rune(n) + 'g')
} else {
out.WriteString(strconv.FormatInt(n, 16))
}
}
return out.String()
}

0 comments on commit 67c93ee

Please sign in to comment.