dsh-napcat-keeper 2.8.2 → 2.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/README.md +19 -27
- package/lib/client.js +55 -101
- package/lib/index.js +472 -688
- package/package.json +3 -3
- package/scripts/selftest-qrpanel.mjs +260 -0
- package/scripts/smoke-client.mjs +57 -55
- package/scripts/selftest-setup.mjs +0 -210
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-napcat-keeper",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.9.1",
|
|
4
4
|
"description": "发现没有能够随着dsh打开自动启动napcat的插件,遂使用大肥鱼制作此插件,建议搭配Yuuyuko-uu的dsh-qq-bridge使用~",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -27,9 +27,9 @@
|
|
|
27
27
|
"selftest:save": "node scripts/selftest-save.mjs",
|
|
28
28
|
"selftest:probe": "node scripts/selftest-probe.mjs",
|
|
29
29
|
"selftest:companion": "node scripts/selftest-companion.mjs",
|
|
30
|
-
"selftest:setup": "node scripts/selftest-setup.mjs",
|
|
31
30
|
"selftest:layout": "node scripts/selftest-layout.mjs",
|
|
32
|
-
"
|
|
31
|
+
"selftest:qrpanel": "node scripts/selftest-qrpanel.mjs",
|
|
32
|
+
"test": "node scripts/check-manifest.mjs && node scripts/check-privacy.mjs && node scripts/smoke-client.mjs && node scripts/selftest-scan.mjs && node scripts/selftest-save.mjs && node scripts/selftest-probe.mjs && node scripts/selftest-companion.mjs && node scripts/selftest-layout.mjs && node scripts/selftest-qrpanel.mjs"
|
|
33
33
|
},
|
|
34
34
|
"keywords": [
|
|
35
35
|
"deepseek-harness",
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「扫码页」回归自检。
|
|
3
|
+
*
|
|
4
|
+
* 守的是这几个坑:
|
|
5
|
+
* 1. DSH 的 webServer 对外部浏览器一律 403(Browser access is disabled),
|
|
6
|
+
* 所以扫码页必须另有自己的小端口 —— 只挂 DSH 路由等于点不开。
|
|
7
|
+
* 2. 页面里的 qr.png / qr-state / qr-restart 用相对路径,
|
|
8
|
+
* 在 / 下和在 /dsh-napcat-keeper/qr 下都得指对地方。
|
|
9
|
+
* 3. 关掉开关 / 换端口之后,旧 server 必须关掉(不然端口越占越多)。
|
|
10
|
+
*
|
|
11
|
+
* 全程在本机环回、临时 DSH_HOME 上跑,不碰真配置、不碰真 NapCat。
|
|
12
|
+
* node scripts/selftest-qrpanel.mjs
|
|
13
|
+
*/
|
|
14
|
+
import http from 'node:http'
|
|
15
|
+
import net from 'node:net'
|
|
16
|
+
import os from 'node:os'
|
|
17
|
+
import path from 'node:path'
|
|
18
|
+
import fs from 'node:fs'
|
|
19
|
+
|
|
20
|
+
let pass = 0
|
|
21
|
+
let fail = 0
|
|
22
|
+
const ok = (cond, name, extra = '') => {
|
|
23
|
+
if (cond) {
|
|
24
|
+
pass += 1
|
|
25
|
+
console.log(` ✓ ${name}`)
|
|
26
|
+
} else {
|
|
27
|
+
fail += 1
|
|
28
|
+
console.log(` ✗ ${name}${extra ? ' — ' + extra : ''}`)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** 临时家目录:所有写操作都落在这里,不碰真实 DSH_HOME */
|
|
33
|
+
const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), 'napcat-keeper-qrpanel-'))
|
|
34
|
+
process.env.DSH_HOME = tmpHome
|
|
35
|
+
|
|
36
|
+
const mod = await import('../lib/index.js?t=' + Date.now())
|
|
37
|
+
const I = mod.__internals
|
|
38
|
+
|
|
39
|
+
const freePort = () =>
|
|
40
|
+
new Promise((resolve) => {
|
|
41
|
+
const srv = net.createServer()
|
|
42
|
+
srv.listen(0, '127.0.0.1', () => {
|
|
43
|
+
const port = srv.address().port
|
|
44
|
+
srv.close(() => resolve(port))
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
const get = (url, { raw = false } = {}) =>
|
|
49
|
+
new Promise((resolve, reject) => {
|
|
50
|
+
const req = http.get(url, { timeout: 8000 }, (res) => {
|
|
51
|
+
const chunks = []
|
|
52
|
+
res.on('data', (c) => chunks.push(c))
|
|
53
|
+
res.on('end', () => {
|
|
54
|
+
const body = Buffer.concat(chunks)
|
|
55
|
+
if (raw) return resolve({ status: res.statusCode, headers: res.headers, body })
|
|
56
|
+
let json = null
|
|
57
|
+
try {
|
|
58
|
+
json = JSON.parse(body.toString('utf8'))
|
|
59
|
+
} catch {}
|
|
60
|
+
resolve({ status: res.statusCode, headers: res.headers, text: body.toString('utf8'), json })
|
|
61
|
+
})
|
|
62
|
+
})
|
|
63
|
+
req.on('error', reject)
|
|
64
|
+
req.on('timeout', () => {
|
|
65
|
+
req.destroy(new Error('timeout'))
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
const waitListening = async (port, ms = 4000) => {
|
|
70
|
+
const deadline = Date.now() + ms
|
|
71
|
+
while (Date.now() < deadline) {
|
|
72
|
+
const okNow = await new Promise((resolve) => {
|
|
73
|
+
const s = net.connect(port, '127.0.0.1')
|
|
74
|
+
s.on('connect', () => {
|
|
75
|
+
s.destroy()
|
|
76
|
+
resolve(true)
|
|
77
|
+
})
|
|
78
|
+
s.on('error', () => resolve(false))
|
|
79
|
+
})
|
|
80
|
+
if (okNow) return true
|
|
81
|
+
await new Promise((r) => setTimeout(r, 60))
|
|
82
|
+
}
|
|
83
|
+
return false
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
console.log('扫码页自检')
|
|
87
|
+
|
|
88
|
+
/* 1. 配置项默认值 + 归一化 ------------------------------------------------ */
|
|
89
|
+
{
|
|
90
|
+
const d = I.resolveConfig({})
|
|
91
|
+
ok(d.qrPanelEnabled === true, '默认开着扫码页')
|
|
92
|
+
ok(d.qrPanelPort === 8899, '默认端口 8899', String(d.qrPanelPort))
|
|
93
|
+
|
|
94
|
+
const c = I.normalizeConfig({ qrPanelEnabled: false, qrPanelPort: 70000 })
|
|
95
|
+
ok(c.qrPanelEnabled === false, 'qrPanelEnabled=false 能关掉')
|
|
96
|
+
ok(c.qrPanelPort === 65535, '越界端口被夹到合法上界', String(c.qrPanelPort))
|
|
97
|
+
ok(I.normalizeConfig({ qrPanelPort: 80 }).qrPanelPort === 1024, '小于 1024 的端口被夹到 1024')
|
|
98
|
+
|
|
99
|
+
const c2 = I.normalizeConfig({ qrPanelEnabled: 'false', qrPanelPort: 9123 })
|
|
100
|
+
ok(c2.qrPanelEnabled === false, '字符串 "false" 也认作关')
|
|
101
|
+
ok(c2.qrPanelPort === 9123, '合法端口原样保留', String(c2.qrPanelPort))
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/* 2. 起服务 → 页面 / 状态 / 图片 / 未知路径 ------------------------------- */
|
|
105
|
+
const port = await freePort()
|
|
106
|
+
const cfg = { ...I.resolveConfig({}), qrPanelEnabled: true, qrPanelPort: port, enabled: false }
|
|
107
|
+
I.state.cfg = cfg
|
|
108
|
+
|
|
109
|
+
{
|
|
110
|
+
const started = I.startQrPanel(cfg)
|
|
111
|
+
ok(started.ok === true, 'startQrPanel 返回 ok')
|
|
112
|
+
ok(await waitListening(port), `127.0.0.1:${port} 真的在听`)
|
|
113
|
+
|
|
114
|
+
const pub = I.qrPanelPublic(cfg)
|
|
115
|
+
ok(pub.running === true, 'qrPanelPublic.running = true')
|
|
116
|
+
ok(pub.url === `http://127.0.0.1:${port}/`, '给出的地址带端口', pub.url)
|
|
117
|
+
ok(I.qrPanelUrl(cfg) === pub.url, 'qrPanelUrl 与 qrPanelPublic 一致')
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
{
|
|
121
|
+
const r = await get(`http://127.0.0.1:${port}/`)
|
|
122
|
+
ok(r.status === 200, 'GET / → 200', String(r.status))
|
|
123
|
+
ok(/text\/html/.test(String(r.headers['content-type'] || '')), '/ 是 HTML')
|
|
124
|
+
ok(r.text.includes('扫码'), '页面里有「扫码」字样')
|
|
125
|
+
ok(r.text.includes('qr.png'), '页面用相对路径引 qr.png')
|
|
126
|
+
ok(!/src="\/qr\.png/.test(r.text), '页面没有写死绝对路径(子路径下也能用)')
|
|
127
|
+
ok(r.text.includes('smsbox'), '页面留了短信验证提示的位置')
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
{
|
|
131
|
+
const r = await get(`http://127.0.0.1:${port}/qr-state`)
|
|
132
|
+
ok(r.status === 200, 'GET /qr-state → 200', String(r.status))
|
|
133
|
+
ok(r.json && r.json.ok === true, '/qr-state 返回 ok:true')
|
|
134
|
+
ok(r.json && typeof r.json.qrTtlSec === 'number', '/qr-state 带 qrTtlSec')
|
|
135
|
+
ok(r.json && r.json.panel && r.json.panel.port === port, '/qr-state 里带 panel.port', JSON.stringify(r.json && r.json.panel))
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
{
|
|
139
|
+
const r = await get(`http://127.0.0.1:${port}/qr.png`, { raw: true })
|
|
140
|
+
// 临时家里没有二维码,正常就该是 404;真出过码时是 200 + image/png
|
|
141
|
+
const fine = r.status === 404 || (r.status === 200 && /image\/png/.test(String(r.headers['content-type'] || '')))
|
|
142
|
+
ok(fine, '/qr.png 只会是 404 或 image/png', String(r.status))
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
{
|
|
146
|
+
const r = await get(`http://127.0.0.1:${port}/nope`)
|
|
147
|
+
ok(r.status === 404, '不认识的路径 → 404', String(r.status))
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
{
|
|
151
|
+
// DSH 路由那条是 /dsh-napcat-keeper/qr,靠 serveQrPage 剥前缀,这里用小服务复现
|
|
152
|
+
const r = await get(`http://127.0.0.1:${port}/dsh-napcat-keeper/qr`)
|
|
153
|
+
ok(r.status === 200 && r.text.includes('扫码'), '带 ROUTE 前缀的路径也能出页面', String(r.status))
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/* 3. 端口被占时给的是人话 ------------------------------------------------ */
|
|
157
|
+
{
|
|
158
|
+
const busy = net.createServer()
|
|
159
|
+
await new Promise((r) => busy.listen(0, '127.0.0.1', r))
|
|
160
|
+
const busyPort = busy.address().port
|
|
161
|
+
const cfg2 = { ...cfg, qrPanelPort: busyPort }
|
|
162
|
+
I.startQrPanel(cfg2)
|
|
163
|
+
await new Promise((r) => setTimeout(r, 250))
|
|
164
|
+
const pub = I.qrPanelPublic(cfg2)
|
|
165
|
+
ok(pub.running === false, '端口被占时 running=false')
|
|
166
|
+
ok(/被占/.test(pub.note) || /被占/.test(pub.error), '提示里说了端口被占', pub.note || pub.error)
|
|
167
|
+
await new Promise((r) => busy.close(r))
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/* 4. 关掉开关 → 端口立刻释放 -------------------------------------------- */
|
|
171
|
+
{
|
|
172
|
+
const cfg3 = { ...cfg, qrPanelEnabled: false }
|
|
173
|
+
I.startQrPanel(cfg3)
|
|
174
|
+
const still = await new Promise((resolve) => {
|
|
175
|
+
const s = net.connect(port, '127.0.0.1')
|
|
176
|
+
s.on('connect', () => {
|
|
177
|
+
s.destroy()
|
|
178
|
+
resolve(true)
|
|
179
|
+
})
|
|
180
|
+
s.on('error', () => resolve(false))
|
|
181
|
+
})
|
|
182
|
+
ok(still === false, '关掉后端口不再接受连接')
|
|
183
|
+
ok(I.qrPanelPublic(cfg3).enabled === false, 'qrPanelPublic.enabled = false')
|
|
184
|
+
ok(I.qrPanelUrl(cfg3) === '', '关掉后不给地址')
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/* 5. 换端口:旧的就该关掉 ---------------------------------------------- */
|
|
188
|
+
{
|
|
189
|
+
const p2 = await freePort()
|
|
190
|
+
I.startQrPanel({ ...cfg, qrPanelPort: port })
|
|
191
|
+
ok(await waitListening(port), '重新打开原端口')
|
|
192
|
+
I.startQrPanel({ ...cfg, qrPanelPort: p2 })
|
|
193
|
+
ok(await waitListening(p2), `换到 ${p2} 后新端口在听`)
|
|
194
|
+
const oldStill = await new Promise((resolve) => {
|
|
195
|
+
const s = net.connect(port, '127.0.0.1')
|
|
196
|
+
s.on('connect', () => {
|
|
197
|
+
s.destroy()
|
|
198
|
+
resolve(true)
|
|
199
|
+
})
|
|
200
|
+
s.on('error', () => resolve(false))
|
|
201
|
+
})
|
|
202
|
+
ok(oldStill === false, '换端口后旧端口已释放')
|
|
203
|
+
I.stopQrPanel(cfg, '自检收尾')
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/* 6. 状态里带 qrPanel(设置页要用) ------------------------------------ */
|
|
207
|
+
{
|
|
208
|
+
const st = await I.qrPageState({ ...cfg, qrPanelPort: port })
|
|
209
|
+
ok(st && st.panel && typeof st.panel.enabled === 'boolean', 'qrPageState 带 panel.enabled')
|
|
210
|
+
ok(st && st.sms && typeof st.sms === 'object', 'qrPageState 带 sms')
|
|
211
|
+
ok(st && st.sms && st.sms.url === '', '临时家里没有运行日志时 sms.url 是空串')
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/* 7. 短信验证:从假日志里抠 ErrCode / proofWaterUrl --------------------- */
|
|
215
|
+
{
|
|
216
|
+
const shell = path.join(tmpHome, 'fake-napcat')
|
|
217
|
+
fs.mkdirSync(shell, { recursive: true })
|
|
218
|
+
fs.writeFileSync(
|
|
219
|
+
path.join(shell, '_run.log'),
|
|
220
|
+
'01-01 00:00:01 [INFO] start\n01-01 00:00:02 [ERROR] ErrCode: 46\n' +
|
|
221
|
+
'proofWaterUrl: https://tn.qq.com/proof?x=1\n01-01 00:00:03 Login Success\n',
|
|
222
|
+
'latin1',
|
|
223
|
+
)
|
|
224
|
+
const cfg4 = { ...cfg, shellDir: shell.replace(/\\/g, '/'), qrPanelPort: port }
|
|
225
|
+
const st = await I.qrPageState(cfg4)
|
|
226
|
+
ok(st.sms.code === 46, '能抠到 ErrCode', String(st.sms.code))
|
|
227
|
+
ok(st.sms.url === 'https://tn.qq.com/proof?x=1', '能抠到 proofWaterUrl', st.sms.url)
|
|
228
|
+
ok(st.sms.success === true, '能认出「登录成功」')
|
|
229
|
+
ok(st.sms.needSms === false, '登录成功之后的老 ErrCode 不算「这次要验证」')
|
|
230
|
+
ok(st.sms.file.includes('_run.log'), '记录读的是哪个文件', st.sms.file)
|
|
231
|
+
|
|
232
|
+
/* 登录成功之后又冒出 ErrCode = 这次真的要短信验证(时间戳用「现在」,否则会被判成过期) */
|
|
233
|
+
const now2 = new Date()
|
|
234
|
+
const p2 = (n) => String(n).padStart(2, '0')
|
|
235
|
+
const stamp = `${p2(now2.getMonth() + 1)}-${p2(now2.getDate())} ${p2(now2.getHours())}:${p2(now2.getMinutes())}:${p2(now2.getSeconds())}`
|
|
236
|
+
fs.writeFileSync(
|
|
237
|
+
path.join(shell, '_run.log'),
|
|
238
|
+
`${stamp} Login Success\n${stamp} [error] ErrCode: 46\n${stamp} [info] 需要验证码, proofWaterUrl: https://tn.qq.com/proof?x=2\n`,
|
|
239
|
+
'latin1',
|
|
240
|
+
)
|
|
241
|
+
const st2 = await I.qrPageState(cfg4)
|
|
242
|
+
ok(st2.sms.needSms === true, '登录后又掉线 → 判定要短信验证')
|
|
243
|
+
ok(st2.sms.url === 'https://tn.qq.com/proof?x=2', '拿到新的验证链接', st2.sms.url)
|
|
244
|
+
|
|
245
|
+
/* 很老的验证链接就别拿出来让人点了 */
|
|
246
|
+
fs.writeFileSync(
|
|
247
|
+
path.join(shell, '_run.log'),
|
|
248
|
+
`01-01 00:10:00 [error] ErrCode: 46\n01-01 00:10:00 [info] 需要验证码, proofWaterUrl: https://tn.qq.com/proof?old=1\n`,
|
|
249
|
+
'latin1',
|
|
250
|
+
)
|
|
251
|
+
const st3 = await I.qrPageState(cfg4)
|
|
252
|
+
ok(st3.sms.url === '', '过期(>30 分钟)的验证链接被丢掉')
|
|
253
|
+
ok(st3.sms.urlStale === true, '并且标了 urlStale')
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
fs.rmSync(tmpHome, { recursive: true, force: true })
|
|
257
|
+
|
|
258
|
+
console.log('')
|
|
259
|
+
console.log(`扫码页:${pass} 过 / ${fail} 挂`)
|
|
260
|
+
if (fail) process.exitCode = 1
|
package/scripts/smoke-client.mjs
CHANGED
|
@@ -43,12 +43,19 @@ function collect(tree) {
|
|
|
43
43
|
const boxes = [];
|
|
44
44
|
const selects = [];
|
|
45
45
|
const texts = [];
|
|
46
|
+
const links = [];
|
|
46
47
|
(function walk(n) {
|
|
47
48
|
if (n === null || n === undefined || n === false) return;
|
|
48
49
|
if (typeof n === 'string' || typeof n === 'number') { texts.push(String(n)); return; }
|
|
49
50
|
if (Array.isArray(n)) { n.forEach(walk); return; }
|
|
50
51
|
if (n.__el) {
|
|
51
52
|
if (n.type === 'button') buttons.push(n.children.filter((c) => typeof c === 'string').join(''));
|
|
53
|
+
if (n.type === 'a') {
|
|
54
|
+
links.push({
|
|
55
|
+
text: n.children.filter((c) => typeof c === 'string').join(''),
|
|
56
|
+
href: String((n.props && n.props.href) || ''),
|
|
57
|
+
});
|
|
58
|
+
}
|
|
52
59
|
if (n.type === 'input' && n.props && n.props.type === 'checkbox') {
|
|
53
60
|
boxes.push({ name: String(n.props.name || ''), checked: Boolean(n.props.checked), disabled: Boolean(n.props.disabled) });
|
|
54
61
|
}
|
|
@@ -61,7 +68,7 @@ function collect(tree) {
|
|
|
61
68
|
n.children.forEach(walk);
|
|
62
69
|
}
|
|
63
70
|
})(tree);
|
|
64
|
-
return { buttons, boxes, selects, text: texts.join(' ') };
|
|
71
|
+
return { buttons, boxes, selects, links, text: texts.join(' ') };
|
|
65
72
|
}
|
|
66
73
|
|
|
67
74
|
render(); // 第一次:态=null 的 loading 分支
|
|
@@ -84,33 +91,6 @@ states[0] = {
|
|
|
84
91
|
},
|
|
85
92
|
scan: { running: false, found: [], drives: ['C:'], visited: 0, elapsedMs: 0, limitHit: false, cancelled: false },
|
|
86
93
|
drives: ['C:', 'D:', 'E:'],
|
|
87
|
-
setup: {
|
|
88
|
-
dir: 'C:/Users/x/.dsh/dsh-napcat-keeper/setup',
|
|
89
|
-
cmdPath: 'C:/Users/x/.dsh/dsh-napcat-keeper/setup/apply-admin.cmd',
|
|
90
|
-
ps1Path: 'C:/Users/x/.dsh/dsh-napcat-keeper/setup/apply.ps1',
|
|
91
|
-
undoCmdPath: 'C:/Users/x/.dsh/dsh-napcat-keeper/setup/undo-admin.cmd',
|
|
92
|
-
exists: true,
|
|
93
|
-
needs: [
|
|
94
|
-
{ id: 'launcher', label: '自动启动脚本', hint: '补一个 napcat.quick.auto.bat', ready: true, why: '', needAdmin: false },
|
|
95
|
-
{ id: 'envbat', label: '中间层(密码)', hint: '写中间层 bat', ready: true, why: '', needAdmin: false },
|
|
96
|
-
{ id: 'task', label: '计划任务', hint: '建计划任务', ready: true, why: '', needAdmin: false },
|
|
97
|
-
{ id: 'firewall', label: '防火墙放行', hint: '放行 OneBot 端口', ready: false, why: '端口没填', needAdmin: true },
|
|
98
|
-
],
|
|
99
|
-
plan: {
|
|
100
|
-
createdAt: '2026-10-27T00:12:00.000Z',
|
|
101
|
-
needAdmin: true,
|
|
102
|
-
steps: [{ id: 'a', label: '建计划任务' }, { id: 'b', label: '放行防火墙' }],
|
|
103
|
-
},
|
|
104
|
-
lastRun: {
|
|
105
|
-
mode: 'apply',
|
|
106
|
-
ok: false,
|
|
107
|
-
elevated: true,
|
|
108
|
-
results: [
|
|
109
|
-
{ id: 'a', label: '建计划任务', ok: true, skipped: false, message: '好了' },
|
|
110
|
-
{ id: 'b', label: '放行防火墙', ok: false, skipped: false, message: '没权限' },
|
|
111
|
-
],
|
|
112
|
-
},
|
|
113
|
-
},
|
|
114
94
|
log: ['line1'],
|
|
115
95
|
};
|
|
116
96
|
|
|
@@ -140,6 +120,38 @@ const rev = collect(render());
|
|
|
140
120
|
check('反向 WS 时状态条说「反向 WS 已连接」', rev.text.includes('反向 WS 已连接'));
|
|
141
121
|
check('反向 WS 时显示客户端数', /客户端 2/.test(rev.text));
|
|
142
122
|
check('反向 WS 时显示探测方式与 HTTP 状态', rev.text.includes('反向 WebSocket') && rev.text.includes('HTTP 426'));
|
|
123
|
+
// 扫码页:要扫码时顶上得冒黄横幅,链接指向插件自己的小端口(DSH 路由对外 403,只能当兜底)
|
|
124
|
+
{
|
|
125
|
+
states[0].runtime.online = true;
|
|
126
|
+
states[0].runtime.phase = 'online';
|
|
127
|
+
states[0].qrPanel = { enabled: true, port: 8899, running: true, url: 'http://127.0.0.1:8899/', path: '/dsh-napcat-keeper/qr', note: '', error: '' };
|
|
128
|
+
const calm = collect(render());
|
|
129
|
+
check('没要扫码时不画扫码横幅', !calm.text.includes('要手机扫码登录'));
|
|
130
|
+
check('平时也有「打开扫码页」链接', calm.links.some((l) => l.text.includes('打开扫码页')));
|
|
131
|
+
|
|
132
|
+
states[0].runtime.online = false;
|
|
133
|
+
states[0].runtime.phase = 'need-scan';
|
|
134
|
+
states[0].runtime.qr = { file: 'q.png', ageSec: 20, copy: '' };
|
|
135
|
+
const scan = collect(render());
|
|
136
|
+
check('要扫码时顶上出黄横幅', scan.text.includes('要手机扫码登录'));
|
|
137
|
+
check('横幅里有「点开扫码页」', scan.links.some((l) => l.text.includes('点开扫码页')));
|
|
138
|
+
check('扫码页链接走小端口而不是 DSH 路由',
|
|
139
|
+
scan.links.filter((l) => l.text.includes('扫码页')).every((l) => l.href === 'http://127.0.0.1:8899/'),
|
|
140
|
+
JSON.stringify(scan.links));
|
|
141
|
+
|
|
142
|
+
states[0].qrPanel = { enabled: true, port: 8899, running: false, url: '', path: '/dsh-napcat-keeper/qr', note: '端口 8899 被占了(改一个 qrPanelPort,或关掉占它的程序)', error: '' };
|
|
143
|
+
const fallback = collect(render());
|
|
144
|
+
check('小端口没起来时退回 DSH 路由',
|
|
145
|
+
fallback.links.filter((l) => l.text.includes('扫码页')).every((l) => l.href === '/dsh-napcat-keeper/qr'),
|
|
146
|
+
JSON.stringify(fallback.links));
|
|
147
|
+
check('小端口没起来时把 note 写出来', fallback.text.includes('被占了'));
|
|
148
|
+
|
|
149
|
+
states[0].runtime.online = true;
|
|
150
|
+
states[0].runtime.phase = 'online';
|
|
151
|
+
delete states[0].runtime.qr;
|
|
152
|
+
states[0].qrPanel = { enabled: true, port: 8899, running: true, url: 'http://127.0.0.1:8899/', path: '/dsh-napcat-keeper/qr', note: '', error: '' };
|
|
153
|
+
}
|
|
154
|
+
|
|
143
155
|
states[0].runtime.mode = 'auth';
|
|
144
156
|
states[0].runtime.online = false;
|
|
145
157
|
states[0].runtime.portStatus = 401;
|
|
@@ -259,34 +271,24 @@ const legacyOut = collect(render());
|
|
|
259
271
|
check('老后端不给判定时不误标壳模板', !legacyOut.text.includes('多半是 OneKey 装完留下的壳模板'));
|
|
260
272
|
check('老后端时提示要重启 DSH', legacyOut.text.includes('重启 DSH'));
|
|
261
273
|
|
|
262
|
-
//
|
|
274
|
+
// 2.9.1:提权交接(生成脚本那份)整块删了 —— 面板里不该再有它的按钮/勾选框,
|
|
275
|
+
// 服务端旧状态里还带着 setup 字段时也不能炸。
|
|
263
276
|
states[0].scan = { running: false, found: [], drives: ['C:'], visited: 0, elapsedMs: 0, limitHit: false, cancelled: false };
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
check('
|
|
272
|
-
check('
|
|
273
|
-
|
|
274
|
-
check('
|
|
275
|
-
|
|
276
|
-
check('
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
const keepSetup = states[0].setup;
|
|
280
|
-
states[0].setup = { ...keepSetup, plan: null, lastRun: null };
|
|
281
|
-
const emptyOut = collect(render());
|
|
282
|
-
check('没生成过时提示', emptyOut.text.includes('还没生成过脚本'));
|
|
283
|
-
states[0].setup = keepSetup;
|
|
284
|
-
|
|
285
|
-
// 没有 setup 字段也不炸
|
|
286
|
-
states[0].setup = undefined;
|
|
287
|
-
const noSetup = collect(render());
|
|
288
|
-
check('缺 setup 字段时降级提示', noSetup.text.includes('读不到脚本状态'));
|
|
289
|
-
states[0].setup = keepSetup;
|
|
277
|
+
states[0].setup = {
|
|
278
|
+
dir: 'C:/Users/x/.dsh/dsh-napcat-keeper/setup',
|
|
279
|
+
needs: [{ id: 'launcher', label: '自动启动脚本', ready: true }],
|
|
280
|
+
plan: { steps: [{ id: 'a', label: '建计划任务' }] },
|
|
281
|
+
lastRun: { mode: 'apply', ok: true, elevated: false, results: [] },
|
|
282
|
+
};
|
|
283
|
+
const setupGone = collect(render());
|
|
284
|
+
check('不再有「生成脚本并打开文件夹」', !setupGone.buttons.includes('生成脚本并打开文件夹'));
|
|
285
|
+
check('不再有「打开文件夹」「读结果」',
|
|
286
|
+
!setupGone.buttons.includes('打开文件夹') && !setupGone.buttons.includes('读结果'));
|
|
287
|
+
check('不再有 need 勾选框', setupGone.boxes.filter((b) => b.name.startsWith('npk-need-')).length === 0,
|
|
288
|
+
String(setupGone.boxes.length));
|
|
289
|
+
check('不再出现「需要你点一下的脚本」这一组', !setupGone.text.includes('需要你点一下的脚本'));
|
|
290
|
+
check('旧状态里带 setup 字段也不炸', setupGone.text.includes('napcat桥') || setupGone.text.length > 200);
|
|
291
|
+
delete states[0].setup;
|
|
290
292
|
|
|
291
293
|
// 「进程」那行:按安装形态换词(boot 恒 0 不代表没进程)
|
|
292
294
|
const keepRuntime = states[0].runtime;
|
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 「提权交接」自检:在临时目录里造一个假的 NapCat 环境,
|
|
3
|
-
* 走一遍 生成脚本 → 真跑 PowerShell → 读结果 → 撤销 的全流程。
|
|
4
|
-
*
|
|
5
|
-
* 全程不碰真实配置、不碰真实 NapCat 目录,也不需要管理员(这几步本来就不用)。
|
|
6
|
-
* node scripts/selftest-setup.mjs
|
|
7
|
-
*/
|
|
8
|
-
import { execFileSync } from 'node:child_process'
|
|
9
|
-
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
|
10
|
-
import { tmpdir } from 'node:os'
|
|
11
|
-
import path from 'node:path'
|
|
12
|
-
|
|
13
|
-
const internals = (await import('../lib/index.js')).__internals
|
|
14
|
-
|
|
15
|
-
const root = mkdtempSync(path.join(tmpdir(), 'npk-selftest-'))
|
|
16
|
-
const fakeShell = path.join(root, 'NapCat.Selftest.Shell')
|
|
17
|
-
const fakeLogDir = path.join(root, 'logs')
|
|
18
|
-
mkdirSync(fakeShell, { recursive: true })
|
|
19
|
-
mkdirSync(fakeLogDir, { recursive: true })
|
|
20
|
-
// 假 NapCat:一个 bat + 一个 boot exe,够了
|
|
21
|
-
writeFileSync(path.join(fakeShell, 'napcat.quick2.bat'), '@echo off\r\necho hi\r\n', 'utf8')
|
|
22
|
-
writeFileSync(path.join(fakeShell, 'NapCatWinBootMain.exe'), '', 'utf8')
|
|
23
|
-
|
|
24
|
-
const cfg = internals.resolveConfig({})
|
|
25
|
-
cfg.shellDir = fakeShell
|
|
26
|
-
cfg.launcherScript = 'napcat.quick2.bat'
|
|
27
|
-
cfg.launcherLog = path.join(fakeLogDir, 'launch.log')
|
|
28
|
-
cfg.taskName = 'NapCat-Keeper-Selftest'
|
|
29
|
-
cfg.launchAccount = '10001'
|
|
30
|
-
cfg.launchArgs = ''
|
|
31
|
-
cfg.rememberPassword = true
|
|
32
|
-
cfg.quickPassword = 'selftest-pass-123'
|
|
33
|
-
cfg.hideConsole = true
|
|
34
|
-
cfg.onebotPort = 3000
|
|
35
|
-
|
|
36
|
-
let pass = 0
|
|
37
|
-
let fail = 0
|
|
38
|
-
const ok = (cond, name, extra = '') => {
|
|
39
|
-
if (cond) {
|
|
40
|
-
pass += 1
|
|
41
|
-
console.log(` ✓ ${name}`)
|
|
42
|
-
} else {
|
|
43
|
-
fail += 1
|
|
44
|
-
console.log(` ✗ ${name}${extra ? ' — ' + extra : ''}`)
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
console.log(`临时目录:${root}\n`)
|
|
49
|
-
|
|
50
|
-
/* 1. 需求清单 ------------------------------------------------------------- */
|
|
51
|
-
console.log('1) needCatalog')
|
|
52
|
-
const catalog = internals.needCatalog(cfg)
|
|
53
|
-
ok(Array.isArray(catalog) && catalog.length === 4, '四项需要都在', JSON.stringify(catalog.map((c) => c.id)))
|
|
54
|
-
ok(catalog.every((c) => c.id && c.label), '每项都有 id/label')
|
|
55
|
-
ok(catalog.every((c) => c.ready), '假环境下四项都能做', JSON.stringify(catalog.filter((c) => !c.ready)))
|
|
56
|
-
ok(catalog.find((c) => c.id === 'firewall').needAdmin === true, 'firewall 标了要管理员')
|
|
57
|
-
ok(catalog.find((c) => c.id === 'task').needAdmin === false, 'task 不需要管理员')
|
|
58
|
-
|
|
59
|
-
/* 2. 生成脚本 ------------------------------------------------------------- */
|
|
60
|
-
console.log('\n2) writeSetupBundle')
|
|
61
|
-
const made = internals.writeSetupBundle(cfg, ['launcher', 'envbat', 'task'])
|
|
62
|
-
ok(made.ok === true, '生成成功', made.error || '')
|
|
63
|
-
if (!made.ok) {
|
|
64
|
-
console.log('生成失败,后面不用看了')
|
|
65
|
-
process.exit(1)
|
|
66
|
-
}
|
|
67
|
-
const p = internals.setupPaths()
|
|
68
|
-
ok(existsSync(p.ps1), 'apply.ps1 在')
|
|
69
|
-
ok(existsSync(p.cmd), 'apply-admin.cmd 在')
|
|
70
|
-
ok(existsSync(p.undoCmd), 'undo-admin.cmd 在')
|
|
71
|
-
ok(existsSync(p.plan), 'plan.json 在')
|
|
72
|
-
ok(existsSync(p.readme), 'README.txt 在')
|
|
73
|
-
ok(made.needAdmin === false, '这批步骤不需要管理员', String(made.needAdmin))
|
|
74
|
-
ok(!JSON.stringify(made.steps).includes('selftest-pass-123'), '返回给前端的摘要里没有密码明文')
|
|
75
|
-
|
|
76
|
-
const ps1 = readFileSync(p.ps1)
|
|
77
|
-
ok(ps1[0] === 0xef && ps1[1] === 0xbb && ps1[2] === 0xbf, 'apply.ps1 带 UTF-8 BOM')
|
|
78
|
-
const ps1Text = ps1.toString('utf8')
|
|
79
|
-
ok(ps1Text.includes('Start-Process -FilePath'), '脚本里有自我提权那一句')
|
|
80
|
-
ok(ps1Text.includes('ConvertTo-Json'), '脚本会写回 result.json')
|
|
81
|
-
|
|
82
|
-
const cmdText = readFileSync(p.cmd, 'utf8')
|
|
83
|
-
ok(/^[\x00-\x7f]*$/.test(cmdText), 'cmd 是纯 ASCII(不会乱码)')
|
|
84
|
-
ok(cmdText.includes('apply.ps1'), 'cmd 指向 apply.ps1')
|
|
85
|
-
|
|
86
|
-
const plan = JSON.parse(readFileSync(p.plan, 'utf8'))
|
|
87
|
-
ok(plan.steps.length === 4, '4 步:wrapper + envbat + launcher + task', String(plan.steps.length))
|
|
88
|
-
ok(plan.needAdmin === false, 'plan 里 needAdmin 是 false')
|
|
89
|
-
ok(
|
|
90
|
-
plan.steps.some((s) => s.id === 'task' && Array.isArray(s.requires) && s.requires.includes('envbat')),
|
|
91
|
-
'task 依赖 envbat(失败就不建任务)',
|
|
92
|
-
)
|
|
93
|
-
|
|
94
|
-
/* 3. 真跑一遍(不提权) --------------------------------------------------- */
|
|
95
|
-
console.log('\n3) 真跑 apply.ps1')
|
|
96
|
-
const runPs = () =>
|
|
97
|
-
execFileSync('powershell.exe', ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', p.ps1], {
|
|
98
|
-
encoding: 'utf8',
|
|
99
|
-
env: { ...process.env, NPK_NO_PAUSE: '1' },
|
|
100
|
-
timeout: 120000,
|
|
101
|
-
windowsHide: true,
|
|
102
|
-
})
|
|
103
|
-
|
|
104
|
-
let ranOut = ''
|
|
105
|
-
try {
|
|
106
|
-
ranOut = runPs()
|
|
107
|
-
ok(true, '脚本跑完了(退出码 0)')
|
|
108
|
-
} catch (error) {
|
|
109
|
-
ok(false, '脚本报错', String((error && error.stdout) || (error && error.message) || error))
|
|
110
|
-
console.log(String(ranOut))
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const first = internals.readSetupResult()
|
|
114
|
-
const amAdmin = (() => {
|
|
115
|
-
try {
|
|
116
|
-
return (
|
|
117
|
-
execFileSync(
|
|
118
|
-
'powershell.exe',
|
|
119
|
-
[
|
|
120
|
-
'-NoProfile',
|
|
121
|
-
'-Command',
|
|
122
|
-
'([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)',
|
|
123
|
-
],
|
|
124
|
-
{ encoding: 'utf8', windowsHide: true },
|
|
125
|
-
).trim() === 'True'
|
|
126
|
-
)
|
|
127
|
-
} catch {
|
|
128
|
-
return false
|
|
129
|
-
}
|
|
130
|
-
})()
|
|
131
|
-
ok(Boolean(first), 'result.json 读得回来')
|
|
132
|
-
if (first) {
|
|
133
|
-
ok(first.mode === 'apply', 'mode = apply')
|
|
134
|
-
ok(first.ok === true, '每步都成了', JSON.stringify(first.results.filter((r) => !r.ok)))
|
|
135
|
-
ok(
|
|
136
|
-
first.elevated === amAdmin,
|
|
137
|
-
`elevated(${first.elevated}) 与当前进程是否管理员(${amAdmin}) 一致`,
|
|
138
|
-
)
|
|
139
|
-
ok(first.results.length === 4, '4 条结果')
|
|
140
|
-
}
|
|
141
|
-
ok(existsSync(path.join(fakeShell, 'napcat.quick.auto.bat')), 'launcher: 启动脚本写出来了')
|
|
142
|
-
ok(existsSync(path.join(fakeLogDir, 'env.bat')), 'envbat: 中间层写出来了')
|
|
143
|
-
ok(existsSync(path.join(fakeLogDir, 'launch.vbs')), 'task-wrapper: vbs 写出来了')
|
|
144
|
-
|
|
145
|
-
const envBat = readFileSync(path.join(fakeLogDir, 'env.bat'), 'utf8')
|
|
146
|
-
ok(envBat.includes('NAPCAT_QUICK_PASSWORD=selftest-pass-123'), 'env bat 里带密码(这是它的用途)')
|
|
147
|
-
ok(envBat.includes('call "'), 'env bat 会 call 真启动脚本')
|
|
148
|
-
|
|
149
|
-
const autoBat = readFileSync(path.join(fakeShell, 'napcat.quick.auto.bat'), 'utf8')
|
|
150
|
-
ok(autoBat.includes('NapCatWinBootMain.exe'), '自动启动脚本调的是 boot main')
|
|
151
|
-
ok(autoBat.includes('10001'), '自动启动脚本带账号')
|
|
152
|
-
|
|
153
|
-
try {
|
|
154
|
-
const q = execFileSync('schtasks.exe', ['/query', '/tn', cfg.taskName], {
|
|
155
|
-
encoding: 'utf8',
|
|
156
|
-
windowsHide: true,
|
|
157
|
-
})
|
|
158
|
-
ok(q.includes(cfg.taskName), '计划任务真的建上了')
|
|
159
|
-
} catch (error) {
|
|
160
|
-
ok(false, '计划任务没查着', String((error && error.message) || error))
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/* 4. 再跑一次(幂等) ---------------------------------------------------- */
|
|
164
|
-
console.log('\n4) 重复跑一次')
|
|
165
|
-
try {
|
|
166
|
-
runPs()
|
|
167
|
-
const again = internals.readSetupResult()
|
|
168
|
-
ok(again && again.ok === true, '第二次也全成(不炸)')
|
|
169
|
-
} catch (error) {
|
|
170
|
-
ok(false, '第二次跑报错', String((error && error.message) || error))
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/* 5. 撤销 --------------------------------------------------------------- */
|
|
174
|
-
console.log('\n5) undo')
|
|
175
|
-
try {
|
|
176
|
-
execFileSync(
|
|
177
|
-
'powershell.exe',
|
|
178
|
-
['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', p.ps1, '-Undo'],
|
|
179
|
-
{ encoding: 'utf8', env: { ...process.env, NPK_NO_PAUSE: '1' }, timeout: 120000, windowsHide: true },
|
|
180
|
-
)
|
|
181
|
-
ok(true, 'undo 跑完')
|
|
182
|
-
} catch (error) {
|
|
183
|
-
ok(false, 'undo 报错', String((error && error.message) || error))
|
|
184
|
-
}
|
|
185
|
-
const undone = internals.readSetupResult()
|
|
186
|
-
ok(undone && undone.mode === 'undo', 'result 里 mode = undo')
|
|
187
|
-
ok(undone && undone.ok === true, '撤回每步都成', JSON.stringify(undone && undone.results.filter((r) => !r.ok)))
|
|
188
|
-
ok(!existsSync(path.join(fakeShell, 'napcat.quick.auto.bat')), '启动脚本删掉了')
|
|
189
|
-
ok(!existsSync(path.join(fakeLogDir, 'env.bat')), '中间层删掉了(密码不留在磁盘)')
|
|
190
|
-
ok(!existsSync(path.join(fakeLogDir, 'launch.vbs')), 'vbs 删掉了')
|
|
191
|
-
try {
|
|
192
|
-
execFileSync('schtasks.exe', ['/query', '/tn', cfg.taskName], { stdio: 'ignore', windowsHide: true })
|
|
193
|
-
ok(false, '计划任务还在(应该删掉)')
|
|
194
|
-
} catch {
|
|
195
|
-
ok(true, '计划任务删掉了')
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/* 收尾 ---------------------------------------------------------------- */
|
|
199
|
-
console.log('\n6) 再生成一次(留着给人点,也算看下真身)')
|
|
200
|
-
const final = internals.writeSetupBundle(cfg, ['launcher', 'envbat', 'task'])
|
|
201
|
-
ok(final.ok === true, '最后一次生成成功')
|
|
202
|
-
ok(internals.readSetupResult() === null, '旧 result 被清掉(不会误读上一次)')
|
|
203
|
-
|
|
204
|
-
try {
|
|
205
|
-
rmSync(root, { recursive: true, force: true })
|
|
206
|
-
} catch {}
|
|
207
|
-
console.log(`\n临时目录已清:${root}`)
|
|
208
|
-
console.log(`真身留在:${p.dir}`)
|
|
209
|
-
console.log(`\n结果:${pass} 通过 / ${fail} 失败`)
|
|
210
|
-
process.exit(fail === 0 ? 0 : 1)
|