forked from JasonKhew96/biliroaming-go-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpClient.go
378 lines (327 loc) · 10.8 KB
/
httpClient.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package main
import (
"bytes"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/JasonKhew96/biliroaming-go-server/entity"
"github.com/mailru/easyjson"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpproxy"
)
type HttpCookiesParams struct {
Key []byte
Value []byte
}
type HttpRequestParams struct {
Method []byte
Url []byte
UserAgent []byte
Cookie []HttpCookiesParams
}
type ErrorHttpStatus struct {
Code int
Message string
}
func (e *ErrorHttpStatus) Error() string {
return fmt.Sprintf("status code error %d with message %s", e.Code, e.Message)
}
func (e *ErrorHttpStatus) Is(tgt error) bool {
target, ok := tgt.(*ErrorHttpStatus)
if !ok {
return false
}
return e.Code == target.Code
}
func NewErrorHttpLimited(code int) *ErrorHttpStatus {
return &ErrorHttpStatus{
Code: code,
}
}
var (
ErrorHttpStatusLimited = NewErrorHttpLimited(-412)
)
func (b *BiliroamingGo) initProxy(c *Config) {
b.cnClient = b.newClient(c.Proxy.CN)
b.hkClient = b.newClient(c.Proxy.HK)
b.twClient = b.newClient(c.Proxy.TW)
b.thClient = b.newClient(c.Proxy.TH)
b.defaultClient = b.newClient(c.Proxy.Default)
}
func (b *BiliroamingGo) newClient(proxy string) *fasthttp.Client {
var dialFunc fasthttp.DialFunc
switch {
case strings.HasPrefix(proxy, "socks5://"), strings.HasPrefix(proxy, "socks5h://"):
b.sugar.Debug("New socks proxy client: ", proxy)
dialFunc = fasthttpproxy.FasthttpSocksDialer(proxy)
case proxy != "":
b.sugar.Debug("New http proxy client: ", proxy)
dialFunc = fasthttpproxy.FasthttpHTTPDialer(proxy)
case proxy == "":
b.sugar.Debug("New normal client")
dialFunc = nil
}
return &fasthttp.Client{
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
Dial: dialFunc,
DialDualStack: b.config.IPV6,
}
}
func (b *BiliroamingGo) getClientByArea(area string) *fasthttp.Client {
switch strings.ToLower(area) {
case "cn":
return b.cnClient
case "hk":
return b.hkClient
case "tw":
return b.twClient
case "th":
return b.thClient
default:
return b.defaultClient
}
}
func (b *BiliroamingGo) getReverseProxyByArea(area string) string {
switch strings.ToLower(area) {
case "cn":
return b.config.Reverse.CN
case "hk":
return b.config.Reverse.HK
case "tw":
return b.config.Reverse.TW
case "th":
return b.config.Reverse.TH
default:
return ""
}
}
func (b *BiliroamingGo) getReverseSearchProxyByArea(area string) string {
switch strings.ToLower(area) {
case "cn":
return b.config.ReverseSearch.CN
case "hk":
return b.config.ReverseSearch.HK
case "tw":
return b.config.ReverseSearch.TW
case "th":
return b.config.ReverseSearch.TH
default:
return ""
}
}
func (b *BiliroamingGo) getReverseWebSearchProxyByArea(area string) string {
switch strings.ToLower(area) {
case "cn":
return b.config.ReverseWebSearch.CN
case "hk":
return b.config.ReverseWebSearch.HK
case "tw":
return b.config.ReverseWebSearch.TW
default:
return ""
}
}
func setDefaultHeaders(ctx *fasthttp.RequestCtx) {
ctx.Response.Header.SetBytesKV([]byte("Access-Control-Allow-Origin"), []byte("https://www.bilibili.com"))
ctx.Response.Header.SetBytesKV([]byte("Access-Control-Allow-Credentials"), []byte("true"))
ctx.SetContentTypeBytes([]byte("application/json"))
}
func writeErrorJSON(ctx *fasthttp.RequestCtx, code int, msg string) {
setDefaultHeaders(ctx)
resp := &entity.SimpleResponse{
Code: code,
Message: fmt.Sprintf("解析服务器: %s", msg),
}
respData, err := easyjson.Marshal(resp)
if err != nil {
ctx.Write([]byte(`{"code":500,"message":"解析服务器发送错误"}`))
return
}
ctx.Write(respData)
// ctx.Write([]byte(`{"accept_format":"mp4","code":0,"seek_param":"start","is_preview":0,"fnval":1,"video_project":true,"fnver":0,"type":"MP4","bp":0,"result":"suee","seek_type":"offset","qn_extras":[{"attribute":0,"icon":"http://i0.hdslb.com/bfs/app/81dab3a04370aafa93525053c4e760ac834fcc2f.png","icon2":"http://i0.hdslb.com/bfs/app/4e6f14c2806f7cc508d8b6f5f1d8306f94a71ecc.png","need_login":true,"need_vip":true,"qn":112},{"attribute":0,"icon":"","icon2":"","need_login":false,"need_vip":false,"qn":80},{"attribute":0,"icon":"","icon2":"","need_login":false,"need_vip":false,"qn":64},{"attribute":0,"icon":"","icon2":"","need_login":false,"need_vip":false,"qn":32},{"attribute":0,"icon":"","icon2":"","need_login":false,"need_vip":false,"qn":16}],"accept_watermark":[false,false,false,false,false],"from":"local","video_codecid":7,"durl":[{"order":1,"length":16740,"size":172775,"ahead":"","vhead":"","url":"https://s1.hdslb.com/bfs/static/player/media/error.mp4","backup_url":[]}],"no_rexcode":0,"format":"mp4","support_formats":[{"display_desc":"360P","superscript":"","format":"mp4","description":"流畅 360P","quality":16,"new_description":"360P 流畅"}],"message":"","accept_quality":[16],"quality":16,"timelength":16740,"has_paid":false,"accept_description":["流畅 360P"],"status":2}`))
}
func writeHealthJSON(ctx *fasthttp.RequestCtx, health *entity.Health) {
setDefaultHeaders(ctx)
if health == nil {
ctx.Write([]byte(`{"code":500,"message":"解析服务器发送错误"}`))
return
}
respData, err := easyjson.Marshal(health)
if err != nil {
ctx.Write([]byte(`{"code":500,"message":"解析服务器发送错误"}`))
return
}
ctx.Write(respData)
}
func getClientPlatform(ctx *fasthttp.RequestCtx, appkey string) ClientType {
platform := string(ctx.Request.Header.PeekBytes([]byte("platform-from-biliroaming")))
if platform == "" && appkey == "" {
return ClientTypeIphone
}
clientType := ClientType(platform)
if clientType.IsValid() {
return clientType
}
if appkey != "" {
return getClientTypeFromAppkey(appkey)
}
return ClientTypeUnknown
}
func (b *BiliroamingGo) checkRoamingVer(ctx *fasthttp.RequestCtx) bool {
versionCode := ctx.Request.Header.PeekBytes([]byte("build"))
versionName := ctx.Request.Header.PeekBytes([]byte("x-from-biliroaming"))
if len(versionCode) == 0 && len(versionName) == 0 {
return true
}
if len(versionCode) > 0 && len(versionName) > 0 {
build, err := strconv.Atoi(string(versionCode))
if err != nil {
writeErrorJSON(ctx, ERROR_CODE_HEADER_WRONG, MSG_ERROR_HEADER_WRONG)
return false
}
if build < b.config.RoamingMinVer {
writeErrorJSON(ctx, ERROR_CODE_HEADER_MIN_VERSION, MSG_ERROR_HEADER_MIN_VERSION)
return false
}
return true
}
writeErrorJSON(ctx, ERROR_CODE_HEADER_WRONG, MSG_ERROR_HEADER_WRONG)
return false
}
func (b *BiliroamingGo) doRequest(client *fasthttp.Client, params *HttpRequestParams) ([]byte, error) {
if params == nil {
return nil, errors.New("params is nil")
}
if params.Url == nil {
return nil, errors.New("url is empty")
}
if params.UserAgent == nil {
return nil, errors.New("user agent is empty")
}
if params.Method == nil {
params.Method = []byte(fasthttp.MethodGet)
}
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
req.SetRequestURIBytes(params.Url)
req.Header.SetBytesKV([]byte("Accept-Encoding"), []byte("br, gzip, deflate"))
req.Header.SetUserAgentBytes(params.UserAgent)
req.Header.SetMethodBytes(params.Method)
if params.Cookie != nil {
for _, cookie := range params.Cookie {
req.Header.SetCookieBytesKV(cookie.Key, cookie.Value)
}
}
b.sugar.Debugf("doRequest: %s", req.RequestURI())
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)
err := client.DoRedirects(req, resp, 1)
if err != nil {
return nil, err
}
b.sugar.Debugf("doRedirects: %d", resp.StatusCode())
if resp.StatusCode() != fasthttp.StatusOK {
return nil, NewErrorHttpLimited(resp.StatusCode())
}
contentEncoding := resp.Header.Peek("Content-Encoding")
var bodyBytes []byte
if bytes.EqualFold(contentEncoding, []byte("gzip")) {
bodyBytes, err = resp.BodyGunzip()
} else if bytes.EqualFold(contentEncoding, []byte("br")) {
bodyBytes, err = resp.BodyUnbrotli()
} else if bytes.EqualFold(contentEncoding, []byte("deflate")) {
bodyBytes, err = resp.BodyInflate()
} else {
bodyBytes = resp.Body()
}
if err != nil {
return nil, err
}
if isLimited, err := isResponseLimited(bodyBytes); err != nil {
return nil, err
} else if isLimited {
return nil, ErrorHttpStatusLimited
}
b.sugar.Debug("Content: ", string(bodyBytes))
return bodyBytes, nil
}
func (b *BiliroamingGo) doRequestJson(client *fasthttp.Client, params *HttpRequestParams) ([]byte, error) {
if params == nil {
return nil, errors.New("params is nil")
}
if params.Url == nil {
return nil, errors.New("url is empty")
}
if params.UserAgent == nil {
return nil, errors.New("user agent is empty")
}
if params.Method == nil {
params.Method = []byte(fasthttp.MethodGet)
}
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
req.SetRequestURIBytes(params.Url)
req.Header.SetBytesKV([]byte("Accept-Encoding"), []byte("br, gzip, deflate"))
req.Header.SetUserAgentBytes(params.UserAgent)
req.Header.SetMethodBytes(params.Method)
if params.Cookie != nil {
for _, cookie := range params.Cookie {
req.Header.SetCookieBytesKV(cookie.Key, cookie.Value)
}
}
b.sugar.Debugf("doRequest: %s", req.RequestURI())
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)
err := client.DoRedirects(req, resp, 3)
if err != nil {
return nil, err
}
b.sugar.Debugf("doRedirects: %d", resp.StatusCode())
if resp.StatusCode() != fasthttp.StatusOK {
return nil, NewErrorHttpLimited(resp.StatusCode())
}
// Verify the content type
contentType := resp.Header.Peek("Content-Type")
if bytes.Index(contentType, []byte("application/json")) != 0 {
return nil, fmt.Errorf("expected content-type json but %s", string(contentType))
}
contentEncoding := resp.Header.Peek("Content-Encoding")
var bodyBytes []byte
if bytes.EqualFold(contentEncoding, []byte("gzip")) {
bodyBytes, err = resp.BodyGunzip()
} else if bytes.EqualFold(contentEncoding, []byte("br")) {
bodyBytes, err = resp.BodyUnbrotli()
} else if bytes.EqualFold(contentEncoding, []byte("deflate")) {
bodyBytes, err = resp.BodyInflate()
} else {
bodyBytes = resp.Body()
}
if err != nil {
return nil, err
}
if isLimited, err := isResponseLimited(bodyBytes); err != nil {
return nil, err
} else if isLimited {
return nil, ErrorHttpStatusLimited
}
body := string(bodyBytes)
b.sugar.Debug("Content: ", body)
// Remove mid from json content
// if strings.Contains(url, "/playurl?") {
// body = removeMid(body)
// b.sugar.Debug("New content: ", body)
// }
return []byte(body), nil
}
func processNotFound(ctx *fasthttp.RequestCtx) {
ctx.Error(fasthttp.StatusMessage(fasthttp.StatusNotFound), fasthttp.StatusNotFound)
}
func (b *BiliroamingGo) processError(ctx *fasthttp.RequestCtx, err error) {
if !errors.Is(err, fasthttp.ErrTimeout) && !errors.Is(err, fasthttp.ErrTLSHandshakeTimeout) && !errors.Is(err, fasthttp.ErrConnectionClosed) {
b.sugar.Error(err)
}
writeErrorJSON(ctx, ERROR_CODE_INTERNAL_SERVER, MSG_ERROR_INTERNAL_SERVER)
}