qq-bot-roost 1.1.19 → 1.2.0
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/README.md +2 -2
- package/docs//345/256/277/344/270/273/345/215/217/350/256/256v1.md +77 -3
- package/package.json +3 -3
- package/src/bot.js +26 -17
- package/src/command-perm.js +47 -0
- package/src/config-reset.js +186 -0
- package/src/config.js +8 -2
- package/src/host/ext-bridge.js +257 -34
- package/src/host/inbound-forward.js +11 -1
- package/src/host/index.js +194 -7
- package/src/host/methods.js +12 -3
- package/src/main.js +42 -7
- package/src/plugin/context.js +27 -1
- package/src/web/key-strength.js +94 -0
- package/src/web/server.js +247 -13
- package/web/app.js +95 -16
- package/web/index.html +13 -4
- package/web/login.js +68 -0
package/src/main.js
CHANGED
|
@@ -149,11 +149,13 @@ function registerCoreCommands(rt) {
|
|
|
149
149
|
* 把注册表里的菜单转成 renderMenuMd 需要的分组结构。
|
|
150
150
|
* 命令项做成可点击标签,非命令项(被动触发的关键词)只显示文字。
|
|
151
151
|
*/
|
|
152
|
-
function menuGroupsOf(m, { withCommands = true } = {}) {
|
|
152
|
+
function menuGroupsOf(m, { withCommands = true, myLv = LEVEL.owner } = {}) {
|
|
153
153
|
const groups = [];
|
|
154
154
|
for (const sec of m.sections) {
|
|
155
|
-
|
|
156
|
-
|
|
155
|
+
/** 主人专属的命令对非主人不显示:列出来点了只会回一句「只有主人能用」 */
|
|
156
|
+
const items = sec.items.filter((it) => !(it.ownerOnly && myLv > LEVEL.owner));
|
|
157
|
+
if (!items.length) continue;
|
|
158
|
+
groups.push({ title: sec.title, items: items.map((it) => menuItemOf(it, withCommands)) });
|
|
157
159
|
}
|
|
158
160
|
return groups;
|
|
159
161
|
}
|
|
@@ -192,6 +194,7 @@ function registerCoreCommands(rt) {
|
|
|
192
194
|
for (const it of sec.items) {
|
|
193
195
|
if (it.admin && myLv > LEVEL.admin) continue;
|
|
194
196
|
if (it.globalAdmin && myLv > LEVEL.admin) continue;
|
|
197
|
+
if (it.ownerOnly && myLv > LEVEL.owner) continue;
|
|
195
198
|
items.push(menuItemOf(it));
|
|
196
199
|
}
|
|
197
200
|
if (items.length) out.set(sec.title, items);
|
|
@@ -207,14 +210,24 @@ function registerCoreCommands(rt) {
|
|
|
207
210
|
* 这段正常走不到。留着是兜底——哪天放开 `help`(比如只对管理员回),
|
|
208
211
|
* `msg.coreOnly` 标记还在,这条过滤依然生效,不用回来重写一遍。
|
|
209
212
|
*/
|
|
210
|
-
const
|
|
213
|
+
const allMenus = msg.coreOnly ? [] : [...registry.menus.values()].filter((m) => m.id !== '__core');
|
|
214
|
+
/**
|
|
215
|
+
* 普通插件 vs 外接协议连接方。
|
|
216
|
+
*
|
|
217
|
+
* `ext:` 开头的是外接协议连接方(`ext:dsh` 宿主本体、`ext:<id>` 它装的扩展)。
|
|
218
|
+
* 它们的命令注册在框架侧,但**它们不是插件**——混进「插件」列表,用户看到的
|
|
219
|
+
* 就是 `__host DSH · 13 条` 这种谁也认不出的内部代号。
|
|
220
|
+
* 单独一栏「协议」,栏标题用对方自报的名字。
|
|
221
|
+
*/
|
|
222
|
+
const pluginMenus = allMenus.filter((m) => !m.id.startsWith('ext:'));
|
|
223
|
+
const extMenus = allMenus.filter((m) => m.id.startsWith('ext:'));
|
|
211
224
|
|
|
212
225
|
/* ---------- 有参数:进下级 ---------- */
|
|
213
226
|
if (arg) {
|
|
214
227
|
// 1) 插件 id
|
|
215
228
|
const m = registry.menus.get(arg);
|
|
216
229
|
if (m) {
|
|
217
|
-
return msg.replyMenu(`${m.title}${m.version ? ' v' + m.version : ''}`, menuGroupsOf(m));
|
|
230
|
+
return msg.replyMenu(`${m.title}${m.version ? ' v' + m.version : ''}`, menuGroupsOf(m, { myLv }));
|
|
218
231
|
}
|
|
219
232
|
|
|
220
233
|
// 2) 「插件」→ 插件列表
|
|
@@ -237,12 +250,26 @@ function registerCoreCommands(rt) {
|
|
|
237
250
|
]);
|
|
238
251
|
}
|
|
239
252
|
|
|
240
|
-
// 3)
|
|
253
|
+
// 3) 「协议」→ 外接协议连接方在框架侧注册的命令
|
|
254
|
+
if (raw === '协议' || arg === 'protocol' || raw === '外接' || arg === 'ext') {
|
|
255
|
+
if (!extMenus.length) {
|
|
256
|
+
return msg.reply('这会儿没有协议连接连着。连上 dsh-roost 之后,它注册的命令会出现在这里。');
|
|
257
|
+
}
|
|
258
|
+
return msg.replyMenu(
|
|
259
|
+
'协议',
|
|
260
|
+
extMenus.map((m) => ({
|
|
261
|
+
title: `${m.title}${m.version ? ' v' + m.version : ''}`,
|
|
262
|
+
items: menuGroupsOf(m, { myLv }).flatMap((g) => g.items),
|
|
263
|
+
})),
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// 4) 内核分组名(基础 / 管理 / 绑定 / 其它)
|
|
241
268
|
const secs = coreSections();
|
|
242
269
|
const hit = secs.get(raw);
|
|
243
270
|
if (hit) return msg.replyMenu(`框架 · ${raw}`, [{ title: raw, items: hit }]);
|
|
244
271
|
|
|
245
|
-
const names = [...secs.keys(), '插件', ...pluginMenus.map((x) => x.id)];
|
|
272
|
+
const names = [...secs.keys(), '插件', ...(extMenus.length ? ['协议'] : []), ...pluginMenus.map((x) => x.id)];
|
|
246
273
|
return msg.reply(`没有「${raw}」这个分类或插件。\n可用:${names.join('、')}`);
|
|
247
274
|
}
|
|
248
275
|
|
|
@@ -286,6 +313,14 @@ function registerCoreCommands(rt) {
|
|
|
286
313
|
cmd: `${P}help 插件`,
|
|
287
314
|
});
|
|
288
315
|
}
|
|
316
|
+
if (extMenus.length) {
|
|
317
|
+
const cnt = extMenus.reduce((n, m) => n + m.sections.reduce((k, s) => k + s.items.length, 0), 0);
|
|
318
|
+
entries.push({
|
|
319
|
+
text: '协议',
|
|
320
|
+
desc: `${extMenus.map((x) => x.title).join('、')} · ${cnt} 条`,
|
|
321
|
+
cmd: `${P}help 协议`,
|
|
322
|
+
});
|
|
323
|
+
}
|
|
289
324
|
if (entries.length) groups.push({ title: '分类', items: entries });
|
|
290
325
|
|
|
291
326
|
const bot = rt.metrics.botName || '菜单';
|
package/src/plugin/context.js
CHANGED
|
@@ -185,6 +185,7 @@ export class Registry {
|
|
|
185
185
|
isCommand: Boolean(typeof it === 'object' && it.cmd) || false,
|
|
186
186
|
admin: Boolean(typeof it === 'object' && it.admin),
|
|
187
187
|
globalAdmin: Boolean(typeof it === 'object' && it.globalAdmin),
|
|
188
|
+
ownerOnly: Boolean(typeof it === 'object' && it.ownerOnly),
|
|
188
189
|
});
|
|
189
190
|
}
|
|
190
191
|
return sec;
|
|
@@ -210,12 +211,23 @@ export class Registry {
|
|
|
210
211
|
* 内核的十几个命令全靠这个分到「基础 / 管理 / 其它」三组——
|
|
211
212
|
* 不然 `/help` 主菜单会把它们全部平铺出来,二十多行,
|
|
212
213
|
* 在手机上要滑两屏才看得完。
|
|
214
|
+
*
|
|
215
|
+
* `hidden` 是给别名用的:别名要能直呼,但不该在菜单里和主名并列出现。
|
|
213
216
|
*/
|
|
217
|
+
if (meta.hidden === true) return;
|
|
214
218
|
this.addMenuSection(
|
|
215
219
|
pluginId,
|
|
216
220
|
{ name: meta.plugin },
|
|
217
221
|
meta.section ?? '指令',
|
|
218
|
-
[
|
|
222
|
+
[
|
|
223
|
+
{
|
|
224
|
+
cmd: meta.pattern ?? key,
|
|
225
|
+
desc: meta.desc ?? '',
|
|
226
|
+
admin: meta.admin,
|
|
227
|
+
globalAdmin: meta.globalAdmin,
|
|
228
|
+
ownerOnly: meta.ownerOnly,
|
|
229
|
+
},
|
|
230
|
+
],
|
|
219
231
|
);
|
|
220
232
|
}
|
|
221
233
|
|
|
@@ -683,7 +695,21 @@ export function createContext({ meta, paths, registry, api, cfg, config, configS
|
|
|
683
695
|
desc: meta2?.desc ?? '',
|
|
684
696
|
usage: meta2?.usage ?? `${cfg.commandPrefix}${name}`,
|
|
685
697
|
pattern: String(name).trim(),
|
|
698
|
+
/**
|
|
699
|
+
* 三个权限标记都要原样透传:
|
|
700
|
+
* admin ≥ 管理员(主人也算)
|
|
701
|
+
* globalAdmin 另需全局作用域权限
|
|
702
|
+
* ownerOnly 只有主人
|
|
703
|
+
*
|
|
704
|
+
* 以前只传了 `admin`,导致插件命令设 `globalAdmin` / `ownerOnly`
|
|
705
|
+
* 静默失效——命令表收下了字段,但这里压根没送进去。
|
|
706
|
+
*/
|
|
686
707
|
admin: Boolean(meta2?.admin),
|
|
708
|
+
globalAdmin: Boolean(meta2?.globalAdmin),
|
|
709
|
+
ownerOnly: Boolean(meta2?.ownerOnly),
|
|
710
|
+
hidden: Boolean(meta2?.hidden),
|
|
711
|
+
// 只影响 /help 分组,不传就落「指令」组
|
|
712
|
+
section: meta2?.section,
|
|
687
713
|
plugin: meta.name ?? id,
|
|
688
714
|
// 合并多插件回复时要靠 id 分组,name 只用来显示
|
|
689
715
|
pluginId: id,
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 面板访问密钥强度(2026-11-28)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要它:`accessKey` 等价于「能装插件 = 子进程执行任意代码」,而现状
|
|
5
|
+
* 只查「非空」+ 20 次/分钟限流。弱 key(如 `123456`)一天可被穷举完(20/分钟
|
|
6
|
+
* ≈ 2.8 万次/天),限流只是拖慢,不是挡住。
|
|
7
|
+
*
|
|
8
|
+
* 设计依据(已查证,见归档设计稿):
|
|
9
|
+
* - NIST SP 800-63B Q-B10:建议增加长度,**明确反对**大小写/数字/符号组合规则;
|
|
10
|
+
* Q-B19:黑名单比对**完整值**,不查子串。
|
|
11
|
+
* - Gitea:`MIN_PASSWORD_LENGTH` 默认 8、`PASSWORD_COMPLEXITY` 默认 off。
|
|
12
|
+
* 但 Gitea 的 8 是给人记的密码;我们这是复制粘贴的机器令牌且泄露即 RCE,
|
|
13
|
+
* 门槛提到 16——即便全是小写字母也有 ~76 bit 熵。
|
|
14
|
+
*
|
|
15
|
+
* 纯函数、零依赖,测试不需要起服务。
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** 机器令牌长度门槛:比人密码的 8 更高,因为用户是复制粘贴而非手输 */
|
|
19
|
+
export const MIN_KEY_LENGTH = 16;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 常见弱密钥黑名单(NIST Q-B08:放「很可能被用到的值」,不必穷举字典)。
|
|
23
|
+
* 比对完整值、大小写不敏感(Q-B19:不查子串,所以 `password12345` 不算弱)。
|
|
24
|
+
* 这些值一旦被拿去当面板 key,等价于把 RCE 权限写在门口。
|
|
25
|
+
*/
|
|
26
|
+
export const WEAK_KEYS = new Set([
|
|
27
|
+
// 纯数字 / 连续键盘
|
|
28
|
+
'123456', '1234567', '12345678', '123456789', '1234567890', '12345678901',
|
|
29
|
+
'123123123', '111111', '000000', '666666', '888888', '654321',
|
|
30
|
+
'qwerty', 'qwertyuiop', 'asdfgh', '1q2w3e4r', 'zxcvbnm',
|
|
31
|
+
// 常见口令
|
|
32
|
+
'password', 'password1', 'password123', 'admin', 'admin123', 'administrator',
|
|
33
|
+
'root', 'toor', 'test', 'test123', 'guest', 'welcome', 'iloveyou', 'letmein',
|
|
34
|
+
'monkey', 'dragon', 'sunshine', 'princess', 'football', 'baseball',
|
|
35
|
+
// 与其它项目默认值撞车(迁移/复制粘贴最常见的来源)
|
|
36
|
+
'secret', 'secretkey', 'accesskey', 'changeme', 'default', 'roost', 'bot',
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 校验面板密钥强度。
|
|
41
|
+
* @returns {{ok: boolean, reason?: 'length'|'weak', message?: string}}
|
|
42
|
+
*/
|
|
43
|
+
export function checkKeyStrength(key) {
|
|
44
|
+
const s = String(key ?? '');
|
|
45
|
+
/**
|
|
46
|
+
* 先查黑名单再查长度:常见弱口令(123456 / password)都短于 16,
|
|
47
|
+
* 若先判长度就永远走不到黑名单——黑名单一行形同虚设,而且「太常见」
|
|
48
|
+
* 比「太短」对用户更有指导性(知道该换个思路,而不是再加几个字符)。
|
|
49
|
+
*/
|
|
50
|
+
if (WEAK_KEYS.has(s.toLowerCase().trim())) {
|
|
51
|
+
return { ok: false, reason: 'weak', message: '这个密钥太常见,请换一个随机字符串' };
|
|
52
|
+
}
|
|
53
|
+
if (s.length < MIN_KEY_LENGTH) {
|
|
54
|
+
return {
|
|
55
|
+
ok: false,
|
|
56
|
+
reason: 'length',
|
|
57
|
+
message: `访问密钥至少需要 ${MIN_KEY_LENGTH} 个字符(当前 ${s.length} 个)`,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
return { ok: true };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 登录密码强度(**给人用**,区别于给机器用的 accessKey)。
|
|
65
|
+
*
|
|
66
|
+
* NIST SP 800-63B 的 8 字符是「人记的密码」底线,Gitea 默认也是 8:人要在
|
|
67
|
+
* 登录页输这个,16 位对人不合理。但同样反对字符组合规则、同样要黑名单。
|
|
68
|
+
* accessKey 仍是 16+——它给机器用(复制粘贴),且泄露即 RCE。
|
|
69
|
+
*/
|
|
70
|
+
export const MIN_LOGIN_PASSWORD_LENGTH = 8;
|
|
71
|
+
|
|
72
|
+
export function checkLoginPasswordStrength(password) {
|
|
73
|
+
const s = String(password ?? '');
|
|
74
|
+
if (WEAK_KEYS.has(s.toLowerCase().trim())) {
|
|
75
|
+
return { ok: false, reason: 'weak', message: '这个登录密码太常见,请换一个' };
|
|
76
|
+
}
|
|
77
|
+
if (s.length < MIN_LOGIN_PASSWORD_LENGTH) {
|
|
78
|
+
return {
|
|
79
|
+
ok: false,
|
|
80
|
+
reason: 'length',
|
|
81
|
+
message: `登录密码至少需要 ${MIN_LOGIN_PASSWORD_LENGTH} 个字符(当前 ${s.length} 个)`,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
return { ok: true };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 给启动日志用的简短提示(不阻断本机 loopback 用户)。
|
|
89
|
+
* 弱 key 仍可用,只是提醒去换——升级不锁死老用户。
|
|
90
|
+
*/
|
|
91
|
+
export function warnWeakKey(key) {
|
|
92
|
+
const r = checkKeyStrength(key);
|
|
93
|
+
return r.ok ? '' : `${r.message};建议到面板「设置」页更换(登录与 ?key= 仍可正常使用)`;
|
|
94
|
+
}
|
package/src/web/server.js
CHANGED
|
@@ -4,6 +4,7 @@ import fsSync from 'node:fs';
|
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import crypto from 'node:crypto';
|
|
6
6
|
import { ROOT } from '../config.js';
|
|
7
|
+
import { RESET_CONFIRM, resetFrameworkConfigs } from '../config-reset.js';
|
|
7
8
|
import { redactText } from '../runtime.js';
|
|
8
9
|
import { DIRS, botPaths, listBotIds, rel } from '../paths.js';
|
|
9
10
|
import { createLogger, recentLogs, onLog, getLevel, setLevel } from '../logger.js';
|
|
@@ -37,6 +38,13 @@ import { toImportRow } from '../ledger.js';
|
|
|
37
38
|
import { ledgerTextOf } from '../ledger-text.js';
|
|
38
39
|
import { createMediaStore } from '../media-store.js';
|
|
39
40
|
import { runRoute } from '../plugin/routes.js';
|
|
41
|
+
import {
|
|
42
|
+
MIN_KEY_LENGTH,
|
|
43
|
+
MIN_LOGIN_PASSWORD_LENGTH,
|
|
44
|
+
checkKeyStrength,
|
|
45
|
+
checkLoginPasswordStrength,
|
|
46
|
+
warnWeakKey,
|
|
47
|
+
} from './key-strength.js';
|
|
40
48
|
import { toggleGroupPlugin } from '../plugin/groupscope.js';
|
|
41
49
|
import { suspendPluginWatch } from '../services/plugin-watch.js';
|
|
42
50
|
import { syncNickname } from '../services/nickname.js';
|
|
@@ -537,24 +545,74 @@ async function startWebServerInstance({
|
|
|
537
545
|
|
|
538
546
|
const host = wc.host || '127.0.0.1';
|
|
539
547
|
const port = Number(wc.port) || 15207;
|
|
540
|
-
|
|
548
|
+
/**
|
|
549
|
+
* 两把钥匙(2026-11-28):
|
|
550
|
+
* - `accessKey` 给**机器**用(dsh-roost / roost-mcp 的 X-Access-Key / ?key=),16+;
|
|
551
|
+
* - `loginPassword` 给**人**用(登录页输入),8+。
|
|
552
|
+
*
|
|
553
|
+
* 必须是 `let` 而不是 const:面板改凭证后要即时生效并吊销旧凭证(旧版是
|
|
554
|
+
* const 固化,实测改完 key 旧 key 仍能进、新 key 反而 401——换了等于没换)。
|
|
555
|
+
* `loginPassword` 留空时登录页退回收 `accessKey`,老用户升级不断档。
|
|
556
|
+
*/
|
|
557
|
+
let accessKey = wc.accessKey || wc.token || '';
|
|
558
|
+
let loginPassword = wc.loginPassword || '';
|
|
541
559
|
|
|
542
560
|
/**
|
|
543
|
-
* 硬阻断:监听非 loopback
|
|
561
|
+
* 硬阻断:监听非 loopback 却没设/设了弱 accessKey。
|
|
544
562
|
*
|
|
545
563
|
* 以前只在 doctor 里软告警。`authorized()` 在 accessKey 为空时无条件放行,
|
|
546
564
|
* 于是 `webui.host=0.0.0.0` 且忘设 key 的面板在全部网卡零认证开放——
|
|
547
565
|
* 远程可建/删机器人、改配置、装任意插件(插件代码在子进程执行 = RCE)。
|
|
548
|
-
*
|
|
566
|
+
* 2026-11-28 起再加一道:外部暴露时弱 key 也拒绝(20 次/分钟的限流挡不住
|
|
567
|
+
* 短口令被穷举)。本机 loopback 完全不拦,只给弱 key 打 warn——升级不锁死
|
|
568
|
+
* 老用户(历史短 key 仍能用,但会被提醒去换)。
|
|
549
569
|
*/
|
|
550
570
|
const LOOPBACK_HOSTS = new Set(['127.0.0.1', 'localhost', '::1', '']);
|
|
551
|
-
|
|
571
|
+
const exposed = !LOOPBACK_HOSTS.has(String(host));
|
|
572
|
+
const keyStrength = checkKeyStrength(accessKey);
|
|
573
|
+
if (exposed && !accessKey) {
|
|
552
574
|
throw new Error(
|
|
553
575
|
`面板监听 ${host} 但未设置 webui.accessKey。\n` +
|
|
554
576
|
`这样任何人访问该端口都能无认证接管面板(含安装插件=执行任意代码)。\n` +
|
|
555
577
|
`请在 config/bot.json 里设置 webui.accessKey,或把 webui.host 改回 127.0.0.1。`,
|
|
556
578
|
);
|
|
557
579
|
}
|
|
580
|
+
if (exposed && !keyStrength.ok) {
|
|
581
|
+
throw new Error(
|
|
582
|
+
`面板监听 ${host} 但 webui.accessKey 强度不足:${keyStrength.message}。\n` +
|
|
583
|
+
`这把钥匙给机器调用 API 用(dsh-roost / 脚本),外部暴露时弱密钥可被穷举。\n` +
|
|
584
|
+
`请换成至少 ${MIN_KEY_LENGTH} 位的随机字符串(如 node -e "console.log(require('crypto').randomBytes(24).toString('hex'))"),` +
|
|
585
|
+
`或把 webui.host 改回 127.0.0.1。`,
|
|
586
|
+
);
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* 外网暴露 = 给人登录:必须设独立的 loginPassword(8+),不能只靠机器 key。
|
|
590
|
+
* 两把钥匙分开后,人不用记 16 位的机器令牌;机器也不用把人密码写在脚本里。
|
|
591
|
+
*/
|
|
592
|
+
const loginStrength = checkLoginPasswordStrength(loginPassword);
|
|
593
|
+
if (exposed && !loginPassword) {
|
|
594
|
+
throw new Error(
|
|
595
|
+
`面板监听 ${host} 但未设置 webui.loginPassword。\n` +
|
|
596
|
+
`外网访问是给人在登录页输密码的:请单独设一个至少 ${MIN_LOGIN_PASSWORD_LENGTH} 位的登录密码,` +
|
|
597
|
+
`它和给机器用的 accessKey 分开,改完重启生效。\n` +
|
|
598
|
+
`或把 webui.host 改回 127.0.0.1(本机不需要独立登录密码)。`,
|
|
599
|
+
);
|
|
600
|
+
}
|
|
601
|
+
if (exposed && !loginStrength.ok) {
|
|
602
|
+
throw new Error(
|
|
603
|
+
`面板监听 ${host} 但 webui.loginPassword 强度不足:${loginStrength.message}。\n` +
|
|
604
|
+
`请换成至少 ${MIN_LOGIN_PASSWORD_LENGTH} 位、且不是常见口令的登录密码。`,
|
|
605
|
+
);
|
|
606
|
+
}
|
|
607
|
+
if (!exposed) {
|
|
608
|
+
if (accessKey) {
|
|
609
|
+
const hint = warnWeakKey(accessKey);
|
|
610
|
+
if (hint) log.warn(`面板机器密钥强度不足:${hint}`);
|
|
611
|
+
}
|
|
612
|
+
if (loginPassword && !loginStrength.ok) {
|
|
613
|
+
log.warn(`面板登录密码强度不足:${loginStrength.message};建议更换`);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
558
616
|
|
|
559
617
|
/**
|
|
560
618
|
* 反向代理设置。
|
|
@@ -747,6 +805,11 @@ async function startWebServerInstance({
|
|
|
747
805
|
* 暂停是个紧急操作,等用户点进某个页签才发现「原来还暂停着」就太晚了。
|
|
748
806
|
*/
|
|
749
807
|
snap.gate = rt?.gate?.peek?.() ?? null;
|
|
808
|
+
/**
|
|
809
|
+
* 外接协议状态:概览页的卡片和「外接」页的实时行共用这一份快照。
|
|
810
|
+
* snapshot() 不含 token,跟着 5 秒一次的 status 走,不必再开轮询接口。
|
|
811
|
+
*/
|
|
812
|
+
snap.host = rt?.host?.snapshot?.() ?? null;
|
|
750
813
|
return snap;
|
|
751
814
|
};
|
|
752
815
|
|
|
@@ -1209,16 +1272,38 @@ async function startWebServerInstance({
|
|
|
1209
1272
|
const sid0 = /(?:^|;\s*)roost_sid=([a-f0-9]{64})/.exec(ck)?.[1];
|
|
1210
1273
|
const exp0 = sid0 ? sessions.get(sid0) : 0;
|
|
1211
1274
|
if (!exp0 || exp0 <= Date.now()) {
|
|
1275
|
+
/**
|
|
1276
|
+
* 登录表单(2026-11-28):密钥只在 POST body 里交给 /api/login,不再让用户
|
|
1277
|
+
* 把密钥贴进网址。URL 会进历史 / 书签同步 / Referer / 反代 access_log,
|
|
1278
|
+
* 每一条都是明文泄露面;表单 + HttpOnly Cookie 把这条路径堵上。
|
|
1279
|
+
*
|
|
1280
|
+
* 脚本仍是 `script-src 'self'`(见 CSP_MAIN),所以不能内联:
|
|
1281
|
+
* 用同源的 /login.js,它在下面的鉴权闸门里显式放行——用户还没会话时
|
|
1282
|
+
* 必须加载得到,否则表单就是个按不动的空壳。
|
|
1283
|
+
*
|
|
1284
|
+
* 保留兼容:`?key=` 首跳(1186 行)和 `authorized()` 的 query 分支都还在,
|
|
1285
|
+
* dsh-roost / roost-mcp 那条路一行没动。这里只多一条给人走的入口。
|
|
1286
|
+
*/
|
|
1212
1287
|
res.writeHead(401, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
1213
1288
|
return res.end(
|
|
1214
|
-
'<!doctype html><meta charset="utf-8"><
|
|
1289
|
+
'<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">' +
|
|
1290
|
+
'<title>面板登录</title>' +
|
|
1215
1291
|
'<body style="font:15px/1.6 system-ui;max-width:34em;margin:14vh auto;padding:0 1.2em">' +
|
|
1216
|
-
'<h1 style="font-size:18px"
|
|
1217
|
-
'<p
|
|
1218
|
-
'
|
|
1219
|
-
'<
|
|
1220
|
-
|
|
1221
|
-
'<
|
|
1292
|
+
'<h1 style="font-size:18px">面板登录</h1>' +
|
|
1293
|
+
'<p>请输入面板登录密码(<code>webui.loginPassword</code>;没设过时仍可用旧的访问密钥)。' +
|
|
1294
|
+
'登录状态在浏览器里保留 7 天,程序重启后需要重新登录。</p>' +
|
|
1295
|
+
'<form id="loginForm">' +
|
|
1296
|
+
'<label for="panelKey">访问密钥</label><br>' +
|
|
1297
|
+
'<input id="panelKey" type="password" autocomplete="current-password" required ' +
|
|
1298
|
+
'style="box-sizing:border-box;width:100%;max-width:28em;padding:8px 10px;margin:6px 0 10px">' +
|
|
1299
|
+
'<button type="submit" style="padding:8px 18px">登录</button>' +
|
|
1300
|
+
'</form>' +
|
|
1301
|
+
'<p id="loginError" role="alert" style="color:#e5484d;min-height:1.6em"></p>' +
|
|
1302
|
+
'<p style="opacity:.65;font-size:13px">登录密码在 <code>config/bot.json</code> 的 ' +
|
|
1303
|
+
'<code>webui.loginPassword</code>,或环境变量里。<br>' +
|
|
1304
|
+
'程序化调用(dsh-roost / 脚本)用的是另一把 <code>webui.accessKey</code>,' +
|
|
1305
|
+
'通过 <code>X-Access-Key</code> 头或 <code>?key=</code> 参数传递,照常工作。</p>' +
|
|
1306
|
+
'<script src="/login.js" defer></script>',
|
|
1222
1307
|
);
|
|
1223
1308
|
}
|
|
1224
1309
|
}
|
|
@@ -1338,6 +1423,83 @@ async function startWebServerInstance({
|
|
|
1338
1423
|
return res.end();
|
|
1339
1424
|
}
|
|
1340
1425
|
|
|
1426
|
+
/**
|
|
1427
|
+
* 面板登录 / 登出(2026-11-28,**必须在 authorized() 之前**)。
|
|
1428
|
+
*
|
|
1429
|
+
* 登录是「拿密钥换会话」的入口,调用方此时还没有会话,走统一鉴权会
|
|
1430
|
+
* 直接 401——自己和自己死锁。所以这两个端点在鉴权闸门前面处理,但
|
|
1431
|
+
* 安全要求一条不少:
|
|
1432
|
+
* · 写操作必须 application/json(复用 isJsonRequest)
|
|
1433
|
+
* · 必须同源(复用 isSameOrigin,Referer 兜底)
|
|
1434
|
+
* · 失败走 authFails 同一份 per-IP 计数——不能让攻击者拿这个端点
|
|
1435
|
+
* 把「20 次/分钟」的限流翻倍
|
|
1436
|
+
* · 成功新建 sid(crypto.randomBytes(32)),与 ?key= 首跳完全同构,
|
|
1437
|
+
* 天然防 session fixation
|
|
1438
|
+
*
|
|
1439
|
+
* 登出删的是服务端 session + 清 Cookie。只清浏览器 Cookie 不删服务端
|
|
1440
|
+
* 记录等于没登出——偷到 sid 的人还能继续用。
|
|
1441
|
+
*/
|
|
1442
|
+
if ((pathname === '/api/login' || pathname === '/api/logout') && req.method === 'POST') {
|
|
1443
|
+
if (!isJsonRequest(req)) {
|
|
1444
|
+
return json(res, 415, { error: '写操作需要 Content-Type: application/json' });
|
|
1445
|
+
}
|
|
1446
|
+
if (!isSameOrigin(req)) {
|
|
1447
|
+
return json(res, 403, { error: '跨站请求被拒绝' });
|
|
1448
|
+
}
|
|
1449
|
+
const ip = clientIp(req);
|
|
1450
|
+
const now = Date.now();
|
|
1451
|
+
const rec = authFails.get(ip);
|
|
1452
|
+
if (rec && now - rec.start < AUTH_WINDOW && rec.count >= AUTH_MAX_FAILS) {
|
|
1453
|
+
log.error(
|
|
1454
|
+
`面板登录失败次数过多,已临时封禁 ${ip}(${Math.ceil((AUTH_WINDOW - (now - rec.start)) / 1000)} 秒后解除)`,
|
|
1455
|
+
);
|
|
1456
|
+
return json(res, 429, { error: '尝试次数过多,请稍后再试' });
|
|
1457
|
+
}
|
|
1458
|
+
let body;
|
|
1459
|
+
try {
|
|
1460
|
+
body = await readBody(req);
|
|
1461
|
+
} catch (e) {
|
|
1462
|
+
return json(res, 400, { error: `请求体不合法:${e?.message || e}` });
|
|
1463
|
+
}
|
|
1464
|
+
if (pathname === '/api/login') {
|
|
1465
|
+
const given = body?.key;
|
|
1466
|
+
/**
|
|
1467
|
+
* 两把钥匙:设了 loginPassword 就只认它(人密码),没设才退回
|
|
1468
|
+
* accessKey(老用户升级不断档)。机器令牌不能当登录密码用——
|
|
1469
|
+
* 否则把它贴进登录页又回到「密钥进表单」的老路。
|
|
1470
|
+
*/
|
|
1471
|
+
const expected = loginPassword || accessKey;
|
|
1472
|
+
/** 没设任何凭证时本机本就无需鉴权,别开一道「空密码也能登」的门 */
|
|
1473
|
+
if (!expected || !safeEqual(given, expected)) {
|
|
1474
|
+
const cur = rec && now - rec.start < AUTH_WINDOW ? rec : { count: 0, start: now };
|
|
1475
|
+
cur.count += 1;
|
|
1476
|
+
authFails.set(ip, cur);
|
|
1477
|
+
log.warn(`面板登录失败:来源 ${ip},本窗口第 ${cur.count} 次`);
|
|
1478
|
+
if (authFails.size > 5000) authFails.clear();
|
|
1479
|
+
return json(res, 401, { error: '访问密钥无效' });
|
|
1480
|
+
}
|
|
1481
|
+
authFails.delete(ip);
|
|
1482
|
+
const sid = crypto.randomBytes(32).toString('hex');
|
|
1483
|
+
sessions.set(sid, Date.now() + SESSION_TTL_MS);
|
|
1484
|
+
res.writeHead(200, {
|
|
1485
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
1486
|
+
'Cache-Control': 'no-store',
|
|
1487
|
+
'Set-Cookie': `roost_sid=${sid}; HttpOnly; SameSite=Strict; Path=/; Max-Age=${Math.floor(
|
|
1488
|
+
SESSION_TTL_MS / 1000,
|
|
1489
|
+
)}`,
|
|
1490
|
+
});
|
|
1491
|
+
return res.end(JSON.stringify({ ok: true }));
|
|
1492
|
+
}
|
|
1493
|
+
const cookieSid = /(?:^|;\s*)roost_sid=([a-f0-9]{64})/.exec(String(req.headers.cookie || ''))?.[1];
|
|
1494
|
+
if (cookieSid) sessions.delete(cookieSid);
|
|
1495
|
+
res.writeHead(200, {
|
|
1496
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
1497
|
+
'Cache-Control': 'no-store',
|
|
1498
|
+
'Set-Cookie': 'roost_sid=; HttpOnly; SameSite=Strict; Path=/; Max-Age=0',
|
|
1499
|
+
});
|
|
1500
|
+
return res.end(JSON.stringify({ ok: true }));
|
|
1501
|
+
}
|
|
1502
|
+
|
|
1341
1503
|
if (!authorized(req, url, ptPage)) return json(res, 401, { error: '访问密钥无效' });
|
|
1342
1504
|
|
|
1343
1505
|
/**
|
|
@@ -1981,6 +2143,9 @@ async function startWebServerInstance({
|
|
|
1981
2143
|
...(globalCfg.webui ?? {}),
|
|
1982
2144
|
accessKey: '',
|
|
1983
2145
|
accessKeySet: Boolean(globalCfg.webui?.accessKey),
|
|
2146
|
+
// 登录密码与机器密钥一样不回显(本机任意进程一条 GET 就能抄走)
|
|
2147
|
+
loginPassword: '',
|
|
2148
|
+
loginPasswordSet: Boolean(globalCfg.webui?.loginPassword),
|
|
1984
2149
|
},
|
|
1985
2150
|
logLevel: globalCfg.logLevel ?? 'info',
|
|
1986
2151
|
/**
|
|
@@ -2126,15 +2291,51 @@ async function startWebServerInstance({
|
|
|
2126
2291
|
: [],
|
|
2127
2292
|
};
|
|
2128
2293
|
}
|
|
2294
|
+
/** 凭证是否变更——变更后要即时生效 + 吊销所有会话 */
|
|
2295
|
+
let credentialsChanged = false;
|
|
2129
2296
|
if (body.webui && typeof body.webui === 'object') {
|
|
2130
2297
|
// webui 是全局设置,不能写进机器人 cfg:
|
|
2131
2298
|
// 写进去 saveBotConfig 也不保存它(白名单式保存),
|
|
2132
2299
|
// 结果是面板提示「已保存」但什么都没变。
|
|
2133
2300
|
const patch = { ...body.webui };
|
|
2134
2301
|
// 面板不回显密钥,所以空值代表「不修改」,不是「清空」
|
|
2135
|
-
|
|
2302
|
+
const newAccessKey = patch.accessKey ? String(patch.accessKey).trim() : '';
|
|
2303
|
+
const newLoginPassword = patch.loginPassword ? String(patch.loginPassword).trim() : '';
|
|
2304
|
+
delete patch.accessKey;
|
|
2305
|
+
delete patch.loginPassword;
|
|
2136
2306
|
delete patch.accessKeySet;
|
|
2307
|
+
delete patch.loginPasswordSet;
|
|
2308
|
+
|
|
2309
|
+
if (newAccessKey) {
|
|
2310
|
+
// 只校验「本次真的改了」的值——历史弱 key 用户不填这栏照样能保存别的设置。
|
|
2311
|
+
const strength = checkKeyStrength(newAccessKey);
|
|
2312
|
+
if (!strength.ok) return json(res, 400, { ok: false, error: strength.message });
|
|
2313
|
+
if (newAccessKey !== accessKey) {
|
|
2314
|
+
accessKey = newAccessKey;
|
|
2315
|
+
credentialsChanged = true;
|
|
2316
|
+
}
|
|
2317
|
+
}
|
|
2318
|
+
if (newLoginPassword) {
|
|
2319
|
+
const strength = checkLoginPasswordStrength(newLoginPassword);
|
|
2320
|
+
if (!strength.ok) return json(res, 400, { ok: false, error: strength.message });
|
|
2321
|
+
if (newLoginPassword !== loginPassword) {
|
|
2322
|
+
loginPassword = newLoginPassword;
|
|
2323
|
+
credentialsChanged = true;
|
|
2324
|
+
}
|
|
2325
|
+
}
|
|
2326
|
+
|
|
2137
2327
|
globalCfg.webui = { ...globalCfg.webui, ...patch };
|
|
2328
|
+
if (newAccessKey) globalCfg.webui.accessKey = newAccessKey;
|
|
2329
|
+
if (newLoginPassword) globalCfg.webui.loginPassword = newLoginPassword;
|
|
2330
|
+
if (credentialsChanged) {
|
|
2331
|
+
/**
|
|
2332
|
+
* 吊销必须**紧跟凭证赋值**,不能挪到本请求末尾的 await 之后:
|
|
2333
|
+
* 后面还有 syncBotName() 这类要几百毫秒甚至几秒的异步操作,
|
|
2334
|
+
* 那段时间旧 cookie 仍有效——攻击者拿着旧会话就能继续操作。
|
|
2335
|
+
*/
|
|
2336
|
+
sessions.clear();
|
|
2337
|
+
log.warn('面板凭证已更新:旧密钥/密码立即失效,所有会话已吊销,请用新凭证重新登录');
|
|
2338
|
+
}
|
|
2138
2339
|
await saveGlobalConfig?.(globalCfg).catch(() => {});
|
|
2139
2340
|
configureMirror(globalCfg);
|
|
2140
2341
|
configureProxy(globalCfg);
|
|
@@ -2239,9 +2440,21 @@ async function startWebServerInstance({
|
|
|
2239
2440
|
|
|
2240
2441
|
// 填完 AppID / AppSecret 顺手把「默认机器人」换成官方昵称
|
|
2241
2442
|
const name = await syncBotName(rt);
|
|
2443
|
+
if (credentialsChanged) {
|
|
2444
|
+
/**
|
|
2445
|
+
* 凭证换了必须即时吊销旧凭证换来的会话——只清浏览器 Cookie 不够,
|
|
2446
|
+
* 偷到 sid 的人还能继续用。sessions 只存内存,清掉即全部失效。
|
|
2447
|
+
* 前端收到 credentialsChanged 后跳登录页,用户用新密码重登。
|
|
2448
|
+
*/
|
|
2449
|
+
sessions.clear();
|
|
2450
|
+
log.warn('面板凭证已更新:旧密钥/密码立即失效,所有会话已吊销,请用新凭证重新登录');
|
|
2451
|
+
}
|
|
2242
2452
|
return json(res, 200, {
|
|
2243
2453
|
ok: true,
|
|
2244
2454
|
name: name ?? rt.name,
|
|
2455
|
+
...(credentialsChanged
|
|
2456
|
+
? { credentialsChanged: true, message: '凭证已更新,旧凭证立即失效,请用新凭证重新登录' }
|
|
2457
|
+
: {}),
|
|
2245
2458
|
// 换了连接方式必须重启才生效,明确告诉前端,别让用户以为已经切了
|
|
2246
2459
|
...(connectionChanged
|
|
2247
2460
|
? {
|
|
@@ -2256,6 +2469,27 @@ async function startWebServerInstance({
|
|
|
2256
2469
|
});
|
|
2257
2470
|
}
|
|
2258
2471
|
|
|
2472
|
+
/**
|
|
2473
|
+
* 恢复默认设置(只设置,不动 data/)。给 dsh-roost 面板的支持弹窗用:
|
|
2474
|
+
* 它自己不写框架配置,而是带面板密钥调这里,避免跨进程抢同一份状态。
|
|
2475
|
+
*
|
|
2476
|
+
* 二次确认放在服务端也做一遍——前端按钮只是一层 UI,裸调接口不该生效。
|
|
2477
|
+
* 成功后的配置由 config-watch 在 ~3s 内热加载;端口不会重新 bind,
|
|
2478
|
+
* 所以 webui.host/port 是保留项(见 config-reset.js)。
|
|
2479
|
+
*/
|
|
2480
|
+
if (pathname === '/api/config/reset' && req.method === 'POST') {
|
|
2481
|
+
const body = await readBody(req);
|
|
2482
|
+
if (body?.confirm !== RESET_CONFIRM) {
|
|
2483
|
+
return json(res, 400, { ok: false, error: `需要 confirm: "${RESET_CONFIRM}"` });
|
|
2484
|
+
}
|
|
2485
|
+
try {
|
|
2486
|
+
const r = await resetFrameworkConfigs({ confirm: body.confirm });
|
|
2487
|
+
return json(res, r.ok ? 200 : 500, r);
|
|
2488
|
+
} catch (e) {
|
|
2489
|
+
return json(res, 500, { ok: false, error: `恢复默认失败:${e?.message || e}` });
|
|
2490
|
+
}
|
|
2491
|
+
}
|
|
2492
|
+
|
|
2259
2493
|
// 插件
|
|
2260
2494
|
if (pathname === '/api/plugins' && req.method === 'GET') {
|
|
2261
2495
|
/**
|
|
@@ -5093,7 +5327,7 @@ async function startWebServerInstance({
|
|
|
5093
5327
|
|
|
5094
5328
|
log.info(`可视化面板: ${baseUrl}`);
|
|
5095
5329
|
if (accessKey) {
|
|
5096
|
-
log.info('
|
|
5330
|
+
log.info('面板已开启访问密钥:打开面板后在登录页输入密钥即可(密钥只在 POST body 里传,不进网址/日志);程序化调用仍可用 X-Access-Key 头或 ?key=');
|
|
5097
5331
|
}
|
|
5098
5332
|
|
|
5099
5333
|
let closed = false;
|