-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
check_bduss.go
93 lines (80 loc) · 2.37 KB
/
check_bduss.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package pcsconfig
import (
"fmt"
"github.com/bitly/go-simplejson"
"github.com/iikira/BaiduPCS-Go/downloader"
"github.com/iikira/BaiduPCS-Go/util"
"strconv"
)
type Baidu struct {
UID uint64 `json:"uid"`
Name string `json:"name"`
BDUSS string `json:"bduss"`
Workdir string `json:"workdir"`
}
// NewWithBDUSS 检测BDUSS有效性, 同时获取百度详细信息
func NewWithBDUSS(bduss string) (*Baidu, error) {
h := downloader.NewHTTPClient()
timestamp := pcsutil.BeijingTimeOption("")
post := map[string]string{
"bdusstoken": bduss + "|null",
"channel_id": "",
"channel_uid": "",
"stErrorNums": "0",
"subapp_type": "mini",
"timestamp": timestamp + "922",
}
pcsutil.TiebaClientSignature(post)
header := map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": "ka=open",
"net": "1",
"User-Agent": "bdtb for Android 6.9.2.1",
"client_logid": timestamp + "416",
"Connection": "Keep-Alive",
}
body, err := h.Fetch("POST", "http://tieba.baidu.com/c/s/login", post, header) // 获取百度ID的TBS,UID,BDUSS等
if err != nil {
return nil, fmt.Errorf("检测BDUSS有效性失败, %s", err)
}
json, err := simplejson.NewJson(body)
if err != nil {
return nil, fmt.Errorf("检测BDUSS有效性json解析出错: %s", err)
}
errCode := json.Get("error_code").MustString()
errMsg := json.Get("error_msg").MustString()
switch errCode {
case "0":
case "1":
return nil, fmt.Errorf("检测BDUSS有效性错误, 百度BDUSS格式不正确或者已过期")
default:
return nil, fmt.Errorf("检测BDUSS有效性错误代码: %s, 消息: %s", errCode, errMsg)
}
uidStr := json.GetPath("user", "id").MustString()
uid, _ := strconv.ParseUint(uidStr, 10, 64)
username, err := GetUserNameByUID(uid)
if err != nil {
return nil, err
}
return &Baidu{
UID: uid,
Name: username,
BDUSS: bduss,
Workdir: "/",
}, nil
}
func GetUserNameByUID(uid uint64) (username string, err error) {
rawQuery := "has_plist=0&need_post_count=1&rn=1&uid=" + fmt.Sprint(uid)
urlStr := "http://c.tieba.baidu.com/c/u/user/profile?" + pcsutil.TiebaClientRawQuerySignature(rawQuery)
body, err := downloader.HTTPGet(urlStr)
if err != nil {
return "", err
}
json, err := simplejson.NewJson(body)
if err != nil {
return "", err
}
userJSON := json.GetPath("user")
username = userJSON.Get("name").MustString()
return
}