This repository has been archived by the owner on Aug 11, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
auth.go
264 lines (223 loc) · 6.49 KB
/
auth.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
package main
import (
"database/sql"
"errors"
"fmt"
"net/url"
"strings"
"time"
"github.com/JasonKhew96/biliroaming-go-server/entity"
"github.com/mailru/easyjson"
"github.com/valyala/fasthttp"
)
// BlockTypeEnum block type
type BlockTypeEnum int
// BlockType
const (
BlockTypeDisabled BlockTypeEnum = iota
BlockTypeEnabled
BlockTypeWhitelist
)
type userStatus struct {
isLogin bool
isVip bool
isBlacklist bool
isWhitelist bool
uid int64
banUntil time.Time
}
func (b *BiliroamingGo) getAuthByArea(area string) bool {
switch strings.ToLower(area) {
case "cn":
return b.config.Auth.CN
case "hk":
return b.config.Auth.HK
case "tw":
return b.config.Auth.TW
case "th":
return b.config.Auth.TH
default:
return true
}
}
func (b *BiliroamingGo) checkBWlist(ctx *fasthttp.RequestCtx, uid int64) (*entity.BlackWhitelist, error) {
apiUrl := fmt.Sprintf(b.config.BlacklistApiUrl, uid)
reqParams := &HttpRequestParams{
Method: []byte(fasthttp.MethodGet),
Url: []byte(apiUrl),
UserAgent: []byte(DEFAULT_NAME),
}
data, err := b.doRequestJson(b.defaultClient, reqParams)
if err != nil {
return nil, err
}
blackwhitelist := &entity.BlackWhitelist{}
if err := easyjson.Unmarshal(data, blackwhitelist); err != nil {
return nil, err
}
return blackwhitelist, nil
}
func (b *BiliroamingGo) isAuth(ctx *fasthttp.RequestCtx, accessKey string, clientType ClientType, isForced bool) (*userStatus, error) {
userStatus := &userStatus{
uid: -1,
}
keyData, err := b.db.GetUserFromKey(accessKey)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
// unknown error
b.sugar.Error("GetUserFromKey error ", err)
return userStatus, err
} else if err == nil && !isForced && keyData.UpdatedAt.After(time.Now().Add(-b.config.Cache.User)) {
// cached
b.sugar.Debug("Get vip status from cache: ", keyData)
userStatus.uid = keyData.UID
userStatus.isLogin = true
if b.config.BlockType != BlockTypeDisabled {
b.sugar.Debugf("isAuth %d %s", keyData.UID, accessKey)
bwlist, err := b.checkBWlist(ctx, keyData.UID)
if err != nil {
return userStatus, err
}
if bwlist.Code == 0 {
userStatus.isBlacklist = bwlist.Data.Status == 1
userStatus.isWhitelist = bwlist.Data.Status == 2
userStatus.banUntil = time.Unix(bwlist.Data.BanUntil, 0)
}
}
if keyData.VipDueDate.After(time.Now()) {
userStatus.isVip = true
}
return userStatus, nil
}
body, err := b.getMyInfo(ctx, accessKey, clientType)
if err != nil {
return userStatus, err
}
data := &entity.AccInfo{}
err = easyjson.Unmarshal(body, data)
if err != nil {
return userStatus, err
}
if data.Code != 0 {
return userStatus, errors.New(data.Message)
}
b.sugar.Debugf("mid: %d, name: %s, due_date: %s", data.Data.Mid, data.Data.Name, time.Unix(data.Data.VIP.DueDate/1000, 0).String())
userStatus.uid = data.Data.Mid
userStatus.isLogin = true
vipDue := time.Unix(data.Data.VIP.DueDate/1000, 0)
err = b.db.InsertOrUpdateUser(data.Data.Mid, data.Data.Name, vipDue)
if err != nil {
return userStatus, err
}
err = b.db.InsertOrUpdateKey(accessKey, data.Data.Mid, clientType.String())
if err != nil {
return userStatus, err
}
if vipDue.After(time.Now()) {
userStatus.isVip = true
}
if b.config.BlockType != BlockTypeDisabled {
bwlist, err := b.checkBWlist(ctx, data.Data.Mid)
if err != nil {
return userStatus, err
}
if bwlist.Code == 0 {
userStatus.isBlacklist = bwlist.Data.Status == 1
userStatus.isWhitelist = bwlist.Data.Status == 2
userStatus.banUntil = time.Unix(bwlist.Data.BanUntil, 0)
}
}
return userStatus, nil
}
func (b *BiliroamingGo) getMyInfo(ctx *fasthttp.RequestCtx, accessKey string, clientType ClientType) ([]byte, error) {
apiURL := "https://app.bilibili.com/x/v2/account/myinfo"
v := url.Values{}
v.Set("access_key", accessKey)
if clientType == ClientTypeBstarA || clientType == ClientTypeUnknown {
clientType = ClientTypeIphone
}
params, err := SignParams(v, clientType)
if err != nil {
return nil, err
}
apiURL += "?" + params
b.sugar.Debug(apiURL)
reqParams := &HttpRequestParams{
Method: []byte(fasthttp.MethodGet),
Url: []byte(apiURL),
UserAgent: ctx.UserAgent(),
}
body, err := b.doRequestJson(b.defaultClient, reqParams)
if err != nil {
return nil, err
}
b.sugar.Debug("Content: ", string(body))
return body, nil
}
func (b *BiliroamingGo) doAuth(ctx *fasthttp.RequestCtx, accessKey string, clientType ClientType, area string, isForced bool) (bool, *userStatus) {
if len(accessKey) == 0 {
writeErrorJSON(ctx, ERROR_CODE_AUTH_NOT_LOGIN, MSG_ERROR_AUTH_NOT_LOGIN)
return false, nil
}
if len(accessKey) != 32 {
writeErrorJSON(ctx, ERROR_CODE_AUTH_ACCESS_KEY, MSG_ERROR_AUTH_ACCESS_KEY)
return false, nil
}
key, ok := b.getKey(accessKey)
if ok {
if !b.doCheckUidLimiter(ctx, key.uid) {
writeErrorJSON(ctx, ERROR_CODE_TOO_MANY_REQUESTS, MSG_ERROR_TOO_MANY_REQUESTS)
return false, nil
}
switch b.config.BlockType {
case BlockTypeEnabled:
if key.isBlacklist {
writeErrorJSON(ctx, ERROR_CODE_AUTH_BLACKLIST, fmt.Sprintf(MSG_ERROR_AUTH_BLACKLIST, key.uid, key.banUntil.In(LOCATION_SHANGHAI).Format(TIME_FORMAT)))
return false, nil
}
case BlockTypeWhitelist:
if !key.isWhitelist {
writeErrorJSON(ctx, ERROR_CODE_AUTH_WHITELIST, MSG_ERROR_AUTH_WHITELIST)
return false, nil
}
}
if !key.isLogin {
writeErrorJSON(ctx, ERROR_CODE_AUTH_NOT_LOGIN, MSG_ERROR_AUTH_NOT_LOGIN)
return false, nil
}
return key.isLogin, &userStatus{
isLogin: key.isLogin,
isVip: key.isVip,
isBlacklist: key.isBlacklist,
isWhitelist: key.isWhitelist,
uid: key.uid,
banUntil: key.banUntil,
}
}
status, err := b.isAuth(ctx, accessKey, clientType, isForced)
if err != nil {
b.setKey(accessKey, status)
if status.isLogin {
return true, status
}
writeErrorJSON(ctx, ERROR_CODE_AUTH_NOT_LOGIN, MSG_ERROR_AUTH_NOT_LOGIN)
return false, nil
}
b.setKey(accessKey, status)
switch b.config.BlockType {
case BlockTypeEnabled:
if status.isBlacklist {
writeErrorJSON(ctx, ERROR_CODE_AUTH_BLACKLIST, fmt.Sprintf(MSG_ERROR_AUTH_BLACKLIST, status.uid, status.banUntil.In(LOCATION_SHANGHAI).Format(TIME_FORMAT)))
return false, nil
}
case BlockTypeWhitelist:
if !status.isWhitelist {
writeErrorJSON(ctx, ERROR_CODE_AUTH_WHITELIST, MSG_ERROR_AUTH_WHITELIST)
return false, nil
}
}
if !b.doCheckUidLimiter(ctx, status.uid) {
writeErrorJSON(ctx, ERROR_CODE_TOO_MANY_REQUESTS, MSG_ERROR_TOO_MANY_REQUESTS)
return false, nil
}
return true, status
}