dsh-napcat-keeper 2.7.3 → 2.8.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 +31 -0
- package/README.md +79 -6
- package/lib/client.js +106 -9
- package/lib/index.js +917 -21
- package/package.json +3 -1
- package/scripts/selftest-companion.mjs +488 -0
- package/scripts/selftest-probe.mjs +191 -0
- package/scripts/smoke-client.mjs +95 -1
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「在线探测」回归自检。
|
|
3
|
+
*
|
|
4
|
+
* 守的是这次适配反向 WebSocket 的那个坑:老版本 postOneBot 只看 body 不看状态码,
|
|
5
|
+
* 于是 dsh-onebot-qq 之类反向 WS 服务占着端口时(HTTP 换来 401/426),
|
|
6
|
+
* 一律被当成「端口不通」→ 卡死判定 → 杀掉重拉 → 无限循环。
|
|
7
|
+
*
|
|
8
|
+
* 现在 probe() 要能把这几种情况分清楚:
|
|
9
|
+
* 200 OneBot JSON → online / http
|
|
10
|
+
* 426 → reverse(端口只收 WebSocket)
|
|
11
|
+
* 401 → auth(令牌不对)或 reverse(有客户端连进来时)
|
|
12
|
+
* 端口没人听 → dead
|
|
13
|
+
* probeMode=process→ 直接不碰端口
|
|
14
|
+
*
|
|
15
|
+
* 全程在本地环回上起假服务器,端口用临时的,不碰真配置、不碰真 NapCat。
|
|
16
|
+
* node scripts/selftest-probe.mjs
|
|
17
|
+
*/
|
|
18
|
+
import http from 'node:http'
|
|
19
|
+
import net from 'node:net'
|
|
20
|
+
import { spawn } from 'node:child_process'
|
|
21
|
+
|
|
22
|
+
let pass = 0
|
|
23
|
+
let fail = 0
|
|
24
|
+
const ok = (cond, name, extra = '') => {
|
|
25
|
+
if (cond) {
|
|
26
|
+
pass += 1
|
|
27
|
+
console.log(` ✓ ${name}`)
|
|
28
|
+
} else {
|
|
29
|
+
fail += 1
|
|
30
|
+
console.log(` ✗ ${name}${extra ? ' — ' + extra : ''}`)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const loadInternals = async () => (await import('../lib/index.js?t=' + Date.now())).__internals
|
|
35
|
+
|
|
36
|
+
/** 起一个假服务器,返回 { port, close } */
|
|
37
|
+
const serve = (handler) =>
|
|
38
|
+
new Promise((resolve) => {
|
|
39
|
+
const srv = http.createServer(handler)
|
|
40
|
+
srv.listen(0, '127.0.0.1', () => {
|
|
41
|
+
resolve({
|
|
42
|
+
port: srv.address().port,
|
|
43
|
+
close: () => new Promise((r) => srv.close(r)),
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
/** 找一个没人用的端口(开着再关掉),用来模拟「端口没开」 */
|
|
49
|
+
const deadPort = () =>
|
|
50
|
+
new Promise((resolve) => {
|
|
51
|
+
const srv = net.createServer()
|
|
52
|
+
srv.listen(0, '127.0.0.1', () => {
|
|
53
|
+
const port = srv.address().port
|
|
54
|
+
srv.close(() => resolve(port))
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
/** 起一个孩子进程,朝 port 建一条常连接(模拟 NapCat 反向连进来) */
|
|
59
|
+
const connectAndHold = (port) => {
|
|
60
|
+
const code =
|
|
61
|
+
`const net=require('net');` +
|
|
62
|
+
`const s=net.connect(${port},'127.0.0.1',()=>{process.stdout.write('up\\n')});` +
|
|
63
|
+
's.on("error",()=>{});' +
|
|
64
|
+
'setInterval(()=>{},1000)'
|
|
65
|
+
const child = spawn(process.execPath, ['-e', code], { stdio: 'ignore', windowsHide: true })
|
|
66
|
+
return {
|
|
67
|
+
child,
|
|
68
|
+
up: () => new Promise((r) => setTimeout(r, 900)),
|
|
69
|
+
stop: () => {
|
|
70
|
+
try {
|
|
71
|
+
child.kill()
|
|
72
|
+
} catch {}
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const { probe: probeRaw, portClients, PROBE_MODES } = await loadInternals()
|
|
78
|
+
/**
|
|
79
|
+
* 这个自检只看「端口本身」的行为,把「跟随 dsh-onebot-qq」钉死成关:
|
|
80
|
+
* 否则会去读本机真的 ~/.dsh,装了那个插件的人跑测试结果就不一样了。
|
|
81
|
+
*/
|
|
82
|
+
const probe = (cfg) => probeRaw(cfg, { companion: null })
|
|
83
|
+
|
|
84
|
+
/* 1. 普通 NapCat HTTP 接口 ------------------------------------------------- */
|
|
85
|
+
console.log('1) 端口上是正常的 OneBot HTTP 接口')
|
|
86
|
+
let port1
|
|
87
|
+
{
|
|
88
|
+
const srv = await serve((req, res) => {
|
|
89
|
+
res.writeHead(200, { 'content-type': 'application/json' })
|
|
90
|
+
res.end(JSON.stringify({ status: 'ok', retcode: 0, data: { user_id: 10086, nickname: '测试号' } }))
|
|
91
|
+
})
|
|
92
|
+
port1 = srv.port
|
|
93
|
+
const health = await probe({ onebotPort: port1, accessToken: '', probeMode: 'auto' })
|
|
94
|
+
ok(health.online === true, '判为在线')
|
|
95
|
+
ok(health.mode === 'http', 'mode = http', health.mode)
|
|
96
|
+
ok(health.status === 200, '抓到 HTTP 200', String(health.status))
|
|
97
|
+
await srv.close()
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/* 2. 只收 WebSocket 的端口:426,且没人连 -------------------------------- */
|
|
101
|
+
console.log('\n2) 端口只收 WebSocket(426)、还没有客户端连进来')
|
|
102
|
+
let port2
|
|
103
|
+
{
|
|
104
|
+
const srv = await serve((req, res) => {
|
|
105
|
+
res.writeHead(426, { 'content-type': 'text/plain' })
|
|
106
|
+
res.end('Upgrade Required')
|
|
107
|
+
})
|
|
108
|
+
port2 = srv.port
|
|
109
|
+
const health = await probe({ onebotPort: port2, accessToken: '', probeMode: 'auto' })
|
|
110
|
+
ok(health.online === false, '没客户端时不判在线')
|
|
111
|
+
ok(health.mode === 'reverse', 'mode = reverse', health.mode)
|
|
112
|
+
ok(health.portAlive === true, '认得出「端口是活的」', JSON.stringify(health.portAlive))
|
|
113
|
+
ok(health.status === 426, '抓到 HTTP 426', String(health.status))
|
|
114
|
+
const links = await portClients({ onebotPort: port2 }, 0)
|
|
115
|
+
ok(links.count === 0 || links.count === null, '客户端数 = 0 或查不到(不是 1)', String(links.count))
|
|
116
|
+
await srv.close()
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/* 3. 426 + 真有客户端连进来 → 反向 WS 在线 -------------------------------- */
|
|
120
|
+
console.log('\n3) 反向 WS:端口上有客户端连进来了')
|
|
121
|
+
{
|
|
122
|
+
const srv = await serve((req, res) => {
|
|
123
|
+
res.writeHead(426, { 'content-type': 'text/plain' })
|
|
124
|
+
res.end('Upgrade Required')
|
|
125
|
+
})
|
|
126
|
+
const client = connectAndHold(srv.port)
|
|
127
|
+
await client.up()
|
|
128
|
+
const health = await probe({ onebotPort: srv.port, accessToken: '', probeMode: 'auto' })
|
|
129
|
+
ok(health.mode === 'reverse', 'mode = reverse', health.mode)
|
|
130
|
+
ok(health.online === true, '有客户端就判在线')
|
|
131
|
+
ok(health.clients !== null && health.clients >= 1, '数到了客户端', String(health.clients))
|
|
132
|
+
client.stop()
|
|
133
|
+
await srv.close()
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/* 4. 401:可能是反向 WS 服务,也可能是令牌不对 --------------------------- */
|
|
137
|
+
console.log('\n4) 端口回 401')
|
|
138
|
+
{
|
|
139
|
+
const srv = await serve((req, res) => {
|
|
140
|
+
res.writeHead(401, { 'content-type': 'text/plain' })
|
|
141
|
+
res.end('Unauthorized')
|
|
142
|
+
})
|
|
143
|
+
const health = await probe({ onebotPort: srv.port, accessToken: '', probeMode: 'auto' })
|
|
144
|
+
ok(health.mode === 'auth', '没客户端时判 auth(多半是令牌不对)', health.mode)
|
|
145
|
+
ok(health.online === false, '不判在线')
|
|
146
|
+
ok(health.portAlive === true, '但仍认得出端口是活的(所以不会去杀进程)')
|
|
147
|
+
ok(String(health.hint || '').includes('401'), '给得出 401 的提示', health.hint)
|
|
148
|
+
await srv.close()
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/* 5. 端口没开 ------------------------------------------------------------ */
|
|
152
|
+
console.log('\n5) 端口没人听')
|
|
153
|
+
{
|
|
154
|
+
const port = await deadPort()
|
|
155
|
+
const health = await probe({ onebotPort: port, accessToken: '', probeMode: 'auto' })
|
|
156
|
+
ok(health.online === false, '不判在线')
|
|
157
|
+
ok(health.mode === 'dead', 'mode = dead', health.mode)
|
|
158
|
+
ok(health.portAlive === false, 'portAlive = false')
|
|
159
|
+
ok(health.status === 0, '没有 HTTP 状态码', String(health.status))
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/* 6. probeMode 的几种强制值 --------------------------------------------- */
|
|
163
|
+
console.log('\n6) probeMode 强制值')
|
|
164
|
+
{
|
|
165
|
+
const srv = await serve((req, res) => {
|
|
166
|
+
res.writeHead(426)
|
|
167
|
+
res.end('Upgrade Required')
|
|
168
|
+
})
|
|
169
|
+
const asProcess = await probe({ onebotPort: srv.port, accessToken: '', probeMode: 'process' })
|
|
170
|
+
ok(asProcess.mode === 'process', 'process:连端口都不看', asProcess.mode)
|
|
171
|
+
const asHttp = await probe({ onebotPort: srv.port, accessToken: '', probeMode: 'http' })
|
|
172
|
+
ok(asHttp.mode === 'auth' || asHttp.mode === 'http', 'http:不认反向(判成 auth/http)', asHttp.mode)
|
|
173
|
+
const asReverse = await probe({ onebotPort: srv.port, accessToken: '', probeMode: 'reverse' })
|
|
174
|
+
ok(asReverse.mode === 'reverse', 'reverse:直接按反向 WS 处理', asReverse.mode)
|
|
175
|
+
const bad = await probe({ onebotPort: srv.port, accessToken: '', probeMode: '乱填' })
|
|
176
|
+
ok(bad.mode === 'reverse', '乱填的 probeMode 退回 auto(这里应判成 reverse)', bad.mode)
|
|
177
|
+
await srv.close()
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/* 7. 归一化 -------------------------------------------------------------- */
|
|
181
|
+
console.log('\n7) 配置归一化')
|
|
182
|
+
{
|
|
183
|
+
const { normalizeConfig } = await loadInternals()
|
|
184
|
+
ok(normalizeConfig({ probeMode: 'REVERSE' }).probeMode === 'reverse', '大小写归一')
|
|
185
|
+
ok(normalizeConfig({ probeMode: '' }).probeMode === 'auto', '空值 → auto')
|
|
186
|
+
ok(normalizeConfig({ probeMode: 'xyz' }).probeMode === 'auto', '非法值 → auto')
|
|
187
|
+
ok(PROBE_MODES.has('auto') && PROBE_MODES.has('reverse'), 'PROBE_MODES 导出可用')
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
console.log(`\n结果:${pass} 通过,${fail} 失败`)
|
|
191
|
+
process.exit(fail ? 1 : 0)
|
package/scripts/smoke-client.mjs
CHANGED
|
@@ -41,6 +41,7 @@ function render() { idx = 0; return Section({}); }
|
|
|
41
41
|
function collect(tree) {
|
|
42
42
|
const buttons = [];
|
|
43
43
|
const boxes = [];
|
|
44
|
+
const selects = [];
|
|
44
45
|
const texts = [];
|
|
45
46
|
(function walk(n) {
|
|
46
47
|
if (n === null || n === undefined || n === false) return;
|
|
@@ -51,10 +52,16 @@ function collect(tree) {
|
|
|
51
52
|
if (n.type === 'input' && n.props && n.props.type === 'checkbox') {
|
|
52
53
|
boxes.push({ name: String(n.props.name || ''), checked: Boolean(n.props.checked), disabled: Boolean(n.props.disabled) });
|
|
53
54
|
}
|
|
55
|
+
if (n.type === 'select') {
|
|
56
|
+
selects.push({
|
|
57
|
+
value: String(n.props && n.props.value),
|
|
58
|
+
options: n.children.filter((c) => c && c.__el && c.type === 'option').map((o) => String(o.props.value)),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
54
61
|
n.children.forEach(walk);
|
|
55
62
|
}
|
|
56
63
|
})(tree);
|
|
57
|
-
return { buttons, boxes, text: texts.join(' ') };
|
|
64
|
+
return { buttons, boxes, selects, text: texts.join(' ') };
|
|
58
65
|
}
|
|
59
66
|
|
|
60
67
|
render(); // 第一次:态=null 的 loading 分支
|
|
@@ -121,6 +128,93 @@ check('三块盘三个按钮', ['扫 C:', '扫 D:', '扫 E:'].every((b) => base.
|
|
|
121
128
|
check('盘数提示 3 块', /读到 3 块盘:C:、D:、E:/.test(base.text));
|
|
122
129
|
check('shellDir 指向分盘扫描', base.text.includes('不想手填就用上面「分盘扫描」'));
|
|
123
130
|
|
|
131
|
+
// 反向 WebSocket:下拉得在、默认 auto、四项齐;runtime 报反向态时状态条得换词
|
|
132
|
+
const modeSel = base.selects[0];
|
|
133
|
+
check('有「在线探测方式」下拉,默认 auto', Boolean(modeSel) && modeSel.value === 'auto', JSON.stringify(modeSel));
|
|
134
|
+
check('下拉四项齐全', Boolean(modeSel) && ['auto', 'http', 'reverse', 'process'].every((v) => modeSel.options.includes(v)),
|
|
135
|
+
JSON.stringify(modeSel && modeSel.options));
|
|
136
|
+
states[0].runtime.mode = 'reverse';
|
|
137
|
+
states[0].runtime.portStatus = 426;
|
|
138
|
+
states[0].runtime.clients = 2;
|
|
139
|
+
const rev = collect(render());
|
|
140
|
+
check('反向 WS 时状态条说「反向 WS 已连接」', rev.text.includes('反向 WS 已连接'));
|
|
141
|
+
check('反向 WS 时显示客户端数', /客户端 2/.test(rev.text));
|
|
142
|
+
check('反向 WS 时显示探测方式与 HTTP 状态', rev.text.includes('反向 WebSocket') && rev.text.includes('HTTP 426'));
|
|
143
|
+
states[0].runtime.mode = 'auth';
|
|
144
|
+
states[0].runtime.online = false;
|
|
145
|
+
states[0].runtime.portStatus = 401;
|
|
146
|
+
delete states[0].runtime.clients;
|
|
147
|
+
const auth = collect(render());
|
|
148
|
+
check('401 时显示「端口要求鉴权」', auth.text.includes('端口要求鉴权'));
|
|
149
|
+
|
|
150
|
+
// 和 dsh-onebot-qq 共存:没装时说没检测到;装了要说清版本 / 挂没挂 / 端口 / 令牌
|
|
151
|
+
states[0].runtime.mode = 'reverse';
|
|
152
|
+
states[0].runtime.online = true;
|
|
153
|
+
states[0].runtime.portStatus = 426;
|
|
154
|
+
const noComp = collect(render());
|
|
155
|
+
check('没装共存插件时也有「共存插件」那行', noComp.text.includes('共存插件'));
|
|
156
|
+
check('没装时写「没检测到 dsh-onebot-qq」', noComp.text.includes('没检测到 dsh-onebot-qq'));
|
|
157
|
+
check('没装时设置页不画「跟随 dsh-onebot-qq」这一行', !noComp.text.includes('只在 dsh-onebot-qq 装了'));
|
|
158
|
+
states[0].runtime.companion = { version: '0.9.6', profile: 'desktop', mounted: true, mode: 'reverse', host: '127.0.0.1', port: 8643, url: 'ws://127.0.0.1:3001', tokenKnown: true };
|
|
159
|
+
states[0].runtime.companionReady = true;
|
|
160
|
+
states[0].runtime.companionTokenMatch = false;
|
|
161
|
+
states[0].runtime.portUsed = 8643;
|
|
162
|
+
states[0].runtime.followed = true;
|
|
163
|
+
const comp = collect(render());
|
|
164
|
+
check('装了会说版本与「已启用」', comp.text.includes('dsh-onebot-qq 0.9.6') && comp.text.includes('已启用'));
|
|
165
|
+
check('反向模式写出监听端口', /反向 WS :8643/.test(comp.text));
|
|
166
|
+
check('令牌不一致要标出来', comp.text.includes('令牌不一致'));
|
|
167
|
+
check('跟随了就说跟到了哪个端口', comp.text.includes('端口 8643') && comp.text.includes('跟随 dsh-onebot-qq'));
|
|
168
|
+
check('装了且启用时才画「跟随 dsh-onebot-qq」这一行', comp.text.includes('只在 dsh-onebot-qq 装了'));
|
|
169
|
+
states[0].runtime.companion.mounted = false;
|
|
170
|
+
states[0].runtime.companionReady = false;
|
|
171
|
+
states[0].runtime.companionTokenMatch = null;
|
|
172
|
+
states[0].runtime.followed = false;
|
|
173
|
+
const notMounted = collect(render());
|
|
174
|
+
check('没挂进 profile 时说清楚(免得以为在生效)', notMounted.text.includes('已安装但没挂进 profile'));
|
|
175
|
+
check('没启用时那一行也不画', !notMounted.text.includes('只在 dsh-onebot-qq 装了'));
|
|
176
|
+
check('共存那行顺带说明「跟随」开关不会出现', notMounted.text.includes('「跟随」那个开关不会出现'));
|
|
177
|
+
delete states[0].runtime.companion;
|
|
178
|
+
delete states[0].runtime.companionReady;
|
|
179
|
+
delete states[0].runtime.companionTokenMatch;
|
|
180
|
+
|
|
181
|
+
// 和 dsh-qq-bridge 共存:它是「客户端」,要能看出装没装、挂没挂、连的端口对不对
|
|
182
|
+
const noBridge = collect(render());
|
|
183
|
+
check('没装桥接时也有「桥接插件」那行', noBridge.text.includes('桥接插件'));
|
|
184
|
+
check('没装时写「没检测到 dsh-qq-bridge」', noBridge.text.includes('没检测到 dsh-qq-bridge'));
|
|
185
|
+
states[0].runtime.bridgePlugin = {
|
|
186
|
+
version: '1.0.7',
|
|
187
|
+
profile: 'desktop',
|
|
188
|
+
mounted: true,
|
|
189
|
+
enabled: true,
|
|
190
|
+
dryRun: false,
|
|
191
|
+
napcatUrl: 'http://127.0.0.1:8643',
|
|
192
|
+
napcatPort: 8643,
|
|
193
|
+
tokenKnown: false,
|
|
194
|
+
bindings: 0,
|
|
195
|
+
configKnown: true,
|
|
196
|
+
check: 'bridge-hits-reverse',
|
|
197
|
+
note: 'dsh-qq-bridge 连的是 http://127.0.0.1:8643,而那个端口现在是反向 WebSocket',
|
|
198
|
+
};
|
|
199
|
+
const withBridge = collect(render());
|
|
200
|
+
check('装了会说版本与状态', withBridge.text.includes('dsh-qq-bridge 1.0.7') && withBridge.text.includes('已启用'));
|
|
201
|
+
check('在发 / 试运行 要分得清', withBridge.text.includes('在发'));
|
|
202
|
+
check('连的是反向 WS 端口要标出来', withBridge.text.includes('连的是反向 WS 端口'));
|
|
203
|
+
check('提示文案单独一行给出来', withBridge.text.includes('桥接提示:') && withBridge.text.includes('反向 WebSocket'));
|
|
204
|
+
states[0].runtime.bridgePlugin = {
|
|
205
|
+
...states[0].runtime.bridgePlugin,
|
|
206
|
+
mounted: false,
|
|
207
|
+
enabled: false,
|
|
208
|
+
dryRun: true,
|
|
209
|
+
check: 'not-mounted',
|
|
210
|
+
note: '',
|
|
211
|
+
};
|
|
212
|
+
const bridgeOff = collect(render());
|
|
213
|
+
check('没挂进 profile 时说清楚', bridgeOff.text.includes('已安装但没挂进 profile'));
|
|
214
|
+
check('总开关关着要说出来', bridgeOff.text.includes('总开关关着'));
|
|
215
|
+
|
|
216
|
+
delete states[0].runtime.bridgePlugin;
|
|
217
|
+
|
|
124
218
|
// 盘数跟 drives 走:1 块 / 5 块 / 0 块
|
|
125
219
|
for (const drives of [['C:'], ['C:', 'D:', 'E:', 'F:', 'G:'], []]) {
|
|
126
220
|
states[0].drives = drives;
|