dsh-napcat-keeper 2.8.2 → 2.9.2
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 +35 -0
- package/README.md +45 -59
- package/lib/client.js +61 -105
- package/lib/index.js +529 -708
- package/package.json +5 -5
- package/scripts/selftest-companion.mjs +25 -6
- package/scripts/selftest-qrpanel.mjs +260 -0
- package/scripts/smoke-client.mjs +61 -59
- package/scripts/selftest-setup.mjs +0 -210
package/lib/index.js
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* dsh-napcat-keeper —— NapCat 看门狗 v2.
|
|
2
|
+
* dsh-napcat-keeper —— NapCat 看门狗 v2.9.2(可发布通用版)
|
|
3
3
|
*
|
|
4
4
|
* 解决的问题:DSH 是桌面应用,重启 DSH 时 NapCat(QQ 协议端) 不会跟着起来,
|
|
5
|
-
* 于是 dsh-qq-
|
|
5
|
+
* 于是 dsh-qq-link 一直轮询一个死端口,你在 QQ 里说的话就石沉大海。
|
|
6
|
+
*
|
|
7
|
+
* v2.9.2:跟着上游改名 —— 桥接插件 dsh-qq-bridge 已更名为 **dsh-qq-link**
|
|
8
|
+
* - 包名/仓库名换了,插件内部 id(`qq-bridge`)、配置 `~/.dsh/qq-bridge-config.json`、
|
|
9
|
+
* 状态 `~/.dsh/qq-bridge-state.json`、路由前缀 `/dsh-qq-bridge/*` 全都没变(实测 1.5.1);
|
|
10
|
+
* - 所以只改「认哪个包」:BRIDGE_PACKAGES = ['dsh-qq-link', 'dsh-qq-bridge'],
|
|
11
|
+
* 新名优先、老名兜底(还没升级的机器仍认得出),界面与文档统一说 dsh-qq-link;
|
|
12
|
+
* - 桥接行显示的是实际装的那个包名(bridgePublic.packageName),不是硬编码文案。
|
|
6
13
|
*
|
|
7
14
|
* v2.8.2:「进程」那行按安装形态说人话 + 清一处重复模板
|
|
8
15
|
* - node 型(无头 Shell)装法里 QQ 本体跑在 node.exe 里,机器上没有 QQ.exe 也没有快登壳,
|
|
@@ -18,7 +25,7 @@
|
|
|
18
25
|
* 得出「装没装 / 挂没挂 / 反向还是正向 / 监听哪个端口 / 令牌配没配」;
|
|
19
26
|
* 你填的端口不通、它确实挂着反向 WS 时自动跟随(companionFollow,默认开),
|
|
20
27
|
* 你填的端口一通就绝不插手;没挂进 profile 或它是正向模式则不跟,并说明为什么;
|
|
21
|
-
* - 读 dsh-qq-
|
|
28
|
+
* - 读 dsh-qq-link 的 ~/.dsh/qq-bridge-config.json,把它填的 NapCat 地址与本插件
|
|
22
29
|
* 看到的实际情况交叉比对,五种结论(没挂载 / 连了反向 WS 端口 / 端口对不上 /
|
|
23
30
|
* 总开关关着 / 试运行中)写进设置页「桥接插件」行与 napcat_status;只看配置,
|
|
24
31
|
* 不碰它的进程和文件;
|
|
@@ -58,11 +65,12 @@
|
|
|
58
65
|
* 和 DSH 自己的文件混在一起,一眼看不出哪些是插件产生的;
|
|
59
66
|
* - 现在统一落在 ~/.dsh/dsh-napcat-keeper/ 一个目录里,文件名去掉重复前缀:
|
|
60
67
|
* config.json / keeper.log / launch.log / state.json / lock.json / qr.png /
|
|
61
|
-
* env.bat / launch.vbs / autogen.bat / autogen-node.bat
|
|
68
|
+
* env.bat / launch.vbs / autogen.bat / autogen-node.bat;
|
|
69
|
+
* (2.9.1 起不再有 setup/ 那个「提权交接」脚本目录)
|
|
62
70
|
* - 老版本升级时 migrateLegacyFiles() 自动搬一次(能 rename 就 rename),
|
|
63
71
|
* 遗留的 *.bak、旧备份目录挪进 legacy/ 留档;计划任务里写死的旧路径由
|
|
64
72
|
* repairTaskActions() 用 /change 改指新位置,不 /end、不打扰正在跑的 QQ;
|
|
65
|
-
* - qq-bridge-state.json 不搬:那是 dsh-qq-
|
|
73
|
+
* - qq-bridge-state.json 不搬:那是 dsh-qq-link 的文件,只是被读一下。
|
|
66
74
|
*
|
|
67
75
|
* v2.6.0:清死代码 + README 与代码对齐(行为一字没变)
|
|
68
76
|
* - 删掉 v2.2.2 起就没人再调用的目录自动探测三件套(looksLikeShellDir / findShellDirs /
|
|
@@ -104,13 +112,12 @@
|
|
|
104
112
|
* - 改本插件任何 json/js 请用 write/edit 工具或 [IO.File]::WriteAllText + UTF8Encoding($false);
|
|
105
113
|
*
|
|
106
114
|
* v2.3.0:提权交接 —— 需要管理员的活,交给「你点一下」的脚本
|
|
107
|
-
* -
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
* - 能做的事(needs):补启动脚本 / 写「记住密码」中间层 / 建计划任务 / 放行 OneBot 入站端口。
|
|
115
|
+
* - 2.9.1 已整块删除:这套「生成 plan.json + apply-admin.cmd 让人去双击」的机制是多余的。
|
|
116
|
+
* 它本来要做的几件事,插件自己早就能直接做完,且全都不需要管理员:
|
|
117
|
+
* 写 env.bat(writeEnvBat)/ 建计划任务(ensureTask,schtasks 用户级)/
|
|
118
|
+
* 补启动脚本(autogen.bat、autogen-node.bat 写在插件自己目录里)。
|
|
119
|
+
* 唯一真要管理员的是给 OneBot 端口开防火墙入站 —— 那一步也一并去掉,
|
|
120
|
+
* 需要放行时按 README「端口放行」那一节的 netsh 命令自己跑一次即可。
|
|
114
121
|
*
|
|
115
122
|
* v2.2.2:目录只认「人选」
|
|
116
123
|
* - 去掉「自动探测 NapCat Shell 目录」:不再替用户猜位置、也不再在配置里悄悄填,
|
|
@@ -171,7 +178,7 @@ import { homedir, tmpdir } from 'node:os'
|
|
|
171
178
|
import path from 'node:path'
|
|
172
179
|
|
|
173
180
|
export const name = 'napcat-keeper'
|
|
174
|
-
export const version = '2.
|
|
181
|
+
export const version = '2.9.2'
|
|
175
182
|
|
|
176
183
|
/** tools 用来注册 napcat_* 工具;webServer 用来挂设置界面接口。拿不到也不至于把 DSH 拖崩。 */
|
|
177
184
|
export const inject = ['tools', 'webServer']
|
|
@@ -260,6 +267,13 @@ const DEFAULTS = {
|
|
|
260
267
|
statePath: '',
|
|
261
268
|
/** 二维码副本(方便 agent 或你自己直接看图) */
|
|
262
269
|
qrCopyPath: '',
|
|
270
|
+
/**
|
|
271
|
+
* 扫码页:NapCat 要手机扫码时,点开就能看见一张会自己刷新的二维码。
|
|
272
|
+
* 默认开;只监听 127.0.0.1,不对外。
|
|
273
|
+
*/
|
|
274
|
+
qrPanelEnabled: true,
|
|
275
|
+
/** 扫码页端口(127.0.0.1 上) */
|
|
276
|
+
qrPanelPort: 8899,
|
|
263
277
|
/** qq-bridge 的 state 文件 */
|
|
264
278
|
bridgeStatePath: '',
|
|
265
279
|
/** 桥接 state 超过这么多秒没更新就提醒 */
|
|
@@ -332,7 +346,6 @@ const LEGACY_FILES = [
|
|
|
332
346
|
['napcat-autogen-node.bat', 'autogen-node.bat'],
|
|
333
347
|
['napcat-autogen.bat', 'autogen.bat'],
|
|
334
348
|
]
|
|
335
|
-
const LEGACY_DIRS = [['napcat-keeper-setup', 'setup']]
|
|
336
349
|
/** 老版本留在 ~/.dsh 根上的东西(没搬走的、配置备份、bom-backup 之类):统一归档进 legacy/ */
|
|
337
350
|
const LEGACY_LEFTOVER = /^(napcat-keeper[.-]|napcat-qr\.|napcat-autogen)/
|
|
338
351
|
|
|
@@ -434,14 +447,8 @@ function migrateLegacyFiles(dir) {
|
|
|
434
447
|
}
|
|
435
448
|
if (moveOne(path.join(home, oldName), path.join(target, newName))) moved += 1
|
|
436
449
|
}
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
const from = path.join(home, oldName)
|
|
440
|
-
if (existsSync(from) && !existsSync(path.join(target, newName)) && moveOne(from, path.join(target, newName))) {
|
|
441
|
-
moved += 1
|
|
442
|
-
}
|
|
443
|
-
} catch {}
|
|
444
|
-
}
|
|
450
|
+
// 2.9.1 起没有需要改名搬家的目录了(原来的 setup/ 提权交接目录整块删掉),
|
|
451
|
+
// 只留下面的归档兜底。旧版本留下的 ~/.dsh/dsh-napcat-keeper/setup/ 可以手动删。
|
|
445
452
|
// 兜底:还留在根上的(目标位已有同名文件、或者正被别的进程占用搬不动)一律归档进 legacy/,
|
|
446
453
|
// 别让它们继续躺在 ~/.dsh 根上冒充「插件还在用老布局」。这一趟搬不动的,下次启动再来。
|
|
447
454
|
try {
|
|
@@ -831,6 +838,8 @@ function normalizeConfig(cfg) {
|
|
|
831
838
|
cfg.probeIntervalSec = clampNum(cfg.probeIntervalSec, 10, 3600, 45)
|
|
832
839
|
cfg.launchGraceSec = clampNum(cfg.launchGraceSec, 20, 3600, 150)
|
|
833
840
|
cfg.qrFreshSec = clampNum(cfg.qrFreshSec, 30, 3600, 300)
|
|
841
|
+
cfg.qrPanelEnabled = cfg.qrPanelEnabled !== false && cfg.qrPanelEnabled !== 'false' && cfg.qrPanelEnabled !== 0
|
|
842
|
+
cfg.qrPanelPort = clampNum(cfg.qrPanelPort, 1024, 65535, 8899)
|
|
834
843
|
cfg.scanTimeoutSec = clampNum(cfg.scanTimeoutSec, 30, 7200, 300)
|
|
835
844
|
cfg.maxScanRetries = clampNum(cfg.maxScanRetries, 0, 20, 2)
|
|
836
845
|
cfg.stuckSec = clampNum(cfg.stuckSec, 60, 7200, 420)
|
|
@@ -877,7 +886,7 @@ function resolveConfig(raw) {
|
|
|
877
886
|
if (!cfg.lockPath) cfg.lockPath = path.join(dir, 'lock.json')
|
|
878
887
|
if (!cfg.statePath) cfg.statePath = path.join(dir, 'state.json')
|
|
879
888
|
if (!cfg.qrCopyPath) cfg.qrCopyPath = path.join(dir, 'qr.png')
|
|
880
|
-
// qq-bridge-state.json 是 dsh-qq-
|
|
889
|
+
// qq-bridge-state.json 是 dsh-qq-link 写的,不归我们管,留在 ~/.dsh 根上
|
|
881
890
|
if (!cfg.bridgeStatePath) cfg.bridgeStatePath = path.join(home, 'qq-bridge-state.json')
|
|
882
891
|
if (!cfg.configPath) cfg.configPath = configFilePath(dir)
|
|
883
892
|
return cfg
|
|
@@ -1143,7 +1152,7 @@ function parseCompanionConfig(text) {
|
|
|
1143
1152
|
/**
|
|
1144
1153
|
* 扫一遍 `~/.dsh/profiles/*`,找出「装了这个包」的 profile。
|
|
1145
1154
|
*
|
|
1146
|
-
* 两个共存插件(dsh-onebot-qq / dsh-qq-
|
|
1155
|
+
* 两个共存插件(dsh-onebot-qq / dsh-qq-link)用的是同一套判断:
|
|
1147
1156
|
* - 装了 = 它的 package.json 在 node_modules 里,或者写在 profile 的 dependencies 里;
|
|
1148
1157
|
* - 挂了 = 出现在 `dsh.profile.bundles` 里,或者被 `cordis.patch.yml` / `cordis.yml`
|
|
1149
1158
|
* 单独 insert 过(认 `id: xxx`)。只装在 dependencies 里而没进 bundles 的,DSH 不会加载它,
|
|
@@ -1283,18 +1292,44 @@ function companionTokenMatch(cfg, comp) {
|
|
|
1283
1292
|
}
|
|
1284
1293
|
|
|
1285
1294
|
/* ------------------------------------------------------------------ *
|
|
1286
|
-
* 第二个共存插件:dsh-qq-
|
|
1295
|
+
* 第二个共存插件:dsh-qq-link(把 NapCat 的消息接到 QQ 聊天里)
|
|
1287
1296
|
*
|
|
1288
1297
|
* 它和 dsh-onebot-qq 是两种东西:onebot 提供「接口」,qq-bridge 是「客户端」——
|
|
1289
1298
|
* 它拿 HTTP/WS 去连 NapCat,所以两边端口必须对上,对不上就静默不工作。
|
|
1290
1299
|
* 它的配置不在 profile 里,摊在 ~/.dsh/qq-bridge-config.json(中文键名)。
|
|
1291
1300
|
* ------------------------------------------------------------------ */
|
|
1292
1301
|
|
|
1293
|
-
|
|
1302
|
+
/**
|
|
1303
|
+
* 认哪个包?上游 2026-11 把 `dsh-qq-link` 更名成 `dsh-qq-link`,但**插件内部一个字没改**:
|
|
1304
|
+
* `export const name = 'qq-bridge'`、配置 `~/.dsh/qq-bridge-config.json`、
|
|
1305
|
+
* 状态 `~/.dsh/qq-bridge-state.json`、路由前缀 `/dsh-qq-bridge/*`。
|
|
1306
|
+
* 所以这里只换「包名」,别把 id 和文件名一起换掉 —— 换了就永远认不到。
|
|
1307
|
+
*
|
|
1308
|
+
* 新名排前面、老名兜底:还没升级的机器装着 `dsh-qq-bridge` 也照样认得出。
|
|
1309
|
+
*/
|
|
1310
|
+
const BRIDGE_PACKAGES = ['dsh-qq-link', 'dsh-qq-bridge']
|
|
1311
|
+
const BRIDGE_PACKAGE = BRIDGE_PACKAGES[0]
|
|
1294
1312
|
const BRIDGE_ID = 'qq-bridge'
|
|
1295
1313
|
const BRIDGE_DEFAULTS = { napcat: 'http://127.0.0.1:3000', enabled: false, dryRun: true }
|
|
1296
1314
|
const bridgeCache = { at: 0, value: null }
|
|
1297
1315
|
|
|
1316
|
+
/**
|
|
1317
|
+
* 把两个包名的探测结果并起来(一个 profile 只留一条,新名优先)。
|
|
1318
|
+
* 正常机器只会命中一个;同时装了新旧两代的,说新名那个。
|
|
1319
|
+
*/
|
|
1320
|
+
function bridgeHits() {
|
|
1321
|
+
const seen = new Map()
|
|
1322
|
+
for (const pkg of BRIDGE_PACKAGES) {
|
|
1323
|
+
for (const hit of profileHits(pkg, BRIDGE_ID)) {
|
|
1324
|
+
if (!seen.has(hit.dir)) seen.set(hit.dir, { ...hit, pkg })
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
const current = path.join(dshHome(), 'profiles', 'desktop')
|
|
1328
|
+
return [...seen.values()].sort((a, b) =>
|
|
1329
|
+
a.dir === current ? -1 : b.dir === current ? 1 : String(a.profile).localeCompare(String(b.profile)),
|
|
1330
|
+
)
|
|
1331
|
+
}
|
|
1332
|
+
|
|
1298
1333
|
/** 从 OneBot 地址里抠端口(认不出就 0,不要瞎猜) */
|
|
1299
1334
|
function urlPort(raw) {
|
|
1300
1335
|
try {
|
|
@@ -1321,7 +1356,7 @@ function detectBridge(options = {}) {
|
|
|
1321
1356
|
: 20000
|
|
1322
1357
|
if (!force && bridgeCache.value && Date.now() - bridgeCache.at < maxAgeMs) return bridgeCache.value
|
|
1323
1358
|
|
|
1324
|
-
const hits =
|
|
1359
|
+
const hits = bridgeHits()
|
|
1325
1360
|
const top = hits.find((h) => h.mounted) || hits[0] || null
|
|
1326
1361
|
const rc = readBridgeConfig()
|
|
1327
1362
|
const cfg = rc.cfg || {}
|
|
@@ -1329,6 +1364,7 @@ function detectBridge(options = {}) {
|
|
|
1329
1364
|
|
|
1330
1365
|
const result = {
|
|
1331
1366
|
found: Boolean(top),
|
|
1367
|
+
packageName: String((top && top.pkg) || BRIDGE_PACKAGE),
|
|
1332
1368
|
packageVersion: String((top && top.version) || ''),
|
|
1333
1369
|
profile: String((top && top.profile) || ''),
|
|
1334
1370
|
mounted: Boolean(top && top.mounted),
|
|
@@ -1340,7 +1376,7 @@ function detectBridge(options = {}) {
|
|
|
1340
1376
|
napcatPort: urlPort(napcatUrl),
|
|
1341
1377
|
tokenKnown: rc.known ? String(cfg.令牌 || '').trim() !== '' : false,
|
|
1342
1378
|
bindings: rc.known && Array.isArray(cfg.绑定) ? cfg.绑定.length : 0,
|
|
1343
|
-
candidates: hits.map((h) => ({ profile: h.profile, mounted: h.mounted, version: h.version })),
|
|
1379
|
+
candidates: hits.map((h) => ({ profile: h.profile, packageName: h.pkg, mounted: h.mounted, version: h.version })),
|
|
1344
1380
|
}
|
|
1345
1381
|
bridgeCache.at = Date.now()
|
|
1346
1382
|
bridgeCache.value = result
|
|
@@ -1351,6 +1387,7 @@ function detectBridge(options = {}) {
|
|
|
1351
1387
|
function bridgePublic(bridge) {
|
|
1352
1388
|
if (!bridge || !bridge.found) return null
|
|
1353
1389
|
return {
|
|
1390
|
+
packageName: bridge.packageName,
|
|
1354
1391
|
version: bridge.packageVersion,
|
|
1355
1392
|
profile: bridge.profile,
|
|
1356
1393
|
mounted: bridge.mounted,
|
|
@@ -1383,19 +1420,19 @@ function bridgeCheck(health, bridge) {
|
|
|
1383
1420
|
let note = ''
|
|
1384
1421
|
if (!bridge.mounted) {
|
|
1385
1422
|
state = 'not-mounted'
|
|
1386
|
-
note = 'dsh-qq-
|
|
1423
|
+
note = 'dsh-qq-link 装了但没挂进 profile(bundles 里没有它、patch 也没 insert),桥接不会跑'
|
|
1387
1424
|
} else if (reverse && bp && portUsed && bp === portUsed) {
|
|
1388
1425
|
state = 'bridge-hits-reverse'
|
|
1389
|
-
note = `dsh-qq-
|
|
1426
|
+
note = `dsh-qq-link 连的是 ${bridge.napcatUrl},而那个端口现在是反向 WebSocket(只收 WebSocket 握手)—— 桥接用 HTTP 敲它就是 426/401。让它改连 NapCat 自己 httpServers 的端口`
|
|
1390
1427
|
} else if (!reverse && bp && portUsed && bp !== portUsed) {
|
|
1391
1428
|
state = 'port-mismatch'
|
|
1392
|
-
note = `dsh-qq-
|
|
1429
|
+
note = `dsh-qq-link 连的是 ${bridge.napcatUrl}(端口 ${bp}),NapCat 的 OneBot 接口在 ${portUsed} —— 两边对不上,消息发不出去`
|
|
1393
1430
|
} else if (bridge.configKnown && !bridge.enabled) {
|
|
1394
1431
|
state = 'disabled'
|
|
1395
|
-
note = 'dsh-qq-
|
|
1432
|
+
note = 'dsh-qq-link 的「总开关」是关的:它在跑,但一句都不会发'
|
|
1396
1433
|
} else if (bridge.configKnown && bridge.dryRun) {
|
|
1397
1434
|
state = 'dry-run'
|
|
1398
|
-
note = 'dsh-qq-
|
|
1435
|
+
note = 'dsh-qq-link 开着「试运行」:只记账,不真发到 QQ'
|
|
1399
1436
|
}
|
|
1400
1437
|
return { state, note, bridgePort: bp, portUsed, reverse }
|
|
1401
1438
|
}
|
|
@@ -1552,7 +1589,7 @@ async function probe(cfg, options = {}) {
|
|
|
1552
1589
|
? options.companion
|
|
1553
1590
|
: detectCompanion()
|
|
1554
1591
|
const tokenMatch = companionTokenMatch(cfg, comp)
|
|
1555
|
-
// 桥接(dsh-qq-
|
|
1592
|
+
// 桥接(dsh-qq-link)只是「顺便看一眼」,不参与「探哪个端口」的决策,所以可以注入桩
|
|
1556
1593
|
const bridge =
|
|
1557
1594
|
options && Object.prototype.hasOwnProperty.call(options, 'bridge') ? options.bridge : detectBridge()
|
|
1558
1595
|
const wrap = (health, extra = {}) => {
|
|
@@ -2296,78 +2333,6 @@ function repairTaskActions(cfg) {
|
|
|
2296
2333
|
}
|
|
2297
2334
|
}
|
|
2298
2335
|
|
|
2299
|
-
/* ── 提权交接:插件不自己去要管理员,改成给你一个双击就行的脚本 ──────────────
|
|
2300
|
-
*
|
|
2301
|
-
* 为什么要这样:DSH 自己是普通权限进程。插件偶尔要做几件「可能要管理员」的事
|
|
2302
|
-
* (往 Program Files 里的 NapCat 目录补启动脚本、建计划任务、给 OneBot 端口开防火墙),
|
|
2303
|
-
* 而插件既不该、也没法替人按下 UAC 里那个「是」。
|
|
2304
|
-
*
|
|
2305
|
-
* 所以改成这样:
|
|
2306
|
-
* 1. 插件把要做的每一步写进 plan.json(步骤由插件自己生成,来源可信);
|
|
2307
|
-
* 2. 生成 apply.ps1:先数一遍「这些步骤里有没有真需要管理员的」,
|
|
2308
|
-
* 没有就直接干;有才 Start-Process -Verb RunAs 自我提权 ——
|
|
2309
|
-
* UAC 弹窗就是用户亲手点的那一下确认;
|
|
2310
|
-
* 3. 生成 apply-admin.cmd:双击入口,省得用户去右键「以管理员身份运行」;
|
|
2311
|
-
* 4. 每步结果写 result.json,设置页读回来说清楚哪条成了、哪条没成;
|
|
2312
|
-
* 5. 另外给一个 undo-admin.cmd,反着把插件建过的东西删掉。
|
|
2313
|
-
*
|
|
2314
|
-
* 所有产物只在 ~/.dsh/dsh-napcat-keeper/setup 一个目录里,整个删掉就等于撤回。
|
|
2315
|
-
*/
|
|
2316
|
-
|
|
2317
|
-
const SETUP_NEEDS = {
|
|
2318
|
-
launcher: {
|
|
2319
|
-
label: '补一个启动脚本',
|
|
2320
|
-
hint: '往 Shell 目录写 napcat.quick.auto.bat,直接调 NapCatWinBootMain.exe(目录里没有别的 bat 时才需要)',
|
|
2321
|
-
},
|
|
2322
|
-
envbat: {
|
|
2323
|
-
label: '写「记住密码」中间层',
|
|
2324
|
-
hint: 'napcat-keeper-env.bat:账号与密码走环境变量交给 NapCat,密码只落这一个文件',
|
|
2325
|
-
},
|
|
2326
|
-
task: {
|
|
2327
|
-
label: '建 / 重建计划任务',
|
|
2328
|
-
hint: '开机不靠 DSH 也能拉起 QQ;用户级任务,通常不需要管理员',
|
|
2329
|
-
},
|
|
2330
|
-
firewall: {
|
|
2331
|
-
label: '放行 OneBot 端口(入站)',
|
|
2332
|
-
hint: '别的设备要连这个端口时才需要;这条一定会弹 UAC',
|
|
2333
|
-
},
|
|
2334
|
-
}
|
|
2335
|
-
|
|
2336
|
-
function setupPaths() {
|
|
2337
|
-
const dir = path.join(keeperDir(), 'setup')
|
|
2338
|
-
migrateLegacyFiles()
|
|
2339
|
-
return {
|
|
2340
|
-
dir,
|
|
2341
|
-
ps1: path.join(dir, 'apply.ps1'),
|
|
2342
|
-
cmd: path.join(dir, 'apply-admin.cmd'),
|
|
2343
|
-
undoCmd: path.join(dir, 'undo-admin.cmd'),
|
|
2344
|
-
plan: path.join(dir, 'plan.json'),
|
|
2345
|
-
result: path.join(dir, 'result.json'),
|
|
2346
|
-
runLog: path.join(dir, 'run.log'),
|
|
2347
|
-
readme: path.join(dir, 'README.txt'),
|
|
2348
|
-
}
|
|
2349
|
-
}
|
|
2350
|
-
|
|
2351
|
-
/** 目录能不能写:自己不存在就往上看,找一个存在的祖先试(免得把「没建过」当成「不可写」) */
|
|
2352
|
-
function dirWritable(dir) {
|
|
2353
|
-
if (!dir) return false
|
|
2354
|
-
let probe = dir
|
|
2355
|
-
for (let i = 0; i < 8; i += 1) {
|
|
2356
|
-
if (existsSync(probe)) {
|
|
2357
|
-
try {
|
|
2358
|
-
accessSync(probe, fsConstants.W_OK)
|
|
2359
|
-
return true
|
|
2360
|
-
} catch {
|
|
2361
|
-
return false
|
|
2362
|
-
}
|
|
2363
|
-
}
|
|
2364
|
-
const up = path.dirname(probe)
|
|
2365
|
-
if (up === probe || !up) break
|
|
2366
|
-
probe = up
|
|
2367
|
-
}
|
|
2368
|
-
return false
|
|
2369
|
-
}
|
|
2370
|
-
|
|
2371
2336
|
/** findBootMain 会递归翻目录,设置页每次刷新都调太贵 —— 缓存 60 秒,换了 Shell 目录就重算 */
|
|
2372
2337
|
function cachedBootMain(cfg, maxAgeMs = 60000) {
|
|
2373
2338
|
const key = cfg.shellDir || ''
|
|
@@ -2424,532 +2389,11 @@ function procsLayout(cfg, p) {
|
|
|
2424
2389
|
return value
|
|
2425
2390
|
}
|
|
2426
2391
|
|
|
2427
|
-
/**
|
|
2428
|
-
function launcherBatLines(cfg, boot) {
|
|
2429
|
-
const account = batValue(cfg.launchAccount)
|
|
2430
|
-
const args = String(cfg.launchArgs || '').trim()
|
|
2431
|
-
return [
|
|
2432
|
-
'@echo off',
|
|
2433
|
-
'chcp 65001 >nul',
|
|
2434
|
-
'rem dsh-napcat-keeper 自动生成:目录里没有别的启动脚本时才用它,直接调 NapCatWinBootMain.exe',
|
|
2435
|
-
'cd /d "%~dp0"',
|
|
2436
|
-
`set "ACCOUNT=${account}"`,
|
|
2437
|
-
`start "" "${boot}"${account ? ` ${account}` : ''}${args ? ` ${args}` : ''}`,
|
|
2438
|
-
'',
|
|
2439
|
-
]
|
|
2440
|
-
}
|
|
2441
|
-
|
|
2442
|
-
/** 任务里那条命令(纯字符串版本:不写盘,供「脚本」复用 taskCommandLine 的规则) */
|
|
2392
|
+
/** 任务里那条命令(纯字符串版本:不写盘,供别处复用 taskCommandLine 的规则) */
|
|
2443
2393
|
function taskCommandLinePure(cfg, bat) {
|
|
2444
2394
|
return taskCmdLineOf(cfg, passwordActive(cfg) && bat ? envBatPath(cfg) : bat)
|
|
2445
2395
|
}
|
|
2446
2396
|
|
|
2447
|
-
/** 给设置页看的清单:每个需要能不能做、要不要管理员 */
|
|
2448
|
-
function needCatalog(cfg) {
|
|
2449
|
-
const boot = (() => {
|
|
2450
|
-
try {
|
|
2451
|
-
return cachedBootMain(cfg)
|
|
2452
|
-
} catch {
|
|
2453
|
-
return ''
|
|
2454
|
-
}
|
|
2455
|
-
})()
|
|
2456
|
-
const bat = resolveLauncherPath(cfg)
|
|
2457
|
-
return Object.entries(SETUP_NEEDS).map(([id, meta]) => {
|
|
2458
|
-
let ready = true
|
|
2459
|
-
let why = ''
|
|
2460
|
-
let needAdmin = false
|
|
2461
|
-
if (id === 'launcher') {
|
|
2462
|
-
ready = Boolean(boot)
|
|
2463
|
-
if (!boot) why = 'Shell 目录里没找到 NapCatWinBootMain.exe'
|
|
2464
|
-
needAdmin = Boolean(boot) && !dirWritable(cfg.shellDir)
|
|
2465
|
-
if (needAdmin) why = 'Shell 目录当前不可写(大概在 Program Files 里,要管理员)'
|
|
2466
|
-
} else if (id === 'envbat') {
|
|
2467
|
-
ready = passwordActive(cfg) && Boolean(bat)
|
|
2468
|
-
if (!passwordActive(cfg)) why = '先打开「记住密码」并填好账号'
|
|
2469
|
-
else if (!bat) why = '先在 Shell 目录里找到启动脚本'
|
|
2470
|
-
needAdmin = Boolean(ready) && !dirWritable(path.dirname(envBatPath(cfg)))
|
|
2471
|
-
} else if (id === 'task') {
|
|
2472
|
-
ready = Boolean(cfg.taskName) && Boolean(bat)
|
|
2473
|
-
if (!cfg.taskName) why = '先填计划任务名'
|
|
2474
|
-
else if (!bat) why = '先填 Shell 目录(找得到启动脚本才建任务)'
|
|
2475
|
-
} else if (id === 'firewall') {
|
|
2476
|
-
ready = Boolean(cfg.onebotPort)
|
|
2477
|
-
if (!ready) why = '先填 OneBot 端口'
|
|
2478
|
-
needAdmin = true
|
|
2479
|
-
}
|
|
2480
|
-
return { id, label: meta.label, hint: meta.hint, ready, why, needAdmin }
|
|
2481
|
-
})
|
|
2482
|
-
}
|
|
2483
|
-
|
|
2484
|
-
/** 把「要做的事」摊成步骤数组(plan.json 里的 steps) */
|
|
2485
|
-
function buildSetupSteps(cfg, wants) {
|
|
2486
|
-
const picked = Array.isArray(wants) && wants.length ? wants : Object.keys(SETUP_NEEDS)
|
|
2487
|
-
const want = new Set(picked.filter((id) => SETUP_NEEDS[id]))
|
|
2488
|
-
const steps = []
|
|
2489
|
-
const bat = resolveLauncherPath(cfg)
|
|
2490
|
-
|
|
2491
|
-
if (want.has('launcher')) {
|
|
2492
|
-
const boot = (() => {
|
|
2493
|
-
try {
|
|
2494
|
-
return cachedBootMain(cfg)
|
|
2495
|
-
} catch {
|
|
2496
|
-
return ''
|
|
2497
|
-
}
|
|
2498
|
-
})()
|
|
2499
|
-
if (boot) {
|
|
2500
|
-
const file = path.join(cfg.shellDir, 'napcat.quick.auto.bat')
|
|
2501
|
-
steps.push({
|
|
2502
|
-
id: 'launcher',
|
|
2503
|
-
label: '补启动脚本 napcat.quick.auto.bat',
|
|
2504
|
-
kind: 'writeFile',
|
|
2505
|
-
path: file,
|
|
2506
|
-
admin: !dirWritable(cfg.shellDir),
|
|
2507
|
-
note: '直接调 NapCatWinBootMain.exe,账号走环境变量',
|
|
2508
|
-
contentBase64: Buffer.from(launcherBatLines(cfg, boot).join('\r\n'), 'utf8').toString('base64'),
|
|
2509
|
-
})
|
|
2510
|
-
}
|
|
2511
|
-
}
|
|
2512
|
-
|
|
2513
|
-
if (want.has('envbat') && passwordActive(cfg) && bat) {
|
|
2514
|
-
const file = envBatPath(cfg)
|
|
2515
|
-
steps.push({
|
|
2516
|
-
id: 'envbat',
|
|
2517
|
-
label: '写「记住密码」中间层 napcat-keeper-env.bat',
|
|
2518
|
-
kind: 'writeFile',
|
|
2519
|
-
path: file,
|
|
2520
|
-
admin: !dirWritable(path.dirname(file)),
|
|
2521
|
-
note: '只在本机磁盘里,关掉「记住密码」或点「清除密码」就删',
|
|
2522
|
-
secret: true,
|
|
2523
|
-
contentBase64: Buffer.from(envBatLines(cfg, bat).join('\r\n'), 'utf8').toString('base64'),
|
|
2524
|
-
})
|
|
2525
|
-
}
|
|
2526
|
-
|
|
2527
|
-
if (want.has('task') && cfg.taskName && bat) {
|
|
2528
|
-
const requires = []
|
|
2529
|
-
let inner = ''
|
|
2530
|
-
if (cfg.hideConsole !== false) {
|
|
2531
|
-
const vbs = taskWrapperPath(cfg)
|
|
2532
|
-
const cmdLine = taskCommandLinePure(cfg, bat).replace(/"/g, '""')
|
|
2533
|
-
const body = [
|
|
2534
|
-
'Option Explicit',
|
|
2535
|
-
'Dim sh',
|
|
2536
|
-
'Set sh = CreateObject("WScript.Shell")',
|
|
2537
|
-
`sh.Run "${cmdLine}", 0, False`,
|
|
2538
|
-
'',
|
|
2539
|
-
].join('\r\n')
|
|
2540
|
-
steps.push({
|
|
2541
|
-
id: 'task-wrapper',
|
|
2542
|
-
label: '写无窗口壳 launch.vbs',
|
|
2543
|
-
kind: 'writeFile',
|
|
2544
|
-
path: vbs,
|
|
2545
|
-
admin: !dirWritable(path.dirname(vbs)),
|
|
2546
|
-
note: '计划任务跑它,桌面才不会挂一个黑框',
|
|
2547
|
-
contentBase64: Buffer.concat([Buffer.from([0xff, 0xfe]), Buffer.from(body, 'utf16le')]).toString(
|
|
2548
|
-
'base64',
|
|
2549
|
-
),
|
|
2550
|
-
})
|
|
2551
|
-
requires.push('task-wrapper')
|
|
2552
|
-
inner = `wscript.exe "${vbs}"`
|
|
2553
|
-
} else {
|
|
2554
|
-
inner = taskCommandLinePure(cfg, bat)
|
|
2555
|
-
}
|
|
2556
|
-
if (passwordActive(cfg)) requires.push('envbat')
|
|
2557
|
-
steps.push({
|
|
2558
|
-
id: 'task',
|
|
2559
|
-
label: `建计划任务「${cfg.taskName}」`,
|
|
2560
|
-
kind: 'scheduledTask',
|
|
2561
|
-
admin: false,
|
|
2562
|
-
note: '用户级任务,一般不需要管理员;系统策略挡着时脚本会把错误原样告诉你',
|
|
2563
|
-
requires,
|
|
2564
|
-
name: cfg.taskName,
|
|
2565
|
-
command: inner,
|
|
2566
|
-
})
|
|
2567
|
-
}
|
|
2568
|
-
|
|
2569
|
-
if (want.has('firewall') && cfg.onebotPort) {
|
|
2570
|
-
const port = Number(cfg.onebotPort) || 3000
|
|
2571
|
-
steps.push({
|
|
2572
|
-
id: 'firewall',
|
|
2573
|
-
label: `放行 TCP ${port} 入站`,
|
|
2574
|
-
kind: 'firewallRule',
|
|
2575
|
-
admin: true,
|
|
2576
|
-
note: '只有别的设备要连 OneBot 端口时才需要;这条一定会弹 UAC',
|
|
2577
|
-
name: `dsh-napcat-keeper OneBot ${port}`,
|
|
2578
|
-
port,
|
|
2579
|
-
})
|
|
2580
|
-
}
|
|
2581
|
-
return steps
|
|
2582
|
-
}
|
|
2583
|
-
|
|
2584
|
-
/** apply.ps1 的全文:这段是插件的固定产物,跟 plan.json 一起决定脚本会干什么 */
|
|
2585
|
-
function applyPs1Source() {
|
|
2586
|
-
return `# dsh-napcat-keeper 提权交接脚本(插件自动生成,改了也会被覆盖)
|
|
2587
|
-
# 双击同目录的 apply-admin.cmd 干活;想撤干净就双击 undo-admin.cmd。
|
|
2588
|
-
param([switch]$Elevated, [switch]$Undo)
|
|
2589
|
-
$ErrorActionPreference = 'Continue'
|
|
2590
|
-
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
2591
|
-
$planPath = Join-Path $here 'plan.json'
|
|
2592
|
-
$resultPath = Join-Path $here 'result.json'
|
|
2593
|
-
$runLog = Join-Path $here 'run.log'
|
|
2594
|
-
# run.log 只追加不滚动会长到没人看:超过 256KB 就换新的一份
|
|
2595
|
-
try {
|
|
2596
|
-
if ((Test-Path -LiteralPath $runLog) -and ((Get-Item -LiteralPath $runLog).Length -gt 262144)) {
|
|
2597
|
-
Remove-Item -LiteralPath $runLog -Force -ErrorAction SilentlyContinue
|
|
2598
|
-
}
|
|
2599
|
-
} catch {}
|
|
2600
|
-
|
|
2601
|
-
function Say($text) {
|
|
2602
|
-
$line = ('[{0}] {1}' -f (Get-Date -Format 'HH:mm:ss'), $text)
|
|
2603
|
-
Write-Host $line
|
|
2604
|
-
try { Add-Content -LiteralPath $runLog -Value $line -Encoding UTF8 } catch {}
|
|
2605
|
-
}
|
|
2606
|
-
|
|
2607
|
-
function Wait-Key {
|
|
2608
|
-
if ($env:NPK_NO_PAUSE -eq '1') { return }
|
|
2609
|
-
Write-Host ''
|
|
2610
|
-
Write-Host '按任意键关掉这个窗口。'
|
|
2611
|
-
try { $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') } catch {}
|
|
2612
|
-
}
|
|
2613
|
-
|
|
2614
|
-
if (-not (Test-Path -LiteralPath $planPath)) {
|
|
2615
|
-
Say 'plan.json 不在了 —— 回 DSH 设置页重新生成一次。'
|
|
2616
|
-
Wait-Key
|
|
2617
|
-
exit 2
|
|
2618
|
-
}
|
|
2619
|
-
|
|
2620
|
-
$plan = Get-Content -LiteralPath $planPath -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
2621
|
-
$steps = @($plan.steps)
|
|
2622
|
-
$mode = 'apply'
|
|
2623
|
-
if ($Undo) {
|
|
2624
|
-
$mode = 'undo'
|
|
2625
|
-
$rev = @()
|
|
2626
|
-
for ($i = $steps.Count - 1; $i -ge 0; $i--) { $rev += $steps[$i] }
|
|
2627
|
-
$steps = $rev
|
|
2628
|
-
}
|
|
2629
|
-
|
|
2630
|
-
Say ('开始(' + $mode + '):' + $steps.Count + ' 步')
|
|
2631
|
-
|
|
2632
|
-
$admin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
|
|
2633
|
-
$needAdmin = @($steps | Where-Object { $_.admin -eq $true }).Count -gt 0
|
|
2634
|
-
|
|
2635
|
-
if ((!$Undo) -and $needAdmin -and (-not $admin) -and (-not $Elevated)) {
|
|
2636
|
-
Say '这批步骤里有要管理员才行的,马上弹 UAC —— 请点「是」;点「否」就什么都不改。'
|
|
2637
|
-
$argList = @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', ('"' + $PSCommandPath + '"'), '-Elevated')
|
|
2638
|
-
try {
|
|
2639
|
-
Start-Process -FilePath 'powershell.exe' -Verb RunAs -ArgumentList $argList
|
|
2640
|
-
Say '已经把活交给提权那个窗口了,跑完看同目录的 result.json。'
|
|
2641
|
-
} catch {
|
|
2642
|
-
Say '没拿到管理员权限(UAC 被拒或这台机器不给提权),这次什么都没改。'
|
|
2643
|
-
}
|
|
2644
|
-
Wait-Key
|
|
2645
|
-
exit 0
|
|
2646
|
-
}
|
|
2647
|
-
|
|
2648
|
-
$results = @()
|
|
2649
|
-
$failed = @{}
|
|
2650
|
-
foreach ($s in $steps) {
|
|
2651
|
-
$entry = [ordered]@{ id = $s.id; label = $s.label; ok = $false; skipped = $false; message = '' }
|
|
2652
|
-
if ((!$Undo) -and $s.requires) {
|
|
2653
|
-
$miss = @($s.requires | Where-Object { $failed.ContainsKey($_) })
|
|
2654
|
-
if ($miss.Count -gt 0) {
|
|
2655
|
-
$entry.skipped = $true
|
|
2656
|
-
$entry.message = '前一步没成(' + ($miss -join '、') + '),这步跳过'
|
|
2657
|
-
Say ('跳过 ' + $s.label + ':' + $entry.message)
|
|
2658
|
-
$results += $entry
|
|
2659
|
-
continue
|
|
2660
|
-
}
|
|
2661
|
-
}
|
|
2662
|
-
try {
|
|
2663
|
-
switch ($s.kind) {
|
|
2664
|
-
'writeFile' {
|
|
2665
|
-
if ($Undo) {
|
|
2666
|
-
if (Test-Path -LiteralPath $s.path) {
|
|
2667
|
-
Remove-Item -LiteralPath $s.path -Force
|
|
2668
|
-
$entry.message = '已删除'
|
|
2669
|
-
} else {
|
|
2670
|
-
$entry.message = '本来就没有'
|
|
2671
|
-
}
|
|
2672
|
-
} else {
|
|
2673
|
-
$dir = Split-Path -Parent $s.path
|
|
2674
|
-
if ($dir -and (-not (Test-Path -LiteralPath $dir))) { New-Item -ItemType Directory -Force -Path $dir | Out-Null }
|
|
2675
|
-
$bytes = [Convert]::FromBase64String($s.contentBase64)
|
|
2676
|
-
[IO.File]::WriteAllBytes($s.path, $bytes)
|
|
2677
|
-
if ($s.secret) { $entry.message = '已写入(内容不外显)' } else { $entry.message = '已写入' }
|
|
2678
|
-
}
|
|
2679
|
-
$entry.ok = $true
|
|
2680
|
-
}
|
|
2681
|
-
'mkdir' {
|
|
2682
|
-
if (-not $Undo) {
|
|
2683
|
-
New-Item -ItemType Directory -Force -Path $s.path | Out-Null
|
|
2684
|
-
$entry.message = '已建目录'
|
|
2685
|
-
} else {
|
|
2686
|
-
$entry.message = '不用撤'
|
|
2687
|
-
}
|
|
2688
|
-
$entry.ok = $true
|
|
2689
|
-
}
|
|
2690
|
-
'scheduledTask' {
|
|
2691
|
-
if ($Undo) {
|
|
2692
|
-
$null = & schtasks.exe /delete /tn $s.name /f 2>&1
|
|
2693
|
-
$entry.message = '任务已删(本来没有也算)'
|
|
2694
|
-
} else {
|
|
2695
|
-
# 先结束同名旧任务(正在跑时 /f 覆盖会被拒);/st 00:00 会带一句
|
|
2696
|
-
# 「时间已过」的自我提醒,成功时不必让用户看见,失败时才把原始输出抛出来
|
|
2697
|
-
$null = & schtasks.exe /end /tn $s.name 2>&1
|
|
2698
|
-
$createOut = (& schtasks.exe /create /tn $s.name /tr $s.command /sc ONCE /st 00:00 /f 2>&1 | Out-String)
|
|
2699
|
-
if ($LASTEXITCODE -ne 0) { throw ('schtasks 失败(退出码 ' + $LASTEXITCODE + '):' + $createOut.Trim()) }
|
|
2700
|
-
$entry.message = '任务已就绪'
|
|
2701
|
-
}
|
|
2702
|
-
$entry.ok = $true
|
|
2703
|
-
}
|
|
2704
|
-
'firewallRule' {
|
|
2705
|
-
if ($Undo) {
|
|
2706
|
-
Get-NetFirewallRule -DisplayName $s.name -ErrorAction SilentlyContinue | Remove-NetFirewallRule -ErrorAction SilentlyContinue
|
|
2707
|
-
$entry.message = '规则已删(本来没有也算)'
|
|
2708
|
-
} else {
|
|
2709
|
-
Get-NetFirewallRule -DisplayName $s.name -ErrorAction SilentlyContinue | Remove-NetFirewallRule -ErrorAction SilentlyContinue
|
|
2710
|
-
New-NetFirewallRule -DisplayName $s.name -Direction Inbound -Action Allow -Protocol TCP -LocalPort ([int]$s.port) -Profile Private, Domain -ErrorAction Stop | Out-Null
|
|
2711
|
-
$entry.message = ('已放行 TCP ' + [int]$s.port)
|
|
2712
|
-
}
|
|
2713
|
-
$entry.ok = $true
|
|
2714
|
-
}
|
|
2715
|
-
default {
|
|
2716
|
-
$entry.message = ('不认识的步骤类型 ' + $s.kind)
|
|
2717
|
-
}
|
|
2718
|
-
}
|
|
2719
|
-
} catch {
|
|
2720
|
-
$entry.ok = $false
|
|
2721
|
-
$entry.message = '出错:' + $_.Exception.Message
|
|
2722
|
-
}
|
|
2723
|
-
if (-not $entry.ok) { $failed[$s.id] = $true }
|
|
2724
|
-
if ($entry.ok) { Say ('好了 ' + $s.label) } else { Say ('没成 ' + $s.label + ':' + $entry.message) }
|
|
2725
|
-
$results += $entry
|
|
2726
|
-
}
|
|
2727
|
-
|
|
2728
|
-
$bad = @($results | Where-Object { -not $_.ok })
|
|
2729
|
-
$payload = [ordered]@{
|
|
2730
|
-
at = (Get-Date).ToString('o')
|
|
2731
|
-
mode = $mode
|
|
2732
|
-
elevated = $admin
|
|
2733
|
-
adminNeeded = $needAdmin
|
|
2734
|
-
ok = ($bad.Count -eq 0)
|
|
2735
|
-
results = $results
|
|
2736
|
-
}
|
|
2737
|
-
try {
|
|
2738
|
-
$json = $payload | ConvertTo-Json -Depth 8
|
|
2739
|
-
[IO.File]::WriteAllText($resultPath, $json, (New-Object System.Text.UTF8Encoding($false)))
|
|
2740
|
-
Say ('收工:' + $bad.Count + ' 步没成;结果已写回 result.json,回 DSH 设置页点「读结果」就行。')
|
|
2741
|
-
} catch {
|
|
2742
|
-
Say ('结果没写出来:' + $_.Exception.Message)
|
|
2743
|
-
}
|
|
2744
|
-
Wait-Key
|
|
2745
|
-
`
|
|
2746
|
-
}
|
|
2747
|
-
|
|
2748
|
-
/** 双击入口 .cmd:内容全 ASCII,cmd 才不会把它显示成乱码 */
|
|
2749
|
-
function setupCmdSource() {
|
|
2750
|
-
return [
|
|
2751
|
-
'@echo off',
|
|
2752
|
-
'rem dsh-napcat-keeper - elevation handoff (auto-generated; edits get overwritten)',
|
|
2753
|
-
'rem Double-click this file. PowerShell asks for admin only if the plan really needs it.',
|
|
2754
|
-
'title dsh-napcat-keeper setup',
|
|
2755
|
-
'cd /d "%~dp0"',
|
|
2756
|
-
'powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0apply.ps1"',
|
|
2757
|
-
'if errorlevel 1 pause',
|
|
2758
|
-
'',
|
|
2759
|
-
].join('\r\n')
|
|
2760
|
-
}
|
|
2761
|
-
|
|
2762
|
-
function setupUndoCmdSource() {
|
|
2763
|
-
return [
|
|
2764
|
-
'@echo off',
|
|
2765
|
-
'rem dsh-napcat-keeper - undo what this plan created (reverse order)',
|
|
2766
|
-
'title dsh-napcat-keeper undo',
|
|
2767
|
-
'cd /d "%~dp0"',
|
|
2768
|
-
'powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0apply.ps1" -Undo',
|
|
2769
|
-
'if errorlevel 1 pause',
|
|
2770
|
-
'',
|
|
2771
|
-
].join('\r\n')
|
|
2772
|
-
}
|
|
2773
|
-
|
|
2774
|
-
/** 说明文件:给人看的,写清楚会动哪些文件、怎么撤回 */
|
|
2775
|
-
function setupReadmeText(cfg, plan) {
|
|
2776
|
-
const lines = [
|
|
2777
|
-
'dsh-napcat-keeper 提权交接目录',
|
|
2778
|
-
'',
|
|
2779
|
-
'这是什么:',
|
|
2780
|
-
' DSH 自己是普通权限进程,插件偶尔要做几件「可能要管理员」的事。',
|
|
2781
|
-
' 插件不自己去要管理员,而是把要做的事写在这里,由你双击一下、在 UAC 上点「是」。',
|
|
2782
|
-
'',
|
|
2783
|
-
'怎么用:',
|
|
2784
|
-
' 1) 双击 apply-admin.cmd;',
|
|
2785
|
-
' 2) 如果这批步骤需要管理员,会弹 UAC —— 点「是」;点「否」就什么都不会改;',
|
|
2786
|
-
' 3) 跑完回 DSH 设置页点「读结果」,能看到每一步成没成。',
|
|
2787
|
-
'',
|
|
2788
|
-
'想撤回:双击 undo-admin.cmd(按相反顺序把这次建的东西删掉)。',
|
|
2789
|
-
'',
|
|
2790
|
-
`生成时间:${plan.createdAt}`,
|
|
2791
|
-
`Shell 目录:${cfg.shellDir || '(未配置)'}`,
|
|
2792
|
-
`这批步骤${plan.needAdmin ? '需要' : '不需要'}管理员权限。`,
|
|
2793
|
-
'',
|
|
2794
|
-
'会动的文件 / 规则:',
|
|
2795
|
-
...plan.steps.map((s, i) => {
|
|
2796
|
-
const where = s.path || s.name || s.kind
|
|
2797
|
-
return ` ${i + 1}. [${s.kind}] ${where}${s.admin ? '(要管理员)' : ''}`
|
|
2798
|
-
}),
|
|
2799
|
-
'',
|
|
2800
|
-
'这几个文件里没有密码明文说明:napcat-keeper-env.bat 里有(那是给 NapCat 用的),',
|
|
2801
|
-
'所以这个目录别往外发;关掉「记住密码」或在设置页点「清除密码」就会删掉它。',
|
|
2802
|
-
'',
|
|
2803
|
-
]
|
|
2804
|
-
return lines.join('\r\n')
|
|
2805
|
-
}
|
|
2806
|
-
|
|
2807
|
-
/** 生成整套脚本,返回给设置页用的摘要(不含任何 secret 内容) */
|
|
2808
|
-
function writeSetupBundle(cfg, wants) {
|
|
2809
|
-
if (process.platform !== 'win32') {
|
|
2810
|
-
return { ok: false, error: '这套脚本只对 Windows 有意义' }
|
|
2811
|
-
}
|
|
2812
|
-
const steps = buildSetupSteps(cfg, wants)
|
|
2813
|
-
if (!steps.length) {
|
|
2814
|
-
return { ok: false, error: '这次一条可做的步骤都没有(先把 Shell 目录 / 账号 / 任务名填上)' }
|
|
2815
|
-
}
|
|
2816
|
-
const p = setupPaths()
|
|
2817
|
-
const plan = {
|
|
2818
|
-
plugin: 'dsh-napcat-keeper',
|
|
2819
|
-
version,
|
|
2820
|
-
createdAt: new Date().toISOString(),
|
|
2821
|
-
shellDir: cfg.shellDir || '',
|
|
2822
|
-
account: cfg.launchAccount || '',
|
|
2823
|
-
needAdmin: steps.some((s) => s.admin === true),
|
|
2824
|
-
steps,
|
|
2825
|
-
}
|
|
2826
|
-
try {
|
|
2827
|
-
mkdirSync(p.dir, { recursive: true })
|
|
2828
|
-
writeFileSync(p.plan, `${JSON.stringify(plan, null, 2)}\n`, 'utf8')
|
|
2829
|
-
// ps1 必须带 BOM:PowerShell 5.1 没有 BOM 会按 ANSI 解,中文全变乱码
|
|
2830
|
-
writeFileSync(
|
|
2831
|
-
p.ps1,
|
|
2832
|
-
Buffer.concat([Buffer.from([0xef, 0xbb, 0xbf]), Buffer.from(applyPs1Source(), 'utf8')]),
|
|
2833
|
-
)
|
|
2834
|
-
writeFileSync(p.cmd, setupCmdSource(), 'utf8')
|
|
2835
|
-
writeFileSync(p.undoCmd, setupUndoCmdSource(), 'utf8')
|
|
2836
|
-
writeFileSync(
|
|
2837
|
-
p.readme,
|
|
2838
|
-
Buffer.concat([Buffer.from([0xef, 0xbb, 0xbf]), Buffer.from(setupReadmeText(cfg, plan), 'utf8')]),
|
|
2839
|
-
)
|
|
2840
|
-
try {
|
|
2841
|
-
if (existsSync(p.result)) unlinkSync(p.result)
|
|
2842
|
-
} catch {}
|
|
2843
|
-
log(cfg, `setup bundle 写好了:${p.dir}(${steps.length} 步,needAdmin=${plan.needAdmin})`)
|
|
2844
|
-
} catch (error) {
|
|
2845
|
-
const msg = String((error && error.message) || error)
|
|
2846
|
-
log(cfg, `setup bundle 写不出来:${msg}`)
|
|
2847
|
-
return { ok: false, error: msg }
|
|
2848
|
-
}
|
|
2849
|
-
return {
|
|
2850
|
-
ok: true,
|
|
2851
|
-
dir: p.dir,
|
|
2852
|
-
cmd: p.cmd,
|
|
2853
|
-
ps1: p.ps1,
|
|
2854
|
-
undoCmd: p.undoCmd,
|
|
2855
|
-
needAdmin: plan.needAdmin,
|
|
2856
|
-
steps: steps.map(({ contentBase64, ...rest }) => rest),
|
|
2857
|
-
}
|
|
2858
|
-
}
|
|
2859
|
-
|
|
2860
|
-
/** 读脚本写回来的结果(不存在就是「还没跑」) */
|
|
2861
|
-
function readSetupResult() {
|
|
2862
|
-
const p = setupPaths()
|
|
2863
|
-
try {
|
|
2864
|
-
if (!existsSync(p.result)) return null
|
|
2865
|
-
const text = readFileSync(p.result, 'utf8').replace(/^\uFEFF/, '')
|
|
2866
|
-
const parsed = JSON.parse(text)
|
|
2867
|
-
if (!parsed || typeof parsed !== 'object') return null
|
|
2868
|
-
let results = parsed.results
|
|
2869
|
-
if (!Array.isArray(results)) results = results ? [results] : []
|
|
2870
|
-
let mtimeMs = 0
|
|
2871
|
-
try {
|
|
2872
|
-
mtimeMs = statSync(p.result).mtimeMs
|
|
2873
|
-
} catch {}
|
|
2874
|
-
return {
|
|
2875
|
-
at: String(parsed.at || ''),
|
|
2876
|
-
mode: String(parsed.mode || 'apply'),
|
|
2877
|
-
elevated: Boolean(parsed.elevated),
|
|
2878
|
-
ok: Boolean(parsed.ok),
|
|
2879
|
-
mtimeMs,
|
|
2880
|
-
results: results.map((r) => ({
|
|
2881
|
-
id: String((r && r.id) || ''),
|
|
2882
|
-
label: String((r && r.label) || ''),
|
|
2883
|
-
ok: Boolean(r && r.ok),
|
|
2884
|
-
skipped: Boolean(r && r.skipped),
|
|
2885
|
-
message: String((r && r.message) || ''),
|
|
2886
|
-
})),
|
|
2887
|
-
}
|
|
2888
|
-
} catch {
|
|
2889
|
-
return null
|
|
2890
|
-
}
|
|
2891
|
-
}
|
|
2892
|
-
|
|
2893
|
-
/** 上一次生成的那份计划(给设置页显示「你上次要它做什么」) */
|
|
2894
|
-
function readSetupPlan() {
|
|
2895
|
-
const p = setupPaths()
|
|
2896
|
-
try {
|
|
2897
|
-
if (!existsSync(p.plan)) return null
|
|
2898
|
-
const parsed = JSON.parse(readFileSync(p.plan, 'utf8').replace(/^\uFEFF/, ''))
|
|
2899
|
-
if (!parsed || typeof parsed !== 'object') return null
|
|
2900
|
-
const steps = Array.isArray(parsed.steps) ? parsed.steps : []
|
|
2901
|
-
return {
|
|
2902
|
-
createdAt: String(parsed.createdAt || ''),
|
|
2903
|
-
needAdmin: Boolean(parsed.needAdmin),
|
|
2904
|
-
steps: steps.map((s) => ({
|
|
2905
|
-
id: String((s && s.id) || ''),
|
|
2906
|
-
label: String((s && s.label) || ''),
|
|
2907
|
-
kind: String((s && s.kind) || ''),
|
|
2908
|
-
where: String((s && (s.path || s.name)) || ''),
|
|
2909
|
-
admin: Boolean(s && s.admin),
|
|
2910
|
-
})),
|
|
2911
|
-
}
|
|
2912
|
-
} catch {
|
|
2913
|
-
return null
|
|
2914
|
-
}
|
|
2915
|
-
}
|
|
2916
|
-
|
|
2917
|
-
function setupState(cfg) {
|
|
2918
|
-
const p = setupPaths()
|
|
2919
|
-
let exists = false
|
|
2920
|
-
try {
|
|
2921
|
-
exists = existsSync(p.cmd)
|
|
2922
|
-
} catch {}
|
|
2923
|
-
return {
|
|
2924
|
-
dir: p.dir,
|
|
2925
|
-
cmdPath: p.cmd,
|
|
2926
|
-
ps1Path: p.ps1,
|
|
2927
|
-
undoCmdPath: p.undoCmd,
|
|
2928
|
-
exists,
|
|
2929
|
-
needs: needCatalog(cfg),
|
|
2930
|
-
plan: readSetupPlan(),
|
|
2931
|
-
lastRun: readSetupResult(),
|
|
2932
|
-
}
|
|
2933
|
-
}
|
|
2934
|
-
|
|
2935
|
-
/** 在资源管理器里选中那个 cmd(不是只打开目录,免得用户还得自己找) */
|
|
2936
|
-
function openSetupFolder() {
|
|
2937
|
-
const p = setupPaths()
|
|
2938
|
-
try {
|
|
2939
|
-
mkdirSync(p.dir, { recursive: true })
|
|
2940
|
-
const hasCmd = existsSync(p.cmd)
|
|
2941
|
-
const child = spawn('explorer.exe', [hasCmd ? `/select,${p.cmd}` : p.dir], {
|
|
2942
|
-
detached: true,
|
|
2943
|
-
stdio: 'ignore',
|
|
2944
|
-
windowsHide: false,
|
|
2945
|
-
})
|
|
2946
|
-
child.unref()
|
|
2947
|
-
return { ok: true, dir: p.dir, cmd: p.cmd, selected: hasCmd }
|
|
2948
|
-
} catch (error) {
|
|
2949
|
-
return { ok: false, error: String((error && error.message) || error), dir: p.dir }
|
|
2950
|
-
}
|
|
2951
|
-
}
|
|
2952
|
-
|
|
2953
2397
|
/** 把已经在跑的 NapCat 控制台窗口藏起来(老链条留下的黑框也能收掉,不用重启 QQ) */
|
|
2954
2398
|
function hideConsoleWindows(cfg) {
|
|
2955
2399
|
const ps = [
|
|
@@ -3540,7 +2984,7 @@ function registerTools(ctx, cfg) {
|
|
|
3540
2984
|
defineTool({
|
|
3541
2985
|
name: 'napcat_status',
|
|
3542
2986
|
description:
|
|
3543
|
-
'看 NapCat(QQ) 看门狗此刻的状态:OneBot 是否在线、登录的是哪个号、进程链数量(有无重复链)、二维码是否在等人扫(含副本路径)、dsh-qq-
|
|
2987
|
+
'看 NapCat(QQ) 看门狗此刻的状态:OneBot 是否在线、登录的是哪个号、进程链数量(有无重复链)、二维码是否在等人扫(含副本路径)、dsh-qq-link 的轮询有多久没更新、这次用的是哪份 Node 运行时(DSH 自带 / 系统 / 手填),以及最近看门狗日志。',
|
|
3544
2988
|
parameters: {},
|
|
3545
2989
|
output: { schema: { type: 'string' }, render: (_args, value) => renderText(value) },
|
|
3546
2990
|
async execute() {
|
|
@@ -3582,12 +3026,25 @@ function registerTools(ctx, cfg) {
|
|
|
3582
3026
|
tokenMatches:
|
|
3583
3027
|
health.companionTokenMatch === undefined ? null : health.companionTokenMatch,
|
|
3584
3028
|
},
|
|
3585
|
-
/** dsh-qq-
|
|
3029
|
+
/** dsh-qq-link:装没装、挂没挂、桥接指向的端口和 NapCat 对不上没有 */
|
|
3586
3030
|
bridge: health.bridge || bridgePublic(detectBridge()) || { found: false },
|
|
3587
3031
|
},
|
|
3588
3032
|
procs,
|
|
3589
3033
|
node: nodePublic(cfg),
|
|
3590
3034
|
qr,
|
|
3035
|
+
/** 要扫码时把这一页发给用户:优先独立小端口(任何浏览器都打得开),其次 DSH 路由 */
|
|
3036
|
+
qrPanel: {
|
|
3037
|
+
path: QR_PANEL_PATH,
|
|
3038
|
+
url: qrPanelPublic(cfg).url || QR_PANEL_PATH,
|
|
3039
|
+
running: qrPanelPublic(cfg).running,
|
|
3040
|
+
port: qrPanelPublic(cfg).port,
|
|
3041
|
+
note: qrPanelPublic(cfg).note,
|
|
3042
|
+
hint: health.online
|
|
3043
|
+
? '已登录,暂时用不上扫码页;下次掉线要扫码时同一个地址还能用。'
|
|
3044
|
+
: qrPanelPublic(cfg).url
|
|
3045
|
+
? '需要手机扫码时点开 qrPanel.url 那个地址(本机浏览器打开即可),二维码会自己换。'
|
|
3046
|
+
: '需要手机扫码时点开 qrPanel.path(挂在 DSH 地址后面),二维码会自己换。',
|
|
3047
|
+
},
|
|
3591
3048
|
bridge: bridgeAge(cfg),
|
|
3592
3049
|
autoLogin: {
|
|
3593
3050
|
rememberPassword: cfg.rememberPassword,
|
|
@@ -3610,7 +3067,7 @@ function registerTools(ctx, cfg) {
|
|
|
3610
3067
|
},
|
|
3611
3068
|
logTail: tail,
|
|
3612
3069
|
note: health.online
|
|
3613
|
-
? '在线。若 QQ 里还是没回音,问题在 dsh-qq-
|
|
3070
|
+
? '在线。若 QQ 里还是没回音,问题在 dsh-qq-link 那一侧(绑定/试运行开关)。'
|
|
3614
3071
|
: health.mode === 'reverse'
|
|
3615
3072
|
? health.clients === null
|
|
3616
3073
|
? '反向 WebSocket 模式:端口是反向 WS 服务在听(HTTP 问不出在线),客户端数还查不到——按在线处理,不会重启。'
|
|
@@ -3628,7 +3085,9 @@ function registerTools(ctx, cfg) {
|
|
|
3628
3085
|
: qr && qr.ageSec <= cfg.qrFreshSec
|
|
3629
3086
|
? passwordActive(cfg)
|
|
3630
3087
|
? '离线,且在等扫码——但「记住密码」是开着的:看一眼日志里“快速登录错误”后面是不是仍回退到二维码(密码登录可能被腾讯要求验证)。'
|
|
3631
|
-
: '
|
|
3088
|
+
: '离线,且二维码是新的:需要手机扫码 —— 打开 ' +
|
|
3089
|
+
(qrPanelPublic(cfg).url || qrPanelPublic(cfg).path) +
|
|
3090
|
+
' 就是一张会自己刷新的码(设置 → napcat桥 顶上那条黄横幅也能点开)。或者打开「记住密码」让 NapCat 自己用密码登录。图片文件在 qrCopyPath。'
|
|
3632
3091
|
: '离线。看门狗会自动拉起;若 procs.dup 为 true 说明被重复拉起过,已自动去重。',
|
|
3633
3092
|
},
|
|
3634
3093
|
null,
|
|
@@ -3730,6 +3189,7 @@ function stop(cfg) {
|
|
|
3730
3189
|
state.timer = null
|
|
3731
3190
|
log(cfg, 'watchdog stopped')
|
|
3732
3191
|
}
|
|
3192
|
+
stopQrPanel(cfg, '插件停了')
|
|
3733
3193
|
}
|
|
3734
3194
|
|
|
3735
3195
|
function armTimer(cfg) {
|
|
@@ -3755,6 +3215,406 @@ function armTimer(cfg) {
|
|
|
3755
3215
|
|
|
3756
3216
|
const ROUTE = '/dsh-napcat-keeper'
|
|
3757
3217
|
|
|
3218
|
+
/** 扫码页地址(相对 DSH 那个地址):NapCat 要手机扫码时,用户点这个就能看见会自己刷新的二维码。 */
|
|
3219
|
+
const QR_PANEL_PATH = `${ROUTE}/qr`
|
|
3220
|
+
|
|
3221
|
+
/** NapCat 的码大约 2 分钟一换;页面按这个算「这张码还剩几秒」。 */
|
|
3222
|
+
const QR_TTL_SEC = 120
|
|
3223
|
+
|
|
3224
|
+
/**
|
|
3225
|
+
* 扫码页(纯字符串内嵌,不带任何静态文件)。
|
|
3226
|
+
* 图 / 状态 / 重拉 全走本插件自己的 /dsh-napcat-keeper/* 路由,所以不用另起端口,
|
|
3227
|
+
* DSH 换端口时这一页跟着换,链接永远有效。
|
|
3228
|
+
* 页面里用的都是相对路径(qr.png / qr-state / qr-restart)—— 挂在 /dsh-napcat-keeper/qr 下正好。
|
|
3229
|
+
*/
|
|
3230
|
+
const QR_PAGE_HTML = String.raw`<!doctype html>
|
|
3231
|
+
<html lang="zh-CN"><head><meta charset="utf-8">
|
|
3232
|
+
<title>NapCat 扫码登录</title>
|
|
3233
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
3234
|
+
<style>
|
|
3235
|
+
:root { --bg:#0f1420; --card:#161d2c; --fg:#e8eefc; --dim:#8b98b4; --ok:#41d392; --warn:#f5c451; --bad:#f0705f; }
|
|
3236
|
+
* { box-sizing: border-box; }
|
|
3237
|
+
body { margin:0; min-height:100vh; background:radial-gradient(1200px 600px at 50% -10%, #1b2740, var(--bg)); color:var(--fg);
|
|
3238
|
+
font:15px/1.6 "Microsoft YaHei UI", system-ui, sans-serif; display:flex; align-items:center; justify-content:center; padding:24px; }
|
|
3239
|
+
.card { width:min(560px,100%); background:var(--card); border:1px solid #24304a; border-radius:18px; padding:22px 22px 18px;
|
|
3240
|
+
box-shadow:0 20px 60px rgba(0,0,0,.45); }
|
|
3241
|
+
h1 { margin:0 0 2px; font-size:19px; font-weight:600; letter-spacing:.5px; }
|
|
3242
|
+
.sub { color:var(--dim); font-size:13px; margin-bottom:16px; }
|
|
3243
|
+
.qrwrap { position:relative; background:#fff; border-radius:14px; padding:14px; display:flex; justify-content:center; min-height:180px; }
|
|
3244
|
+
.qrwrap img { width:100%; max-width:340px; image-rendering:pixelated; display:block; }
|
|
3245
|
+
.overlay { position:absolute; inset:0; display:none; align-items:center; justify-content:center; border-radius:14px;
|
|
3246
|
+
background:rgba(15,20,32,.9); color:var(--fg); font-size:15px; text-align:center; padding:20px; }
|
|
3247
|
+
.overlay.show { display:flex; }
|
|
3248
|
+
.rows { margin:16px 0 4px; }
|
|
3249
|
+
.row { display:flex; justify-content:space-between; gap:12px; padding:6px 0; border-bottom:1px dashed #232e46; font-size:14px; }
|
|
3250
|
+
.row:last-child { border-bottom:none; }
|
|
3251
|
+
.k { color:var(--dim); }
|
|
3252
|
+
.v { font-variant-numeric:tabular-nums; text-align:right; word-break:break-all; }
|
|
3253
|
+
.dot { display:inline-block; width:8px; height:8px; border-radius:50%; margin-right:6px; vertical-align:1px; background:var(--dim); }
|
|
3254
|
+
.ok { color:var(--ok); } .warn { color:var(--warn); } .bad { color:var(--bad); }
|
|
3255
|
+
.ok .dot { background:var(--ok); } .warn .dot { background:var(--warn); } .bad .dot { background:var(--bad); }
|
|
3256
|
+
.bar { display:flex; gap:10px; margin-top:14px; flex-wrap:wrap; }
|
|
3257
|
+
.smsbox { display:none; margin-top:14px; padding:10px 12px; border-radius:10px;
|
|
3258
|
+
border:1px solid #b8860b; background:rgba(184,134,11,.12); }
|
|
3259
|
+
.smsbox.show { display:block; }
|
|
3260
|
+
.smstitle { color:var(--warn); font-size:13px; margin-bottom:4px; }
|
|
3261
|
+
.smlink { display:inline-block; margin-top:8px; padding:7px 12px; border-radius:8px;
|
|
3262
|
+
background:#2a3550; color:#cfe0ff; text-decoration:none; font-size:13px; }
|
|
3263
|
+
button, a.btn { flex:0 0 auto; cursor:pointer; border:1px solid #2d3b58; background:#1d2740; color:var(--fg);
|
|
3264
|
+
border-radius:10px; padding:9px 14px; font-size:14px; text-decoration:none; }
|
|
3265
|
+
button:hover, a.btn:hover { background:#243254; }
|
|
3266
|
+
button.primary { background:#2b6cf6; border-color:#2b6cf6; }
|
|
3267
|
+
button.primary:hover { background:#3a78ff; }
|
|
3268
|
+
.hint { color:var(--dim); font-size:12.5px; margin-top:10px; }
|
|
3269
|
+
</style></head>
|
|
3270
|
+
<body>
|
|
3271
|
+
<div class="card">
|
|
3272
|
+
<h1>NapCat 扫码登录</h1>
|
|
3273
|
+
<div class="sub">二维码每 2 秒自动换新的(NapCat 换码会跟着换,不用手动刷新页面)</div>
|
|
3274
|
+
|
|
3275
|
+
<div class="qrwrap">
|
|
3276
|
+
<img id="qr" alt="二维码还没出来" src="qr.png?t=0">
|
|
3277
|
+
<div class="overlay" id="ov"><div id="ovtext">等 NapCat 出码…</div></div>
|
|
3278
|
+
</div>
|
|
3279
|
+
|
|
3280
|
+
<div class="rows">
|
|
3281
|
+
<div class="row"><span class="k">OneBot 接口</span><span class="v" id="v-api">—</span></div>
|
|
3282
|
+
<div class="row"><span class="k">登录状态</span><span class="v" id="v-login">—</span></div>
|
|
3283
|
+
<div class="row"><span class="k">二维码</span><span class="v" id="v-qr">—</span></div>
|
|
3284
|
+
<div class="row"><span class="k">这张码还剩</span><span class="v" id="v-ttl">—</span></div>
|
|
3285
|
+
<div class="row"><span class="k">看门狗阶段</span><span class="v" id="v-phase">—</span></div>
|
|
3286
|
+
<div class="row"><span class="k">扫码重试</span><span class="v" id="v-retry">—</span></div>
|
|
3287
|
+
<div class="row"><span class="k">最近日志</span><span class="v" id="v-log">—</span></div>
|
|
3288
|
+
</div>
|
|
3289
|
+
|
|
3290
|
+
<div class="smsbox" id="smsbox">
|
|
3291
|
+
<div class="smstitle" id="sms-title">⚠ 这次登录要短信验证</div>
|
|
3292
|
+
<div class="hint">NapCat 的日志里给了验证链接,点开在同一台机器上完成就行。</div>
|
|
3293
|
+
<a class="smlink" id="sms-link" href="#" target="_blank" rel="noreferrer">打开验证页面</a>
|
|
3294
|
+
</div>
|
|
3295
|
+
|
|
3296
|
+
<div class="bar">
|
|
3297
|
+
<button class="primary" id="btn-restart">我要新码(重拉 NapCat)</button>
|
|
3298
|
+
<button id="btn-tick">立即刷新</button>
|
|
3299
|
+
</div>
|
|
3300
|
+
<div class="hint" id="hint">页面开着就行:扫码成功后这里会变绿。</div>
|
|
3301
|
+
</div>
|
|
3302
|
+
|
|
3303
|
+
<script>
|
|
3304
|
+
let restartedAt = 0;
|
|
3305
|
+
const $ = (id) => document.getElementById(id);
|
|
3306
|
+
const PHASES = { idle:'待机', online:'在线', launching:'正在拉起', starting:'正在启动',
|
|
3307
|
+
'need-scan':'在等你扫码', 'need-scan-giveup':'扫码放弃(重试到上限了)', 'reverse-wait':'等反向 WS 连进来',
|
|
3308
|
+
'reverse-giveup':'反向 WS 等不到客户端', 'need-token':'令牌不对', 'port-busy':'端口被占', offline:'离线' };
|
|
3309
|
+
|
|
3310
|
+
function fmt(sec) {
|
|
3311
|
+
if (sec === null || sec === undefined) return '—';
|
|
3312
|
+
if (sec < 60) return sec + ' 秒前';
|
|
3313
|
+
return Math.floor(sec / 60) + ' 分 ' + (sec % 60) + ' 秒前';
|
|
3314
|
+
}
|
|
3315
|
+
|
|
3316
|
+
async function tick() {
|
|
3317
|
+
$('qr').src = 'qr.png?t=' + Date.now();
|
|
3318
|
+
let s;
|
|
3319
|
+
try { s = await (await fetch('qr-state?t=' + Date.now(), { cache:'no-store' })).json() }
|
|
3320
|
+
catch (e) { $('hint').textContent = '接口没响应:' + e; return }
|
|
3321
|
+
|
|
3322
|
+
restartedAt = s.restartedAt || 0;
|
|
3323
|
+
const qr = s.qr || null;
|
|
3324
|
+
const age = qr ? qr.ageSec : null;
|
|
3325
|
+
const left = (age === null || age === undefined) ? null : Math.max(0, (s.qrTtlSec || 120) - age);
|
|
3326
|
+
const fresh = left !== null && left > 20;
|
|
3327
|
+
|
|
3328
|
+
$('v-api').className = 'v ' + (s.online ? 'ok' : 'warn');
|
|
3329
|
+
$('v-api').innerHTML = '<span class="dot"></span>' + (s.online ? '通(在线)' : '还没通') +
|
|
3330
|
+
(typeof s.portStatus === 'number' && s.portStatus ? ' · HTTP ' + s.portStatus : '');
|
|
3331
|
+
|
|
3332
|
+
$('v-qr').className = 'v ' + (age === null ? 'bad' : (fresh ? 'ok' : 'warn'));
|
|
3333
|
+
$('v-qr').innerHTML = '<span class="dot"></span>' + (age === null ? '还没有 qrcode.png' : '更新于 ' + fmt(age));
|
|
3334
|
+
|
|
3335
|
+
$('v-ttl').className = 'v ' + (left === null ? 'bad' : (left > 30 ? 'ok' : 'warn'));
|
|
3336
|
+
$('v-ttl').innerHTML = left === null ? '<span class="dot"></span>—'
|
|
3337
|
+
: '<span class="dot"></span>约 ' + left + ' 秒(到点自己换)';
|
|
3338
|
+
|
|
3339
|
+
$('v-phase').innerHTML = '<span class="dot"></span>' + (PHASES[s.phase] || s.phase || '—');
|
|
3340
|
+
$('v-retry').textContent = (s.scanRetries || 0) + ' 次';
|
|
3341
|
+
$('v-log').textContent = (s.logTail && s.logTail.length ? s.logTail[s.logTail.length - 1] : '—');
|
|
3342
|
+
|
|
3343
|
+
// 要短信验证的时候把腾讯那个验证链接直接摆出来(光有二维码扫不进去)。
|
|
3344
|
+
// 注意再叠一层 !s.online:老日志里的 ErrCode 会一直留着,在线时绝不能误报。
|
|
3345
|
+
const sms = s.sms || null;
|
|
3346
|
+
if (sms && sms.needSms && !s.online && (sms.url || sms.code)) {
|
|
3347
|
+
$('smsbox').classList.add('show');
|
|
3348
|
+
$('sms-title').textContent = sms.code ? ('⚠ 这次登录要短信验证(ErrCode ' + sms.code + ')') : '⚠ 这次登录要短信验证';
|
|
3349
|
+
const link = $('sms-link');
|
|
3350
|
+
if (sms.url) { link.href = sms.url; link.style.display = ''; }
|
|
3351
|
+
else { link.removeAttribute('href'); link.style.display = 'none'; }
|
|
3352
|
+
} else {
|
|
3353
|
+
$('smsbox').classList.remove('show');
|
|
3354
|
+
}
|
|
3355
|
+
|
|
3356
|
+
if (s.online) {
|
|
3357
|
+
$('v-login').className = 'v ok';
|
|
3358
|
+
$('v-login').innerHTML = '<span class="dot"></span>已登录 ' + (s.nickname || '') + '(' + (s.userId || '') + ')';
|
|
3359
|
+
$('ov').classList.remove('show');
|
|
3360
|
+
$('hint').textContent = '连上了,可以关掉这个页面了。';
|
|
3361
|
+
} else {
|
|
3362
|
+
$('v-login').className = 'v warn';
|
|
3363
|
+
$('v-login').innerHTML = '<span class="dot"></span>还没登录,等扫码';
|
|
3364
|
+
if (left !== null && left < 12) {
|
|
3365
|
+
$('ovtext').textContent = '这张码快过期了,等新的…';
|
|
3366
|
+
$('ov').classList.add('show');
|
|
3367
|
+
} else if (age === null) {
|
|
3368
|
+
$('ovtext').textContent = '等 NapCat 出码…';
|
|
3369
|
+
$('ov').classList.add('show');
|
|
3370
|
+
} else {
|
|
3371
|
+
$('ov').classList.remove('show');
|
|
3372
|
+
}
|
|
3373
|
+
$('hint').textContent = '手机 QQ →「我」→ 扫一扫。码约 2 分钟换一张,页面会自动跟着换。';
|
|
3374
|
+
}
|
|
3375
|
+
}
|
|
3376
|
+
|
|
3377
|
+
async function restart() {
|
|
3378
|
+
$('ovtext').textContent = '正在重拉 NapCat…';
|
|
3379
|
+
$('ov').classList.add('show');
|
|
3380
|
+
try { await fetch('qr-restart', { method: 'POST' }) } catch (e) {}
|
|
3381
|
+
setTimeout(() => { $('ov').classList.remove('show'); tick() }, 6000);
|
|
3382
|
+
setTimeout(tick, 14000);
|
|
3383
|
+
}
|
|
3384
|
+
|
|
3385
|
+
$('btn-restart').addEventListener('click', restart);
|
|
3386
|
+
$('btn-tick').addEventListener('click', tick);
|
|
3387
|
+
tick();
|
|
3388
|
+
setInterval(tick, 3000);
|
|
3389
|
+
</script>
|
|
3390
|
+
</body></html>`
|
|
3391
|
+
|
|
3392
|
+
/* --- 扫码页:一份处理,两个入口(DSH 路由 + 独立小端口) --------------- */
|
|
3393
|
+
|
|
3394
|
+
/**
|
|
3395
|
+
* 从 NapCat 自己的运行日志里抠「这次登录要不要短信验证」。
|
|
3396
|
+
* 它出码之后常常还跟一条 ErrCode + proofWaterUrl(腾讯的验证页),
|
|
3397
|
+
* 光有二维码扫不进去 —— 所以页面上得把这个链接直接摆出来。
|
|
3398
|
+
* 日志里混着 ANSI 码和 GBK 乱码,只按 ASCII 令牌匹配,读的时候用 latin1。
|
|
3399
|
+
*/
|
|
3400
|
+
function napcatRunLogInfo(cfg) {
|
|
3401
|
+
const dir = String((cfg && cfg.shellDir) || '').replace(/\\/g, '/')
|
|
3402
|
+
const files = []
|
|
3403
|
+
if (dir) {
|
|
3404
|
+
files.push(path.join(dir, '_run.log'))
|
|
3405
|
+
files.push(path.join(dir, 'napcat', '_run.log'))
|
|
3406
|
+
}
|
|
3407
|
+
files.push(path.join(keeperDir(), 'launch.log'))
|
|
3408
|
+
for (const file of files) {
|
|
3409
|
+
try {
|
|
3410
|
+
if (!existsSync(file)) continue
|
|
3411
|
+
const lines = readFileSync(file, 'latin1').split(/\r?\n/).filter(Boolean).slice(-200)
|
|
3412
|
+
const out = { file, code: null, text: '', url: '', success: false, needSms: false, at: '' }
|
|
3413
|
+
let codeAt = -1
|
|
3414
|
+
let okAt = -1
|
|
3415
|
+
let urlStamp = ''
|
|
3416
|
+
lines.forEach((line, i) => {
|
|
3417
|
+
const code = line.match(/ErrCode:\s*(\d+)/)
|
|
3418
|
+
if (code) {
|
|
3419
|
+
out.code = Number(code[1])
|
|
3420
|
+
codeAt = i
|
|
3421
|
+
out.text = line.replace(/\u001b\[[0-9;]*m/g, '').trim().slice(0, 240)
|
|
3422
|
+
}
|
|
3423
|
+
const cap = line.match(/proofWaterUrl:\s*(\S+)/)
|
|
3424
|
+
if (cap) {
|
|
3425
|
+
out.url = cap[1]
|
|
3426
|
+
urlStamp = (line.match(/^(\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})/) || [])[1] || ''
|
|
3427
|
+
}
|
|
3428
|
+
if (/Login Success|登录成功|ONLINE/i.test(line)) { out.success = true; okAt = i }
|
|
3429
|
+
const at = line.match(/^(\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})/)
|
|
3430
|
+
if (at) out.at = at[1]
|
|
3431
|
+
})
|
|
3432
|
+
// 老日志里的 ErrCode 会一直挂着;只有「这次登录还没成功」才算数
|
|
3433
|
+
out.needSms = out.code !== null && codeAt > okAt
|
|
3434
|
+
// 验证链接有时效(几分钟就废),太老的别拿给主人点
|
|
3435
|
+
if (out.url && urlStamp) {
|
|
3436
|
+
const t = new Date(`${new Date().getFullYear()}-${urlStamp.replace(' ', 'T')}`).getTime()
|
|
3437
|
+
if (!Number.isFinite(t) || Date.now() - t > 30 * 60 * 1000) {
|
|
3438
|
+
out.urlStale = true
|
|
3439
|
+
out.url = ''
|
|
3440
|
+
}
|
|
3441
|
+
}
|
|
3442
|
+
return out
|
|
3443
|
+
} catch {}
|
|
3444
|
+
}
|
|
3445
|
+
return { file: '', code: null, text: '', url: '', success: false, at: '' }
|
|
3446
|
+
}
|
|
3447
|
+
|
|
3448
|
+
/** 扫码页轮询用的状态(轻量档,不跑子进程) */
|
|
3449
|
+
async function qrPageState(cfg) {
|
|
3450
|
+
const payload = await uiState(cfg, { light: true })
|
|
3451
|
+
const rt = (payload && payload.runtime) || {}
|
|
3452
|
+
return {
|
|
3453
|
+
ok: true,
|
|
3454
|
+
online: Boolean(rt.online),
|
|
3455
|
+
phase: rt.phase || '',
|
|
3456
|
+
mode: rt.mode || '',
|
|
3457
|
+
portStatus: rt.portStatus || 0,
|
|
3458
|
+
nickname: rt.nickname || '',
|
|
3459
|
+
userId: rt.userId || 0,
|
|
3460
|
+
qr: rt.qr || null,
|
|
3461
|
+
qrTtlSec: QR_TTL_SEC,
|
|
3462
|
+
scanRetries: rt.scanRetries || 0,
|
|
3463
|
+
restartedAt: state.lastLaunchAt ? new Date(state.lastLaunchAt).getTime() : 0,
|
|
3464
|
+
logTail: Array.isArray(payload && payload.log) ? payload.log : [],
|
|
3465
|
+
sms: napcatRunLogInfo(cfg),
|
|
3466
|
+
panel: { port: qrPanel.port || cfg.qrPanelPort, enabled: cfg.qrPanelEnabled !== false },
|
|
3467
|
+
now: Date.now(),
|
|
3468
|
+
}
|
|
3469
|
+
}
|
|
3470
|
+
|
|
3471
|
+
/**
|
|
3472
|
+
* 扫码页的请求处理。路径先剥掉 DSH 那个前缀,于是:
|
|
3473
|
+
* /dsh-napcat-keeper/qr (DSH 路由)→ 页面
|
|
3474
|
+
* /qr / (独立端口) → 页面
|
|
3475
|
+
* 图片 / 状态 / 重拉同名共用。
|
|
3476
|
+
*/
|
|
3477
|
+
async function serveQrPage(req, res, cfg) {
|
|
3478
|
+
cfg = cfg || state.cfg
|
|
3479
|
+
let pathname = String((req && req.url) || '/')
|
|
3480
|
+
const qi = pathname.indexOf('?')
|
|
3481
|
+
if (qi >= 0) pathname = pathname.slice(0, qi)
|
|
3482
|
+
if (pathname.startsWith(ROUTE)) pathname = pathname.slice(ROUTE.length) || '/'
|
|
3483
|
+
if (!pathname) pathname = '/'
|
|
3484
|
+
|
|
3485
|
+
if (pathname === '/' || pathname === '/qr' || pathname === '/qr/') {
|
|
3486
|
+
res.statusCode = 200
|
|
3487
|
+
res.setHeader('content-type', 'text/html; charset=utf-8')
|
|
3488
|
+
res.setHeader('cache-control', 'no-store')
|
|
3489
|
+
res.end(QR_PAGE_HTML)
|
|
3490
|
+
return
|
|
3491
|
+
}
|
|
3492
|
+
|
|
3493
|
+
if (pathname === '/qr.png') {
|
|
3494
|
+
const info = qrInfo(cfg)
|
|
3495
|
+
if (!info || !info.file) {
|
|
3496
|
+
sendJson(res, 404, { ok: false, error: '还没有二维码(NapCat 没进扫码态,或者还没出码)' })
|
|
3497
|
+
return
|
|
3498
|
+
}
|
|
3499
|
+
let buf
|
|
3500
|
+
try {
|
|
3501
|
+
buf = readFileSync(info.file)
|
|
3502
|
+
} catch (error) {
|
|
3503
|
+
sendJson(res, 500, { ok: false, error: String((error && error.message) || error) })
|
|
3504
|
+
return
|
|
3505
|
+
}
|
|
3506
|
+
res.statusCode = 200
|
|
3507
|
+
res.setHeader('content-type', 'image/png')
|
|
3508
|
+
res.setHeader('cache-control', 'no-store')
|
|
3509
|
+
res.setHeader('content-length', String(buf.length))
|
|
3510
|
+
res.end(buf)
|
|
3511
|
+
return
|
|
3512
|
+
}
|
|
3513
|
+
|
|
3514
|
+
if (pathname === '/qr-state') {
|
|
3515
|
+
sendJson(res, 200, await qrPageState(cfg))
|
|
3516
|
+
return
|
|
3517
|
+
}
|
|
3518
|
+
|
|
3519
|
+
if (pathname === '/qr-restart') {
|
|
3520
|
+
const ctx = state.ctx || null
|
|
3521
|
+
const result = ctx ? await doAction(ctx, cfg, 'restart', {}) : { said: '插件还没准备好,稍等一下再点' }
|
|
3522
|
+
sendJson(res, 200, { ok: true, said: (result && result.said) || '已让它重拉,新码大概十几秒后出来' })
|
|
3523
|
+
return
|
|
3524
|
+
}
|
|
3525
|
+
|
|
3526
|
+
sendJson(res, 404, { ok: false, error: '不认识的路径:' + pathname })
|
|
3527
|
+
}
|
|
3528
|
+
|
|
3529
|
+
/** 扫码页的独立小服务状态(跟着 globalThis 走:热重载时旧 server 会被关掉,不会越攒越多) */
|
|
3530
|
+
const QRPANEL_KEY = Symbol.for('dsh.napcat-keeper.qrpanel')
|
|
3531
|
+
const qrPanel =
|
|
3532
|
+
globalThis[QRPANEL_KEY] ||
|
|
3533
|
+
(globalThis[QRPANEL_KEY] = { server: null, port: 0, error: '', since: 0, note: '', serve: null })
|
|
3534
|
+
|
|
3535
|
+
/** 扫码页地址(给用户点的那个) */
|
|
3536
|
+
function qrPanelUrl(cfg) {
|
|
3537
|
+
const c = cfg || state.cfg || {}
|
|
3538
|
+
return c.qrPanelEnabled === false ? '' : `http://127.0.0.1:${c.qrPanelPort || 8899}/`
|
|
3539
|
+
}
|
|
3540
|
+
|
|
3541
|
+
function stopQrPanel(cfg, why = '') {
|
|
3542
|
+
const s = qrPanel.server
|
|
3543
|
+
qrPanel.server = null
|
|
3544
|
+
qrPanel.port = 0
|
|
3545
|
+
qrPanel.serve = null
|
|
3546
|
+
if (s) {
|
|
3547
|
+
try {
|
|
3548
|
+
s.close()
|
|
3549
|
+
} catch {}
|
|
3550
|
+
log(cfg || state.cfg, `扫码页已关${why ? '(' + why + ')' : ''}`)
|
|
3551
|
+
}
|
|
3552
|
+
}
|
|
3553
|
+
|
|
3554
|
+
/** 按配置把扫码页起起来;端口没变且已经在跑就什么都不做 */
|
|
3555
|
+
function startQrPanel(cfg) {
|
|
3556
|
+
if (!cfg || cfg.qrPanelEnabled === false) {
|
|
3557
|
+
stopQrPanel(cfg, '配置里关了')
|
|
3558
|
+
qrPanel.note = '关着(设置里可以打开)'
|
|
3559
|
+
return { ok: false, port: 0, note: qrPanel.note }
|
|
3560
|
+
}
|
|
3561
|
+
const port = Number(cfg.qrPanelPort) || 8899
|
|
3562
|
+
// ⚠ 热重载时旧 server 的闭包还指着**旧的** serveQrPage / QR_PAGE_HTML,
|
|
3563
|
+
// 光看「端口一样、还在听」就复用的话,页面永远是上一版。所以比函数身份:换模块了就重建。
|
|
3564
|
+
if (qrPanel.server && qrPanel.port === port && qrPanel.server.listening && qrPanel.serve === serveQrPage) {
|
|
3565
|
+
return { ok: true, port, note: qrPanel.note }
|
|
3566
|
+
}
|
|
3567
|
+
stopQrPanel(cfg, qrPanel.server ? '热重载换新代码' : '重启')
|
|
3568
|
+
try {
|
|
3569
|
+
const server = http.createServer((req, res) => {
|
|
3570
|
+
serveQrPage(req, res, state.cfg).catch((error) => {
|
|
3571
|
+
try {
|
|
3572
|
+
sendJson(res, 500, { ok: false, error: String((error && error.message) || error) })
|
|
3573
|
+
} catch {}
|
|
3574
|
+
})
|
|
3575
|
+
})
|
|
3576
|
+
server.on('error', (error) => {
|
|
3577
|
+
qrPanel.error = String((error && error.message) || error)
|
|
3578
|
+
qrPanel.note = /EADDRINUSE/.test(qrPanel.error)
|
|
3579
|
+
? `端口 ${port} 被占了(改一个 qrPanelPort,或关掉占它的程序)`
|
|
3580
|
+
: '起不来:' + qrPanel.error
|
|
3581
|
+
log(state.cfg, '扫码页启动失败:' + qrPanel.note)
|
|
3582
|
+
qrPanel.server = null
|
|
3583
|
+
qrPanel.port = 0
|
|
3584
|
+
})
|
|
3585
|
+
server.listen(port, '127.0.0.1', () => {
|
|
3586
|
+
qrPanel.server = server
|
|
3587
|
+
qrPanel.port = port
|
|
3588
|
+
qrPanel.serve = serveQrPage
|
|
3589
|
+
qrPanel.error = ''
|
|
3590
|
+
qrPanel.since = Date.now()
|
|
3591
|
+
qrPanel.note = ''
|
|
3592
|
+
log(state.cfg, `扫码页已起:http://127.0.0.1:${port}/(要扫码时点开它)`)
|
|
3593
|
+
})
|
|
3594
|
+
server.unref?.()
|
|
3595
|
+
return { ok: true, port, note: '正在起' }
|
|
3596
|
+
} catch (error) {
|
|
3597
|
+
qrPanel.error = String((error && error.message) || error)
|
|
3598
|
+
qrPanel.note = '起不来:' + qrPanel.error
|
|
3599
|
+
log(state.cfg, '扫码页启动失败:' + qrPanel.note)
|
|
3600
|
+
return { ok: false, port: 0, note: qrPanel.note }
|
|
3601
|
+
}
|
|
3602
|
+
}
|
|
3603
|
+
|
|
3604
|
+
function qrPanelPublic(cfg) {
|
|
3605
|
+
const port = qrPanel.port || (cfg && cfg.qrPanelPort) || 8899
|
|
3606
|
+
const running = Boolean(qrPanel.server && qrPanel.server.listening)
|
|
3607
|
+
return {
|
|
3608
|
+
enabled: !(cfg && cfg.qrPanelEnabled === false),
|
|
3609
|
+
port,
|
|
3610
|
+
running,
|
|
3611
|
+
url: running ? `http://127.0.0.1:${port}/` : '',
|
|
3612
|
+
path: QR_PANEL_PATH,
|
|
3613
|
+
note: qrPanel.note || (running ? '' : '没在跑'),
|
|
3614
|
+
error: qrPanel.error || '',
|
|
3615
|
+
}
|
|
3616
|
+
}
|
|
3617
|
+
|
|
3758
3618
|
function readBody(req, limit = 262144) {
|
|
3759
3619
|
return new Promise((resolve) => {
|
|
3760
3620
|
let size = 0
|
|
@@ -3876,8 +3736,10 @@ const uiCache = {
|
|
|
3876
3736
|
bootMain: undefined,
|
|
3877
3737
|
bootMainKey: '',
|
|
3878
3738
|
bootMainAt: 0,
|
|
3879
|
-
|
|
3880
|
-
|
|
3739
|
+
/** procsLayout 的目录判形缓存(node 型 / 快登壳型) */
|
|
3740
|
+
layout: 'unknown',
|
|
3741
|
+
layoutKey: '',
|
|
3742
|
+
layoutAt: 0,
|
|
3881
3743
|
qr: null,
|
|
3882
3744
|
qrAt: 0,
|
|
3883
3745
|
log: null,
|
|
@@ -3995,7 +3857,7 @@ async function uiState(cfg, options = {}) {
|
|
|
3995
3857
|
/** 设置页「跟随 dsh-onebot-qq」那一行显不显示,就看它(装了且挂进 profile) */
|
|
3996
3858
|
companionReady: Boolean(cfg.companionReady),
|
|
3997
3859
|
companionTokenMatch: health.companionTokenMatch === undefined ? null : health.companionTokenMatch,
|
|
3998
|
-
/** dsh-qq-
|
|
3860
|
+
/** dsh-qq-link 的共存情况(键名避开下面表示心跳新鲜度的 bridge) */
|
|
3999
3861
|
bridgePlugin: health.bridge || bridgePublic(detectBridge()),
|
|
4000
3862
|
clients: health.clients,
|
|
4001
3863
|
hint: health.hint || '',
|
|
@@ -4021,24 +3883,11 @@ async function uiState(cfg, options = {}) {
|
|
|
4021
3883
|
},
|
|
4022
3884
|
scan: scanPublic(),
|
|
4023
3885
|
drives: cachedDrives(),
|
|
4024
|
-
|
|
3886
|
+
qrPanel: qrPanelPublic(cfg),
|
|
4025
3887
|
log: cachedLog(cfg, 40),
|
|
4026
3888
|
}
|
|
4027
3889
|
}
|
|
4028
3890
|
|
|
4029
|
-
/** 提权交接那块的状态:light(轮询)时放宽缓存,免得每 2 秒去翻一次 NapCat 目录 */
|
|
4030
|
-
function cachedSetup(cfg, light) {
|
|
4031
|
-
const maxAgeMs = light ? 10000 : 2000
|
|
4032
|
-
if (uiCache.setup && Date.now() - (uiCache.setupAt || 0) < maxAgeMs) return uiCache.setup
|
|
4033
|
-
try {
|
|
4034
|
-
uiCache.setup = setupState(cfg)
|
|
4035
|
-
} catch {
|
|
4036
|
-
uiCache.setup = uiCache.setup || null
|
|
4037
|
-
}
|
|
4038
|
-
uiCache.setupAt = Date.now()
|
|
4039
|
-
return uiCache.setup
|
|
4040
|
-
}
|
|
4041
|
-
|
|
4042
3891
|
/** 并发 /state 合并成一次:崩溃重挂载时不会叠加成一串请求。 */
|
|
4043
3892
|
function uiStateFull(cfg) {
|
|
4044
3893
|
if (uiCache.fullInflight) return uiCache.fullInflight
|
|
@@ -4125,6 +3974,12 @@ function saveAndApply(ctx, patch) {
|
|
|
4125
3974
|
}
|
|
4126
3975
|
}
|
|
4127
3976
|
armTimer(next)
|
|
3977
|
+
// 扫码页的开关/端口可能刚被改过,重新对一遍(端口没变时这里什么都不做)
|
|
3978
|
+
try {
|
|
3979
|
+
startQrPanel(next)
|
|
3980
|
+
} catch (error) {
|
|
3981
|
+
log(next, `扫码页重起失败:${String((error && error.message) || error)}`)
|
|
3982
|
+
}
|
|
4128
3983
|
if (next.enabled) {
|
|
4129
3984
|
tick(next).catch((error) => log(next, `tick after save error: ${String((error && error.message) || error)}`))
|
|
4130
3985
|
}
|
|
@@ -4195,53 +4050,6 @@ async function doAction(ctx, cfg, action, payload) {
|
|
|
4195
4050
|
: `密码已删,但任务重建失败:${made.error || ''}`,
|
|
4196
4051
|
}
|
|
4197
4052
|
}
|
|
4198
|
-
case 'makeSetup': {
|
|
4199
|
-
// 生成「提权交接」脚本:插件自己不动系统,把活写成脚本,由人点一下
|
|
4200
|
-
let wants = []
|
|
4201
|
-
if (payload && Array.isArray(payload.needs)) wants = payload.needs
|
|
4202
|
-
else if (payload && typeof payload.needs === 'string' && payload.needs.trim()) {
|
|
4203
|
-
wants = payload.needs.split(',').map((s) => s.trim()).filter(Boolean)
|
|
4204
|
-
}
|
|
4205
|
-
// 显式传了空数组 = 一条都没勾,别当成「全要」
|
|
4206
|
-
if (payload && Array.isArray(payload.needs) && payload.needs.length === 0) {
|
|
4207
|
-
return { said: '没勾任何要做的事 —— 先在「需要你点一下的脚本」里勾一条,再点生成' }
|
|
4208
|
-
}
|
|
4209
|
-
const made = writeSetupBundle(cfg, wants)
|
|
4210
|
-
uiCache.setup = null
|
|
4211
|
-
uiCache.setupAt = 0
|
|
4212
|
-
if (!made.ok) return { said: `没生成:${made.error}`, setup: made }
|
|
4213
|
-
if (payload && payload.open) openSetupFolder()
|
|
4214
|
-
const adminNote = made.needAdmin ? '(这批要点 UAC 的「是」)' : '(这几步不需要管理员,双击就跑)'
|
|
4215
|
-
return {
|
|
4216
|
-
said: `脚本写好了:${made.dir} —— 双击 apply-admin.cmd ${adminNote},跑完回来点「读结果」`,
|
|
4217
|
-
setup: made,
|
|
4218
|
-
}
|
|
4219
|
-
}
|
|
4220
|
-
case 'openSetup': {
|
|
4221
|
-
const r = openSetupFolder()
|
|
4222
|
-
uiCache.setup = null
|
|
4223
|
-
uiCache.setupAt = 0
|
|
4224
|
-
return { said: r.ok ? `已打开 ${r.dir}` : `打开失败:${r.error}`, opened: r }
|
|
4225
|
-
}
|
|
4226
|
-
case 'setupResult': {
|
|
4227
|
-
uiCache.setup = null
|
|
4228
|
-
uiCache.setupAt = 0
|
|
4229
|
-
const r = readSetupResult()
|
|
4230
|
-
if (!r) return { said: '还没跑过(目录里没有 result.json)' }
|
|
4231
|
-
const bad = r.results.filter((x) => !x.ok)
|
|
4232
|
-
const skipped = r.results.filter((x) => x.skipped)
|
|
4233
|
-
const head = r.mode === 'undo' ? '撤回结果' : '执行结果'
|
|
4234
|
-
return {
|
|
4235
|
-
said:
|
|
4236
|
-
`${head}:${r.results.length - bad.length}/${r.results.length} 步成` +
|
|
4237
|
-
(bad.length
|
|
4238
|
-
? `,没成:${bad.map((x) => `${x.label}(${x.message})`).join(';')}`
|
|
4239
|
-
: '') +
|
|
4240
|
-
(skipped.length ? `,跳过 ${skipped.length} 步` : '') +
|
|
4241
|
-
(r.elevated ? '|拿到管理员' : '|没用管理员'),
|
|
4242
|
-
setupRun: r,
|
|
4243
|
-
}
|
|
4244
|
-
}
|
|
4245
4053
|
case 'checkNode': {
|
|
4246
4054
|
// 清掉缓存再解析一次:用户刚装完 node / 刚改了路径时,想让结果立刻变
|
|
4247
4055
|
uiCache.node = null
|
|
@@ -4351,10 +4159,22 @@ function registerRoutes(ctx) {
|
|
|
4351
4159
|
const lines = match ? Number(match[1]) : 80
|
|
4352
4160
|
sendJson(res, 200, { ok: true, lines: logTail(state.cfg, lines) })
|
|
4353
4161
|
})
|
|
4162
|
+
|
|
4163
|
+
/*--- 扫码页 ---------------------------------------------------------
|
|
4164
|
+
* NapCat 要手机扫码的时候,用户点一个链接就能看见二维码(挑得到的那条都行):
|
|
4165
|
+
* /dsh-napcat-keeper/qr DSH 自己地址下的这一页(会自己刷新)
|
|
4166
|
+
* 127.0.0.1:qrPanelPort/ 同一个页面的独立小服务(默认 8899,任何浏览器都打得开)
|
|
4167
|
+
* 页面里的 qr.png / qr-state / qr-restart 两个入口共用同一份处理。
|
|
4168
|
+
*/
|
|
4169
|
+
route('/qr', async (req, res) => serveQrPage(req, res, state.cfg))
|
|
4170
|
+
route('/qr.png', async (req, res) => serveQrPage(req, res, state.cfg))
|
|
4171
|
+
route('/qr-state', async (req, res) => serveQrPage(req, res, state.cfg))
|
|
4172
|
+
route('/qr-restart', async (req, res) => serveQrPage(req, res, state.cfg))
|
|
4354
4173
|
}
|
|
4355
4174
|
|
|
4356
4175
|
export function apply(ctx, config) {
|
|
4357
4176
|
state.patchConfig = config || {}
|
|
4177
|
+
state.ctx = ctx
|
|
4358
4178
|
const cfg = resolveConfig(state.patchConfig)
|
|
4359
4179
|
state.cfg = cfg
|
|
4360
4180
|
const node = nodePublic(cfg)
|
|
@@ -4373,6 +4193,7 @@ export function apply(ctx, config) {
|
|
|
4373
4193
|
}
|
|
4374
4194
|
registerTools(ctx, cfg)
|
|
4375
4195
|
registerRoutes(ctx)
|
|
4196
|
+
startQrPanel(cfg)
|
|
4376
4197
|
armTimer(cfg)
|
|
4377
4198
|
if (typeof ctx?.on === 'function') {
|
|
4378
4199
|
try {
|
|
@@ -4422,7 +4243,7 @@ export const __internals = {
|
|
|
4422
4243
|
probeOnPort,
|
|
4423
4244
|
COMPANION_DEFAULTS,
|
|
4424
4245
|
COMPANION_PACKAGE,
|
|
4425
|
-
// 与 dsh-qq-
|
|
4246
|
+
// 与 dsh-qq-link 共存:它是「客户端」,配置摊在 ~/.dsh/qq-bridge-config.json
|
|
4426
4247
|
profileHits,
|
|
4427
4248
|
detectBridge,
|
|
4428
4249
|
bridgePublic,
|
|
@@ -4430,8 +4251,10 @@ export const __internals = {
|
|
|
4430
4251
|
readBridgeConfig,
|
|
4431
4252
|
urlPort,
|
|
4432
4253
|
BRIDGE_DEFAULTS,
|
|
4254
|
+
BRIDGE_PACKAGES,
|
|
4433
4255
|
BRIDGE_PACKAGE,
|
|
4434
4256
|
BRIDGE_ID,
|
|
4257
|
+
bridgeHits,
|
|
4435
4258
|
qrInfo,
|
|
4436
4259
|
bridgeAge,
|
|
4437
4260
|
launchNapcat,
|
|
@@ -4443,20 +4266,18 @@ export const __internals = {
|
|
|
4443
4266
|
removeEnvBat,
|
|
4444
4267
|
envBatPath,
|
|
4445
4268
|
md5Hex,
|
|
4446
|
-
SETUP_NEEDS,
|
|
4447
|
-
setupPaths,
|
|
4448
|
-
needCatalog,
|
|
4449
|
-
buildSetupSteps,
|
|
4450
|
-
writeSetupBundle,
|
|
4451
|
-
readSetupResult,
|
|
4452
|
-
readSetupPlan,
|
|
4453
|
-
setupState,
|
|
4454
|
-
openSetupFolder,
|
|
4455
|
-
applyPs1Source,
|
|
4456
|
-
dirWritable,
|
|
4457
4269
|
cachedBootMain,
|
|
4458
4270
|
procsLayout,
|
|
4459
4271
|
taskCommandLine,
|
|
4460
4272
|
taskCommandLinePure,
|
|
4273
|
+
// 扫码页(要扫码时给用户点的那个页面)
|
|
4274
|
+
qrPageState,
|
|
4275
|
+
serveQrPage,
|
|
4276
|
+
startQrPanel,
|
|
4277
|
+
stopQrPanel,
|
|
4278
|
+
qrPanelPublic,
|
|
4279
|
+
qrPanelUrl,
|
|
4280
|
+
QR_PANEL_PATH,
|
|
4281
|
+
QR_TTL_SEC,
|
|
4461
4282
|
state,
|
|
4462
4283
|
}
|