forked from CuteReimu/bilibili
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wbi.go
303 lines (245 loc) · 7 KB
/
wbi.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package bilibili
import (
"crypto/md5"
"encoding/hex"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/go-resty/resty/v2"
"github.com/pkg/errors"
"golang.org/x/sync/singleflight"
)
const (
cacheImgKey = "imgKey"
cacheSubKey = "subKey"
)
var (
_defaultMixinKeyEncTab = []int{
46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49,
33, 9, 42, 19, 29, 28, 14, 39, 12, 38, 41, 13, 37, 48, 7, 16, 24, 55, 40,
61, 26, 17, 0, 1, 60, 51, 30, 4, 22, 25, 54, 21, 56, 59, 6, 63, 57, 62, 11,
36, 20, 34, 44, 52,
}
_defaultStorage = &MemoryStorage{
data: make(map[string]interface{}, 15),
}
)
type Storage interface {
Set(key string, value interface{})
Get(key string) (v interface{}, isSet bool)
}
type MemoryStorage struct {
data map[string]interface{}
mu sync.RWMutex
}
func (impl *MemoryStorage) Set(key string, value interface{}) {
impl.mu.Lock()
defer impl.mu.Unlock()
impl.data[key] = value
}
func (impl *MemoryStorage) Get(key string) (v interface{}, isSet bool) {
impl.mu.RLock()
defer impl.mu.RUnlock()
if v, isSet = impl.data[key]; isSet {
return v, true
}
return nil, false
}
// WBI 签名实现
// 如果希望以登录的方式获取则使用 WithCookies or WithRawCookies 设置cookie
// 如果希望以未登录的方式获取 WithCookies(nil) 设置cookie为 nil 即可, 这是 Default 行为
//
// !!! 使用 WBI 的接口 绝对不可以 set header Referer 会导致失败 !!!
// !!! 大部分使用 WBI 的接口都需要 set header Cookie !!!
//
// see https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/misc/sign/wbi.md
type WBI struct {
cookies []*http.Cookie
mixinKeyEncTab []int
// updateCheckerInterval is the interval to check and update wbi keys
// default is 60 minutes. so if lastInitTime + updateCheckerInterval < now, it will update wbi keys
updateCheckerInterval time.Duration
lastInitTime time.Time
storage Storage
sfg singleflight.Group
}
func NewDefaultWbi() *WBI {
return &WBI{
cookies: nil,
mixinKeyEncTab: _defaultMixinKeyEncTab,
updateCheckerInterval: 60 * time.Minute,
storage: _defaultStorage,
}
}
func (wbi *WBI) WithUpdateInterval(updateInterval time.Duration) *WBI {
wbi.updateCheckerInterval = updateInterval
return wbi
}
func (wbi *WBI) WithCookies(cookies []*http.Cookie) *WBI {
wbi.cookies = cookies
return wbi
}
func (wbi *WBI) WithRawCookies(rawCookies string) *WBI {
header := http.Header{}
header.Add("Cookie", rawCookies)
req := http.Request{Header: header}
wbi.cookies = req.Cookies()
return wbi
}
func (wbi *WBI) WithMixinKeyEncTab(mixinKeyEncTab []int) *WBI {
wbi.mixinKeyEncTab = mixinKeyEncTab
return wbi
}
func (wbi *WBI) WithStorage(storage Storage) *WBI {
wbi.storage = storage
return wbi
}
func (wbi *WBI) GetKeys() (imgKey string, subKey string, err error) {
imgKey, subKey = wbi.getKeys()
// 更新检查
if imgKey == "" || subKey == "" || time.Since(wbi.lastInitTime) > wbi.updateCheckerInterval {
if err = wbi.initWbi(); err != nil {
return "", "", err
}
return wbi.GetKeys()
}
return imgKey, subKey, nil
}
func (wbi *WBI) getKeys() (imgKey string, subKey string) {
if v, isSet := wbi.storage.Get(cacheImgKey); isSet {
imgKey = v.(string)
}
if v, isSet := wbi.storage.Get(cacheSubKey); isSet {
subKey = v.(string)
}
return imgKey, subKey
}
func (wbi *WBI) SetKeys(imgKey, subKey string) {
wbi.storage.Set(cacheImgKey, imgKey)
wbi.storage.Set(cacheSubKey, subKey)
wbi.lastInitTime = time.Now()
}
func (wbi *WBI) GetMixinKey() (string, error) {
imgKey, subKey, err := wbi.GetKeys()
if err != nil {
return "", err
}
return wbi.GenerateMixinKey(imgKey + subKey), nil
}
func (wbi *WBI) GenerateMixinKey(orig string) string {
var str strings.Builder
for _, v := range wbi.mixinKeyEncTab {
if v < len(orig) {
str.WriteByte(orig[v])
}
}
return str.String()[:32]
}
func (wbi *WBI) sanitizeString(s string) string {
unwantedChars := []string{"!", "'", "(", ")", "*"}
for _, char := range unwantedChars {
s = strings.ReplaceAll(s, char, "")
}
return s
}
func (wbi *WBI) SignQuery(query url.Values, ts time.Time) (newQuery url.Values, err error) {
payload := make(map[string]string, 10)
for k := range query {
payload[k] = query.Get(k)
}
newPayload, err := wbi.SignMap(payload, ts)
if err != nil {
return query, err
}
newQuery = url.Values{}
for k, v := range newPayload {
newQuery.Set(k, v)
}
return newQuery, nil
}
func (wbi *WBI) SignMap(payload map[string]string, ts time.Time) (newPayload map[string]string, err error) {
newPayload = make(map[string]string, 10)
for k, v := range payload {
newPayload[k] = v
}
newPayload["wts"] = strconv.FormatInt(ts.Unix(), 10)
// Sort keys
keys := make([]string, 0, 10)
for k := range newPayload {
keys = append(keys, k)
}
sort.Strings(keys)
// Remove unwanted characters
for k, v := range newPayload {
v = wbi.sanitizeString(v)
newPayload[k] = v
}
// Build URL parameters
signQuery := url.Values{}
for _, k := range keys {
signQuery.Set(k, newPayload[k])
}
signQueryStr := signQuery.Encode()
// Get mixin key
mixinKey, err := wbi.GetMixinKey()
if err != nil {
return payload, err
}
// Calculate w_rid
hash := md5.Sum([]byte(signQueryStr + mixinKey))
newPayload["w_rid"] = hex.EncodeToString(hash[:])
return newPayload, nil
}
func (wbi *WBI) initWbi() error {
_, err, _ := wbi.sfg.Do("initWbi", func() (interface{}, error) {
return nil, wbi.doInitWbi()
})
if err != nil {
return errors.WithStack(err)
}
return nil
}
func (wbi *WBI) doInitWbi() error {
result := struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
WbiImg struct {
ImgUrl string `json:"img_url"`
SubUrl string `json:"sub_url"`
} `json:"wbi_img"`
}
}{}
resp, err := resty.New().R().
SetHeader("Accept", "application/json").
SetHeader("Accept-Language", "zh-CN,zh;q=0.9").
SetHeader("Origin", "https://www.bilibili.com").
SetHeader("Referer", "https://www.bilibili.com/").
SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0").
SetCookies(wbi.cookies).
SetResult(&result).
Get("https://api.bilibili.com/x/web-interface/nav")
if err != nil {
return errors.WithStack(err)
}
if resp.StatusCode() != http.StatusOK {
return errors.Errorf("status code: %d", resp.StatusCode())
}
if result.Code != 0 {
if result.Data.WbiImg.ImgUrl == "" || result.Data.WbiImg.SubUrl == "" {
return errors.Errorf("init wbi 失败, 错误码: %d, 错误信息: %s", result.Code, result.Message)
}
}
if len(resp.Cookies()) > 0 {
// update cookie
wbi.cookies = resp.Cookies()
}
imgKey := strings.Split(strings.Split(result.Data.WbiImg.ImgUrl, "/")[len(strings.Split(result.Data.WbiImg.ImgUrl, "/"))-1], ".")[0]
subKey := strings.Split(strings.Split(result.Data.WbiImg.SubUrl, "/")[len(strings.Split(result.Data.WbiImg.SubUrl, "/"))-1], ".")[0]
wbi.SetKeys(imgKey, subKey)
return nil
}