-
Notifications
You must be signed in to change notification settings - Fork 211
/
guard.js
165 lines (140 loc) · 4.24 KB
/
guard.js
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
const got = require('../utils/got')
const got_unsafe = require('got')
const config = require('../utils/config')
const logger = require('../utils/logger')
const share = require('../utils/share').guard
const sleep = require('../utils/sleep')
let csrfToken
let list_cache = []
const getCsrf = () => {
const cookies = got.defaults.options.cookieJar.getCookiesSync('https://api.bilibili.com/')
for (const cookie of cookies) {
const found = `${cookie}`.match(/bili_jct=([0-9a-f]*)/i)
if (found) return found[1]
}
throw new Error('guard: csrf 提取失败')
}
const main = async () => {
// 锁定流程,防止重复执行
share.lock = Date.now() + 24 * 60 * 60 * 1000
csrfToken = getCsrf()
const uid = config.get('uid', '')
if (uid === '') throw new Error('uid获取失败')
// 获取列表
const list = await getGuardList(uid)
const originList = list.filter(item => !list_cache.includes(item.Id))
if (list_cache.length > 10000) list_cache.splice(0, 9000)
for (const currentItem of originList) {
const guardId = currentItem.Id
const originRoomid = currentItem.RoomId
// 记录已经检查过的 GuardId
list_cache.push(guardId)
// 非特定时间跳过领取
const guardHours = config.get('guard.hours', [])
if (!guardHours.includes((new Date).getHours())) {
logger.debug('guard:非特定时间跳过领取')
continue
}
// 概率性跳过领取
const guardPercent = config.get('guard.percent', 100)
if (Math.random() * 100 >= guardPercent) {
logger.debug('guard:概率性跳过领取')
continue
}
// 检测是否是真实存在的room
const isTrueRoom = await checkTrueRoom(originRoomid)
if (isTrueRoom) {
// 如果已经在这个房间就不用再进一遍
if (share.lastGuardRoom !== originRoomid) {
await goToRoom(originRoomid)
await sleep(2000 + Math.random() * 2000)
share.lastGuardRoom = originRoomid
}
const result = await getLottery(originRoomid, guardId)
if (result.code === 0) {
logger.notice(`guard: ${originRoomid} 舰长经验领取成功,${result.msg}`)
continue
}
if (result.code === 400 && result.msg.includes('领取过')) {
logger.notice(`guard: ${originRoomid} 舰长经验已经领取过`)
continue
}
if (result.code === 400 && result.msg.includes('过期')) {
logger.notice(`guard: ${originRoomid} 舰长经验已经过期`)
continue
}
if (result.code) {
throw new Error('guard: 舰长经验领取失败,稍后重试')
}
}
await sleep(5 * 1000 + Math.random() * 60 * 1000)
}
}
async function getGuardList(uid) {
const { body } = await got_unsafe.get('http://118.25.108.153:8080/guard', {
headers: {
'User-Agent': `bilibili-live-tools/${uid}`
},
timeout: 20000,
json: true
})
return body
}
async function checkTrueRoom(roomId) {
const { body } = await got.get(
`https://api.live.bilibili.com/room/v1/Room/room_init?id=${roomId}`,
{ json: true }
)
if (body.code === 0) {
const { is_hidden, is_locked, encrypted } = body.data
return !(is_hidden || is_locked || encrypted)
} else {
logger.warning('guard: 获取房间信息失败')
return false
}
}
async function goToRoom(roomId) {
const { body } = await got.post(
'https://api.live.bilibili.com/room/v1/Room/room_entry_action',
{
body: {
room_id: roomId,
csrf_token: csrfToken,
csrf: csrfToken,
platform: 'pc',
},
form: true,
json: true
}
)
return body
}
async function getLottery(roomId, guardId) {
const { body } = await got.post(
'https://api.live.bilibili.com/xlive/lottery-interface/v3/guard/join',
{
body: {
roomid: roomId,
id: guardId,
type: 'guard',
csrf_token: csrfToken,
csrf: csrfToken,
},
form: true,
json: true
}
)
return body
}
module.exports = () => {
if (process.env.DISABLE_GUARD === 'true') return
if (share.lock > Date.now()) return
return main()
.then(() => {
share.lock = Date.now() + 5 * 60 * 1000
})
.catch(e => {
logger.error(e.message)
share.lock = Date.now() + 60 * 60 * 1000
})
}