forked from JasonKhew96/biliroaming-go-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subtitle.go
87 lines (73 loc) · 1.95 KB
/
subtitle.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
package main
import (
"errors"
"fmt"
"net/url"
"strconv"
"time"
"github.com/valyala/fasthttp"
"golang.org/x/net/idna"
)
func (b *BiliroamingGo) handleBstarAndroidSubtitle(ctx *fasthttp.RequestCtx) {
if !b.checkRoamingVer(ctx) {
return
}
queryArgs := ctx.URI().QueryArgs()
args := b.processArgs(queryArgs)
args.area = "th"
client := b.getClientByArea(args.area)
if b.getAuthByArea(args.area) {
// if ok, _ := b.doAuth(ctx, accessKey, area, false); !ok {
// return
// }
subtitleCache, err := b.db.GetTHSubtitleCache(args.epId)
if err == nil && len(subtitleCache.Data) > 0 && subtitleCache.UpdatedAt.After(time.Now().Add(-b.config.Cache.THSubtitle)) {
b.sugar.Debug("Replay from cache: ", subtitleCache.Data.String())
setDefaultHeaders(ctx)
ctx.Write(subtitleCache.Data)
return
}
}
v := url.Values{}
v.Set("access_key", args.accessKey)
v.Set("s_locale", "zh_SG")
v.Set("ep_id", strconv.FormatInt(args.epId, 10))
v.Set("mobi_app", "bstar_a")
params, err := SignParams(v, ClientTypeBstarA)
if err != nil {
b.processError(ctx, err)
return
}
reverseProxy := b.getReverseSearchProxyByArea(args.area)
if reverseProxy == "" {
reverseProxy = "app.biliintl.com"
}
domain, err := idna.New().ToASCII(reverseProxy)
if err != nil {
b.processError(ctx, err)
return
}
url := fmt.Sprintf("https://%s/intl/gateway/v2/app/subtitle?%s", domain, params)
b.sugar.Debug("New url: ", url)
reqParams := &HttpRequestParams{
Method: []byte(fasthttp.MethodGet),
Url: []byte(url),
UserAgent: ctx.UserAgent(),
}
data, err := b.doRequestJson(client, reqParams)
if err != nil {
if errors.Is(err, ErrorHttpStatusLimited) {
data = []byte(`{"code":-412,"message":"请求被拦截"}`)
} else {
b.processError(ctx, err)
return
}
}
setDefaultHeaders(ctx)
ctx.Write(data)
if b.getAuthByArea(args.area) {
if err := b.db.InsertOrUpdateTHSubtitleCache(args.epId, data); err != nil {
b.sugar.Error(err)
}
}
}