dsh-napcat-keeper 2.8.1 → 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 +34 -0
- package/README.md +33 -29
- package/lib/client.js +106 -108
- package/lib/index.js +535 -700
- package/package.json +4 -2
- package/scripts/selftest-layout.mjs +93 -0
- package/scripts/selftest-qrpanel.mjs +260 -0
- package/scripts/smoke-client.mjs +77 -54
- package/scripts/selftest-setup.mjs +0 -210
package/lib/index.js
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* dsh-napcat-keeper —— NapCat 看门狗 v2.
|
|
2
|
+
* dsh-napcat-keeper —— NapCat 看门狗 v2.9.1(可发布通用版)
|
|
3
3
|
*
|
|
4
4
|
* 解决的问题:DSH 是桌面应用,重启 DSH 时 NapCat(QQ 协议端) 不会跟着起来,
|
|
5
5
|
* 于是 dsh-qq-bridge 一直轮询一个死端口,你在 QQ 里说的话就石沉大海。
|
|
6
6
|
*
|
|
7
|
+
* v2.8.2:「进程」那行按安装形态说人话 + 清一处重复模板
|
|
8
|
+
* - node 型(无头 Shell)装法里 QQ 本体跑在 node.exe 里,机器上没有 QQ.exe 也没有快登壳,
|
|
9
|
+
* procs.boot 恒为 0 —— 老版设置页还照旧写「NapCatWinBootMain 0」等于骗人。新增
|
|
10
|
+
* procsLayout():先看谁在跑(boot>0 → boot 型,qq>0 → node 型),都没在跑才看 Shell
|
|
11
|
+
* 目录顶层(node.exe + index.js/napcat.mjs,或顶层 NapCatWinBootMain.exe);目录判形
|
|
12
|
+
* 按目录名缓存 60 秒,面板 1.5 秒问一次也不必每次 readdir;「进程」那行补 title 解释 0 的含义;
|
|
13
|
+
* - taskCommandLine 与 taskCommandLinePure 共用 taskCmdLineOf(),不再各抄一份模板;
|
|
14
|
+
* - 新增 selftest:layout(18 项)与 npm test(清单体检 + 隐私体检 + 全部自测一把跑完)。
|
|
15
|
+
*
|
|
7
16
|
* v2.8.1:认得出两个共存插件,端口不用自己抄
|
|
8
17
|
* - 读 dsh-onebot-qq 的配置(profile 的 package.json、cordis.patch.yml / cordis.yml),
|
|
9
18
|
* 得出「装没装 / 挂没挂 / 反向还是正向 / 监听哪个端口 / 令牌配没配」;
|
|
@@ -49,7 +58,8 @@
|
|
|
49
58
|
* 和 DSH 自己的文件混在一起,一眼看不出哪些是插件产生的;
|
|
50
59
|
* - 现在统一落在 ~/.dsh/dsh-napcat-keeper/ 一个目录里,文件名去掉重复前缀:
|
|
51
60
|
* config.json / keeper.log / launch.log / state.json / lock.json / qr.png /
|
|
52
|
-
* env.bat / launch.vbs / autogen.bat / autogen-node.bat
|
|
61
|
+
* env.bat / launch.vbs / autogen.bat / autogen-node.bat;
|
|
62
|
+
* (2.9.1 起不再有 setup/ 那个「提权交接」脚本目录)
|
|
53
63
|
* - 老版本升级时 migrateLegacyFiles() 自动搬一次(能 rename 就 rename),
|
|
54
64
|
* 遗留的 *.bak、旧备份目录挪进 legacy/ 留档;计划任务里写死的旧路径由
|
|
55
65
|
* repairTaskActions() 用 /change 改指新位置,不 /end、不打扰正在跑的 QQ;
|
|
@@ -95,13 +105,12 @@
|
|
|
95
105
|
* - 改本插件任何 json/js 请用 write/edit 工具或 [IO.File]::WriteAllText + UTF8Encoding($false);
|
|
96
106
|
*
|
|
97
107
|
* v2.3.0:提权交接 —— 需要管理员的活,交给「你点一下」的脚本
|
|
98
|
-
* -
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
* - 能做的事(needs):补启动脚本 / 写「记住密码」中间层 / 建计划任务 / 放行 OneBot 入站端口。
|
|
108
|
+
* - 2.9.1 已整块删除:这套「生成 plan.json + apply-admin.cmd 让人去双击」的机制是多余的。
|
|
109
|
+
* 它本来要做的几件事,插件自己早就能直接做完,且全都不需要管理员:
|
|
110
|
+
* 写 env.bat(writeEnvBat)/ 建计划任务(ensureTask,schtasks 用户级)/
|
|
111
|
+
* 补启动脚本(autogen.bat、autogen-node.bat 写在插件自己目录里)。
|
|
112
|
+
* 唯一真要管理员的是给 OneBot 端口开防火墙入站 —— 那一步也一并去掉,
|
|
113
|
+
* 需要放行时按 README「端口放行」那一节的 netsh 命令自己跑一次即可。
|
|
105
114
|
*
|
|
106
115
|
* v2.2.2:目录只认「人选」
|
|
107
116
|
* - 去掉「自动探测 NapCat Shell 目录」:不再替用户猜位置、也不再在配置里悄悄填,
|
|
@@ -162,7 +171,7 @@ import { homedir, tmpdir } from 'node:os'
|
|
|
162
171
|
import path from 'node:path'
|
|
163
172
|
|
|
164
173
|
export const name = 'napcat-keeper'
|
|
165
|
-
export const version = '2.
|
|
174
|
+
export const version = '2.9.1'
|
|
166
175
|
|
|
167
176
|
/** tools 用来注册 napcat_* 工具;webServer 用来挂设置界面接口。拿不到也不至于把 DSH 拖崩。 */
|
|
168
177
|
export const inject = ['tools', 'webServer']
|
|
@@ -251,6 +260,13 @@ const DEFAULTS = {
|
|
|
251
260
|
statePath: '',
|
|
252
261
|
/** 二维码副本(方便 agent 或你自己直接看图) */
|
|
253
262
|
qrCopyPath: '',
|
|
263
|
+
/**
|
|
264
|
+
* 扫码页:NapCat 要手机扫码时,点开就能看见一张会自己刷新的二维码。
|
|
265
|
+
* 默认开;只监听 127.0.0.1,不对外。
|
|
266
|
+
*/
|
|
267
|
+
qrPanelEnabled: true,
|
|
268
|
+
/** 扫码页端口(127.0.0.1 上) */
|
|
269
|
+
qrPanelPort: 8899,
|
|
254
270
|
/** qq-bridge 的 state 文件 */
|
|
255
271
|
bridgeStatePath: '',
|
|
256
272
|
/** 桥接 state 超过这么多秒没更新就提醒 */
|
|
@@ -323,7 +339,6 @@ const LEGACY_FILES = [
|
|
|
323
339
|
['napcat-autogen-node.bat', 'autogen-node.bat'],
|
|
324
340
|
['napcat-autogen.bat', 'autogen.bat'],
|
|
325
341
|
]
|
|
326
|
-
const LEGACY_DIRS = [['napcat-keeper-setup', 'setup']]
|
|
327
342
|
/** 老版本留在 ~/.dsh 根上的东西(没搬走的、配置备份、bom-backup 之类):统一归档进 legacy/ */
|
|
328
343
|
const LEGACY_LEFTOVER = /^(napcat-keeper[.-]|napcat-qr\.|napcat-autogen)/
|
|
329
344
|
|
|
@@ -425,14 +440,8 @@ function migrateLegacyFiles(dir) {
|
|
|
425
440
|
}
|
|
426
441
|
if (moveOne(path.join(home, oldName), path.join(target, newName))) moved += 1
|
|
427
442
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
const from = path.join(home, oldName)
|
|
431
|
-
if (existsSync(from) && !existsSync(path.join(target, newName)) && moveOne(from, path.join(target, newName))) {
|
|
432
|
-
moved += 1
|
|
433
|
-
}
|
|
434
|
-
} catch {}
|
|
435
|
-
}
|
|
443
|
+
// 2.9.1 起没有需要改名搬家的目录了(原来的 setup/ 提权交接目录整块删掉),
|
|
444
|
+
// 只留下面的归档兜底。旧版本留下的 ~/.dsh/dsh-napcat-keeper/setup/ 可以手动删。
|
|
436
445
|
// 兜底:还留在根上的(目标位已有同名文件、或者正被别的进程占用搬不动)一律归档进 legacy/,
|
|
437
446
|
// 别让它们继续躺在 ~/.dsh 根上冒充「插件还在用老布局」。这一趟搬不动的,下次启动再来。
|
|
438
447
|
try {
|
|
@@ -780,6 +789,12 @@ function psPath(value) {
|
|
|
780
789
|
return String(value || '').replace(/\//g, '\\')
|
|
781
790
|
}
|
|
782
791
|
|
|
792
|
+
/**
|
|
793
|
+
* 所有内联 PowerShell 脚本的公共前缀:把「不需要的错误」压成静默。
|
|
794
|
+
* 三处脚本(数端口客户端 / 进程快照 / 杀晚出现的链)都要它,别再各抄一遍。
|
|
795
|
+
*/
|
|
796
|
+
const PS_QUIET = "$ErrorActionPreference='SilentlyContinue';"
|
|
797
|
+
|
|
783
798
|
/** 进程路径前缀(原样,给 PowerShell 的 -like 用) */
|
|
784
799
|
function procRootRaw(cfg) {
|
|
785
800
|
const explicit = String(cfg.procPathMatch || '').trim()
|
|
@@ -816,6 +831,8 @@ function normalizeConfig(cfg) {
|
|
|
816
831
|
cfg.probeIntervalSec = clampNum(cfg.probeIntervalSec, 10, 3600, 45)
|
|
817
832
|
cfg.launchGraceSec = clampNum(cfg.launchGraceSec, 20, 3600, 150)
|
|
818
833
|
cfg.qrFreshSec = clampNum(cfg.qrFreshSec, 30, 3600, 300)
|
|
834
|
+
cfg.qrPanelEnabled = cfg.qrPanelEnabled !== false && cfg.qrPanelEnabled !== 'false' && cfg.qrPanelEnabled !== 0
|
|
835
|
+
cfg.qrPanelPort = clampNum(cfg.qrPanelPort, 1024, 65535, 8899)
|
|
819
836
|
cfg.scanTimeoutSec = clampNum(cfg.scanTimeoutSec, 30, 7200, 300)
|
|
820
837
|
cfg.maxScanRetries = clampNum(cfg.maxScanRetries, 0, 20, 2)
|
|
821
838
|
cfg.stuckSec = clampNum(cfg.stuckSec, 60, 7200, 420)
|
|
@@ -1417,7 +1434,7 @@ async function portClients(cfg, port = 0, maxAgeMs = 5000) {
|
|
|
1417
1434
|
// 按 LocalPort 找会命中服务端那条 socket,而它的所有者是跑反向 WS 服务的进程(就是 DSH 自己),
|
|
1418
1435
|
// 把它排除掉就一条都数不到了 —— 这个坑第一版就踩了。
|
|
1419
1436
|
const script =
|
|
1420
|
-
|
|
1437
|
+
PS_QUIET +
|
|
1421
1438
|
`$c=@(Get-NetTCPConnection -State Established -RemotePort ${p} -ErrorAction SilentlyContinue | ` +
|
|
1422
1439
|
`Where-Object { [string]$_.OwningProcess -ne '${self}' });` +
|
|
1423
1440
|
`'CLIENTS=' + $c.Count + ' PIDS=' + (($c | Select-Object -ExpandProperty OwningProcess -Unique) -join ',')`
|
|
@@ -1624,7 +1641,7 @@ async function probe(cfg, options = {}) {
|
|
|
1624
1641
|
/* ------------------------------------------------------------------ */
|
|
1625
1642
|
|
|
1626
1643
|
const PS_PROCS =
|
|
1627
|
-
|
|
1644
|
+
PS_QUIET +
|
|
1628
1645
|
// node 也要算:无头版(Node 包)只有 node.exe 一条进程,没有 QQ.exe
|
|
1629
1646
|
"Get-Process QQ,NapCatWinBootMain,node | " +
|
|
1630
1647
|
'ForEach-Object { $p=$null; try { $p=$_.Path } catch {}; ' +
|
|
@@ -2141,17 +2158,21 @@ function removeEnvBat(cfg) {
|
|
|
2141
2158
|
} catch {}
|
|
2142
2159
|
}
|
|
2143
2160
|
|
|
2144
|
-
/**
|
|
2145
|
-
function
|
|
2146
|
-
// 记住密码时改走中间层(set 环境变量 → call 原脚本),其余情况原样直跑
|
|
2147
|
-
const viaEnv = passwordActive(cfg) ? writeEnvBat(cfg, bat) : ''
|
|
2148
|
-
const target = viaEnv || bat
|
|
2161
|
+
/** 命令行的公共模板:chcp 65001 + 切到 Shell 目录 + call 启动脚本,输出进 launcherLog */
|
|
2162
|
+
function taskCmdLineOf(cfg, target) {
|
|
2149
2163
|
return (
|
|
2150
2164
|
`cmd.exe /c chcp 65001 >nul & cd /d "${cfg.shellDir}" & call "${target}" ` +
|
|
2151
2165
|
`>> "${cfg.launcherLog}" 2>&1`
|
|
2152
2166
|
)
|
|
2153
2167
|
}
|
|
2154
2168
|
|
|
2169
|
+
/** 真正要跑的那条 cmd 命令 */
|
|
2170
|
+
function taskCommandLine(cfg, bat) {
|
|
2171
|
+
// 记住密码时改走中间层(set 环境变量 → call 原脚本),其余情况原样直跑
|
|
2172
|
+
const viaEnv = passwordActive(cfg) ? writeEnvBat(cfg, bat) : ''
|
|
2173
|
+
return taskCmdLineOf(cfg, viaEnv || bat)
|
|
2174
|
+
}
|
|
2175
|
+
|
|
2155
2176
|
/**
|
|
2156
2177
|
* 写一个 WScript 壳:sh.Run cmd, 0, False → 子进程以“隐藏窗口”启动,
|
|
2157
2178
|
* 桌面不会多出那个黑框(原来计划任务直接跑 cmd.exe 就会挂一个控制台窗口)。
|
|
@@ -2224,7 +2245,7 @@ function ensureTask(cfg) {
|
|
|
2224
2245
|
)
|
|
2225
2246
|
}
|
|
2226
2247
|
const ps =
|
|
2227
|
-
|
|
2248
|
+
PS_QUIET +
|
|
2228
2249
|
`$t=Get-ScheduledTask -TaskName '${cfg.taskName.replace(/'/g, "''")}';` +
|
|
2229
2250
|
`if($t){$s=$t.Settings;$s.ExecutionTimeLimit='PT0S';$s.DisallowStartIfOnBatteries=$false;` +
|
|
2230
2251
|
`$s.StopIfGoingOnBatteries=$false;Set-ScheduledTask -TaskName '${cfg.taskName.replace(/'/g, "''")}' -Settings $s | Out-Null}`
|
|
@@ -2277,78 +2298,6 @@ function repairTaskActions(cfg) {
|
|
|
2277
2298
|
}
|
|
2278
2299
|
}
|
|
2279
2300
|
|
|
2280
|
-
/* ── 提权交接:插件不自己去要管理员,改成给你一个双击就行的脚本 ──────────────
|
|
2281
|
-
*
|
|
2282
|
-
* 为什么要这样:DSH 自己是普通权限进程。插件偶尔要做几件「可能要管理员」的事
|
|
2283
|
-
* (往 Program Files 里的 NapCat 目录补启动脚本、建计划任务、给 OneBot 端口开防火墙),
|
|
2284
|
-
* 而插件既不该、也没法替人按下 UAC 里那个「是」。
|
|
2285
|
-
*
|
|
2286
|
-
* 所以改成这样:
|
|
2287
|
-
* 1. 插件把要做的每一步写进 plan.json(步骤由插件自己生成,来源可信);
|
|
2288
|
-
* 2. 生成 apply.ps1:先数一遍「这些步骤里有没有真需要管理员的」,
|
|
2289
|
-
* 没有就直接干;有才 Start-Process -Verb RunAs 自我提权 ——
|
|
2290
|
-
* UAC 弹窗就是用户亲手点的那一下确认;
|
|
2291
|
-
* 3. 生成 apply-admin.cmd:双击入口,省得用户去右键「以管理员身份运行」;
|
|
2292
|
-
* 4. 每步结果写 result.json,设置页读回来说清楚哪条成了、哪条没成;
|
|
2293
|
-
* 5. 另外给一个 undo-admin.cmd,反着把插件建过的东西删掉。
|
|
2294
|
-
*
|
|
2295
|
-
* 所有产物只在 ~/.dsh/dsh-napcat-keeper/setup 一个目录里,整个删掉就等于撤回。
|
|
2296
|
-
*/
|
|
2297
|
-
|
|
2298
|
-
const SETUP_NEEDS = {
|
|
2299
|
-
launcher: {
|
|
2300
|
-
label: '补一个启动脚本',
|
|
2301
|
-
hint: '往 Shell 目录写 napcat.quick.auto.bat,直接调 NapCatWinBootMain.exe(目录里没有别的 bat 时才需要)',
|
|
2302
|
-
},
|
|
2303
|
-
envbat: {
|
|
2304
|
-
label: '写「记住密码」中间层',
|
|
2305
|
-
hint: 'napcat-keeper-env.bat:账号与密码走环境变量交给 NapCat,密码只落这一个文件',
|
|
2306
|
-
},
|
|
2307
|
-
task: {
|
|
2308
|
-
label: '建 / 重建计划任务',
|
|
2309
|
-
hint: '开机不靠 DSH 也能拉起 QQ;用户级任务,通常不需要管理员',
|
|
2310
|
-
},
|
|
2311
|
-
firewall: {
|
|
2312
|
-
label: '放行 OneBot 端口(入站)',
|
|
2313
|
-
hint: '别的设备要连这个端口时才需要;这条一定会弹 UAC',
|
|
2314
|
-
},
|
|
2315
|
-
}
|
|
2316
|
-
|
|
2317
|
-
function setupPaths() {
|
|
2318
|
-
const dir = path.join(keeperDir(), 'setup')
|
|
2319
|
-
migrateLegacyFiles()
|
|
2320
|
-
return {
|
|
2321
|
-
dir,
|
|
2322
|
-
ps1: path.join(dir, 'apply.ps1'),
|
|
2323
|
-
cmd: path.join(dir, 'apply-admin.cmd'),
|
|
2324
|
-
undoCmd: path.join(dir, 'undo-admin.cmd'),
|
|
2325
|
-
plan: path.join(dir, 'plan.json'),
|
|
2326
|
-
result: path.join(dir, 'result.json'),
|
|
2327
|
-
runLog: path.join(dir, 'run.log'),
|
|
2328
|
-
readme: path.join(dir, 'README.txt'),
|
|
2329
|
-
}
|
|
2330
|
-
}
|
|
2331
|
-
|
|
2332
|
-
/** 目录能不能写:自己不存在就往上看,找一个存在的祖先试(免得把「没建过」当成「不可写」) */
|
|
2333
|
-
function dirWritable(dir) {
|
|
2334
|
-
if (!dir) return false
|
|
2335
|
-
let probe = dir
|
|
2336
|
-
for (let i = 0; i < 8; i += 1) {
|
|
2337
|
-
if (existsSync(probe)) {
|
|
2338
|
-
try {
|
|
2339
|
-
accessSync(probe, fsConstants.W_OK)
|
|
2340
|
-
return true
|
|
2341
|
-
} catch {
|
|
2342
|
-
return false
|
|
2343
|
-
}
|
|
2344
|
-
}
|
|
2345
|
-
const up = path.dirname(probe)
|
|
2346
|
-
if (up === probe || !up) break
|
|
2347
|
-
probe = up
|
|
2348
|
-
}
|
|
2349
|
-
return false
|
|
2350
|
-
}
|
|
2351
|
-
|
|
2352
2301
|
/** findBootMain 会递归翻目录,设置页每次刷新都调太贵 —— 缓存 60 秒,换了 Shell 目录就重算 */
|
|
2353
2302
|
function cachedBootMain(cfg, maxAgeMs = 60000) {
|
|
2354
2303
|
const key = cfg.shellDir || ''
|
|
@@ -2374,540 +2323,46 @@ function cachedBootMain(cfg, maxAgeMs = 60000) {
|
|
|
2374
2323
|
return found
|
|
2375
2324
|
}
|
|
2376
2325
|
|
|
2377
|
-
/**
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
`cmd.exe /c chcp 65001 >nul & cd /d "${cfg.shellDir}" & call "${target}" ` +
|
|
2397
|
-
`>> "${cfg.launcherLog}" 2>&1`
|
|
2398
|
-
)
|
|
2399
|
-
}
|
|
2400
|
-
|
|
2401
|
-
/** 给设置页看的清单:每个需要能不能做、要不要管理员 */
|
|
2402
|
-
function needCatalog(cfg) {
|
|
2403
|
-
const boot = (() => {
|
|
2404
|
-
try {
|
|
2405
|
-
return cachedBootMain(cfg)
|
|
2406
|
-
} catch {
|
|
2407
|
-
return ''
|
|
2408
|
-
}
|
|
2409
|
-
})()
|
|
2410
|
-
const bat = resolveLauncherPath(cfg)
|
|
2411
|
-
return Object.entries(SETUP_NEEDS).map(([id, meta]) => {
|
|
2412
|
-
let ready = true
|
|
2413
|
-
let why = ''
|
|
2414
|
-
let needAdmin = false
|
|
2415
|
-
if (id === 'launcher') {
|
|
2416
|
-
ready = Boolean(boot)
|
|
2417
|
-
if (!boot) why = 'Shell 目录里没找到 NapCatWinBootMain.exe'
|
|
2418
|
-
needAdmin = Boolean(boot) && !dirWritable(cfg.shellDir)
|
|
2419
|
-
if (needAdmin) why = 'Shell 目录当前不可写(大概在 Program Files 里,要管理员)'
|
|
2420
|
-
} else if (id === 'envbat') {
|
|
2421
|
-
ready = passwordActive(cfg) && Boolean(bat)
|
|
2422
|
-
if (!passwordActive(cfg)) why = '先打开「记住密码」并填好账号'
|
|
2423
|
-
else if (!bat) why = '先在 Shell 目录里找到启动脚本'
|
|
2424
|
-
needAdmin = Boolean(ready) && !dirWritable(path.dirname(envBatPath(cfg)))
|
|
2425
|
-
} else if (id === 'task') {
|
|
2426
|
-
ready = Boolean(cfg.taskName) && Boolean(bat)
|
|
2427
|
-
if (!cfg.taskName) why = '先填计划任务名'
|
|
2428
|
-
else if (!bat) why = '先填 Shell 目录(找得到启动脚本才建任务)'
|
|
2429
|
-
} else if (id === 'firewall') {
|
|
2430
|
-
ready = Boolean(cfg.onebotPort)
|
|
2431
|
-
if (!ready) why = '先填 OneBot 端口'
|
|
2432
|
-
needAdmin = true
|
|
2433
|
-
}
|
|
2434
|
-
return { id, label: meta.label, hint: meta.hint, ready, why, needAdmin }
|
|
2435
|
-
})
|
|
2436
|
-
}
|
|
2437
|
-
|
|
2438
|
-
/** 把「要做的事」摊成步骤数组(plan.json 里的 steps) */
|
|
2439
|
-
function buildSetupSteps(cfg, wants) {
|
|
2440
|
-
const picked = Array.isArray(wants) && wants.length ? wants : Object.keys(SETUP_NEEDS)
|
|
2441
|
-
const want = new Set(picked.filter((id) => SETUP_NEEDS[id]))
|
|
2442
|
-
const steps = []
|
|
2443
|
-
const bat = resolveLauncherPath(cfg)
|
|
2444
|
-
|
|
2445
|
-
if (want.has('launcher')) {
|
|
2446
|
-
const boot = (() => {
|
|
2447
|
-
try {
|
|
2448
|
-
return cachedBootMain(cfg)
|
|
2449
|
-
} catch {
|
|
2450
|
-
return ''
|
|
2451
|
-
}
|
|
2452
|
-
})()
|
|
2453
|
-
if (boot) {
|
|
2454
|
-
const file = path.join(cfg.shellDir, 'napcat.quick.auto.bat')
|
|
2455
|
-
steps.push({
|
|
2456
|
-
id: 'launcher',
|
|
2457
|
-
label: '补启动脚本 napcat.quick.auto.bat',
|
|
2458
|
-
kind: 'writeFile',
|
|
2459
|
-
path: file,
|
|
2460
|
-
admin: !dirWritable(cfg.shellDir),
|
|
2461
|
-
note: '直接调 NapCatWinBootMain.exe,账号走环境变量',
|
|
2462
|
-
contentBase64: Buffer.from(launcherBatLines(cfg, boot).join('\r\n'), 'utf8').toString('base64'),
|
|
2463
|
-
})
|
|
2464
|
-
}
|
|
2465
|
-
}
|
|
2466
|
-
|
|
2467
|
-
if (want.has('envbat') && passwordActive(cfg) && bat) {
|
|
2468
|
-
const file = envBatPath(cfg)
|
|
2469
|
-
steps.push({
|
|
2470
|
-
id: 'envbat',
|
|
2471
|
-
label: '写「记住密码」中间层 napcat-keeper-env.bat',
|
|
2472
|
-
kind: 'writeFile',
|
|
2473
|
-
path: file,
|
|
2474
|
-
admin: !dirWritable(path.dirname(file)),
|
|
2475
|
-
note: '只在本机磁盘里,关掉「记住密码」或点「清除密码」就删',
|
|
2476
|
-
secret: true,
|
|
2477
|
-
contentBase64: Buffer.from(envBatLines(cfg, bat).join('\r\n'), 'utf8').toString('base64'),
|
|
2478
|
-
})
|
|
2479
|
-
}
|
|
2480
|
-
|
|
2481
|
-
if (want.has('task') && cfg.taskName && bat) {
|
|
2482
|
-
const requires = []
|
|
2483
|
-
let inner = ''
|
|
2484
|
-
if (cfg.hideConsole !== false) {
|
|
2485
|
-
const vbs = taskWrapperPath(cfg)
|
|
2486
|
-
const cmdLine = taskCommandLinePure(cfg, bat).replace(/"/g, '""')
|
|
2487
|
-
const body = [
|
|
2488
|
-
'Option Explicit',
|
|
2489
|
-
'Dim sh',
|
|
2490
|
-
'Set sh = CreateObject("WScript.Shell")',
|
|
2491
|
-
`sh.Run "${cmdLine}", 0, False`,
|
|
2492
|
-
'',
|
|
2493
|
-
].join('\r\n')
|
|
2494
|
-
steps.push({
|
|
2495
|
-
id: 'task-wrapper',
|
|
2496
|
-
label: '写无窗口壳 launch.vbs',
|
|
2497
|
-
kind: 'writeFile',
|
|
2498
|
-
path: vbs,
|
|
2499
|
-
admin: !dirWritable(path.dirname(vbs)),
|
|
2500
|
-
note: '计划任务跑它,桌面才不会挂一个黑框',
|
|
2501
|
-
contentBase64: Buffer.concat([Buffer.from([0xff, 0xfe]), Buffer.from(body, 'utf16le')]).toString(
|
|
2502
|
-
'base64',
|
|
2503
|
-
),
|
|
2504
|
-
})
|
|
2505
|
-
requires.push('task-wrapper')
|
|
2506
|
-
inner = `wscript.exe "${vbs}"`
|
|
2507
|
-
} else {
|
|
2508
|
-
inner = taskCommandLinePure(cfg, bat)
|
|
2509
|
-
}
|
|
2510
|
-
if (passwordActive(cfg)) requires.push('envbat')
|
|
2511
|
-
steps.push({
|
|
2512
|
-
id: 'task',
|
|
2513
|
-
label: `建计划任务「${cfg.taskName}」`,
|
|
2514
|
-
kind: 'scheduledTask',
|
|
2515
|
-
admin: false,
|
|
2516
|
-
note: '用户级任务,一般不需要管理员;系统策略挡着时脚本会把错误原样告诉你',
|
|
2517
|
-
requires,
|
|
2518
|
-
name: cfg.taskName,
|
|
2519
|
-
command: inner,
|
|
2520
|
-
})
|
|
2521
|
-
}
|
|
2522
|
-
|
|
2523
|
-
if (want.has('firewall') && cfg.onebotPort) {
|
|
2524
|
-
const port = Number(cfg.onebotPort) || 3000
|
|
2525
|
-
steps.push({
|
|
2526
|
-
id: 'firewall',
|
|
2527
|
-
label: `放行 TCP ${port} 入站`,
|
|
2528
|
-
kind: 'firewallRule',
|
|
2529
|
-
admin: true,
|
|
2530
|
-
note: '只有别的设备要连 OneBot 端口时才需要;这条一定会弹 UAC',
|
|
2531
|
-
name: `dsh-napcat-keeper OneBot ${port}`,
|
|
2532
|
-
port,
|
|
2533
|
-
})
|
|
2534
|
-
}
|
|
2535
|
-
return steps
|
|
2536
|
-
}
|
|
2537
|
-
|
|
2538
|
-
/** apply.ps1 的全文:这段是插件的固定产物,跟 plan.json 一起决定脚本会干什么 */
|
|
2539
|
-
function applyPs1Source() {
|
|
2540
|
-
return `# dsh-napcat-keeper 提权交接脚本(插件自动生成,改了也会被覆盖)
|
|
2541
|
-
# 双击同目录的 apply-admin.cmd 干活;想撤干净就双击 undo-admin.cmd。
|
|
2542
|
-
param([switch]$Elevated, [switch]$Undo)
|
|
2543
|
-
$ErrorActionPreference = 'Continue'
|
|
2544
|
-
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
2545
|
-
$planPath = Join-Path $here 'plan.json'
|
|
2546
|
-
$resultPath = Join-Path $here 'result.json'
|
|
2547
|
-
$runLog = Join-Path $here 'run.log'
|
|
2548
|
-
# run.log 只追加不滚动会长到没人看:超过 256KB 就换新的一份
|
|
2549
|
-
try {
|
|
2550
|
-
if ((Test-Path -LiteralPath $runLog) -and ((Get-Item -LiteralPath $runLog).Length -gt 262144)) {
|
|
2551
|
-
Remove-Item -LiteralPath $runLog -Force -ErrorAction SilentlyContinue
|
|
2552
|
-
}
|
|
2553
|
-
} catch {}
|
|
2554
|
-
|
|
2555
|
-
function Say($text) {
|
|
2556
|
-
$line = ('[{0}] {1}' -f (Get-Date -Format 'HH:mm:ss'), $text)
|
|
2557
|
-
Write-Host $line
|
|
2558
|
-
try { Add-Content -LiteralPath $runLog -Value $line -Encoding UTF8 } catch {}
|
|
2559
|
-
}
|
|
2560
|
-
|
|
2561
|
-
function Wait-Key {
|
|
2562
|
-
if ($env:NPK_NO_PAUSE -eq '1') { return }
|
|
2563
|
-
Write-Host ''
|
|
2564
|
-
Write-Host '按任意键关掉这个窗口。'
|
|
2565
|
-
try { $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') } catch {}
|
|
2566
|
-
}
|
|
2567
|
-
|
|
2568
|
-
if (-not (Test-Path -LiteralPath $planPath)) {
|
|
2569
|
-
Say 'plan.json 不在了 —— 回 DSH 设置页重新生成一次。'
|
|
2570
|
-
Wait-Key
|
|
2571
|
-
exit 2
|
|
2572
|
-
}
|
|
2573
|
-
|
|
2574
|
-
$plan = Get-Content -LiteralPath $planPath -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
2575
|
-
$steps = @($plan.steps)
|
|
2576
|
-
$mode = 'apply'
|
|
2577
|
-
if ($Undo) {
|
|
2578
|
-
$mode = 'undo'
|
|
2579
|
-
$rev = @()
|
|
2580
|
-
for ($i = $steps.Count - 1; $i -ge 0; $i--) { $rev += $steps[$i] }
|
|
2581
|
-
$steps = $rev
|
|
2582
|
-
}
|
|
2583
|
-
|
|
2584
|
-
Say ('开始(' + $mode + '):' + $steps.Count + ' 步')
|
|
2585
|
-
|
|
2586
|
-
$admin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
|
|
2587
|
-
$needAdmin = @($steps | Where-Object { $_.admin -eq $true }).Count -gt 0
|
|
2588
|
-
|
|
2589
|
-
if ((!$Undo) -and $needAdmin -and (-not $admin) -and (-not $Elevated)) {
|
|
2590
|
-
Say '这批步骤里有要管理员才行的,马上弹 UAC —— 请点「是」;点「否」就什么都不改。'
|
|
2591
|
-
$argList = @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', ('"' + $PSCommandPath + '"'), '-Elevated')
|
|
2592
|
-
try {
|
|
2593
|
-
Start-Process -FilePath 'powershell.exe' -Verb RunAs -ArgumentList $argList
|
|
2594
|
-
Say '已经把活交给提权那个窗口了,跑完看同目录的 result.json。'
|
|
2595
|
-
} catch {
|
|
2596
|
-
Say '没拿到管理员权限(UAC 被拒或这台机器不给提权),这次什么都没改。'
|
|
2597
|
-
}
|
|
2598
|
-
Wait-Key
|
|
2599
|
-
exit 0
|
|
2600
|
-
}
|
|
2601
|
-
|
|
2602
|
-
$results = @()
|
|
2603
|
-
$failed = @{}
|
|
2604
|
-
foreach ($s in $steps) {
|
|
2605
|
-
$entry = [ordered]@{ id = $s.id; label = $s.label; ok = $false; skipped = $false; message = '' }
|
|
2606
|
-
if ((!$Undo) -and $s.requires) {
|
|
2607
|
-
$miss = @($s.requires | Where-Object { $failed.ContainsKey($_) })
|
|
2608
|
-
if ($miss.Count -gt 0) {
|
|
2609
|
-
$entry.skipped = $true
|
|
2610
|
-
$entry.message = '前一步没成(' + ($miss -join '、') + '),这步跳过'
|
|
2611
|
-
Say ('跳过 ' + $s.label + ':' + $entry.message)
|
|
2612
|
-
$results += $entry
|
|
2613
|
-
continue
|
|
2614
|
-
}
|
|
2615
|
-
}
|
|
2616
|
-
try {
|
|
2617
|
-
switch ($s.kind) {
|
|
2618
|
-
'writeFile' {
|
|
2619
|
-
if ($Undo) {
|
|
2620
|
-
if (Test-Path -LiteralPath $s.path) {
|
|
2621
|
-
Remove-Item -LiteralPath $s.path -Force
|
|
2622
|
-
$entry.message = '已删除'
|
|
2623
|
-
} else {
|
|
2624
|
-
$entry.message = '本来就没有'
|
|
2625
|
-
}
|
|
2626
|
-
} else {
|
|
2627
|
-
$dir = Split-Path -Parent $s.path
|
|
2628
|
-
if ($dir -and (-not (Test-Path -LiteralPath $dir))) { New-Item -ItemType Directory -Force -Path $dir | Out-Null }
|
|
2629
|
-
$bytes = [Convert]::FromBase64String($s.contentBase64)
|
|
2630
|
-
[IO.File]::WriteAllBytes($s.path, $bytes)
|
|
2631
|
-
if ($s.secret) { $entry.message = '已写入(内容不外显)' } else { $entry.message = '已写入' }
|
|
2632
|
-
}
|
|
2633
|
-
$entry.ok = $true
|
|
2634
|
-
}
|
|
2635
|
-
'mkdir' {
|
|
2636
|
-
if (-not $Undo) {
|
|
2637
|
-
New-Item -ItemType Directory -Force -Path $s.path | Out-Null
|
|
2638
|
-
$entry.message = '已建目录'
|
|
2639
|
-
} else {
|
|
2640
|
-
$entry.message = '不用撤'
|
|
2641
|
-
}
|
|
2642
|
-
$entry.ok = $true
|
|
2643
|
-
}
|
|
2644
|
-
'scheduledTask' {
|
|
2645
|
-
if ($Undo) {
|
|
2646
|
-
$null = & schtasks.exe /delete /tn $s.name /f 2>&1
|
|
2647
|
-
$entry.message = '任务已删(本来没有也算)'
|
|
2648
|
-
} else {
|
|
2649
|
-
# 先结束同名旧任务(正在跑时 /f 覆盖会被拒);/st 00:00 会带一句
|
|
2650
|
-
# 「时间已过」的自我提醒,成功时不必让用户看见,失败时才把原始输出抛出来
|
|
2651
|
-
$null = & schtasks.exe /end /tn $s.name 2>&1
|
|
2652
|
-
$createOut = (& schtasks.exe /create /tn $s.name /tr $s.command /sc ONCE /st 00:00 /f 2>&1 | Out-String)
|
|
2653
|
-
if ($LASTEXITCODE -ne 0) { throw ('schtasks 失败(退出码 ' + $LASTEXITCODE + '):' + $createOut.Trim()) }
|
|
2654
|
-
$entry.message = '任务已就绪'
|
|
2655
|
-
}
|
|
2656
|
-
$entry.ok = $true
|
|
2657
|
-
}
|
|
2658
|
-
'firewallRule' {
|
|
2659
|
-
if ($Undo) {
|
|
2660
|
-
Get-NetFirewallRule -DisplayName $s.name -ErrorAction SilentlyContinue | Remove-NetFirewallRule -ErrorAction SilentlyContinue
|
|
2661
|
-
$entry.message = '规则已删(本来没有也算)'
|
|
2662
|
-
} else {
|
|
2663
|
-
Get-NetFirewallRule -DisplayName $s.name -ErrorAction SilentlyContinue | Remove-NetFirewallRule -ErrorAction SilentlyContinue
|
|
2664
|
-
New-NetFirewallRule -DisplayName $s.name -Direction Inbound -Action Allow -Protocol TCP -LocalPort ([int]$s.port) -Profile Private, Domain -ErrorAction Stop | Out-Null
|
|
2665
|
-
$entry.message = ('已放行 TCP ' + [int]$s.port)
|
|
2666
|
-
}
|
|
2667
|
-
$entry.ok = $true
|
|
2668
|
-
}
|
|
2669
|
-
default {
|
|
2670
|
-
$entry.message = ('不认识的步骤类型 ' + $s.kind)
|
|
2671
|
-
}
|
|
2672
|
-
}
|
|
2673
|
-
} catch {
|
|
2674
|
-
$entry.ok = $false
|
|
2675
|
-
$entry.message = '出错:' + $_.Exception.Message
|
|
2676
|
-
}
|
|
2677
|
-
if (-not $entry.ok) { $failed[$s.id] = $true }
|
|
2678
|
-
if ($entry.ok) { Say ('好了 ' + $s.label) } else { Say ('没成 ' + $s.label + ':' + $entry.message) }
|
|
2679
|
-
$results += $entry
|
|
2680
|
-
}
|
|
2681
|
-
|
|
2682
|
-
$bad = @($results | Where-Object { -not $_.ok })
|
|
2683
|
-
$payload = [ordered]@{
|
|
2684
|
-
at = (Get-Date).ToString('o')
|
|
2685
|
-
mode = $mode
|
|
2686
|
-
elevated = $admin
|
|
2687
|
-
adminNeeded = $needAdmin
|
|
2688
|
-
ok = ($bad.Count -eq 0)
|
|
2689
|
-
results = $results
|
|
2690
|
-
}
|
|
2691
|
-
try {
|
|
2692
|
-
$json = $payload | ConvertTo-Json -Depth 8
|
|
2693
|
-
[IO.File]::WriteAllText($resultPath, $json, (New-Object System.Text.UTF8Encoding($false)))
|
|
2694
|
-
Say ('收工:' + $bad.Count + ' 步没成;结果已写回 result.json,回 DSH 设置页点「读结果」就行。')
|
|
2695
|
-
} catch {
|
|
2696
|
-
Say ('结果没写出来:' + $_.Exception.Message)
|
|
2697
|
-
}
|
|
2698
|
-
Wait-Key
|
|
2699
|
-
`
|
|
2700
|
-
}
|
|
2701
|
-
|
|
2702
|
-
/** 双击入口 .cmd:内容全 ASCII,cmd 才不会把它显示成乱码 */
|
|
2703
|
-
function setupCmdSource() {
|
|
2704
|
-
return [
|
|
2705
|
-
'@echo off',
|
|
2706
|
-
'rem dsh-napcat-keeper - elevation handoff (auto-generated; edits get overwritten)',
|
|
2707
|
-
'rem Double-click this file. PowerShell asks for admin only if the plan really needs it.',
|
|
2708
|
-
'title dsh-napcat-keeper setup',
|
|
2709
|
-
'cd /d "%~dp0"',
|
|
2710
|
-
'powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0apply.ps1"',
|
|
2711
|
-
'if errorlevel 1 pause',
|
|
2712
|
-
'',
|
|
2713
|
-
].join('\r\n')
|
|
2714
|
-
}
|
|
2715
|
-
|
|
2716
|
-
function setupUndoCmdSource() {
|
|
2717
|
-
return [
|
|
2718
|
-
'@echo off',
|
|
2719
|
-
'rem dsh-napcat-keeper - undo what this plan created (reverse order)',
|
|
2720
|
-
'title dsh-napcat-keeper undo',
|
|
2721
|
-
'cd /d "%~dp0"',
|
|
2722
|
-
'powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0apply.ps1" -Undo',
|
|
2723
|
-
'if errorlevel 1 pause',
|
|
2724
|
-
'',
|
|
2725
|
-
].join('\r\n')
|
|
2726
|
-
}
|
|
2727
|
-
|
|
2728
|
-
/** 说明文件:给人看的,写清楚会动哪些文件、怎么撤回 */
|
|
2729
|
-
function setupReadmeText(cfg, plan) {
|
|
2730
|
-
const lines = [
|
|
2731
|
-
'dsh-napcat-keeper 提权交接目录',
|
|
2732
|
-
'',
|
|
2733
|
-
'这是什么:',
|
|
2734
|
-
' DSH 自己是普通权限进程,插件偶尔要做几件「可能要管理员」的事。',
|
|
2735
|
-
' 插件不自己去要管理员,而是把要做的事写在这里,由你双击一下、在 UAC 上点「是」。',
|
|
2736
|
-
'',
|
|
2737
|
-
'怎么用:',
|
|
2738
|
-
' 1) 双击 apply-admin.cmd;',
|
|
2739
|
-
' 2) 如果这批步骤需要管理员,会弹 UAC —— 点「是」;点「否」就什么都不会改;',
|
|
2740
|
-
' 3) 跑完回 DSH 设置页点「读结果」,能看到每一步成没成。',
|
|
2741
|
-
'',
|
|
2742
|
-
'想撤回:双击 undo-admin.cmd(按相反顺序把这次建的东西删掉)。',
|
|
2743
|
-
'',
|
|
2744
|
-
`生成时间:${plan.createdAt}`,
|
|
2745
|
-
`Shell 目录:${cfg.shellDir || '(未配置)'}`,
|
|
2746
|
-
`这批步骤${plan.needAdmin ? '需要' : '不需要'}管理员权限。`,
|
|
2747
|
-
'',
|
|
2748
|
-
'会动的文件 / 规则:',
|
|
2749
|
-
...plan.steps.map((s, i) => {
|
|
2750
|
-
const where = s.path || s.name || s.kind
|
|
2751
|
-
return ` ${i + 1}. [${s.kind}] ${where}${s.admin ? '(要管理员)' : ''}`
|
|
2752
|
-
}),
|
|
2753
|
-
'',
|
|
2754
|
-
'这几个文件里没有密码明文说明:napcat-keeper-env.bat 里有(那是给 NapCat 用的),',
|
|
2755
|
-
'所以这个目录别往外发;关掉「记住密码」或在设置页点「清除密码」就会删掉它。',
|
|
2756
|
-
'',
|
|
2757
|
-
]
|
|
2758
|
-
return lines.join('\r\n')
|
|
2759
|
-
}
|
|
2760
|
-
|
|
2761
|
-
/** 生成整套脚本,返回给设置页用的摘要(不含任何 secret 内容) */
|
|
2762
|
-
function writeSetupBundle(cfg, wants) {
|
|
2763
|
-
if (process.platform !== 'win32') {
|
|
2764
|
-
return { ok: false, error: '这套脚本只对 Windows 有意义' }
|
|
2765
|
-
}
|
|
2766
|
-
const steps = buildSetupSteps(cfg, wants)
|
|
2767
|
-
if (!steps.length) {
|
|
2768
|
-
return { ok: false, error: '这次一条可做的步骤都没有(先把 Shell 目录 / 账号 / 任务名填上)' }
|
|
2769
|
-
}
|
|
2770
|
-
const p = setupPaths()
|
|
2771
|
-
const plan = {
|
|
2772
|
-
plugin: 'dsh-napcat-keeper',
|
|
2773
|
-
version,
|
|
2774
|
-
createdAt: new Date().toISOString(),
|
|
2775
|
-
shellDir: cfg.shellDir || '',
|
|
2776
|
-
account: cfg.launchAccount || '',
|
|
2777
|
-
needAdmin: steps.some((s) => s.admin === true),
|
|
2778
|
-
steps,
|
|
2779
|
-
}
|
|
2780
|
-
try {
|
|
2781
|
-
mkdirSync(p.dir, { recursive: true })
|
|
2782
|
-
writeFileSync(p.plan, `${JSON.stringify(plan, null, 2)}\n`, 'utf8')
|
|
2783
|
-
// ps1 必须带 BOM:PowerShell 5.1 没有 BOM 会按 ANSI 解,中文全变乱码
|
|
2784
|
-
writeFileSync(
|
|
2785
|
-
p.ps1,
|
|
2786
|
-
Buffer.concat([Buffer.from([0xef, 0xbb, 0xbf]), Buffer.from(applyPs1Source(), 'utf8')]),
|
|
2787
|
-
)
|
|
2788
|
-
writeFileSync(p.cmd, setupCmdSource(), 'utf8')
|
|
2789
|
-
writeFileSync(p.undoCmd, setupUndoCmdSource(), 'utf8')
|
|
2790
|
-
writeFileSync(
|
|
2791
|
-
p.readme,
|
|
2792
|
-
Buffer.concat([Buffer.from([0xef, 0xbb, 0xbf]), Buffer.from(setupReadmeText(cfg, plan), 'utf8')]),
|
|
2793
|
-
)
|
|
2794
|
-
try {
|
|
2795
|
-
if (existsSync(p.result)) unlinkSync(p.result)
|
|
2796
|
-
} catch {}
|
|
2797
|
-
log(cfg, `setup bundle 写好了:${p.dir}(${steps.length} 步,needAdmin=${plan.needAdmin})`)
|
|
2798
|
-
} catch (error) {
|
|
2799
|
-
const msg = String((error && error.message) || error)
|
|
2800
|
-
log(cfg, `setup bundle 写不出来:${msg}`)
|
|
2801
|
-
return { ok: false, error: msg }
|
|
2802
|
-
}
|
|
2803
|
-
return {
|
|
2804
|
-
ok: true,
|
|
2805
|
-
dir: p.dir,
|
|
2806
|
-
cmd: p.cmd,
|
|
2807
|
-
ps1: p.ps1,
|
|
2808
|
-
undoCmd: p.undoCmd,
|
|
2809
|
-
needAdmin: plan.needAdmin,
|
|
2810
|
-
steps: steps.map(({ contentBase64, ...rest }) => rest),
|
|
2811
|
-
}
|
|
2812
|
-
}
|
|
2813
|
-
|
|
2814
|
-
/** 读脚本写回来的结果(不存在就是「还没跑」) */
|
|
2815
|
-
function readSetupResult() {
|
|
2816
|
-
const p = setupPaths()
|
|
2817
|
-
try {
|
|
2818
|
-
if (!existsSync(p.result)) return null
|
|
2819
|
-
const text = readFileSync(p.result, 'utf8').replace(/^\uFEFF/, '')
|
|
2820
|
-
const parsed = JSON.parse(text)
|
|
2821
|
-
if (!parsed || typeof parsed !== 'object') return null
|
|
2822
|
-
let results = parsed.results
|
|
2823
|
-
if (!Array.isArray(results)) results = results ? [results] : []
|
|
2824
|
-
let mtimeMs = 0
|
|
2825
|
-
try {
|
|
2826
|
-
mtimeMs = statSync(p.result).mtimeMs
|
|
2827
|
-
} catch {}
|
|
2828
|
-
return {
|
|
2829
|
-
at: String(parsed.at || ''),
|
|
2830
|
-
mode: String(parsed.mode || 'apply'),
|
|
2831
|
-
elevated: Boolean(parsed.elevated),
|
|
2832
|
-
ok: Boolean(parsed.ok),
|
|
2833
|
-
mtimeMs,
|
|
2834
|
-
results: results.map((r) => ({
|
|
2835
|
-
id: String((r && r.id) || ''),
|
|
2836
|
-
label: String((r && r.label) || ''),
|
|
2837
|
-
ok: Boolean(r && r.ok),
|
|
2838
|
-
skipped: Boolean(r && r.skipped),
|
|
2839
|
-
message: String((r && r.message) || ''),
|
|
2840
|
-
})),
|
|
2841
|
-
}
|
|
2842
|
-
} catch {
|
|
2843
|
-
return null
|
|
2844
|
-
}
|
|
2845
|
-
}
|
|
2846
|
-
|
|
2847
|
-
/** 上一次生成的那份计划(给设置页显示「你上次要它做什么」) */
|
|
2848
|
-
function readSetupPlan() {
|
|
2849
|
-
const p = setupPaths()
|
|
2850
|
-
try {
|
|
2851
|
-
if (!existsSync(p.plan)) return null
|
|
2852
|
-
const parsed = JSON.parse(readFileSync(p.plan, 'utf8').replace(/^\uFEFF/, ''))
|
|
2853
|
-
if (!parsed || typeof parsed !== 'object') return null
|
|
2854
|
-
const steps = Array.isArray(parsed.steps) ? parsed.steps : []
|
|
2855
|
-
return {
|
|
2856
|
-
createdAt: String(parsed.createdAt || ''),
|
|
2857
|
-
needAdmin: Boolean(parsed.needAdmin),
|
|
2858
|
-
steps: steps.map((s) => ({
|
|
2859
|
-
id: String((s && s.id) || ''),
|
|
2860
|
-
label: String((s && s.label) || ''),
|
|
2861
|
-
kind: String((s && s.kind) || ''),
|
|
2862
|
-
where: String((s && (s.path || s.name)) || ''),
|
|
2863
|
-
admin: Boolean(s && s.admin),
|
|
2864
|
-
})),
|
|
2865
|
-
}
|
|
2866
|
-
} catch {
|
|
2867
|
-
return null
|
|
2868
|
-
}
|
|
2869
|
-
}
|
|
2870
|
-
|
|
2871
|
-
function setupState(cfg) {
|
|
2872
|
-
const p = setupPaths()
|
|
2873
|
-
let exists = false
|
|
2326
|
+
/**
|
|
2327
|
+
* 「进程」那行该按哪种安装形态措辞:
|
|
2328
|
+
* - `boot`:目录里有 NapCatWinBootMain.exe(注入/快登壳型),进程表里有快登壳计数可看;
|
|
2329
|
+
* - `node`:无头 Shell(Node 包)安装,QQ 本体跑在 node.exe 里,既没有 QQ.exe 也没有快登壳,
|
|
2330
|
+
* `procs.boot` 恒为 0 —— 这时候还写「NapCatWinBootMain 0」只会让人以为没进程。
|
|
2331
|
+
*/
|
|
2332
|
+
function procsLayout(cfg, p) {
|
|
2333
|
+
const boot = Number(p && p.boot) || 0
|
|
2334
|
+
const qq = Number(p && p.qq) || 0
|
|
2335
|
+
// ① 谁在跑就按谁说话:有快登壳进程 → boot 型;只有 node 进程 → node 型
|
|
2336
|
+
if (boot > 0) return 'boot'
|
|
2337
|
+
if (qq > 0) return 'node'
|
|
2338
|
+
// ② 都没在跑(离线 / 待拉起):只能看 Shell 目录是哪种安装形态。
|
|
2339
|
+
// 注意:node 型目录里也常带一个 napcat/NapCatWinBootMain.exe 模板,不能拿「有这文件」当判据。
|
|
2340
|
+
// 这一步要 readdir,而面板每轮都来问一次(轻量档 1.5 秒一轮),所以按目录名缓存 60 秒。
|
|
2341
|
+
const dir = String((cfg && cfg.shellDir) || '')
|
|
2342
|
+
const now = Date.now()
|
|
2343
|
+
if (uiCache.layoutKey === dir && now - (uiCache.layoutAt || 0) < 60000) return uiCache.layout
|
|
2344
|
+
let value = 'unknown'
|
|
2874
2345
|
try {
|
|
2875
|
-
|
|
2346
|
+
const names = readdirSync(dir).map((n) => String(n).toLowerCase())
|
|
2347
|
+
const 有node壳 = names.includes('node.exe') && (names.includes('index.js') || names.includes('napcat.mjs'))
|
|
2348
|
+
if (有node壳) value = 'node'
|
|
2349
|
+
else if (names.includes('napcatwinbootmain.exe')) value = 'boot'
|
|
2876
2350
|
} catch {}
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
undoCmdPath: p.undoCmd,
|
|
2882
|
-
exists,
|
|
2883
|
-
needs: needCatalog(cfg),
|
|
2884
|
-
plan: readSetupPlan(),
|
|
2885
|
-
lastRun: readSetupResult(),
|
|
2886
|
-
}
|
|
2351
|
+
uiCache.layout = value
|
|
2352
|
+
uiCache.layoutKey = dir
|
|
2353
|
+
uiCache.layoutAt = now
|
|
2354
|
+
return value
|
|
2887
2355
|
}
|
|
2888
2356
|
|
|
2889
|
-
/**
|
|
2890
|
-
function
|
|
2891
|
-
|
|
2892
|
-
try {
|
|
2893
|
-
mkdirSync(p.dir, { recursive: true })
|
|
2894
|
-
const hasCmd = existsSync(p.cmd)
|
|
2895
|
-
const child = spawn('explorer.exe', [hasCmd ? `/select,${p.cmd}` : p.dir], {
|
|
2896
|
-
detached: true,
|
|
2897
|
-
stdio: 'ignore',
|
|
2898
|
-
windowsHide: false,
|
|
2899
|
-
})
|
|
2900
|
-
child.unref()
|
|
2901
|
-
return { ok: true, dir: p.dir, cmd: p.cmd, selected: hasCmd }
|
|
2902
|
-
} catch (error) {
|
|
2903
|
-
return { ok: false, error: String((error && error.message) || error), dir: p.dir }
|
|
2904
|
-
}
|
|
2357
|
+
/** 任务里那条命令(纯字符串版本:不写盘,供别处复用 taskCommandLine 的规则) */
|
|
2358
|
+
function taskCommandLinePure(cfg, bat) {
|
|
2359
|
+
return taskCmdLineOf(cfg, passwordActive(cfg) && bat ? envBatPath(cfg) : bat)
|
|
2905
2360
|
}
|
|
2906
2361
|
|
|
2907
2362
|
/** 把已经在跑的 NapCat 控制台窗口藏起来(老链条留下的黑框也能收掉,不用重启 QQ) */
|
|
2908
2363
|
function hideConsoleWindows(cfg) {
|
|
2909
2364
|
const ps = [
|
|
2910
|
-
|
|
2365
|
+
PS_QUIET,
|
|
2911
2366
|
"Add-Type -Namespace K -Name U -MemberDefinition '",
|
|
2912
2367
|
'[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr h, int n);',
|
|
2913
2368
|
'[DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr h);',
|
|
@@ -3042,7 +2497,7 @@ async function killNewerChains(cfg, oldestLastMs) {
|
|
|
3042
2497
|
}
|
|
3043
2498
|
const root = psPath(base).replace(/'/g, "''")
|
|
3044
2499
|
const script =
|
|
3045
|
-
|
|
2500
|
+
PS_QUIET +
|
|
3046
2501
|
`$cut=([datetime]'1970-01-01').AddMilliseconds(${Math.round(oldestLastMs)});` +
|
|
3047
2502
|
'Get-Process QQ,NapCatWinBootMain,node | ' +
|
|
3048
2503
|
`Where-Object { $_.Path -like '${root}*' -and $_.StartTime -gt $cut } | ` +
|
|
@@ -3542,6 +2997,19 @@ function registerTools(ctx, cfg) {
|
|
|
3542
2997
|
procs,
|
|
3543
2998
|
node: nodePublic(cfg),
|
|
3544
2999
|
qr,
|
|
3000
|
+
/** 要扫码时把这一页发给用户:优先独立小端口(任何浏览器都打得开),其次 DSH 路由 */
|
|
3001
|
+
qrPanel: {
|
|
3002
|
+
path: QR_PANEL_PATH,
|
|
3003
|
+
url: qrPanelPublic(cfg).url || QR_PANEL_PATH,
|
|
3004
|
+
running: qrPanelPublic(cfg).running,
|
|
3005
|
+
port: qrPanelPublic(cfg).port,
|
|
3006
|
+
note: qrPanelPublic(cfg).note,
|
|
3007
|
+
hint: health.online
|
|
3008
|
+
? '已登录,暂时用不上扫码页;下次掉线要扫码时同一个地址还能用。'
|
|
3009
|
+
: qrPanelPublic(cfg).url
|
|
3010
|
+
? '需要手机扫码时点开 qrPanel.url 那个地址(本机浏览器打开即可),二维码会自己换。'
|
|
3011
|
+
: '需要手机扫码时点开 qrPanel.path(挂在 DSH 地址后面),二维码会自己换。',
|
|
3012
|
+
},
|
|
3545
3013
|
bridge: bridgeAge(cfg),
|
|
3546
3014
|
autoLogin: {
|
|
3547
3015
|
rememberPassword: cfg.rememberPassword,
|
|
@@ -3582,7 +3050,9 @@ function registerTools(ctx, cfg) {
|
|
|
3582
3050
|
: qr && qr.ageSec <= cfg.qrFreshSec
|
|
3583
3051
|
? passwordActive(cfg)
|
|
3584
3052
|
? '离线,且在等扫码——但「记住密码」是开着的:看一眼日志里“快速登录错误”后面是不是仍回退到二维码(密码登录可能被腾讯要求验证)。'
|
|
3585
|
-
: '
|
|
3053
|
+
: '离线,且二维码是新的:需要手机扫码 —— 打开 ' +
|
|
3054
|
+
(qrPanelPublic(cfg).url || qrPanelPublic(cfg).path) +
|
|
3055
|
+
' 就是一张会自己刷新的码(设置 → napcat桥 顶上那条黄横幅也能点开)。或者打开「记住密码」让 NapCat 自己用密码登录。图片文件在 qrCopyPath。'
|
|
3586
3056
|
: '离线。看门狗会自动拉起;若 procs.dup 为 true 说明被重复拉起过,已自动去重。',
|
|
3587
3057
|
},
|
|
3588
3058
|
null,
|
|
@@ -3684,6 +3154,7 @@ function stop(cfg) {
|
|
|
3684
3154
|
state.timer = null
|
|
3685
3155
|
log(cfg, 'watchdog stopped')
|
|
3686
3156
|
}
|
|
3157
|
+
stopQrPanel(cfg, '插件停了')
|
|
3687
3158
|
}
|
|
3688
3159
|
|
|
3689
3160
|
function armTimer(cfg) {
|
|
@@ -3709,6 +3180,406 @@ function armTimer(cfg) {
|
|
|
3709
3180
|
|
|
3710
3181
|
const ROUTE = '/dsh-napcat-keeper'
|
|
3711
3182
|
|
|
3183
|
+
/** 扫码页地址(相对 DSH 那个地址):NapCat 要手机扫码时,用户点这个就能看见会自己刷新的二维码。 */
|
|
3184
|
+
const QR_PANEL_PATH = `${ROUTE}/qr`
|
|
3185
|
+
|
|
3186
|
+
/** NapCat 的码大约 2 分钟一换;页面按这个算「这张码还剩几秒」。 */
|
|
3187
|
+
const QR_TTL_SEC = 120
|
|
3188
|
+
|
|
3189
|
+
/**
|
|
3190
|
+
* 扫码页(纯字符串内嵌,不带任何静态文件)。
|
|
3191
|
+
* 图 / 状态 / 重拉 全走本插件自己的 /dsh-napcat-keeper/* 路由,所以不用另起端口,
|
|
3192
|
+
* DSH 换端口时这一页跟着换,链接永远有效。
|
|
3193
|
+
* 页面里用的都是相对路径(qr.png / qr-state / qr-restart)—— 挂在 /dsh-napcat-keeper/qr 下正好。
|
|
3194
|
+
*/
|
|
3195
|
+
const QR_PAGE_HTML = String.raw`<!doctype html>
|
|
3196
|
+
<html lang="zh-CN"><head><meta charset="utf-8">
|
|
3197
|
+
<title>NapCat 扫码登录</title>
|
|
3198
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
3199
|
+
<style>
|
|
3200
|
+
:root { --bg:#0f1420; --card:#161d2c; --fg:#e8eefc; --dim:#8b98b4; --ok:#41d392; --warn:#f5c451; --bad:#f0705f; }
|
|
3201
|
+
* { box-sizing: border-box; }
|
|
3202
|
+
body { margin:0; min-height:100vh; background:radial-gradient(1200px 600px at 50% -10%, #1b2740, var(--bg)); color:var(--fg);
|
|
3203
|
+
font:15px/1.6 "Microsoft YaHei UI", system-ui, sans-serif; display:flex; align-items:center; justify-content:center; padding:24px; }
|
|
3204
|
+
.card { width:min(560px,100%); background:var(--card); border:1px solid #24304a; border-radius:18px; padding:22px 22px 18px;
|
|
3205
|
+
box-shadow:0 20px 60px rgba(0,0,0,.45); }
|
|
3206
|
+
h1 { margin:0 0 2px; font-size:19px; font-weight:600; letter-spacing:.5px; }
|
|
3207
|
+
.sub { color:var(--dim); font-size:13px; margin-bottom:16px; }
|
|
3208
|
+
.qrwrap { position:relative; background:#fff; border-radius:14px; padding:14px; display:flex; justify-content:center; min-height:180px; }
|
|
3209
|
+
.qrwrap img { width:100%; max-width:340px; image-rendering:pixelated; display:block; }
|
|
3210
|
+
.overlay { position:absolute; inset:0; display:none; align-items:center; justify-content:center; border-radius:14px;
|
|
3211
|
+
background:rgba(15,20,32,.9); color:var(--fg); font-size:15px; text-align:center; padding:20px; }
|
|
3212
|
+
.overlay.show { display:flex; }
|
|
3213
|
+
.rows { margin:16px 0 4px; }
|
|
3214
|
+
.row { display:flex; justify-content:space-between; gap:12px; padding:6px 0; border-bottom:1px dashed #232e46; font-size:14px; }
|
|
3215
|
+
.row:last-child { border-bottom:none; }
|
|
3216
|
+
.k { color:var(--dim); }
|
|
3217
|
+
.v { font-variant-numeric:tabular-nums; text-align:right; word-break:break-all; }
|
|
3218
|
+
.dot { display:inline-block; width:8px; height:8px; border-radius:50%; margin-right:6px; vertical-align:1px; background:var(--dim); }
|
|
3219
|
+
.ok { color:var(--ok); } .warn { color:var(--warn); } .bad { color:var(--bad); }
|
|
3220
|
+
.ok .dot { background:var(--ok); } .warn .dot { background:var(--warn); } .bad .dot { background:var(--bad); }
|
|
3221
|
+
.bar { display:flex; gap:10px; margin-top:14px; flex-wrap:wrap; }
|
|
3222
|
+
.smsbox { display:none; margin-top:14px; padding:10px 12px; border-radius:10px;
|
|
3223
|
+
border:1px solid #b8860b; background:rgba(184,134,11,.12); }
|
|
3224
|
+
.smsbox.show { display:block; }
|
|
3225
|
+
.smstitle { color:var(--warn); font-size:13px; margin-bottom:4px; }
|
|
3226
|
+
.smlink { display:inline-block; margin-top:8px; padding:7px 12px; border-radius:8px;
|
|
3227
|
+
background:#2a3550; color:#cfe0ff; text-decoration:none; font-size:13px; }
|
|
3228
|
+
button, a.btn { flex:0 0 auto; cursor:pointer; border:1px solid #2d3b58; background:#1d2740; color:var(--fg);
|
|
3229
|
+
border-radius:10px; padding:9px 14px; font-size:14px; text-decoration:none; }
|
|
3230
|
+
button:hover, a.btn:hover { background:#243254; }
|
|
3231
|
+
button.primary { background:#2b6cf6; border-color:#2b6cf6; }
|
|
3232
|
+
button.primary:hover { background:#3a78ff; }
|
|
3233
|
+
.hint { color:var(--dim); font-size:12.5px; margin-top:10px; }
|
|
3234
|
+
</style></head>
|
|
3235
|
+
<body>
|
|
3236
|
+
<div class="card">
|
|
3237
|
+
<h1>NapCat 扫码登录</h1>
|
|
3238
|
+
<div class="sub">二维码每 2 秒自动换新的(NapCat 换码会跟着换,不用手动刷新页面)</div>
|
|
3239
|
+
|
|
3240
|
+
<div class="qrwrap">
|
|
3241
|
+
<img id="qr" alt="二维码还没出来" src="qr.png?t=0">
|
|
3242
|
+
<div class="overlay" id="ov"><div id="ovtext">等 NapCat 出码…</div></div>
|
|
3243
|
+
</div>
|
|
3244
|
+
|
|
3245
|
+
<div class="rows">
|
|
3246
|
+
<div class="row"><span class="k">OneBot 接口</span><span class="v" id="v-api">—</span></div>
|
|
3247
|
+
<div class="row"><span class="k">登录状态</span><span class="v" id="v-login">—</span></div>
|
|
3248
|
+
<div class="row"><span class="k">二维码</span><span class="v" id="v-qr">—</span></div>
|
|
3249
|
+
<div class="row"><span class="k">这张码还剩</span><span class="v" id="v-ttl">—</span></div>
|
|
3250
|
+
<div class="row"><span class="k">看门狗阶段</span><span class="v" id="v-phase">—</span></div>
|
|
3251
|
+
<div class="row"><span class="k">扫码重试</span><span class="v" id="v-retry">—</span></div>
|
|
3252
|
+
<div class="row"><span class="k">最近日志</span><span class="v" id="v-log">—</span></div>
|
|
3253
|
+
</div>
|
|
3254
|
+
|
|
3255
|
+
<div class="smsbox" id="smsbox">
|
|
3256
|
+
<div class="smstitle" id="sms-title">⚠ 这次登录要短信验证</div>
|
|
3257
|
+
<div class="hint">NapCat 的日志里给了验证链接,点开在同一台机器上完成就行。</div>
|
|
3258
|
+
<a class="smlink" id="sms-link" href="#" target="_blank" rel="noreferrer">打开验证页面</a>
|
|
3259
|
+
</div>
|
|
3260
|
+
|
|
3261
|
+
<div class="bar">
|
|
3262
|
+
<button class="primary" id="btn-restart">我要新码(重拉 NapCat)</button>
|
|
3263
|
+
<button id="btn-tick">立即刷新</button>
|
|
3264
|
+
</div>
|
|
3265
|
+
<div class="hint" id="hint">页面开着就行:扫码成功后这里会变绿。</div>
|
|
3266
|
+
</div>
|
|
3267
|
+
|
|
3268
|
+
<script>
|
|
3269
|
+
let restartedAt = 0;
|
|
3270
|
+
const $ = (id) => document.getElementById(id);
|
|
3271
|
+
const PHASES = { idle:'待机', online:'在线', launching:'正在拉起', starting:'正在启动',
|
|
3272
|
+
'need-scan':'在等你扫码', 'need-scan-giveup':'扫码放弃(重试到上限了)', 'reverse-wait':'等反向 WS 连进来',
|
|
3273
|
+
'reverse-giveup':'反向 WS 等不到客户端', 'need-token':'令牌不对', 'port-busy':'端口被占', offline:'离线' };
|
|
3274
|
+
|
|
3275
|
+
function fmt(sec) {
|
|
3276
|
+
if (sec === null || sec === undefined) return '—';
|
|
3277
|
+
if (sec < 60) return sec + ' 秒前';
|
|
3278
|
+
return Math.floor(sec / 60) + ' 分 ' + (sec % 60) + ' 秒前';
|
|
3279
|
+
}
|
|
3280
|
+
|
|
3281
|
+
async function tick() {
|
|
3282
|
+
$('qr').src = 'qr.png?t=' + Date.now();
|
|
3283
|
+
let s;
|
|
3284
|
+
try { s = await (await fetch('qr-state?t=' + Date.now(), { cache:'no-store' })).json() }
|
|
3285
|
+
catch (e) { $('hint').textContent = '接口没响应:' + e; return }
|
|
3286
|
+
|
|
3287
|
+
restartedAt = s.restartedAt || 0;
|
|
3288
|
+
const qr = s.qr || null;
|
|
3289
|
+
const age = qr ? qr.ageSec : null;
|
|
3290
|
+
const left = (age === null || age === undefined) ? null : Math.max(0, (s.qrTtlSec || 120) - age);
|
|
3291
|
+
const fresh = left !== null && left > 20;
|
|
3292
|
+
|
|
3293
|
+
$('v-api').className = 'v ' + (s.online ? 'ok' : 'warn');
|
|
3294
|
+
$('v-api').innerHTML = '<span class="dot"></span>' + (s.online ? '通(在线)' : '还没通') +
|
|
3295
|
+
(typeof s.portStatus === 'number' && s.portStatus ? ' · HTTP ' + s.portStatus : '');
|
|
3296
|
+
|
|
3297
|
+
$('v-qr').className = 'v ' + (age === null ? 'bad' : (fresh ? 'ok' : 'warn'));
|
|
3298
|
+
$('v-qr').innerHTML = '<span class="dot"></span>' + (age === null ? '还没有 qrcode.png' : '更新于 ' + fmt(age));
|
|
3299
|
+
|
|
3300
|
+
$('v-ttl').className = 'v ' + (left === null ? 'bad' : (left > 30 ? 'ok' : 'warn'));
|
|
3301
|
+
$('v-ttl').innerHTML = left === null ? '<span class="dot"></span>—'
|
|
3302
|
+
: '<span class="dot"></span>约 ' + left + ' 秒(到点自己换)';
|
|
3303
|
+
|
|
3304
|
+
$('v-phase').innerHTML = '<span class="dot"></span>' + (PHASES[s.phase] || s.phase || '—');
|
|
3305
|
+
$('v-retry').textContent = (s.scanRetries || 0) + ' 次';
|
|
3306
|
+
$('v-log').textContent = (s.logTail && s.logTail.length ? s.logTail[s.logTail.length - 1] : '—');
|
|
3307
|
+
|
|
3308
|
+
// 要短信验证的时候把腾讯那个验证链接直接摆出来(光有二维码扫不进去)。
|
|
3309
|
+
// 注意再叠一层 !s.online:老日志里的 ErrCode 会一直留着,在线时绝不能误报。
|
|
3310
|
+
const sms = s.sms || null;
|
|
3311
|
+
if (sms && sms.needSms && !s.online && (sms.url || sms.code)) {
|
|
3312
|
+
$('smsbox').classList.add('show');
|
|
3313
|
+
$('sms-title').textContent = sms.code ? ('⚠ 这次登录要短信验证(ErrCode ' + sms.code + ')') : '⚠ 这次登录要短信验证';
|
|
3314
|
+
const link = $('sms-link');
|
|
3315
|
+
if (sms.url) { link.href = sms.url; link.style.display = ''; }
|
|
3316
|
+
else { link.removeAttribute('href'); link.style.display = 'none'; }
|
|
3317
|
+
} else {
|
|
3318
|
+
$('smsbox').classList.remove('show');
|
|
3319
|
+
}
|
|
3320
|
+
|
|
3321
|
+
if (s.online) {
|
|
3322
|
+
$('v-login').className = 'v ok';
|
|
3323
|
+
$('v-login').innerHTML = '<span class="dot"></span>已登录 ' + (s.nickname || '') + '(' + (s.userId || '') + ')';
|
|
3324
|
+
$('ov').classList.remove('show');
|
|
3325
|
+
$('hint').textContent = '连上了,可以关掉这个页面了。';
|
|
3326
|
+
} else {
|
|
3327
|
+
$('v-login').className = 'v warn';
|
|
3328
|
+
$('v-login').innerHTML = '<span class="dot"></span>还没登录,等扫码';
|
|
3329
|
+
if (left !== null && left < 12) {
|
|
3330
|
+
$('ovtext').textContent = '这张码快过期了,等新的…';
|
|
3331
|
+
$('ov').classList.add('show');
|
|
3332
|
+
} else if (age === null) {
|
|
3333
|
+
$('ovtext').textContent = '等 NapCat 出码…';
|
|
3334
|
+
$('ov').classList.add('show');
|
|
3335
|
+
} else {
|
|
3336
|
+
$('ov').classList.remove('show');
|
|
3337
|
+
}
|
|
3338
|
+
$('hint').textContent = '手机 QQ →「我」→ 扫一扫。码约 2 分钟换一张,页面会自动跟着换。';
|
|
3339
|
+
}
|
|
3340
|
+
}
|
|
3341
|
+
|
|
3342
|
+
async function restart() {
|
|
3343
|
+
$('ovtext').textContent = '正在重拉 NapCat…';
|
|
3344
|
+
$('ov').classList.add('show');
|
|
3345
|
+
try { await fetch('qr-restart', { method: 'POST' }) } catch (e) {}
|
|
3346
|
+
setTimeout(() => { $('ov').classList.remove('show'); tick() }, 6000);
|
|
3347
|
+
setTimeout(tick, 14000);
|
|
3348
|
+
}
|
|
3349
|
+
|
|
3350
|
+
$('btn-restart').addEventListener('click', restart);
|
|
3351
|
+
$('btn-tick').addEventListener('click', tick);
|
|
3352
|
+
tick();
|
|
3353
|
+
setInterval(tick, 3000);
|
|
3354
|
+
</script>
|
|
3355
|
+
</body></html>`
|
|
3356
|
+
|
|
3357
|
+
/* --- 扫码页:一份处理,两个入口(DSH 路由 + 独立小端口) --------------- */
|
|
3358
|
+
|
|
3359
|
+
/**
|
|
3360
|
+
* 从 NapCat 自己的运行日志里抠「这次登录要不要短信验证」。
|
|
3361
|
+
* 它出码之后常常还跟一条 ErrCode + proofWaterUrl(腾讯的验证页),
|
|
3362
|
+
* 光有二维码扫不进去 —— 所以页面上得把这个链接直接摆出来。
|
|
3363
|
+
* 日志里混着 ANSI 码和 GBK 乱码,只按 ASCII 令牌匹配,读的时候用 latin1。
|
|
3364
|
+
*/
|
|
3365
|
+
function napcatRunLogInfo(cfg) {
|
|
3366
|
+
const dir = String((cfg && cfg.shellDir) || '').replace(/\\/g, '/')
|
|
3367
|
+
const files = []
|
|
3368
|
+
if (dir) {
|
|
3369
|
+
files.push(path.join(dir, '_run.log'))
|
|
3370
|
+
files.push(path.join(dir, 'napcat', '_run.log'))
|
|
3371
|
+
}
|
|
3372
|
+
files.push(path.join(keeperDir(), 'launch.log'))
|
|
3373
|
+
for (const file of files) {
|
|
3374
|
+
try {
|
|
3375
|
+
if (!existsSync(file)) continue
|
|
3376
|
+
const lines = readFileSync(file, 'latin1').split(/\r?\n/).filter(Boolean).slice(-200)
|
|
3377
|
+
const out = { file, code: null, text: '', url: '', success: false, needSms: false, at: '' }
|
|
3378
|
+
let codeAt = -1
|
|
3379
|
+
let okAt = -1
|
|
3380
|
+
let urlStamp = ''
|
|
3381
|
+
lines.forEach((line, i) => {
|
|
3382
|
+
const code = line.match(/ErrCode:\s*(\d+)/)
|
|
3383
|
+
if (code) {
|
|
3384
|
+
out.code = Number(code[1])
|
|
3385
|
+
codeAt = i
|
|
3386
|
+
out.text = line.replace(/\u001b\[[0-9;]*m/g, '').trim().slice(0, 240)
|
|
3387
|
+
}
|
|
3388
|
+
const cap = line.match(/proofWaterUrl:\s*(\S+)/)
|
|
3389
|
+
if (cap) {
|
|
3390
|
+
out.url = cap[1]
|
|
3391
|
+
urlStamp = (line.match(/^(\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})/) || [])[1] || ''
|
|
3392
|
+
}
|
|
3393
|
+
if (/Login Success|登录成功|ONLINE/i.test(line)) { out.success = true; okAt = i }
|
|
3394
|
+
const at = line.match(/^(\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})/)
|
|
3395
|
+
if (at) out.at = at[1]
|
|
3396
|
+
})
|
|
3397
|
+
// 老日志里的 ErrCode 会一直挂着;只有「这次登录还没成功」才算数
|
|
3398
|
+
out.needSms = out.code !== null && codeAt > okAt
|
|
3399
|
+
// 验证链接有时效(几分钟就废),太老的别拿给主人点
|
|
3400
|
+
if (out.url && urlStamp) {
|
|
3401
|
+
const t = new Date(`${new Date().getFullYear()}-${urlStamp.replace(' ', 'T')}`).getTime()
|
|
3402
|
+
if (!Number.isFinite(t) || Date.now() - t > 30 * 60 * 1000) {
|
|
3403
|
+
out.urlStale = true
|
|
3404
|
+
out.url = ''
|
|
3405
|
+
}
|
|
3406
|
+
}
|
|
3407
|
+
return out
|
|
3408
|
+
} catch {}
|
|
3409
|
+
}
|
|
3410
|
+
return { file: '', code: null, text: '', url: '', success: false, at: '' }
|
|
3411
|
+
}
|
|
3412
|
+
|
|
3413
|
+
/** 扫码页轮询用的状态(轻量档,不跑子进程) */
|
|
3414
|
+
async function qrPageState(cfg) {
|
|
3415
|
+
const payload = await uiState(cfg, { light: true })
|
|
3416
|
+
const rt = (payload && payload.runtime) || {}
|
|
3417
|
+
return {
|
|
3418
|
+
ok: true,
|
|
3419
|
+
online: Boolean(rt.online),
|
|
3420
|
+
phase: rt.phase || '',
|
|
3421
|
+
mode: rt.mode || '',
|
|
3422
|
+
portStatus: rt.portStatus || 0,
|
|
3423
|
+
nickname: rt.nickname || '',
|
|
3424
|
+
userId: rt.userId || 0,
|
|
3425
|
+
qr: rt.qr || null,
|
|
3426
|
+
qrTtlSec: QR_TTL_SEC,
|
|
3427
|
+
scanRetries: rt.scanRetries || 0,
|
|
3428
|
+
restartedAt: state.lastLaunchAt ? new Date(state.lastLaunchAt).getTime() : 0,
|
|
3429
|
+
logTail: Array.isArray(payload && payload.log) ? payload.log : [],
|
|
3430
|
+
sms: napcatRunLogInfo(cfg),
|
|
3431
|
+
panel: { port: qrPanel.port || cfg.qrPanelPort, enabled: cfg.qrPanelEnabled !== false },
|
|
3432
|
+
now: Date.now(),
|
|
3433
|
+
}
|
|
3434
|
+
}
|
|
3435
|
+
|
|
3436
|
+
/**
|
|
3437
|
+
* 扫码页的请求处理。路径先剥掉 DSH 那个前缀,于是:
|
|
3438
|
+
* /dsh-napcat-keeper/qr (DSH 路由)→ 页面
|
|
3439
|
+
* /qr / (独立端口) → 页面
|
|
3440
|
+
* 图片 / 状态 / 重拉同名共用。
|
|
3441
|
+
*/
|
|
3442
|
+
async function serveQrPage(req, res, cfg) {
|
|
3443
|
+
cfg = cfg || state.cfg
|
|
3444
|
+
let pathname = String((req && req.url) || '/')
|
|
3445
|
+
const qi = pathname.indexOf('?')
|
|
3446
|
+
if (qi >= 0) pathname = pathname.slice(0, qi)
|
|
3447
|
+
if (pathname.startsWith(ROUTE)) pathname = pathname.slice(ROUTE.length) || '/'
|
|
3448
|
+
if (!pathname) pathname = '/'
|
|
3449
|
+
|
|
3450
|
+
if (pathname === '/' || pathname === '/qr' || pathname === '/qr/') {
|
|
3451
|
+
res.statusCode = 200
|
|
3452
|
+
res.setHeader('content-type', 'text/html; charset=utf-8')
|
|
3453
|
+
res.setHeader('cache-control', 'no-store')
|
|
3454
|
+
res.end(QR_PAGE_HTML)
|
|
3455
|
+
return
|
|
3456
|
+
}
|
|
3457
|
+
|
|
3458
|
+
if (pathname === '/qr.png') {
|
|
3459
|
+
const info = qrInfo(cfg)
|
|
3460
|
+
if (!info || !info.file) {
|
|
3461
|
+
sendJson(res, 404, { ok: false, error: '还没有二维码(NapCat 没进扫码态,或者还没出码)' })
|
|
3462
|
+
return
|
|
3463
|
+
}
|
|
3464
|
+
let buf
|
|
3465
|
+
try {
|
|
3466
|
+
buf = readFileSync(info.file)
|
|
3467
|
+
} catch (error) {
|
|
3468
|
+
sendJson(res, 500, { ok: false, error: String((error && error.message) || error) })
|
|
3469
|
+
return
|
|
3470
|
+
}
|
|
3471
|
+
res.statusCode = 200
|
|
3472
|
+
res.setHeader('content-type', 'image/png')
|
|
3473
|
+
res.setHeader('cache-control', 'no-store')
|
|
3474
|
+
res.setHeader('content-length', String(buf.length))
|
|
3475
|
+
res.end(buf)
|
|
3476
|
+
return
|
|
3477
|
+
}
|
|
3478
|
+
|
|
3479
|
+
if (pathname === '/qr-state') {
|
|
3480
|
+
sendJson(res, 200, await qrPageState(cfg))
|
|
3481
|
+
return
|
|
3482
|
+
}
|
|
3483
|
+
|
|
3484
|
+
if (pathname === '/qr-restart') {
|
|
3485
|
+
const ctx = state.ctx || null
|
|
3486
|
+
const result = ctx ? await doAction(ctx, cfg, 'restart', {}) : { said: '插件还没准备好,稍等一下再点' }
|
|
3487
|
+
sendJson(res, 200, { ok: true, said: (result && result.said) || '已让它重拉,新码大概十几秒后出来' })
|
|
3488
|
+
return
|
|
3489
|
+
}
|
|
3490
|
+
|
|
3491
|
+
sendJson(res, 404, { ok: false, error: '不认识的路径:' + pathname })
|
|
3492
|
+
}
|
|
3493
|
+
|
|
3494
|
+
/** 扫码页的独立小服务状态(跟着 globalThis 走:热重载时旧 server 会被关掉,不会越攒越多) */
|
|
3495
|
+
const QRPANEL_KEY = Symbol.for('dsh.napcat-keeper.qrpanel')
|
|
3496
|
+
const qrPanel =
|
|
3497
|
+
globalThis[QRPANEL_KEY] ||
|
|
3498
|
+
(globalThis[QRPANEL_KEY] = { server: null, port: 0, error: '', since: 0, note: '', serve: null })
|
|
3499
|
+
|
|
3500
|
+
/** 扫码页地址(给用户点的那个) */
|
|
3501
|
+
function qrPanelUrl(cfg) {
|
|
3502
|
+
const c = cfg || state.cfg || {}
|
|
3503
|
+
return c.qrPanelEnabled === false ? '' : `http://127.0.0.1:${c.qrPanelPort || 8899}/`
|
|
3504
|
+
}
|
|
3505
|
+
|
|
3506
|
+
function stopQrPanel(cfg, why = '') {
|
|
3507
|
+
const s = qrPanel.server
|
|
3508
|
+
qrPanel.server = null
|
|
3509
|
+
qrPanel.port = 0
|
|
3510
|
+
qrPanel.serve = null
|
|
3511
|
+
if (s) {
|
|
3512
|
+
try {
|
|
3513
|
+
s.close()
|
|
3514
|
+
} catch {}
|
|
3515
|
+
log(cfg || state.cfg, `扫码页已关${why ? '(' + why + ')' : ''}`)
|
|
3516
|
+
}
|
|
3517
|
+
}
|
|
3518
|
+
|
|
3519
|
+
/** 按配置把扫码页起起来;端口没变且已经在跑就什么都不做 */
|
|
3520
|
+
function startQrPanel(cfg) {
|
|
3521
|
+
if (!cfg || cfg.qrPanelEnabled === false) {
|
|
3522
|
+
stopQrPanel(cfg, '配置里关了')
|
|
3523
|
+
qrPanel.note = '关着(设置里可以打开)'
|
|
3524
|
+
return { ok: false, port: 0, note: qrPanel.note }
|
|
3525
|
+
}
|
|
3526
|
+
const port = Number(cfg.qrPanelPort) || 8899
|
|
3527
|
+
// ⚠ 热重载时旧 server 的闭包还指着**旧的** serveQrPage / QR_PAGE_HTML,
|
|
3528
|
+
// 光看「端口一样、还在听」就复用的话,页面永远是上一版。所以比函数身份:换模块了就重建。
|
|
3529
|
+
if (qrPanel.server && qrPanel.port === port && qrPanel.server.listening && qrPanel.serve === serveQrPage) {
|
|
3530
|
+
return { ok: true, port, note: qrPanel.note }
|
|
3531
|
+
}
|
|
3532
|
+
stopQrPanel(cfg, qrPanel.server ? '热重载换新代码' : '重启')
|
|
3533
|
+
try {
|
|
3534
|
+
const server = http.createServer((req, res) => {
|
|
3535
|
+
serveQrPage(req, res, state.cfg).catch((error) => {
|
|
3536
|
+
try {
|
|
3537
|
+
sendJson(res, 500, { ok: false, error: String((error && error.message) || error) })
|
|
3538
|
+
} catch {}
|
|
3539
|
+
})
|
|
3540
|
+
})
|
|
3541
|
+
server.on('error', (error) => {
|
|
3542
|
+
qrPanel.error = String((error && error.message) || error)
|
|
3543
|
+
qrPanel.note = /EADDRINUSE/.test(qrPanel.error)
|
|
3544
|
+
? `端口 ${port} 被占了(改一个 qrPanelPort,或关掉占它的程序)`
|
|
3545
|
+
: '起不来:' + qrPanel.error
|
|
3546
|
+
log(state.cfg, '扫码页启动失败:' + qrPanel.note)
|
|
3547
|
+
qrPanel.server = null
|
|
3548
|
+
qrPanel.port = 0
|
|
3549
|
+
})
|
|
3550
|
+
server.listen(port, '127.0.0.1', () => {
|
|
3551
|
+
qrPanel.server = server
|
|
3552
|
+
qrPanel.port = port
|
|
3553
|
+
qrPanel.serve = serveQrPage
|
|
3554
|
+
qrPanel.error = ''
|
|
3555
|
+
qrPanel.since = Date.now()
|
|
3556
|
+
qrPanel.note = ''
|
|
3557
|
+
log(state.cfg, `扫码页已起:http://127.0.0.1:${port}/(要扫码时点开它)`)
|
|
3558
|
+
})
|
|
3559
|
+
server.unref?.()
|
|
3560
|
+
return { ok: true, port, note: '正在起' }
|
|
3561
|
+
} catch (error) {
|
|
3562
|
+
qrPanel.error = String((error && error.message) || error)
|
|
3563
|
+
qrPanel.note = '起不来:' + qrPanel.error
|
|
3564
|
+
log(state.cfg, '扫码页启动失败:' + qrPanel.note)
|
|
3565
|
+
return { ok: false, port: 0, note: qrPanel.note }
|
|
3566
|
+
}
|
|
3567
|
+
}
|
|
3568
|
+
|
|
3569
|
+
function qrPanelPublic(cfg) {
|
|
3570
|
+
const port = qrPanel.port || (cfg && cfg.qrPanelPort) || 8899
|
|
3571
|
+
const running = Boolean(qrPanel.server && qrPanel.server.listening)
|
|
3572
|
+
return {
|
|
3573
|
+
enabled: !(cfg && cfg.qrPanelEnabled === false),
|
|
3574
|
+
port,
|
|
3575
|
+
running,
|
|
3576
|
+
url: running ? `http://127.0.0.1:${port}/` : '',
|
|
3577
|
+
path: QR_PANEL_PATH,
|
|
3578
|
+
note: qrPanel.note || (running ? '' : '没在跑'),
|
|
3579
|
+
error: qrPanel.error || '',
|
|
3580
|
+
}
|
|
3581
|
+
}
|
|
3582
|
+
|
|
3712
3583
|
function readBody(req, limit = 262144) {
|
|
3713
3584
|
return new Promise((resolve) => {
|
|
3714
3585
|
let size = 0
|
|
@@ -3830,8 +3701,10 @@ const uiCache = {
|
|
|
3830
3701
|
bootMain: undefined,
|
|
3831
3702
|
bootMainKey: '',
|
|
3832
3703
|
bootMainAt: 0,
|
|
3833
|
-
|
|
3834
|
-
|
|
3704
|
+
/** procsLayout 的目录判形缓存(node 型 / 快登壳型) */
|
|
3705
|
+
layout: 'unknown',
|
|
3706
|
+
layoutKey: '',
|
|
3707
|
+
layoutAt: 0,
|
|
3835
3708
|
qr: null,
|
|
3836
3709
|
qrAt: 0,
|
|
3837
3710
|
log: null,
|
|
@@ -3963,6 +3836,8 @@ async function uiState(cfg, options = {}) {
|
|
|
3963
3836
|
lastOnlineAt: state.lastOnlineAt ? new Date(state.lastOnlineAt).toISOString() : '',
|
|
3964
3837
|
lastProbeAt: state.lastProbeAt || '',
|
|
3965
3838
|
procs: { boot: p.boot, qq: p.qq, chains: p.chains, dup: p.dup, known },
|
|
3839
|
+
/** node / boot / unknown —— 设置页「进程」那行的措辞跟着它变(boot 恒 0 不代表没进程) */
|
|
3840
|
+
layout: procsLayout(cfg, p),
|
|
3966
3841
|
qr: qr,
|
|
3967
3842
|
bridge: bridge,
|
|
3968
3843
|
procRoot: light ? '' : procRootRaw(cfg),
|
|
@@ -3973,24 +3848,11 @@ async function uiState(cfg, options = {}) {
|
|
|
3973
3848
|
},
|
|
3974
3849
|
scan: scanPublic(),
|
|
3975
3850
|
drives: cachedDrives(),
|
|
3976
|
-
|
|
3851
|
+
qrPanel: qrPanelPublic(cfg),
|
|
3977
3852
|
log: cachedLog(cfg, 40),
|
|
3978
3853
|
}
|
|
3979
3854
|
}
|
|
3980
3855
|
|
|
3981
|
-
/** 提权交接那块的状态:light(轮询)时放宽缓存,免得每 2 秒去翻一次 NapCat 目录 */
|
|
3982
|
-
function cachedSetup(cfg, light) {
|
|
3983
|
-
const maxAgeMs = light ? 10000 : 2000
|
|
3984
|
-
if (uiCache.setup && Date.now() - (uiCache.setupAt || 0) < maxAgeMs) return uiCache.setup
|
|
3985
|
-
try {
|
|
3986
|
-
uiCache.setup = setupState(cfg)
|
|
3987
|
-
} catch {
|
|
3988
|
-
uiCache.setup = uiCache.setup || null
|
|
3989
|
-
}
|
|
3990
|
-
uiCache.setupAt = Date.now()
|
|
3991
|
-
return uiCache.setup
|
|
3992
|
-
}
|
|
3993
|
-
|
|
3994
3856
|
/** 并发 /state 合并成一次:崩溃重挂载时不会叠加成一串请求。 */
|
|
3995
3857
|
function uiStateFull(cfg) {
|
|
3996
3858
|
if (uiCache.fullInflight) return uiCache.fullInflight
|
|
@@ -4077,6 +3939,12 @@ function saveAndApply(ctx, patch) {
|
|
|
4077
3939
|
}
|
|
4078
3940
|
}
|
|
4079
3941
|
armTimer(next)
|
|
3942
|
+
// 扫码页的开关/端口可能刚被改过,重新对一遍(端口没变时这里什么都不做)
|
|
3943
|
+
try {
|
|
3944
|
+
startQrPanel(next)
|
|
3945
|
+
} catch (error) {
|
|
3946
|
+
log(next, `扫码页重起失败:${String((error && error.message) || error)}`)
|
|
3947
|
+
}
|
|
4080
3948
|
if (next.enabled) {
|
|
4081
3949
|
tick(next).catch((error) => log(next, `tick after save error: ${String((error && error.message) || error)}`))
|
|
4082
3950
|
}
|
|
@@ -4147,53 +4015,6 @@ async function doAction(ctx, cfg, action, payload) {
|
|
|
4147
4015
|
: `密码已删,但任务重建失败:${made.error || ''}`,
|
|
4148
4016
|
}
|
|
4149
4017
|
}
|
|
4150
|
-
case 'makeSetup': {
|
|
4151
|
-
// 生成「提权交接」脚本:插件自己不动系统,把活写成脚本,由人点一下
|
|
4152
|
-
let wants = []
|
|
4153
|
-
if (payload && Array.isArray(payload.needs)) wants = payload.needs
|
|
4154
|
-
else if (payload && typeof payload.needs === 'string' && payload.needs.trim()) {
|
|
4155
|
-
wants = payload.needs.split(',').map((s) => s.trim()).filter(Boolean)
|
|
4156
|
-
}
|
|
4157
|
-
// 显式传了空数组 = 一条都没勾,别当成「全要」
|
|
4158
|
-
if (payload && Array.isArray(payload.needs) && payload.needs.length === 0) {
|
|
4159
|
-
return { said: '没勾任何要做的事 —— 先在「需要你点一下的脚本」里勾一条,再点生成' }
|
|
4160
|
-
}
|
|
4161
|
-
const made = writeSetupBundle(cfg, wants)
|
|
4162
|
-
uiCache.setup = null
|
|
4163
|
-
uiCache.setupAt = 0
|
|
4164
|
-
if (!made.ok) return { said: `没生成:${made.error}`, setup: made }
|
|
4165
|
-
if (payload && payload.open) openSetupFolder()
|
|
4166
|
-
const adminNote = made.needAdmin ? '(这批要点 UAC 的「是」)' : '(这几步不需要管理员,双击就跑)'
|
|
4167
|
-
return {
|
|
4168
|
-
said: `脚本写好了:${made.dir} —— 双击 apply-admin.cmd ${adminNote},跑完回来点「读结果」`,
|
|
4169
|
-
setup: made,
|
|
4170
|
-
}
|
|
4171
|
-
}
|
|
4172
|
-
case 'openSetup': {
|
|
4173
|
-
const r = openSetupFolder()
|
|
4174
|
-
uiCache.setup = null
|
|
4175
|
-
uiCache.setupAt = 0
|
|
4176
|
-
return { said: r.ok ? `已打开 ${r.dir}` : `打开失败:${r.error}`, opened: r }
|
|
4177
|
-
}
|
|
4178
|
-
case 'setupResult': {
|
|
4179
|
-
uiCache.setup = null
|
|
4180
|
-
uiCache.setupAt = 0
|
|
4181
|
-
const r = readSetupResult()
|
|
4182
|
-
if (!r) return { said: '还没跑过(目录里没有 result.json)' }
|
|
4183
|
-
const bad = r.results.filter((x) => !x.ok)
|
|
4184
|
-
const skipped = r.results.filter((x) => x.skipped)
|
|
4185
|
-
const head = r.mode === 'undo' ? '撤回结果' : '执行结果'
|
|
4186
|
-
return {
|
|
4187
|
-
said:
|
|
4188
|
-
`${head}:${r.results.length - bad.length}/${r.results.length} 步成` +
|
|
4189
|
-
(bad.length
|
|
4190
|
-
? `,没成:${bad.map((x) => `${x.label}(${x.message})`).join(';')}`
|
|
4191
|
-
: '') +
|
|
4192
|
-
(skipped.length ? `,跳过 ${skipped.length} 步` : '') +
|
|
4193
|
-
(r.elevated ? '|拿到管理员' : '|没用管理员'),
|
|
4194
|
-
setupRun: r,
|
|
4195
|
-
}
|
|
4196
|
-
}
|
|
4197
4018
|
case 'checkNode': {
|
|
4198
4019
|
// 清掉缓存再解析一次:用户刚装完 node / 刚改了路径时,想让结果立刻变
|
|
4199
4020
|
uiCache.node = null
|
|
@@ -4303,10 +4124,22 @@ function registerRoutes(ctx) {
|
|
|
4303
4124
|
const lines = match ? Number(match[1]) : 80
|
|
4304
4125
|
sendJson(res, 200, { ok: true, lines: logTail(state.cfg, lines) })
|
|
4305
4126
|
})
|
|
4127
|
+
|
|
4128
|
+
/*--- 扫码页 ---------------------------------------------------------
|
|
4129
|
+
* NapCat 要手机扫码的时候,用户点一个链接就能看见二维码(挑得到的那条都行):
|
|
4130
|
+
* /dsh-napcat-keeper/qr DSH 自己地址下的这一页(会自己刷新)
|
|
4131
|
+
* 127.0.0.1:qrPanelPort/ 同一个页面的独立小服务(默认 8899,任何浏览器都打得开)
|
|
4132
|
+
* 页面里的 qr.png / qr-state / qr-restart 两个入口共用同一份处理。
|
|
4133
|
+
*/
|
|
4134
|
+
route('/qr', async (req, res) => serveQrPage(req, res, state.cfg))
|
|
4135
|
+
route('/qr.png', async (req, res) => serveQrPage(req, res, state.cfg))
|
|
4136
|
+
route('/qr-state', async (req, res) => serveQrPage(req, res, state.cfg))
|
|
4137
|
+
route('/qr-restart', async (req, res) => serveQrPage(req, res, state.cfg))
|
|
4306
4138
|
}
|
|
4307
4139
|
|
|
4308
4140
|
export function apply(ctx, config) {
|
|
4309
4141
|
state.patchConfig = config || {}
|
|
4142
|
+
state.ctx = ctx
|
|
4310
4143
|
const cfg = resolveConfig(state.patchConfig)
|
|
4311
4144
|
state.cfg = cfg
|
|
4312
4145
|
const node = nodePublic(cfg)
|
|
@@ -4325,6 +4158,7 @@ export function apply(ctx, config) {
|
|
|
4325
4158
|
}
|
|
4326
4159
|
registerTools(ctx, cfg)
|
|
4327
4160
|
registerRoutes(ctx)
|
|
4161
|
+
startQrPanel(cfg)
|
|
4328
4162
|
armTimer(cfg)
|
|
4329
4163
|
if (typeof ctx?.on === 'function') {
|
|
4330
4164
|
try {
|
|
@@ -4395,17 +4229,18 @@ export const __internals = {
|
|
|
4395
4229
|
removeEnvBat,
|
|
4396
4230
|
envBatPath,
|
|
4397
4231
|
md5Hex,
|
|
4398
|
-
SETUP_NEEDS,
|
|
4399
|
-
setupPaths,
|
|
4400
|
-
needCatalog,
|
|
4401
|
-
buildSetupSteps,
|
|
4402
|
-
writeSetupBundle,
|
|
4403
|
-
readSetupResult,
|
|
4404
|
-
readSetupPlan,
|
|
4405
|
-
setupState,
|
|
4406
|
-
openSetupFolder,
|
|
4407
|
-
applyPs1Source,
|
|
4408
|
-
dirWritable,
|
|
4409
4232
|
cachedBootMain,
|
|
4233
|
+
procsLayout,
|
|
4234
|
+
taskCommandLine,
|
|
4235
|
+
taskCommandLinePure,
|
|
4236
|
+
// 扫码页(要扫码时给用户点的那个页面)
|
|
4237
|
+
qrPageState,
|
|
4238
|
+
serveQrPage,
|
|
4239
|
+
startQrPanel,
|
|
4240
|
+
stopQrPanel,
|
|
4241
|
+
qrPanelPublic,
|
|
4242
|
+
qrPanelUrl,
|
|
4243
|
+
QR_PANEL_PATH,
|
|
4244
|
+
QR_TTL_SEC,
|
|
4410
4245
|
state,
|
|
4411
4246
|
}
|