dsh-napcat-keeper 2.8.0 → 2.8.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 +30 -0
- package/README.md +52 -4
- package/lib/client.js +110 -10
- package/lib/index.js +676 -38
- package/package.json +5 -2
- package/scripts/selftest-companion.mjs +488 -0
- package/scripts/selftest-layout.mjs +93 -0
- package/scripts/selftest-probe.mjs +6 -1
- package/scripts/smoke-client.mjs +89 -0
package/lib/index.js
CHANGED
|
@@ -1,9 +1,33 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* dsh-napcat-keeper —— NapCat 看门狗 v2.8.
|
|
2
|
+
* dsh-napcat-keeper —— NapCat 看门狗 v2.8.2(可发布通用版)
|
|
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
|
+
*
|
|
16
|
+
* v2.8.1:认得出两个共存插件,端口不用自己抄
|
|
17
|
+
* - 读 dsh-onebot-qq 的配置(profile 的 package.json、cordis.patch.yml / cordis.yml),
|
|
18
|
+
* 得出「装没装 / 挂没挂 / 反向还是正向 / 监听哪个端口 / 令牌配没配」;
|
|
19
|
+
* 你填的端口不通、它确实挂着反向 WS 时自动跟随(companionFollow,默认开),
|
|
20
|
+
* 你填的端口一通就绝不插手;没挂进 profile 或它是正向模式则不跟,并说明为什么;
|
|
21
|
+
* - 读 dsh-qq-bridge 的 ~/.dsh/qq-bridge-config.json,把它填的 NapCat 地址与本插件
|
|
22
|
+
* 看到的实际情况交叉比对,五种结论(没挂载 / 连了反向 WS 端口 / 端口对不上 /
|
|
23
|
+
* 总开关关着 / 试运行中)写进设置页「桥接插件」行与 napcat_status;只看配置,
|
|
24
|
+
* 不碰它的进程和文件;
|
|
25
|
+
* - 两个插件共用一套 profileHits 挂载判定;napcat_status.companion 改成
|
|
26
|
+
* { onebot, bridge }(2.8.0 那份单值结构没发出去,直接换掉);
|
|
27
|
+
* - 「跟随」那一行跟着探测结果走:dsh-onebot-qq 没装 / 没挂载就整行不画、值按关算,
|
|
28
|
+
* 挂进 profile 才出现且默认开;用户显式关过就一直关,升级重启不会掰回开;
|
|
29
|
+
* 设置页保存改成只发有变化的键,saveAndApply 也挡住未启用时的 companionFollow 落盘。
|
|
30
|
+
*
|
|
7
31
|
* v2.8.0:认得出反向 WebSocket(和 dsh-onebot-qq 共存)
|
|
8
32
|
* - NapCat 走反向 WS 时端口是别人在听(dsh-onebot-qq 之类),HTTP 换来 401/426;
|
|
9
33
|
* postOneBot 原先只看 body 不看状态码,于是把「端口在回话」当成「端口不通」
|
|
@@ -147,7 +171,7 @@ import { homedir, tmpdir } from 'node:os'
|
|
|
147
171
|
import path from 'node:path'
|
|
148
172
|
|
|
149
173
|
export const name = 'napcat-keeper'
|
|
150
|
-
export const version = '2.8.
|
|
174
|
+
export const version = '2.8.2'
|
|
151
175
|
|
|
152
176
|
/** tools 用来注册 napcat_* 工具;webServer 用来挂设置界面接口。拿不到也不至于把 DSH 拖崩。 */
|
|
153
177
|
export const inject = ['tools', 'webServer']
|
|
@@ -188,6 +212,12 @@ const DEFAULTS = {
|
|
|
188
212
|
* process 只看进程在不在,不碰端口
|
|
189
213
|
*/
|
|
190
214
|
probeMode: 'auto',
|
|
215
|
+
/**
|
|
216
|
+
* 跟随 dsh-onebot-qq:自己填的端口连不上、而机器上挂着的 dsh-onebot-qq 是反向 WS 模式时,
|
|
217
|
+
* 自动把探测指向它监听的那个端口(用户显式填的端口一旦通了就还是用用户的)。
|
|
218
|
+
* 默认开 —— 这正是「装了反向 WS 插件却一直判离线」那个坑的解药。
|
|
219
|
+
*/
|
|
220
|
+
companionFollow: true,
|
|
191
221
|
/** 只在这些路径下找 QQ/NapCat 进程;留空 = 由 shellDir 推导(上层目录) */
|
|
192
222
|
procPathMatch: '',
|
|
193
223
|
/** 复查间隔(秒) */
|
|
@@ -759,6 +789,12 @@ function psPath(value) {
|
|
|
759
789
|
return String(value || '').replace(/\//g, '\\')
|
|
760
790
|
}
|
|
761
791
|
|
|
792
|
+
/**
|
|
793
|
+
* 所有内联 PowerShell 脚本的公共前缀:把「不需要的错误」压成静默。
|
|
794
|
+
* 三处脚本(数端口客户端 / 进程快照 / 杀晚出现的链)都要它,别再各抄一遍。
|
|
795
|
+
*/
|
|
796
|
+
const PS_QUIET = "$ErrorActionPreference='SilentlyContinue';"
|
|
797
|
+
|
|
762
798
|
/** 进程路径前缀(原样,给 PowerShell 的 -like 用) */
|
|
763
799
|
function procRootRaw(cfg) {
|
|
764
800
|
const explicit = String(cfg.procPathMatch || '').trim()
|
|
@@ -788,6 +824,7 @@ function normalizeConfig(cfg) {
|
|
|
788
824
|
cfg.probeMode = PROBE_MODES.has(String(cfg.probeMode || '').trim().toLowerCase())
|
|
789
825
|
? String(cfg.probeMode).trim().toLowerCase()
|
|
790
826
|
: 'auto'
|
|
827
|
+
cfg.companionFollow = cfg.companionFollow !== false && cfg.companionFollow !== 'false'
|
|
791
828
|
cfg.procPathMatch = String(cfg.procPathMatch || '').trim()
|
|
792
829
|
cfg.taskName = String(cfg.taskName || '').trim()
|
|
793
830
|
cfg.onebotPort = clampNum(cfg.onebotPort, 1, 65535, 3000)
|
|
@@ -816,8 +853,23 @@ function resolveConfig(raw) {
|
|
|
816
853
|
// 老版本的东西摊在 ~/.dsh 根上,先搬进 dir 再读配置(否则读的是搬走前的那份)
|
|
817
854
|
migrateLegacyFiles(dir)
|
|
818
855
|
// 优先级:内置默认 < cordis 里的 config < 设置界面存下来的 json
|
|
819
|
-
const
|
|
856
|
+
const inline = raw || {}
|
|
857
|
+
const stored = readConfigFile(dir)
|
|
858
|
+
// 用户(或本插件自己)显式写过 companionFollow 吗?写过就尊重它,没写过才轮到动态默认值
|
|
859
|
+
const followSet =
|
|
860
|
+
Object.prototype.hasOwnProperty.call(stored, 'companionFollow') ||
|
|
861
|
+
Object.prototype.hasOwnProperty.call(inline, 'companionFollow')
|
|
862
|
+
const cfg = { ...DEFAULTS, ...inline, ...stored }
|
|
820
863
|
normalizeConfig(cfg)
|
|
864
|
+
// 跟随 dsh-onebot-qq:装了且挂进 profile(= 启用)才算数,可用时默认开;
|
|
865
|
+
// 没装或没启用直接按关算 —— 设置页也据此不渲染这一行。
|
|
866
|
+
// companionFollowWant 存「你自己想要的值」(显式关过就是 false,没写过就是默认 true),
|
|
867
|
+
// 真正的 cfg.companionFollow 由「可用吗」压一道闸;这样对面后来才挂上 / 又摘掉时,
|
|
868
|
+
// 不用重启也能算对,而且不会把显式关掉的选择弄丢。
|
|
869
|
+
cfg.companionFollowWant = followSet ? cfg.companionFollow !== false : true
|
|
870
|
+
const comp = detectCompanion()
|
|
871
|
+
cfg.companionReady = companionReady(comp)
|
|
872
|
+
cfg.companionFollow = cfg.companionReady ? cfg.companionFollowWant : false
|
|
821
873
|
// v2.2.2:shellDir 不再自动探测/自动填,空着就等用户在设置页里扫盘选或者手填
|
|
822
874
|
if (!cfg.launcherScript) cfg.launcherScript = detectLauncher(cfg.shellDir, cfg.launchAccount)
|
|
823
875
|
if (!cfg.logPath) cfg.logPath = path.join(dir, 'keeper.log')
|
|
@@ -896,7 +948,7 @@ const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
|
|
|
896
948
|
* 对方根本不会回 OneBot 的 JSON,能拿到的唯一线索就是 401 / 426 这类状态码 ——
|
|
897
949
|
* 老版本把状态码丢了,于是一律当成「端口不通」,接着就是杀掉重拉,无限循环。
|
|
898
950
|
*/
|
|
899
|
-
function postOneBot(cfg, action, payload, timeoutMs = 4000) {
|
|
951
|
+
function postOneBot(cfg, action, payload, timeoutMs = 4000, port = 0) {
|
|
900
952
|
return new Promise((resolve) => {
|
|
901
953
|
const body = JSON.stringify(payload || {})
|
|
902
954
|
const headers = {
|
|
@@ -909,7 +961,7 @@ function postOneBot(cfg, action, payload, timeoutMs = 4000) {
|
|
|
909
961
|
req = http.request(
|
|
910
962
|
{
|
|
911
963
|
host: '127.0.0.1',
|
|
912
|
-
port: cfg.onebotPort,
|
|
964
|
+
port: Number(port) || cfg.onebotPort,
|
|
913
965
|
path: `/${action}`,
|
|
914
966
|
method: 'POST',
|
|
915
967
|
headers,
|
|
@@ -957,6 +1009,397 @@ function postOneBot(cfg, action, payload, timeoutMs = 4000) {
|
|
|
957
1009
|
})
|
|
958
1010
|
}
|
|
959
1011
|
|
|
1012
|
+
/* ------------------------------------------------------------------ */
|
|
1013
|
+
/* 和 dsh-onebot-qq 共存:认得出对面那个反向 WebSocket 服务 */
|
|
1014
|
+
/* */
|
|
1015
|
+
/* 反向 WS 部署里端口是「别人」在听(最常见就是 dsh-onebot-qq),NapCat */
|
|
1016
|
+
/* 反过来连进去。这种部署下用户得自己把两边的端口/令牌抄成一样,抄错就表现 */
|
|
1017
|
+
/* 成「永远离线 + 反复重启」。 */
|
|
1018
|
+
/* 那两样东西其实就在本机的 profile 里,读得出来: */
|
|
1019
|
+
/* <DSH_HOME>/profiles/<profile>/package.json 装了没 / 挂进树没 */
|
|
1020
|
+
/* <DSH_HOME>/profiles/<profile>/cordis.patch.yml 它自己的 config */
|
|
1021
|
+
/* 读出来以后:用户自己填的端口优先,填的那个不通再跟随它的端口;令牌对不上 */
|
|
1022
|
+
/* 就直接说清楚。 */
|
|
1023
|
+
/* */
|
|
1024
|
+
/* 铁律:只读文件、只发 HTTP 探测。绝不对它做 WebSocket 握手 —— */
|
|
1025
|
+
/* dsh-onebot-qq 是 last-wins,真握手会把 NapCat 那条连接顶掉。 */
|
|
1026
|
+
/* ------------------------------------------------------------------ */
|
|
1027
|
+
|
|
1028
|
+
const COMPANION_PACKAGE = 'dsh-onebot-qq'
|
|
1029
|
+
/** 它在 cordis 树里的插件 id(patch 里 insert 的是这个,不是包名) */
|
|
1030
|
+
const COMPANION_ID = 'dsh-onebot'
|
|
1031
|
+
/** dsh-onebot-qq lib/index.js:67-77 那份 Config 的默认值;读不到配置就用它 */
|
|
1032
|
+
const COMPANION_DEFAULTS = {
|
|
1033
|
+
mode: 'reverse',
|
|
1034
|
+
host: '127.0.0.1',
|
|
1035
|
+
port: 8643,
|
|
1036
|
+
url: 'ws://127.0.0.1:3001',
|
|
1037
|
+
accessToken: '',
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
const companionCache = { at: 0, value: null }
|
|
1041
|
+
|
|
1042
|
+
/** 读 JSON,读不出来就 null(profile 里的文件可能正被编辑,不能崩) */
|
|
1043
|
+
function readJsonQuiet(file) {
|
|
1044
|
+
try {
|
|
1045
|
+
const parsed = JSON.parse(readFileSync(file, 'utf8').replace(/^\uFEFF/, ''))
|
|
1046
|
+
return parsed && typeof parsed === 'object' ? parsed : null
|
|
1047
|
+
} catch {
|
|
1048
|
+
return null
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
/** YAML 标量:去引号、去行尾注释(` #` 之后才算注释,URL 里的 // 不算)、`!!js` 认不出就当空 */
|
|
1053
|
+
function yamlScalar(raw) {
|
|
1054
|
+
let text = String(raw == null ? '' : raw).trim()
|
|
1055
|
+
if (!text) return ''
|
|
1056
|
+
if (text.startsWith('!!js')) return ''
|
|
1057
|
+
const quoted = /^(['"])([\s\S]*)\1$/.exec(text)
|
|
1058
|
+
if (quoted) return quoted[2]
|
|
1059
|
+
const hash = text.search(/\s#/)
|
|
1060
|
+
if (hash >= 0) text = text.slice(0, hash).trim()
|
|
1061
|
+
if (text === '~' || text === 'null') return ''
|
|
1062
|
+
// 纯整数当数字(端口这类),别的原样留着
|
|
1063
|
+
if (/^-?\d+$/.test(text)) return Number(text)
|
|
1064
|
+
return text
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
/** 行内 `{ a: 1, b: c }` → [[a,'1'],[b,'c']](不处理嵌套,够用了) */
|
|
1068
|
+
function inlinePairs(text) {
|
|
1069
|
+
const pairs = []
|
|
1070
|
+
const body = String(text || '').trim().replace(/^\{/, '').replace(/\}$/, '')
|
|
1071
|
+
if (!body) return pairs
|
|
1072
|
+
for (const chunk of body.split(',')) {
|
|
1073
|
+
const m = /^\s*([A-Za-z_][\w-]*)\s*:\s*([\s\S]*)$/.exec(chunk)
|
|
1074
|
+
if (m) pairs.push([m[1], yamlScalar(m[2])])
|
|
1075
|
+
}
|
|
1076
|
+
return pairs
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
/**
|
|
1080
|
+
* 从 cordis.patch.yml / cordis.yml 的文本里挖出 dsh-onebot 那一项的 config。
|
|
1081
|
+
*
|
|
1082
|
+
* 不引 YAML 库:这文件是我们自己读的,认不出的写法(`!!js` 表达式等)宁可当没有,
|
|
1083
|
+
* 也不要把半个值当真配置拿去用。
|
|
1084
|
+
*/
|
|
1085
|
+
function parseCompanionConfig(text) {
|
|
1086
|
+
const out = {}
|
|
1087
|
+
const lines = String(text || '')
|
|
1088
|
+
.replace(/^\uFEFF/, '')
|
|
1089
|
+
.split(/\r?\n/)
|
|
1090
|
+
const isEntryLine = (body) => /(^|[{,\s])id:\s*['"]?dsh-onebot['"]?(\s|,|}|$)/.test(body)
|
|
1091
|
+
let inEntry = false
|
|
1092
|
+
let entryIndent = 0
|
|
1093
|
+
let configIndent = -1
|
|
1094
|
+
for (const line of lines) {
|
|
1095
|
+
if (!line.trim() || /^\s*#/.test(line)) continue
|
|
1096
|
+
const indent = line.length - line.trimStart().length
|
|
1097
|
+
if (/^\s*-/.test(line)) {
|
|
1098
|
+
const body = line.replace(/^\s*-\s*/, '')
|
|
1099
|
+
if (isEntryLine(body)) {
|
|
1100
|
+
inEntry = true
|
|
1101
|
+
entryIndent = indent
|
|
1102
|
+
configIndent = -1
|
|
1103
|
+
const cm = /config:\s*(\{[\s\S]*\})\s*$/.exec(body)
|
|
1104
|
+
if (cm) {
|
|
1105
|
+
for (const [k, v] of inlinePairs(cm[1])) if (v !== '') out[k] = v
|
|
1106
|
+
}
|
|
1107
|
+
continue
|
|
1108
|
+
}
|
|
1109
|
+
// 同级的下一条 insert 项:上一条结束了
|
|
1110
|
+
if (indent <= entryIndent) {
|
|
1111
|
+
inEntry = false
|
|
1112
|
+
configIndent = -1
|
|
1113
|
+
}
|
|
1114
|
+
continue
|
|
1115
|
+
}
|
|
1116
|
+
if (!inEntry) continue
|
|
1117
|
+
if (indent <= entryIndent) {
|
|
1118
|
+
inEntry = false
|
|
1119
|
+
configIndent = -1
|
|
1120
|
+
continue
|
|
1121
|
+
}
|
|
1122
|
+
if (/^\s*config:\s*$/.test(line)) {
|
|
1123
|
+
configIndent = indent
|
|
1124
|
+
continue
|
|
1125
|
+
}
|
|
1126
|
+
const inline = /^\s*config:\s*(\{[\s\S]*\})\s*$/.exec(line)
|
|
1127
|
+
if (inline) {
|
|
1128
|
+
for (const [k, v] of inlinePairs(inline[1])) if (v !== '') out[k] = v
|
|
1129
|
+
configIndent = -1
|
|
1130
|
+
continue
|
|
1131
|
+
}
|
|
1132
|
+
if (configIndent >= 0 && indent > configIndent) {
|
|
1133
|
+
const m = /^\s*([A-Za-z_][\w-]*)\s*:\s*(.*)$/.exec(line)
|
|
1134
|
+
if (m) {
|
|
1135
|
+
const value = yamlScalar(m[2])
|
|
1136
|
+
if (value !== '') out[m[1]] = value
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
return out
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
/**
|
|
1144
|
+
* 扫一遍 `~/.dsh/profiles/*`,找出「装了这个包」的 profile。
|
|
1145
|
+
*
|
|
1146
|
+
* 两个共存插件(dsh-onebot-qq / dsh-qq-bridge)用的是同一套判断:
|
|
1147
|
+
* - 装了 = 它的 package.json 在 node_modules 里,或者写在 profile 的 dependencies 里;
|
|
1148
|
+
* - 挂了 = 出现在 `dsh.profile.bundles` 里,或者被 `cordis.patch.yml` / `cordis.yml`
|
|
1149
|
+
* 单独 insert 过(认 `id: xxx`)。只装在 dependencies 里而没进 bundles 的,DSH 不会加载它,
|
|
1150
|
+
* 端口/路由都不会起来 —— 这个区别很要紧,不能混成一句「已安装」。
|
|
1151
|
+
*
|
|
1152
|
+
* 正在跑的那个 profile(desktop)排最前:多 profile 并存时优先信它。
|
|
1153
|
+
*/
|
|
1154
|
+
function profileHits(pkgName, bundleId) {
|
|
1155
|
+
const root = path.join(dshHome(), 'profiles')
|
|
1156
|
+
let dirs = []
|
|
1157
|
+
try {
|
|
1158
|
+
dirs = readdirSync(root, { withFileTypes: true })
|
|
1159
|
+
.filter((d) => d.isDirectory())
|
|
1160
|
+
.map((d) => path.join(root, d.name))
|
|
1161
|
+
} catch {
|
|
1162
|
+
dirs = []
|
|
1163
|
+
}
|
|
1164
|
+
const current = path.join(root, 'desktop')
|
|
1165
|
+
dirs.sort((a, b) => (a === current ? -1 : b === current ? 1 : a.localeCompare(b)))
|
|
1166
|
+
|
|
1167
|
+
const idRe = new RegExp(`(^|[{,\\s])id:\\s*['"]?${bundleId}['"]?(\\s|,|}|$)`)
|
|
1168
|
+
const hits = []
|
|
1169
|
+
for (const dir of dirs) {
|
|
1170
|
+
const pkg = readJsonQuiet(path.join(dir, 'package.json'))
|
|
1171
|
+
const deps = (pkg && pkg.dependencies) || {}
|
|
1172
|
+
const meta = readJsonQuiet(path.join(dir, 'node_modules', pkgName, 'package.json'))
|
|
1173
|
+
if (!meta && !Object.prototype.hasOwnProperty.call(deps, pkgName)) continue
|
|
1174
|
+
|
|
1175
|
+
const bundles = (pkg && pkg.dsh && pkg.dsh.profile && pkg.dsh.profile.bundles) || []
|
|
1176
|
+
const patches = []
|
|
1177
|
+
for (const name of ['cordis.patch.yml', 'cordis.yml']) {
|
|
1178
|
+
const file = path.join(dir, name)
|
|
1179
|
+
let text = ''
|
|
1180
|
+
try {
|
|
1181
|
+
text = readFileSync(file, 'utf8')
|
|
1182
|
+
} catch {
|
|
1183
|
+
continue
|
|
1184
|
+
}
|
|
1185
|
+
if (text.includes(pkgName) || idRe.test(text)) patches.push({ name, text })
|
|
1186
|
+
}
|
|
1187
|
+
hits.push({
|
|
1188
|
+
dir,
|
|
1189
|
+
profile: path.basename(dir),
|
|
1190
|
+
version: String((meta && meta.version) || deps[pkgName] || ''),
|
|
1191
|
+
bundles,
|
|
1192
|
+
patches,
|
|
1193
|
+
mounted: bundles.includes(pkgName) || patches.length > 0,
|
|
1194
|
+
})
|
|
1195
|
+
}
|
|
1196
|
+
return hits
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
/** 它装没装、挂没挂、自己配成什么样(20 秒缓存;改了 profile 也不用等太久) */
|
|
1200
|
+
function detectCompanion(options = {}) {
|
|
1201
|
+
const force = Boolean(options && options.force)
|
|
1202
|
+
const maxAgeMs = Number.isFinite(Number(options && options.maxAgeMs))
|
|
1203
|
+
? Math.max(0, Number(options.maxAgeMs))
|
|
1204
|
+
: 20000
|
|
1205
|
+
if (!force && companionCache.value && Date.now() - companionCache.at < maxAgeMs) {
|
|
1206
|
+
return companionCache.value
|
|
1207
|
+
}
|
|
1208
|
+
const result = {
|
|
1209
|
+
found: false,
|
|
1210
|
+
packageVersion: '',
|
|
1211
|
+
profile: '',
|
|
1212
|
+
mounted: false,
|
|
1213
|
+
mode: '',
|
|
1214
|
+
host: '',
|
|
1215
|
+
port: 0,
|
|
1216
|
+
url: '',
|
|
1217
|
+
/** 只有能读到明文时才有值;界面上永远不外发,只回「配没配、一致不」 */
|
|
1218
|
+
token: '',
|
|
1219
|
+
tokenKnown: false,
|
|
1220
|
+
source: '',
|
|
1221
|
+
candidates: [],
|
|
1222
|
+
}
|
|
1223
|
+
for (const hit of profileHits(COMPANION_PACKAGE, COMPANION_ID)) {
|
|
1224
|
+
const patchConfig = {}
|
|
1225
|
+
for (const p of hit.patches) Object.assign(patchConfig, parseCompanionConfig(p.text))
|
|
1226
|
+
|
|
1227
|
+
const cfg = { ...COMPANION_DEFAULTS, ...patchConfig }
|
|
1228
|
+
const candidate = {
|
|
1229
|
+
found: true,
|
|
1230
|
+
packageVersion: hit.version,
|
|
1231
|
+
profile: hit.dir,
|
|
1232
|
+
mounted: hit.mounted,
|
|
1233
|
+
mode: String(cfg.mode || '').toLowerCase() === 'forward' ? 'forward' : 'reverse',
|
|
1234
|
+
host: String(cfg.host || '').trim() || COMPANION_DEFAULTS.host,
|
|
1235
|
+
port: Number(cfg.port) || COMPANION_DEFAULTS.port,
|
|
1236
|
+
url: String(cfg.url || '').trim() || COMPANION_DEFAULTS.url,
|
|
1237
|
+
token: String(cfg.accessToken || ''),
|
|
1238
|
+
tokenKnown: Object.prototype.hasOwnProperty.call(patchConfig, 'accessToken'),
|
|
1239
|
+
source: Object.keys(patchConfig).length ? 'patch' : 'default',
|
|
1240
|
+
}
|
|
1241
|
+
result.candidates.push(candidate)
|
|
1242
|
+
if (!result.found || (candidate.mounted && !result.mounted)) Object.assign(result, candidate)
|
|
1243
|
+
if (candidate.mounted) break
|
|
1244
|
+
}
|
|
1245
|
+
companionCache.at = Date.now()
|
|
1246
|
+
companionCache.value = result
|
|
1247
|
+
return result
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
/**
|
|
1251
|
+
* 「跟随 dsh-onebot-qq」能不能开:装了不算数,得真挂进 profile(bundles 或 cordis patch)。
|
|
1252
|
+
* 只在 dependencies 里躺着的时候它根本不会起,跟着它去看端口只会白等。
|
|
1253
|
+
* 两种入参都吃:detectCompanion() 的完整结果(带 found),以及 companionPublic() 的瘦身版(没有 found,只带 version/profile)。
|
|
1254
|
+
*/
|
|
1255
|
+
function companionReady(comp) {
|
|
1256
|
+
if (!comp) return false
|
|
1257
|
+
const found = Object.prototype.hasOwnProperty.call(comp, 'found')
|
|
1258
|
+
? Boolean(comp.found)
|
|
1259
|
+
: Boolean(comp.version || comp.profile)
|
|
1260
|
+
return found && Boolean(comp.mounted)
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
/** 给界面看的那部分(token / tokenKnown 之外的敏感信息一律不出这个函数) */
|
|
1264
|
+
function companionPublic(comp) {
|
|
1265
|
+
if (!comp || !comp.found) return null
|
|
1266
|
+
return {
|
|
1267
|
+
version: comp.packageVersion,
|
|
1268
|
+
profile: path.basename(String(comp.profile || '')),
|
|
1269
|
+
mounted: Boolean(comp.mounted),
|
|
1270
|
+
mode: comp.mode,
|
|
1271
|
+
host: comp.host,
|
|
1272
|
+
port: comp.port,
|
|
1273
|
+
url: comp.url,
|
|
1274
|
+
tokenKnown: Boolean(comp.tokenKnown),
|
|
1275
|
+
source: comp.source,
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
/** 两边的 accessToken 一样吗?读不到对面的就返回 null(未知 ≠ 不一致) */
|
|
1280
|
+
function companionTokenMatch(cfg, comp) {
|
|
1281
|
+
if (!comp || !comp.tokenKnown) return null
|
|
1282
|
+
return String(cfg.accessToken || '') === String(comp.token || '')
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
/* ------------------------------------------------------------------ *
|
|
1286
|
+
* 第二个共存插件:dsh-qq-bridge(把 NapCat 的消息接到 QQ 聊天里)
|
|
1287
|
+
*
|
|
1288
|
+
* 它和 dsh-onebot-qq 是两种东西:onebot 提供「接口」,qq-bridge 是「客户端」——
|
|
1289
|
+
* 它拿 HTTP/WS 去连 NapCat,所以两边端口必须对上,对不上就静默不工作。
|
|
1290
|
+
* 它的配置不在 profile 里,摊在 ~/.dsh/qq-bridge-config.json(中文键名)。
|
|
1291
|
+
* ------------------------------------------------------------------ */
|
|
1292
|
+
|
|
1293
|
+
const BRIDGE_PACKAGE = 'dsh-qq-bridge'
|
|
1294
|
+
const BRIDGE_ID = 'qq-bridge'
|
|
1295
|
+
const BRIDGE_DEFAULTS = { napcat: 'http://127.0.0.1:3000', enabled: false, dryRun: true }
|
|
1296
|
+
const bridgeCache = { at: 0, value: null }
|
|
1297
|
+
|
|
1298
|
+
/** 从 OneBot 地址里抠端口(认不出就 0,不要瞎猜) */
|
|
1299
|
+
function urlPort(raw) {
|
|
1300
|
+
try {
|
|
1301
|
+
const u = new URL(String(raw || ''))
|
|
1302
|
+
if (u.port) return Number(u.port)
|
|
1303
|
+
return u.protocol === 'https:' ? 443 : 80
|
|
1304
|
+
} catch {
|
|
1305
|
+
return 0
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
/** 读它自己的配置文件;读不到就当「不知道」,别当成「没配」 */
|
|
1310
|
+
function readBridgeConfig() {
|
|
1311
|
+
const file = path.join(dshHome(), 'qq-bridge-config.json')
|
|
1312
|
+
const json = readJsonQuiet(file)
|
|
1313
|
+
return { file, known: Boolean(json && typeof json === 'object'), cfg: json || null }
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
/** 它装没装、挂没挂、桥接指向哪个端口(20 秒缓存) */
|
|
1317
|
+
function detectBridge(options = {}) {
|
|
1318
|
+
const force = Boolean(options && options.force)
|
|
1319
|
+
const maxAgeMs = Number.isFinite(Number(options && options.maxAgeMs))
|
|
1320
|
+
? Math.max(0, Number(options.maxAgeMs))
|
|
1321
|
+
: 20000
|
|
1322
|
+
if (!force && bridgeCache.value && Date.now() - bridgeCache.at < maxAgeMs) return bridgeCache.value
|
|
1323
|
+
|
|
1324
|
+
const hits = profileHits(BRIDGE_PACKAGE, BRIDGE_ID)
|
|
1325
|
+
const top = hits.find((h) => h.mounted) || hits[0] || null
|
|
1326
|
+
const rc = readBridgeConfig()
|
|
1327
|
+
const cfg = rc.cfg || {}
|
|
1328
|
+
const napcatUrl = String(cfg.NapCat || '').trim() || BRIDGE_DEFAULTS.napcat
|
|
1329
|
+
|
|
1330
|
+
const result = {
|
|
1331
|
+
found: Boolean(top),
|
|
1332
|
+
packageVersion: String((top && top.version) || ''),
|
|
1333
|
+
profile: String((top && top.profile) || ''),
|
|
1334
|
+
mounted: Boolean(top && top.mounted),
|
|
1335
|
+
configFile: rc.file,
|
|
1336
|
+
configKnown: rc.known,
|
|
1337
|
+
enabled: rc.known ? cfg.总开关 === true : BRIDGE_DEFAULTS.enabled,
|
|
1338
|
+
dryRun: rc.known ? cfg.试运行 !== false : BRIDGE_DEFAULTS.dryRun,
|
|
1339
|
+
napcatUrl,
|
|
1340
|
+
napcatPort: urlPort(napcatUrl),
|
|
1341
|
+
tokenKnown: rc.known ? String(cfg.令牌 || '').trim() !== '' : false,
|
|
1342
|
+
bindings: rc.known && Array.isArray(cfg.绑定) ? cfg.绑定.length : 0,
|
|
1343
|
+
candidates: hits.map((h) => ({ profile: h.profile, mounted: h.mounted, version: h.version })),
|
|
1344
|
+
}
|
|
1345
|
+
bridgeCache.at = Date.now()
|
|
1346
|
+
bridgeCache.value = result
|
|
1347
|
+
return result
|
|
1348
|
+
}
|
|
1349
|
+
|
|
1350
|
+
/** 给界面/工具看的那部分(令牌只回「配没配」,不回来路不明的明文) */
|
|
1351
|
+
function bridgePublic(bridge) {
|
|
1352
|
+
if (!bridge || !bridge.found) return null
|
|
1353
|
+
return {
|
|
1354
|
+
version: bridge.packageVersion,
|
|
1355
|
+
profile: bridge.profile,
|
|
1356
|
+
mounted: bridge.mounted,
|
|
1357
|
+
enabled: bridge.enabled,
|
|
1358
|
+
dryRun: bridge.dryRun,
|
|
1359
|
+
napcatUrl: bridge.napcatUrl,
|
|
1360
|
+
napcatPort: bridge.napcatPort,
|
|
1361
|
+
tokenKnown: bridge.tokenKnown,
|
|
1362
|
+
bindings: bridge.bindings,
|
|
1363
|
+
configKnown: bridge.configKnown,
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1367
|
+
/**
|
|
1368
|
+
* 拿看护的探测结果和桥接配置对一遍:谁的端口指错了、谁的开关没开。
|
|
1369
|
+
*
|
|
1370
|
+
* 顺序按「改起来最省事、后果最严重」排:
|
|
1371
|
+
* 没挂进 profile → 根本没跑,什么都别谈
|
|
1372
|
+
* 指着反向 WS 端口 → HTTP 客户端敲 WebSocket 端口,永远没响应
|
|
1373
|
+
* 端口对不上 → 敲的是空气
|
|
1374
|
+
* 总开关关 / 试运行 → 程序在跑,但一句都不发
|
|
1375
|
+
*/
|
|
1376
|
+
function bridgeCheck(health, bridge) {
|
|
1377
|
+
if (!bridge || !bridge.found) return { state: 'absent', note: '' }
|
|
1378
|
+
const portUsed = Number((health && health.portUsed) || 0)
|
|
1379
|
+
const bp = Number(bridge.napcatPort || 0)
|
|
1380
|
+
const reverse = String((health && health.mode) || '') === 'reverse'
|
|
1381
|
+
|
|
1382
|
+
let state = 'ok'
|
|
1383
|
+
let note = ''
|
|
1384
|
+
if (!bridge.mounted) {
|
|
1385
|
+
state = 'not-mounted'
|
|
1386
|
+
note = 'dsh-qq-bridge 装了但没挂进 profile(bundles 里没有它、patch 也没 insert),桥接不会跑'
|
|
1387
|
+
} else if (reverse && bp && portUsed && bp === portUsed) {
|
|
1388
|
+
state = 'bridge-hits-reverse'
|
|
1389
|
+
note = `dsh-qq-bridge 连的是 ${bridge.napcatUrl},而那个端口现在是反向 WebSocket(只收 WebSocket 握手)—— 桥接用 HTTP 敲它就是 426/401。让它改连 NapCat 自己 httpServers 的端口`
|
|
1390
|
+
} else if (!reverse && bp && portUsed && bp !== portUsed) {
|
|
1391
|
+
state = 'port-mismatch'
|
|
1392
|
+
note = `dsh-qq-bridge 连的是 ${bridge.napcatUrl}(端口 ${bp}),NapCat 的 OneBot 接口在 ${portUsed} —— 两边对不上,消息发不出去`
|
|
1393
|
+
} else if (bridge.configKnown && !bridge.enabled) {
|
|
1394
|
+
state = 'disabled'
|
|
1395
|
+
note = 'dsh-qq-bridge 的「总开关」是关的:它在跑,但一句都不会发'
|
|
1396
|
+
} else if (bridge.configKnown && bridge.dryRun) {
|
|
1397
|
+
state = 'dry-run'
|
|
1398
|
+
note = 'dsh-qq-bridge 开着「试运行」:只记账,不真发到 QQ'
|
|
1399
|
+
}
|
|
1400
|
+
return { state, note, bridgePort: bp, portUsed, reverse }
|
|
1401
|
+
}
|
|
1402
|
+
|
|
960
1403
|
const PROBE_MODES = new Set(['auto', 'http', 'reverse', 'process'])
|
|
961
1404
|
|
|
962
1405
|
/** portClients 的缓存:设置页轮询很密,别每 1.5 秒起一个 powershell */
|
|
@@ -973,24 +1416,24 @@ const linksCache = { port: 0, at: 0, value: null, inflight: null }
|
|
|
973
1416
|
* 查不出来就返回 count: null。「查不到」和「没连上」是两回事,
|
|
974
1417
|
* 混成一个就会又变成瞎判离线、瞎杀进程。
|
|
975
1418
|
*/
|
|
976
|
-
async function portClients(cfg, maxAgeMs = 5000) {
|
|
977
|
-
const
|
|
978
|
-
if (!
|
|
1419
|
+
async function portClients(cfg, port = 0, maxAgeMs = 5000) {
|
|
1420
|
+
const p = Number(port) || Number(cfg.onebotPort) || 0
|
|
1421
|
+
if (!p) return { count: null, pids: [], source: 'no-port' }
|
|
979
1422
|
// 设置页 1.5s 轮询 /state,而这个函数要新开一个 powershell:
|
|
980
1423
|
// 缓存 + 同刻只允许一个子进程,否则又是一场 PowerShell 风暴(见 uiCache 那段注释)
|
|
981
|
-
if (linksCache.port ===
|
|
1424
|
+
if (linksCache.port === p && linksCache.value && Date.now() - linksCache.at < maxAgeMs) {
|
|
982
1425
|
return linksCache.value
|
|
983
1426
|
}
|
|
984
|
-
if (linksCache.port ===
|
|
985
|
-
linksCache.port =
|
|
1427
|
+
if (linksCache.port === p && linksCache.inflight) return linksCache.inflight
|
|
1428
|
+
linksCache.port = p
|
|
986
1429
|
linksCache.inflight = (async () => {
|
|
987
1430
|
const self = String(process.pid)
|
|
988
1431
|
// 看的是「谁连进了这个端口」,所以按 RemotePort 找客户端那一侧:
|
|
989
1432
|
// 按 LocalPort 找会命中服务端那条 socket,而它的所有者是跑反向 WS 服务的进程(就是 DSH 自己),
|
|
990
1433
|
// 把它排除掉就一条都数不到了 —— 这个坑第一版就踩了。
|
|
991
1434
|
const script =
|
|
992
|
-
|
|
993
|
-
`$c=@(Get-NetTCPConnection -State Established -RemotePort ${
|
|
1435
|
+
PS_QUIET +
|
|
1436
|
+
`$c=@(Get-NetTCPConnection -State Established -RemotePort ${p} -ErrorAction SilentlyContinue | ` +
|
|
994
1437
|
`Where-Object { [string]$_.OwningProcess -ne '${self}' });` +
|
|
995
1438
|
`'CLIENTS=' + $c.Count + ' PIDS=' + (($c | Select-Object -ExpandProperty OwningProcess -Unique) -join ',')`
|
|
996
1439
|
const out = await runPs(script, 12000)
|
|
@@ -1021,9 +1464,8 @@ async function portClients(cfg, maxAgeMs = 5000) {
|
|
|
1021
1464
|
* dead 连不上,端口没开
|
|
1022
1465
|
* process 配置里关了端口探测,只看进程
|
|
1023
1466
|
*/
|
|
1024
|
-
async function
|
|
1025
|
-
const
|
|
1026
|
-
const login = await postOneBot(cfg, 'get_login_info')
|
|
1467
|
+
async function probeOnPort(cfg, port, want) {
|
|
1468
|
+
const login = await postOneBot(cfg, 'get_login_info', {}, 4000, port)
|
|
1027
1469
|
const data = login && login.data
|
|
1028
1470
|
const status = Number(login && login.status) || 0
|
|
1029
1471
|
const online = Boolean(login && login.retcode === 0 && data && (data.user_id || data.uin))
|
|
@@ -1055,7 +1497,7 @@ async function probe(cfg) {
|
|
|
1055
1497
|
return result
|
|
1056
1498
|
}
|
|
1057
1499
|
// 端口肯回 HTTP,说明有个服务在听 —— 这本身就意味着「进程是活的」,不该被杀
|
|
1058
|
-
const links = await portClients(cfg)
|
|
1500
|
+
const links = await portClients(cfg, port)
|
|
1059
1501
|
result.clients = links.count
|
|
1060
1502
|
if (want === 'reverse') {
|
|
1061
1503
|
result.mode = 'reverse'
|
|
@@ -1089,12 +1531,115 @@ async function probe(cfg) {
|
|
|
1089
1531
|
return result
|
|
1090
1532
|
}
|
|
1091
1533
|
|
|
1534
|
+
/**
|
|
1535
|
+
* 探测「NapCat 现在在线吗」,并顺手处理「端口是别人在听」这件事。
|
|
1536
|
+
*
|
|
1537
|
+
* 顺序:
|
|
1538
|
+
* 1. 先探用户自己填的端口(onebotPort)—— 用户显式配置永远优先;
|
|
1539
|
+
* 2. 那个端口不通/不像 OneBot,而机器上挂着 dsh-onebot-qq(反向 WS),
|
|
1540
|
+
* 就去它的端口再问一次:那边只收 WebSocket,会回 426/401,但那已经能说明
|
|
1541
|
+
* 「服务在听、NapCat 有地方连」,这时把 portUsed 指过去(followed=true);
|
|
1542
|
+
* 3. 提示里带上令牌一致性 / 有没有真的挂载 —— 这两条是最常见的人为卡点。
|
|
1543
|
+
*
|
|
1544
|
+
* 所有分支都返回 portUsed(真正在看的端口)和 followed(是不是跟随了对面),
|
|
1545
|
+
* 看门狗和界面都拿这两个值说话,别再各自猜 cfg.onebotPort。
|
|
1546
|
+
*/
|
|
1547
|
+
async function probe(cfg, options = {}) {
|
|
1548
|
+
const want = PROBE_MODES.has(cfg.probeMode) ? cfg.probeMode : 'auto'
|
|
1549
|
+
const ownPort = Number(cfg.onebotPort) || 0
|
|
1550
|
+
const comp =
|
|
1551
|
+
options && Object.prototype.hasOwnProperty.call(options, 'companion')
|
|
1552
|
+
? options.companion
|
|
1553
|
+
: detectCompanion()
|
|
1554
|
+
const tokenMatch = companionTokenMatch(cfg, comp)
|
|
1555
|
+
// 桥接(dsh-qq-bridge)只是「顺便看一眼」,不参与「探哪个端口」的决策,所以可以注入桩
|
|
1556
|
+
const bridge =
|
|
1557
|
+
options && Object.prototype.hasOwnProperty.call(options, 'bridge') ? options.bridge : detectBridge()
|
|
1558
|
+
const wrap = (health, extra = {}) => {
|
|
1559
|
+
const out = {
|
|
1560
|
+
...health,
|
|
1561
|
+
portUsed: health.portUsed || ownPort,
|
|
1562
|
+
followed: Boolean(extra.followed),
|
|
1563
|
+
primaryPort: ownPort,
|
|
1564
|
+
primaryMode: extra.primaryMode || health.mode,
|
|
1565
|
+
companion: companionPublic(comp),
|
|
1566
|
+
companionTokenMatch: tokenMatch,
|
|
1567
|
+
}
|
|
1568
|
+
const check = bridgeCheck(out, bridge)
|
|
1569
|
+
const pub = bridgePublic(bridge)
|
|
1570
|
+
// 提示文案统一挂在 bridge.note 上,不塞进 hint:现有自检对 hint 有精确断言,
|
|
1571
|
+
// 而且「桥接指错端口」和「NapCat 自己正不正常」是两件事,混在一句话里反而看不清。
|
|
1572
|
+
out.bridge = pub ? { ...pub, check: check.state, note: check.note } : null
|
|
1573
|
+
return out
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
const first = await probeOnPort(cfg, ownPort, want)
|
|
1577
|
+
first.portUsed = ownPort
|
|
1578
|
+
if (first.online || want === 'process') return wrap(first)
|
|
1579
|
+
|
|
1580
|
+
const follow =
|
|
1581
|
+
cfg.companionFollow !== false &&
|
|
1582
|
+
comp &&
|
|
1583
|
+
comp.found &&
|
|
1584
|
+
comp.mounted &&
|
|
1585
|
+
comp.mode === 'reverse' &&
|
|
1586
|
+
Number(comp.port) > 0 &&
|
|
1587
|
+
Number(comp.port) !== ownPort
|
|
1588
|
+
|
|
1589
|
+
if (follow) {
|
|
1590
|
+
const alt = await probeOnPort(cfg, Number(comp.port), 'auto')
|
|
1591
|
+
alt.portUsed = Number(comp.port)
|
|
1592
|
+
if (alt.mode === 'reverse' || alt.online) {
|
|
1593
|
+
const hintBits = []
|
|
1594
|
+
if (alt.hint) hintBits.push(alt.hint)
|
|
1595
|
+
hintBits.push(
|
|
1596
|
+
`你填的 ${ownPort} 上没有 OneBot 服务,已跟随 dsh-onebot-qq 的反向 WS 端口 ${comp.port}`,
|
|
1597
|
+
)
|
|
1598
|
+
if (tokenMatch === false) {
|
|
1599
|
+
hintBits.push('⚠ 两边的 accessToken 不一致:NapCat 连上去会被 4401 拒绝,改成一样再试')
|
|
1600
|
+
} else if (comp.tokenKnown && tokenMatch === true && comp.token) {
|
|
1601
|
+
hintBits.push('accessToken 两边一致')
|
|
1602
|
+
}
|
|
1603
|
+
alt.hint = hintBits.join(';')
|
|
1604
|
+
if (alt.error) alt.error = alt.hint
|
|
1605
|
+
return wrap(alt, { followed: true, primaryMode: first.mode })
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
// 没跟上:把「为什么没跟上」讲清楚,别让人自己去猜
|
|
1610
|
+
const bits = []
|
|
1611
|
+
if (first.hint) bits.push(first.hint)
|
|
1612
|
+
if (comp && comp.found) {
|
|
1613
|
+
if (!comp.mounted) {
|
|
1614
|
+
bits.push(
|
|
1615
|
+
`检测到 ${COMPANION_PACKAGE} 已安装(${comp.packageVersion || '版本未知'})但没挂进 profile:` +
|
|
1616
|
+
'bundles 里没有它、patch 里也没有 insert,所以它的反向 WS 端口根本不会开',
|
|
1617
|
+
)
|
|
1618
|
+
} else if (comp.mode === 'forward') {
|
|
1619
|
+
bits.push(
|
|
1620
|
+
`${COMPANION_PACKAGE} 现在是 forward 模式(主动连 ${comp.url}):此时端口该由 NapCat 自己开,` +
|
|
1621
|
+
'反向 WS 那条路用不上',
|
|
1622
|
+
)
|
|
1623
|
+
} else if (comp.port && comp.port !== ownPort && cfg.companionFollow === false) {
|
|
1624
|
+
bits.push(`${COMPANION_PACKAGE} 的反向 WS 在 ${comp.port},但「跟随 dsh-onebot-qq」关了`)
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
|
|
1628
|
+
// 端口回 401 且我们有令牌:多半就是两边令牌不一致,这里直接点破
|
|
1629
|
+
if (first.mode === 'auth' && comp && comp.found && tokenMatch === false) {
|
|
1630
|
+
bits.push(`⚠ ${COMPANION_PACKAGE} 配的 accessToken 和本插件不一致(它的是 ${comp.token ? '另一串' : '空'}),这件事光重启 NapCat 解决不了`)
|
|
1631
|
+
}
|
|
1632
|
+
const merged = bits.filter(Boolean).join(';')
|
|
1633
|
+
if (merged) first.hint = merged
|
|
1634
|
+
return wrap(first, { primaryMode: first.mode })
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1092
1637
|
/* ------------------------------------------------------------------ */
|
|
1093
1638
|
/* 进程链 / 二维码 / 桥接新鲜度 */
|
|
1094
1639
|
/* ------------------------------------------------------------------ */
|
|
1095
1640
|
|
|
1096
1641
|
const PS_PROCS =
|
|
1097
|
-
|
|
1642
|
+
PS_QUIET +
|
|
1098
1643
|
// node 也要算:无头版(Node 包)只有 node.exe 一条进程,没有 QQ.exe
|
|
1099
1644
|
"Get-Process QQ,NapCatWinBootMain,node | " +
|
|
1100
1645
|
'ForEach-Object { $p=$null; try { $p=$_.Path } catch {}; ' +
|
|
@@ -1611,17 +2156,21 @@ function removeEnvBat(cfg) {
|
|
|
1611
2156
|
} catch {}
|
|
1612
2157
|
}
|
|
1613
2158
|
|
|
1614
|
-
/**
|
|
1615
|
-
function
|
|
1616
|
-
// 记住密码时改走中间层(set 环境变量 → call 原脚本),其余情况原样直跑
|
|
1617
|
-
const viaEnv = passwordActive(cfg) ? writeEnvBat(cfg, bat) : ''
|
|
1618
|
-
const target = viaEnv || bat
|
|
2159
|
+
/** 命令行的公共模板:chcp 65001 + 切到 Shell 目录 + call 启动脚本,输出进 launcherLog */
|
|
2160
|
+
function taskCmdLineOf(cfg, target) {
|
|
1619
2161
|
return (
|
|
1620
2162
|
`cmd.exe /c chcp 65001 >nul & cd /d "${cfg.shellDir}" & call "${target}" ` +
|
|
1621
2163
|
`>> "${cfg.launcherLog}" 2>&1`
|
|
1622
2164
|
)
|
|
1623
2165
|
}
|
|
1624
2166
|
|
|
2167
|
+
/** 真正要跑的那条 cmd 命令 */
|
|
2168
|
+
function taskCommandLine(cfg, bat) {
|
|
2169
|
+
// 记住密码时改走中间层(set 环境变量 → call 原脚本),其余情况原样直跑
|
|
2170
|
+
const viaEnv = passwordActive(cfg) ? writeEnvBat(cfg, bat) : ''
|
|
2171
|
+
return taskCmdLineOf(cfg, viaEnv || bat)
|
|
2172
|
+
}
|
|
2173
|
+
|
|
1625
2174
|
/**
|
|
1626
2175
|
* 写一个 WScript 壳:sh.Run cmd, 0, False → 子进程以“隐藏窗口”启动,
|
|
1627
2176
|
* 桌面不会多出那个黑框(原来计划任务直接跑 cmd.exe 就会挂一个控制台窗口)。
|
|
@@ -1694,7 +2243,7 @@ function ensureTask(cfg) {
|
|
|
1694
2243
|
)
|
|
1695
2244
|
}
|
|
1696
2245
|
const ps =
|
|
1697
|
-
|
|
2246
|
+
PS_QUIET +
|
|
1698
2247
|
`$t=Get-ScheduledTask -TaskName '${cfg.taskName.replace(/'/g, "''")}';` +
|
|
1699
2248
|
`if($t){$s=$t.Settings;$s.ExecutionTimeLimit='PT0S';$s.DisallowStartIfOnBatteries=$false;` +
|
|
1700
2249
|
`$s.StopIfGoingOnBatteries=$false;Set-ScheduledTask -TaskName '${cfg.taskName.replace(/'/g, "''")}' -Settings $s | Out-Null}`
|
|
@@ -1844,6 +2393,37 @@ function cachedBootMain(cfg, maxAgeMs = 60000) {
|
|
|
1844
2393
|
return found
|
|
1845
2394
|
}
|
|
1846
2395
|
|
|
2396
|
+
/**
|
|
2397
|
+
* 「进程」那行该按哪种安装形态措辞:
|
|
2398
|
+
* - `boot`:目录里有 NapCatWinBootMain.exe(注入/快登壳型),进程表里有快登壳计数可看;
|
|
2399
|
+
* - `node`:无头 Shell(Node 包)安装,QQ 本体跑在 node.exe 里,既没有 QQ.exe 也没有快登壳,
|
|
2400
|
+
* `procs.boot` 恒为 0 —— 这时候还写「NapCatWinBootMain 0」只会让人以为没进程。
|
|
2401
|
+
*/
|
|
2402
|
+
function procsLayout(cfg, p) {
|
|
2403
|
+
const boot = Number(p && p.boot) || 0
|
|
2404
|
+
const qq = Number(p && p.qq) || 0
|
|
2405
|
+
// ① 谁在跑就按谁说话:有快登壳进程 → boot 型;只有 node 进程 → node 型
|
|
2406
|
+
if (boot > 0) return 'boot'
|
|
2407
|
+
if (qq > 0) return 'node'
|
|
2408
|
+
// ② 都没在跑(离线 / 待拉起):只能看 Shell 目录是哪种安装形态。
|
|
2409
|
+
// 注意:node 型目录里也常带一个 napcat/NapCatWinBootMain.exe 模板,不能拿「有这文件」当判据。
|
|
2410
|
+
// 这一步要 readdir,而面板每轮都来问一次(轻量档 1.5 秒一轮),所以按目录名缓存 60 秒。
|
|
2411
|
+
const dir = String((cfg && cfg.shellDir) || '')
|
|
2412
|
+
const now = Date.now()
|
|
2413
|
+
if (uiCache.layoutKey === dir && now - (uiCache.layoutAt || 0) < 60000) return uiCache.layout
|
|
2414
|
+
let value = 'unknown'
|
|
2415
|
+
try {
|
|
2416
|
+
const names = readdirSync(dir).map((n) => String(n).toLowerCase())
|
|
2417
|
+
const 有node壳 = names.includes('node.exe') && (names.includes('index.js') || names.includes('napcat.mjs'))
|
|
2418
|
+
if (有node壳) value = 'node'
|
|
2419
|
+
else if (names.includes('napcatwinbootmain.exe')) value = 'boot'
|
|
2420
|
+
} catch {}
|
|
2421
|
+
uiCache.layout = value
|
|
2422
|
+
uiCache.layoutKey = dir
|
|
2423
|
+
uiCache.layoutAt = now
|
|
2424
|
+
return value
|
|
2425
|
+
}
|
|
2426
|
+
|
|
1847
2427
|
/** 没有别的启动脚本时用的那个 bat:直接调 NapCatWinBootMain.exe */
|
|
1848
2428
|
function launcherBatLines(cfg, boot) {
|
|
1849
2429
|
const account = batValue(cfg.launchAccount)
|
|
@@ -1861,11 +2441,7 @@ function launcherBatLines(cfg, boot) {
|
|
|
1861
2441
|
|
|
1862
2442
|
/** 任务里那条命令(纯字符串版本:不写盘,供「脚本」复用 taskCommandLine 的规则) */
|
|
1863
2443
|
function taskCommandLinePure(cfg, bat) {
|
|
1864
|
-
|
|
1865
|
-
return (
|
|
1866
|
-
`cmd.exe /c chcp 65001 >nul & cd /d "${cfg.shellDir}" & call "${target}" ` +
|
|
1867
|
-
`>> "${cfg.launcherLog}" 2>&1`
|
|
1868
|
-
)
|
|
2444
|
+
return taskCmdLineOf(cfg, passwordActive(cfg) && bat ? envBatPath(cfg) : bat)
|
|
1869
2445
|
}
|
|
1870
2446
|
|
|
1871
2447
|
/** 给设置页看的清单:每个需要能不能做、要不要管理员 */
|
|
@@ -2377,7 +2953,7 @@ function openSetupFolder() {
|
|
|
2377
2953
|
/** 把已经在跑的 NapCat 控制台窗口藏起来(老链条留下的黑框也能收掉,不用重启 QQ) */
|
|
2378
2954
|
function hideConsoleWindows(cfg) {
|
|
2379
2955
|
const ps = [
|
|
2380
|
-
|
|
2956
|
+
PS_QUIET,
|
|
2381
2957
|
"Add-Type -Namespace K -Name U -MemberDefinition '",
|
|
2382
2958
|
'[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr h, int n);',
|
|
2383
2959
|
'[DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr h);',
|
|
@@ -2512,7 +3088,7 @@ async function killNewerChains(cfg, oldestLastMs) {
|
|
|
2512
3088
|
}
|
|
2513
3089
|
const root = psPath(base).replace(/'/g, "''")
|
|
2514
3090
|
const script =
|
|
2515
|
-
|
|
3091
|
+
PS_QUIET +
|
|
2516
3092
|
`$cut=([datetime]'1970-01-01').AddMilliseconds(${Math.round(oldestLastMs)});` +
|
|
2517
3093
|
'Get-Process QQ,NapCatWinBootMain,node | ' +
|
|
2518
3094
|
`Where-Object { $_.Path -like '${root}*' -and $_.StartTime -gt $cut } | ` +
|
|
@@ -2580,6 +3156,8 @@ async function tick(cfg) {
|
|
|
2580
3156
|
state.cfg = cfg
|
|
2581
3157
|
state.lastProbeAt = new Date().toISOString()
|
|
2582
3158
|
const health = await probe(cfg)
|
|
3159
|
+
/** 真正在看的端口:可能跟随了 dsh-onebot-qq 的反向 WS 端口 */
|
|
3160
|
+
const portShown = health.portUsed || cfg.onebotPort
|
|
2583
3161
|
|
|
2584
3162
|
if (health.online) {
|
|
2585
3163
|
if (state.phase !== 'online') {
|
|
@@ -2621,7 +3199,7 @@ async function tick(cfg) {
|
|
|
2621
3199
|
online: true,
|
|
2622
3200
|
nickname: health.nickname,
|
|
2623
3201
|
userId: health.userId,
|
|
2624
|
-
port:
|
|
3202
|
+
port: portShown,
|
|
2625
3203
|
mode: health.mode,
|
|
2626
3204
|
clients: health.clients,
|
|
2627
3205
|
bridge,
|
|
@@ -2697,7 +3275,7 @@ async function tick(cfg) {
|
|
|
2697
3275
|
mode: health.mode,
|
|
2698
3276
|
clients: null,
|
|
2699
3277
|
procs,
|
|
2700
|
-
port:
|
|
3278
|
+
port: portShown,
|
|
2701
3279
|
updatedAt: new Date().toISOString(),
|
|
2702
3280
|
})
|
|
2703
3281
|
return { phase: state.phase, health, procs }
|
|
@@ -2710,7 +3288,7 @@ async function tick(cfg) {
|
|
|
2710
3288
|
cfg,
|
|
2711
3289
|
`REVERSE-WAIT ${waitedSec}s 端口上还没有客户端连进来 → 温和重拉一次 ` +
|
|
2712
3290
|
`(${state.reverseRetries}/2)。若反复这样,检查 NapCat 里的反向 WS 地址是不是 ` +
|
|
2713
|
-
`ws://127.0.0.1:${
|
|
3291
|
+
`ws://127.0.0.1:${portShown}(令牌要对上 accessToken)`,
|
|
2714
3292
|
)
|
|
2715
3293
|
const cleaned = await killNapcat(cfg)
|
|
2716
3294
|
if (cleaned) {
|
|
@@ -2737,7 +3315,7 @@ async function tick(cfg) {
|
|
|
2737
3315
|
log(
|
|
2738
3316
|
cfg,
|
|
2739
3317
|
`REVERSE-GIVEUP: ${waitedSec}s 里 NapCat 一直没连上反向 WS,重拉 ${state.reverseRetries} 次也没用。` +
|
|
2740
|
-
`去 NapCat 配置里把「反向 WebSocket」地址填成 ws://127.0.0.1:${
|
|
3318
|
+
`去 NapCat 配置里把「反向 WebSocket」地址填成 ws://127.0.0.1:${portShown}` +
|
|
2741
3319
|
'(令牌对上 accessToken),改完把设置里的「在线探测方式」留 auto 就行。',
|
|
2742
3320
|
)
|
|
2743
3321
|
} else {
|
|
@@ -2750,7 +3328,7 @@ async function tick(cfg) {
|
|
|
2750
3328
|
mode: health.mode,
|
|
2751
3329
|
clients: health.clients,
|
|
2752
3330
|
procs,
|
|
2753
|
-
port:
|
|
3331
|
+
port: portShown,
|
|
2754
3332
|
updatedAt: new Date().toISOString(),
|
|
2755
3333
|
})
|
|
2756
3334
|
return { phase: state.phase, health, procs }
|
|
@@ -2774,7 +3352,7 @@ async function tick(cfg) {
|
|
|
2774
3352
|
mode: health.mode,
|
|
2775
3353
|
status: health.status,
|
|
2776
3354
|
procs,
|
|
2777
|
-
port:
|
|
3355
|
+
port: portShown,
|
|
2778
3356
|
updatedAt: new Date().toISOString(),
|
|
2779
3357
|
})
|
|
2780
3358
|
return { phase: state.phase, health, procs }
|
|
@@ -2982,6 +3560,9 @@ function registerTools(ctx, cfg) {
|
|
|
2982
3560
|
phase: state.phase,
|
|
2983
3561
|
onebot: {
|
|
2984
3562
|
port: cfg.onebotPort,
|
|
3563
|
+
/** 真正在看的端口:跟随了 dsh-onebot-qq 时和上面那个不一样 */
|
|
3564
|
+
portUsed: health.portUsed || cfg.onebotPort,
|
|
3565
|
+
followedCompanion: Boolean(health.followed),
|
|
2985
3566
|
online: health.online,
|
|
2986
3567
|
/** http / reverse / auth / http / process / dead —— 反向 WS 时看这个 */
|
|
2987
3568
|
mode: health.mode,
|
|
@@ -2994,6 +3575,16 @@ function registerTools(ctx, cfg) {
|
|
|
2994
3575
|
user_id: health.userId,
|
|
2995
3576
|
error: health.error,
|
|
2996
3577
|
},
|
|
3578
|
+
companion: {
|
|
3579
|
+
/** 本机装没装 dsh-onebot-qq、挂没挂、它自己配成什么样(不含令牌明文) */
|
|
3580
|
+
onebot: {
|
|
3581
|
+
...(health.companion || companionPublic(detectCompanion()) || { found: false }),
|
|
3582
|
+
tokenMatches:
|
|
3583
|
+
health.companionTokenMatch === undefined ? null : health.companionTokenMatch,
|
|
3584
|
+
},
|
|
3585
|
+
/** dsh-qq-bridge:装没装、挂没挂、桥接指向的端口和 NapCat 对不上没有 */
|
|
3586
|
+
bridge: health.bridge || bridgePublic(detectBridge()) || { found: false },
|
|
3587
|
+
},
|
|
2997
3588
|
procs,
|
|
2998
3589
|
node: nodePublic(cfg),
|
|
2999
3590
|
qr,
|
|
@@ -3024,7 +3615,7 @@ function registerTools(ctx, cfg) {
|
|
|
3024
3615
|
? health.clients === null
|
|
3025
3616
|
? '反向 WebSocket 模式:端口是反向 WS 服务在听(HTTP 问不出在线),客户端数还查不到——按在线处理,不会重启。'
|
|
3026
3617
|
: '反向 WebSocket 模式:端口活着,但还没有客户端连进来。让 NapCat 把反向 WS 地址填成 ws://127.0.0.1:' +
|
|
3027
|
-
String(cfg.onebotPort) +
|
|
3618
|
+
String(health.portUsed || cfg.onebotPort) +
|
|
3028
3619
|
'(令牌对上 accessToken);超过 stuckSec 才会温和重拉,最多两次。'
|
|
3029
3620
|
: health.mode === 'auth'
|
|
3030
3621
|
? '端口有响应但要求鉴权(HTTP ' +
|
|
@@ -3218,6 +3809,9 @@ function publicConfig(cfg) {
|
|
|
3218
3809
|
onebotPort: cfg.onebotPort,
|
|
3219
3810
|
accessToken: cfg.accessToken,
|
|
3220
3811
|
probeMode: cfg.probeMode,
|
|
3812
|
+
companionFollow: cfg.companionFollow,
|
|
3813
|
+
/** 这一行该不该出现在设置页:装了且挂进 profile 才给界面渲染 */
|
|
3814
|
+
companionReady: Boolean(cfg.companionReady),
|
|
3221
3815
|
procPathMatch: cfg.procPathMatch,
|
|
3222
3816
|
probeIntervalSec: cfg.probeIntervalSec,
|
|
3223
3817
|
launchGraceSec: cfg.launchGraceSec,
|
|
@@ -3373,6 +3967,14 @@ async function uiState(cfg, options = {}) {
|
|
|
3373
3967
|
const p = procs || { boot: 0, qq: 0, chains: 0, dup: false }
|
|
3374
3968
|
const qr = cachedQr(cfg)
|
|
3375
3969
|
const bridge = bridgeAge(cfg)
|
|
3970
|
+
// 面板每轮都来问一次(轻量档 1.5s 一轮),顺手按最新探测结果校准「跟随」那一项:
|
|
3971
|
+
// 用户中途把 dsh-onebot-qq 挂进 profile(或者摘掉),不用重启 DSH —— 界面上那一行立刻跟着出现/消失,
|
|
3972
|
+
// 值仍然是「他自己想要的那个」被闸门压过之后的结果(显式关过就一直关)。
|
|
3973
|
+
const liveReady = companionReady(health.companion || detectCompanion())
|
|
3974
|
+
if (liveReady !== cfg.companionReady) {
|
|
3975
|
+
cfg.companionReady = liveReady
|
|
3976
|
+
cfg.companionFollow = liveReady ? cfg.companionFollowWant !== false : false
|
|
3977
|
+
}
|
|
3376
3978
|
return {
|
|
3377
3979
|
ok: true,
|
|
3378
3980
|
version,
|
|
@@ -3386,6 +3988,15 @@ async function uiState(cfg, options = {}) {
|
|
|
3386
3988
|
mode: health.mode || '',
|
|
3387
3989
|
portAlive: !!health.portAlive,
|
|
3388
3990
|
portStatus: health.status || 0,
|
|
3991
|
+
/** 真正在看的端口(可能跟随了 dsh-onebot-qq) */
|
|
3992
|
+
portUsed: health.portUsed || cfg.onebotPort,
|
|
3993
|
+
followed: Boolean(health.followed),
|
|
3994
|
+
companion: health.companion || companionPublic(detectCompanion()),
|
|
3995
|
+
/** 设置页「跟随 dsh-onebot-qq」那一行显不显示,就看它(装了且挂进 profile) */
|
|
3996
|
+
companionReady: Boolean(cfg.companionReady),
|
|
3997
|
+
companionTokenMatch: health.companionTokenMatch === undefined ? null : health.companionTokenMatch,
|
|
3998
|
+
/** dsh-qq-bridge 的共存情况(键名避开下面表示心跳新鲜度的 bridge) */
|
|
3999
|
+
bridgePlugin: health.bridge || bridgePublic(detectBridge()),
|
|
3389
4000
|
clients: health.clients,
|
|
3390
4001
|
hint: health.hint || '',
|
|
3391
4002
|
nickname: health.nickname || '',
|
|
@@ -3398,6 +4009,8 @@ async function uiState(cfg, options = {}) {
|
|
|
3398
4009
|
lastOnlineAt: state.lastOnlineAt ? new Date(state.lastOnlineAt).toISOString() : '',
|
|
3399
4010
|
lastProbeAt: state.lastProbeAt || '',
|
|
3400
4011
|
procs: { boot: p.boot, qq: p.qq, chains: p.chains, dup: p.dup, known },
|
|
4012
|
+
/** node / boot / unknown —— 设置页「进程」那行的措辞跟着它变(boot 恒 0 不代表没进程) */
|
|
4013
|
+
layout: procsLayout(cfg, p),
|
|
3401
4014
|
qr: qr,
|
|
3402
4015
|
bridge: bridge,
|
|
3403
4016
|
procRoot: light ? '' : procRootRaw(cfg),
|
|
@@ -3445,6 +4058,9 @@ function saveAndApply(ctx, patch) {
|
|
|
3445
4058
|
const fileCfg = { ...readConfigFile(dir) }
|
|
3446
4059
|
for (const [key, value] of Object.entries(patch || {})) {
|
|
3447
4060
|
if (!Object.prototype.hasOwnProperty.call(DEFAULTS, key)) continue
|
|
4061
|
+
// 界面上没有这一行时(dsh-onebot-qq 没装/没启用),前端发的全量 patch 里可能带着假值,
|
|
4062
|
+
// 别让它把用户以前存的选择覆盖掉 —— 等它真的启用了再说。
|
|
4063
|
+
if (key === 'companionFollow' && !companionReady(detectCompanion())) continue
|
|
3448
4064
|
if (key === 'quickPassword') {
|
|
3449
4065
|
// 设置页不回显密码:空字符串 = 别动它(要清空走 clearPassword 动作)
|
|
3450
4066
|
const text = String(value == null ? '' : value).replace(/[\r\n]/g, '')
|
|
@@ -3797,6 +4413,25 @@ export const __internals = {
|
|
|
3797
4413
|
probeProcs,
|
|
3798
4414
|
portClients,
|
|
3799
4415
|
PROBE_MODES,
|
|
4416
|
+
// 与 dsh-onebot-qq 共存:识别它的安装/挂载/配置(自检脚本直接调这几个)
|
|
4417
|
+
detectCompanion,
|
|
4418
|
+
parseCompanionConfig,
|
|
4419
|
+
companionPublic,
|
|
4420
|
+
companionReady,
|
|
4421
|
+
companionTokenMatch,
|
|
4422
|
+
probeOnPort,
|
|
4423
|
+
COMPANION_DEFAULTS,
|
|
4424
|
+
COMPANION_PACKAGE,
|
|
4425
|
+
// 与 dsh-qq-bridge 共存:它是「客户端」,配置摊在 ~/.dsh/qq-bridge-config.json
|
|
4426
|
+
profileHits,
|
|
4427
|
+
detectBridge,
|
|
4428
|
+
bridgePublic,
|
|
4429
|
+
bridgeCheck,
|
|
4430
|
+
readBridgeConfig,
|
|
4431
|
+
urlPort,
|
|
4432
|
+
BRIDGE_DEFAULTS,
|
|
4433
|
+
BRIDGE_PACKAGE,
|
|
4434
|
+
BRIDGE_ID,
|
|
3800
4435
|
qrInfo,
|
|
3801
4436
|
bridgeAge,
|
|
3802
4437
|
launchNapcat,
|
|
@@ -3820,5 +4455,8 @@ export const __internals = {
|
|
|
3820
4455
|
applyPs1Source,
|
|
3821
4456
|
dirWritable,
|
|
3822
4457
|
cachedBootMain,
|
|
4458
|
+
procsLayout,
|
|
4459
|
+
taskCommandLine,
|
|
4460
|
+
taskCommandLinePure,
|
|
3823
4461
|
state,
|
|
3824
4462
|
}
|