Skip to content

Commit

Permalink
修复分享获取不到密码
Browse files Browse the repository at this point in the history
  • Loading branch information
iikira committed Mar 30, 2020
1 parent fea1d73 commit 9837f8e
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 83 deletions.
2 changes: 2 additions & 0 deletions baidupcs/baidupcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ const (
OperationShareCancel = "取消分享"
// OperationShareList 列出分享列表
OperationShareList = "列出分享列表"
// OperationShareSURLInfo 获取分享详细信息
OperationShareSURLInfo = "获取分享详细信息"
// OperationRecycleList 列出回收站文件列表
OperationRecycleList = "列出回收站文件列表"
// OperationRecycleRestore 还原回收站文件或目录
Expand Down
6 changes: 4 additions & 2 deletions baidupcs/expires/expires.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ type (
}
)

//go:linkname stripMono time.(*Time).stripMono
func stripMono(t *time.Time)

// StripMono strip monotonic clocks
// go:linkname StripMono time.(*Time).stripMono
func StripMono(t *time.Time) {
t.Round(0)
stripMono(t)
}

func NewExpires(dur time.Duration) Expires {
Expand Down
38 changes: 2 additions & 36 deletions baidupcs/internal/panhome/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package panhome

import (
"github.com/iikira/Baidu-Login/bdcrypto"
"github.com/iikira/BaiduPCS-Go/baidupcs/netdisksign"
"github.com/iikira/BaiduPCS-Go/pcsutil/converter"
)

Expand All @@ -25,48 +26,13 @@ func (sr *signRes) Timestamp() string {
return sr.timestamp
}

func Sign2(j, r []rune) []byte {
var (
a = make([]rune, 256)
p = make([]rune, 256)
o = make([]byte, len(r))
v = len(j)
q int
u rune
i int
k rune
dr int
)
if v == 0 {
return o
}
for ; q < 256; q++ {
dr = q % v
a[q] = j[dr : 1+dr][0]
p[q] = rune(q)
}
for q = 0; q < 256; q++ {
u = (u + p[q] + a[q]) % 256
p[q], p[u] = p[u], p[q]
}
u = 0
for q = 0; q < len(r); q++ {
i = (i + 1) % 256
u = (u + p[i]) % 256
p[i], p[u] = p[u], p[i]
k = p[(p[i]+p[u])%256]
o[q] = byte(r[q] ^ k)
}
return o
}

func (ph *PanHome) Signature() (sign SignRes, err error) {
err = ph.getSignInfo()
if err != nil {
return nil, err
}

o := Sign2(ph.sign3, ph.sign1)
o := netdisksign.Sign2(ph.sign3, ph.sign1)
signed := bdcrypto.Base64Encode(o)
return &signRes{
sign: converter.ToString(signed),
Expand Down
20 changes: 20 additions & 0 deletions baidupcs/netdisksign/share_sign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package netdisksign

import (
"crypto/md5"
"encoding/hex"
"github.com/iikira/BaiduPCS-Go/pcsutil/cachepool"
"github.com/iikira/BaiduPCS-Go/pcsutil/converter"
"strconv"
)

func ShareSURLInfoSign(shareID int64) []byte {
s := strconv.FormatInt(shareID, 10)
m := md5.New()
m.Write(converter.ToBytes(s))
m.Write([]byte("_sharesurlinfo!@#"))
res := m.Sum(nil)
resHex := cachepool.RawMallocByteSlice(32)
hex.Encode(resHex, res)
return resHex
}
36 changes: 36 additions & 0 deletions baidupcs/netdisksign/sign2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package netdisksign

func Sign2(j, r []rune) []byte {
var (
a = make([]rune, 256)
p = make([]rune, 256)
o = make([]byte, len(r))
v = len(j)
q int
u rune
i int
k rune
dr int
)
if v == 0 {
return o
}
for ; q < 256; q++ {
dr = q % v
a[q] = j[dr : 1+dr][0]
p[q] = rune(q)
}
for q = 0; q < 256; q++ {
u = (u + p[q] + a[q]) % 256
p[q], p[u] = p[u], p[q]
}
u = 0
for q = 0; q < len(r); q++ {
i = (i + 1) % 256
u = (u + p[i]) % 256
p[i], p[u] = p[u], p[i]
k = p[(p[i]+p[u])%256]
o[q] = byte(r[q] ^ k)
}
return o
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package panhome_test
package netdisksign_test

import (
"fmt"
"github.com/iikira/Baidu-Login/bdcrypto"
"github.com/iikira/BaiduPCS-Go/baidupcs/internal/panhome"
"github.com/iikira/BaiduPCS-Go/baidupcs/netdisksign"
"testing"
)

Expand All @@ -12,7 +12,7 @@ func TestSign2(t *testing.T) {
fmt.Println("standard,", standard)
fmt.Printf("standard s %s\n", standard)

res := panhome.Sign2([]rune("e8c7d729eea7b54551aa594f942decbe"), []rune("37dbe07ade9359c1aa70807e847f768c13360ad2"))
res := netdisksign.Sign2([]rune("e8c7d729eea7b54551aa594f942decbe"), []rune("37dbe07ade9359c1aa70807e847f768c13360ad2"))
fmt.Println(res)
fmt.Printf("%s\n", string(res))
fmt.Println([]byte(string(res)))
Expand Down
41 changes: 35 additions & 6 deletions baidupcs/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,18 @@ func (pcs *BaiduPCS) sendReqReturnReadCloser(rt reqType, op, method, urlStr stri
// PrepareUK 获取用户 UK, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareUK() (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsURL := GetHTTPScheme(pcs.isHTTPS) + "://pan.baidu.com/api/user/getinfo?need_selfinfo=1"

dataReadCloser, pcsError = pcs.sendReqReturnReadCloser(reqTypePCS, OperationGetUK, http.MethodGet, pcsURL, nil, nil)
query := url.Values{}
query.Set("need_selfinfo", "1")

panURL := &url.URL{
Scheme: "https",
Host: PanBaiduCom,
Path: "api/user/getinfo",
RawQuery: query.Encode(),
}

dataReadCloser, pcsError = pcs.sendReqReturnReadCloser(reqTypePCS, OperationGetUK, http.MethodGet, panURL.String(), nil, nil)
return
}

Expand Down Expand Up @@ -446,7 +455,7 @@ func (pcs *BaiduPCS) PrepareUploadCreateSuperFile(checkDir bool, targetPath stri
func (pcs *BaiduPCS) PrepareUploadPrecreate(targetPath, contentMD5, sliceMD5, crc32 string, size int64, bolckList ...string) (dataReadCloser io.ReadCloser, panError pcserror.Error) {
pcs.lazyInit()
panURL := &url.URL{
Scheme: GetHTTPScheme(pcs.isHTTPS),
Scheme: "https",
Host: PanBaiduCom,
Path: "api/precreate",
}
Expand Down Expand Up @@ -580,7 +589,7 @@ func (pcs *BaiduPCS) PrepareCloudDlClearTask() (dataReadCloser io.ReadCloser, pc
func (pcs *BaiduPCS) PrepareSharePSet(paths []string, period int) (dataReadCloser io.ReadCloser, panError pcserror.Error) {
pcs.lazyInit()
panURL := &url.URL{
Scheme: GetHTTPScheme(pcs.isHTTPS),
Scheme: "https",
Host: PanBaiduCom,
Path: "share/pset",
}
Expand All @@ -601,7 +610,7 @@ func (pcs *BaiduPCS) PrepareSharePSet(paths []string, period int) (dataReadClose
func (pcs *BaiduPCS) PrepareShareCancel(shareIDs []int64) (dataReadCloser io.ReadCloser, panError pcserror.Error) {
pcs.lazyInit()
panURL := &url.URL{
Scheme: GetHTTPScheme(pcs.isHTTPS),
Scheme: "https",
Host: PanBaiduCom,
Path: "share/cancel",
}
Expand All @@ -627,7 +636,7 @@ func (pcs *BaiduPCS) PrepareShareList(page int) (dataReadCloser io.ReadCloser, p
query.Set("order", "time")

panURL := &url.URL{
Scheme: GetHTTPScheme(pcs.isHTTPS),
Scheme: "https",
Host: PanBaiduCom,
Path: "share/record",
RawQuery: query.Encode(),
Expand All @@ -638,6 +647,26 @@ func (pcs *BaiduPCS) PrepareShareList(page int) (dataReadCloser io.ReadCloser, p
return
}

// PrepareShareSURLInfo 获取分享的详细信息, 包含密码, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareShareSURLInfo(shareID int64) (dataReadCloser io.ReadCloser, panError pcserror.Error) {
pcs.lazyInit()

query := url.Values{}
query.Set("shareid", strconv.FormatInt(shareID, 10))
query.Set("sign", converter.ToString(netdisksign.ShareSURLInfoSign(shareID)))

panURL := &url.URL{
Scheme: "https",
Host: PanBaiduCom,
Path: "share/surlinfoinrecord",
RawQuery: query.Encode(),
}
baiduPCSVerbose.Infof("%s URL: %s\n", OperationShareSURLInfo, panURL)

dataReadCloser, panError = pcs.sendReqReturnReadCloser(reqTypePan, OperationShareSURLInfo, http.MethodGet, panURL.String(), nil, nil)
return
}

// PrepareRecycleList 列出回收站文件列表, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareRecycleList(page int) (dataReadCloser io.ReadCloser, panError pcserror.Error) {
pcs.lazyInit()
Expand Down
67 changes: 42 additions & 25 deletions baidupcs/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,25 @@ type (
ShareRecordInfo struct {
ShareID int64 `json:"shareId"`
FsIds []int64 `json:"fsIds"`
Passwd string `json:"passwd"`
Passwd string `json:"-"` // 这个字段已经没有用了, 需要从ShareSURLInfo中获取
Shortlink string `json:"shortlink"`
Status int `json:"status"` // 状态
Public int `json:"public"` // 是否为公开分享
TypicalCategory int `json:"typicalCategory"` // 文件类型
TypicalPath string `json:"typicalPath"`
}

shareSURLInfo struct {
*pcserror.PanErrorInfo
*ShareSURLInfo
}

// ShareSURLInfo 分享的子信息
ShareSURLInfo struct {
Pwd string `json:"pwd"` // 新密码
ShortURL string `json:"shorturl"`
}

// ShareRecordInfoList 分享信息列表
ShareRecordInfoList []*ShareRecordInfo

Expand All @@ -49,29 +61,6 @@ var (
ErrShareLinkNotFound = errors.New("未找到分享链接")
)

// Clean 清理
func (sri *ShareRecordInfo) Clean() {
if sri.Passwd == "0" {
sri.Passwd = ""
}
}

// HasPasswd 是否需要提取码
func (sri *ShareRecordInfo) HasPasswd() bool {
return sri.Passwd != "" && sri.Passwd != "0"
}

// Clean 清理
func (sril *ShareRecordInfoList) Clean() {
for _, sri := range *sril {
if sri == nil {
continue
}

sri.Clean()
}
}

// ShareSet 分享文件
func (pcs *BaiduPCS) ShareSet(paths []string, option *ShareOption) (s *Shared, pcsError pcserror.Error) {
if option == nil {
Expand Down Expand Up @@ -152,6 +141,34 @@ func (pcs *BaiduPCS) ShareList(page int) (records ShareRecordInfoList, pcsError
return nil, errInfo
}

jsonData.List.Clean()
return jsonData.List, nil
}

//ShareSURLInfo 获取分享的详细信息, 包含密码
func (pcs *BaiduPCS) ShareSURLInfo(shareID int64) (info *ShareSURLInfo, pcsError pcserror.Error) {
dataReadCloser, pcsError := pcs.PrepareShareSURLInfo(shareID)
if pcsError != nil {
return
}

defer dataReadCloser.Close()

errInfo := pcserror.NewPanErrorInfo(OperationShareSURLInfo)

jsonData := shareSURLInfo{
PanErrorInfo: errInfo,
}

pcsError = pcserror.HandleJSONParse(OperationShareList, dataReadCloser, &jsonData)
if pcsError != nil {
// json解析错误
return
}

// 去掉0
if jsonData.Pwd == "0" {
jsonData.Pwd = ""
}

return jsonData.ShareSURLInfo, nil
}
16 changes: 13 additions & 3 deletions internal/pcscommand/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ func RunShareList(page int) {
if page < 1 {
page = 1
}
records, err := GetBaiduPCS().ShareList(page)

pcs := GetBaiduPCS()
records, err := pcs.ShareList(page)
if err != nil {
fmt.Printf("%s失败: %s\n", baidupcs.OperationShareList, err)
return
Expand All @@ -56,8 +58,16 @@ func RunShareList(page int) {
tb := pcstable.NewTable(os.Stdout)
tb.SetHeader([]string{"#", "ShareID", "分享链接", "提取密码", "特征目录", "特征路径"})
for k, record := range records {
if record == nil {
continue
// 获取Passwd
if record.Public == 0 {
// 私密分享
info, pcsError := pcs.ShareSURLInfo(record.ShareID)
if pcsError != nil {
// 获取错误
fmt.Printf("[%d] 获取分享密码错误: %s\n", k, pcsError)
} else {
record.Passwd = info.Pwd
}
}

tb.Append([]string{strconv.Itoa(k), strconv.FormatInt(record.ShareID, 10), record.Shortlink, record.Passwd, path.Clean(path.Dir(record.TypicalPath)), record.TypicalPath})
Expand Down
4 changes: 3 additions & 1 deletion internal/pcscommand/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,16 @@ func RunUpload(localPaths []string, savePath string, opt *UploadOptions) {
var (
pcs = GetBaiduPCS()
// 使用 task framework
executor = &taskframework.TaskExecutor{
executor = &taskframework.TaskExecutor{
IsFailedDeque: true, // 失败统计
}
subSavePath string
// 统计
statistic = &pcsupload.UploadStatistic{}
)

statistic.StartTimer() // 开始计时

for k := range localPaths {
walkedFiles, err := pcsutil.WalkDir(localPaths[k], "")
if err != nil {
Expand Down
Loading

0 comments on commit 9837f8e

Please sign in to comment.