qq-bot-roost 1.0.13 → 1.0.15
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 +10 -3
- package/config/bot.example.json +30 -0
- package/docs/AI/345/256/277/344/270/273/346/216/245/345/205/245.md +145 -0
- package/docs/DSH/346/216/245/345/205/245.md +267 -0
- package/docs/OneBot/345/205/274/345/256/271/345/261/202/350/256/276/350/256/241.md +1032 -0
- package/docs/images/bot-menu.png +0 -0
- package/docs/images/bot-switcher.png +0 -0
- package/docs/images/events.png +0 -0
- package/docs/images/group-manage.png +0 -0
- package/docs/images/logs.png +0 -0
- package/docs/images/overview.png +0 -0
- package/docs/images/plugin-dash-tab.png +0 -0
- package/docs/images/plugins-dash.png +0 -0
- package/docs/images/plugins.png +0 -0
- package/docs//345/217/221/347/211/210/346/270/205/345/215/225.md +167 -0
- package/docs//345/233/236/346/273/232/351/242/204/346/241/210.md +78 -0
- package/docs//345/256/230/346/226/271/346/216/245/345/217/243/351/231/220/345/210/266.md +375 -0
- package/docs//345/256/277/344/270/273/345/215/217/350/256/256v1.md +402 -0
- package/docs//345/270/270/350/247/201/351/227/256/351/242/230.md +559 -0
- package/docs//346/217/222/344/273/266/345/215/217/350/256/256.md +521 -0
- package/docs//346/217/222/344/273/266/345/274/200/345/217/221/350/247/204/350/214/203.md +1857 -0
- package/docs//346/235/203/351/231/220/346/250/241/345/236/213.md +237 -0
- package/docs//346/236/266/346/236/204/350/257/264/346/230/216.md +311 -0
- package/docs//347/234/237/346/234/272/350/201/224/350/260/203.md +253 -0
- package/docs//351/205/215/347/275/256/345/217/202/350/200/203.md +427 -0
- package/package.json +4 -3
- package/src/api.js +242 -77
- package/src/bot.js +93 -2
- package/src/config.js +150 -6
- package/src/endpoints.js +5 -3
- package/src/ext-deps.js +703 -0
- package/src/host/dispatch.js +125 -0
- package/src/host/index.js +395 -0
- package/src/host/media.js +462 -0
- package/src/host/methods.js +756 -0
- package/src/host/ops.js +462 -0
- package/src/host/serialize.js +79 -0
- package/src/host/server.js +397 -0
- package/src/instance.js +75 -13
- package/src/ledger.js +45 -0
- package/src/main.js +5 -1
- package/src/onebot/actions.js +524 -0
- package/src/onebot/capability.js +95 -0
- package/src/onebot/ids.js +265 -0
- package/src/onebot/index.js +410 -0
- package/src/onebot/segments.js +195 -0
- package/src/onebot/translate.js +219 -0
- package/src/onebot/voice.js +449 -0
- package/src/onebot/ws-server.js +270 -0
- package/src/plugin/loader.js +9 -0
- package/src/plugin/market.js +19 -0
- package/src/web/server.js +408 -1
- package/web/app.js +613 -0
- package/web/index.html +190 -1
- package/web/style.css +69 -0
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OneBot v11 传输层(WS + HTTP 同端口)
|
|
3
|
+
* =====================================
|
|
4
|
+
*
|
|
5
|
+
* 为什么同端口:客户端(aurobot 等)要填 WS 地址和 HTTP 地址两个框,
|
|
6
|
+
* 分成两个端口就会出现「WS 连上了、HTTP 填错」这种半通状态,排查成本很高。
|
|
7
|
+
* 这里一个 `http.Server` 同时处理 `POST /<action>` 和 `upgrade`。
|
|
8
|
+
*
|
|
9
|
+
* 依赖 `ws`(按需安装,见 deps.js)。没装就不该走到这里——上层会先检查。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import http from 'node:http';
|
|
13
|
+
import crypto from 'node:crypto';
|
|
14
|
+
import { createLogger } from '../logger.js';
|
|
15
|
+
|
|
16
|
+
const log = createLogger('onebot-ws');
|
|
17
|
+
|
|
18
|
+
/** 单条报文上限:OneBot 报文很小,超过 1MB 基本就是打错了 */
|
|
19
|
+
const MAX_PAYLOAD = 1024 * 1024;
|
|
20
|
+
|
|
21
|
+
/** 心跳周期:30s 发一次 ping,一个周期没 pong 就断 */
|
|
22
|
+
const PING_MS = 30_000;
|
|
23
|
+
|
|
24
|
+
/** token 比较:长度不等直接 false,长度相等再走 timingSafeEqual(它长度不同会抛) */
|
|
25
|
+
function tokenOk(given, want) {
|
|
26
|
+
if (!want) return true;
|
|
27
|
+
const a = Buffer.from(String(given ?? ''));
|
|
28
|
+
const b = Buffer.from(String(want));
|
|
29
|
+
if (a.length !== b.length) return false;
|
|
30
|
+
return crypto.timingSafeEqual(a, b);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 从请求里取 token:`?access_token=` 或 `Authorization: Bearer xxx` */
|
|
34
|
+
function tokenOf(req, url) {
|
|
35
|
+
const q = url?.searchParams?.get('access_token');
|
|
36
|
+
if (q) return q;
|
|
37
|
+
const h = String(req.headers?.authorization ?? '');
|
|
38
|
+
const m = /^Bearer\s+(.+)$/i.exec(h);
|
|
39
|
+
return m ? m[1] : '';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @param {object} p
|
|
44
|
+
* @param {string} p.host
|
|
45
|
+
* @param {number} p.port
|
|
46
|
+
* @param {string} p.token
|
|
47
|
+
* @param {object} p.ws `ws` 模块本身(由 deps.load('ws') 传进来,**不能静态 import**)
|
|
48
|
+
* @param {(action:string, params:object)=>Promise<any>} p.onAction
|
|
49
|
+
* @param {(client:object)=>void} [p.onConnect]
|
|
50
|
+
* @param {boolean} [p.http] 是否接受 HTTP API
|
|
51
|
+
*/
|
|
52
|
+
export function createOneBotServer({
|
|
53
|
+
host,
|
|
54
|
+
port,
|
|
55
|
+
token,
|
|
56
|
+
ws: wsMod,
|
|
57
|
+
onAction,
|
|
58
|
+
onConnect = null,
|
|
59
|
+
http: enableHttp = true,
|
|
60
|
+
maxConnections = 4,
|
|
61
|
+
}) {
|
|
62
|
+
const { WebSocketServer } = wsMod;
|
|
63
|
+
const wss = new WebSocketServer({ noServer: true, maxPayload: MAX_PAYLOAD, perMessageDeflate: false });
|
|
64
|
+
|
|
65
|
+
const server = http.createServer(async (req, res) => {
|
|
66
|
+
const url = new URL(req.url ?? '/', `http://${req.headers.host ?? '127.0.0.1'}`);
|
|
67
|
+
|
|
68
|
+
if (!enableHttp) {
|
|
69
|
+
res.writeHead(404, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
70
|
+
return res.end(JSON.stringify({ status: 'failed', retcode: 1404, data: null }));
|
|
71
|
+
}
|
|
72
|
+
if (!tokenOk(tokenOf(req, url), token)) {
|
|
73
|
+
res.writeHead(401, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
74
|
+
return res.end(JSON.stringify({ status: 'failed', retcode: 1401, data: null, wording: 'access token 不对' }));
|
|
75
|
+
}
|
|
76
|
+
if (req.method === 'GET' && url.pathname === '/') {
|
|
77
|
+
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
78
|
+
return res.end(
|
|
79
|
+
JSON.stringify({ app: 'roost-onebot', protocol: 'v11', ok: true, note: 'POST /<action> 调用接口' }),
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
if (req.method !== 'POST') {
|
|
83
|
+
res.writeHead(405, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
84
|
+
return res.end(JSON.stringify({ status: 'failed', retcode: 1404, data: null }));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const action = decodeURIComponent(url.pathname.replace(/^\/+/, ''));
|
|
88
|
+
let body = '';
|
|
89
|
+
for await (const chunk of req) {
|
|
90
|
+
body += chunk;
|
|
91
|
+
if (body.length > MAX_PAYLOAD) {
|
|
92
|
+
res.writeHead(413).end();
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
let params = {};
|
|
97
|
+
if (body.trim()) {
|
|
98
|
+
try {
|
|
99
|
+
params = JSON.parse(body);
|
|
100
|
+
} catch {
|
|
101
|
+
res.writeHead(400, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
102
|
+
return res.end(
|
|
103
|
+
JSON.stringify({ status: 'failed', retcode: 1400, data: null, wording: '请求体不是合法 JSON' }),
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
let out;
|
|
109
|
+
try {
|
|
110
|
+
out = { status: 'ok', retcode: 0, data: await onAction(action, params ?? {}), echo: params?.echo };
|
|
111
|
+
} catch (e) {
|
|
112
|
+
out = {
|
|
113
|
+
status: 'failed',
|
|
114
|
+
retcode: Number(e?.retcode) || 1500,
|
|
115
|
+
data: null,
|
|
116
|
+
wording: String(e?.message ?? '内部错误'),
|
|
117
|
+
echo: params?.echo,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
121
|
+
res.end(JSON.stringify(out));
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
server.on('upgrade', (req, socket, head) => {
|
|
125
|
+
const url = new URL(req.url ?? '/', `http://${req.headers.host ?? '127.0.0.1'}`);
|
|
126
|
+
if (!tokenOk(tokenOf(req, url), token)) {
|
|
127
|
+
// 握手阶段就拒,不进 WS 状态机
|
|
128
|
+
socket.write('HTTP/1.1 401 Unauthorized\r\nConnection: close\r\n\r\n');
|
|
129
|
+
socket.destroy();
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
wss.handleUpgrade(req, socket, head, (ws) => wss.emit('connection', ws, req));
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
wss.on('connection', (ws) => {
|
|
136
|
+
// 连接数上限:超了直接关,别让一个客户端把端口占满
|
|
137
|
+
if (wss.clients.size > Math.max(1, Number(maxConnections) || 4)) {
|
|
138
|
+
try {
|
|
139
|
+
ws.close(1013, 'too many connections');
|
|
140
|
+
} catch {
|
|
141
|
+
// 忽略:连接可能已经断了
|
|
142
|
+
}
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
ws.isAlive = true;
|
|
146
|
+
ws.on('pong', () => {
|
|
147
|
+
ws.isAlive = true;
|
|
148
|
+
});
|
|
149
|
+
ws.on('error', (e) => log.warn(`连接异常:${e.message}`));
|
|
150
|
+
ws.on('message', async (raw) => {
|
|
151
|
+
let payload;
|
|
152
|
+
try {
|
|
153
|
+
payload = JSON.parse(String(raw));
|
|
154
|
+
} catch {
|
|
155
|
+
ws.send(JSON.stringify({ status: 'failed', retcode: 1400, data: null, wording: '报文不是合法 JSON' }));
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const action = String(payload?.action ?? '');
|
|
159
|
+
try {
|
|
160
|
+
const data = await onAction(action, payload?.params ?? {});
|
|
161
|
+
ws.send(JSON.stringify({ status: 'ok', retcode: 0, data, echo: payload?.echo }));
|
|
162
|
+
} catch (e) {
|
|
163
|
+
ws.send(
|
|
164
|
+
JSON.stringify({
|
|
165
|
+
status: 'failed',
|
|
166
|
+
retcode: Number(e?.retcode) || 1500,
|
|
167
|
+
data: null,
|
|
168
|
+
wording: String(e?.message ?? '内部错误'),
|
|
169
|
+
echo: payload?.echo,
|
|
170
|
+
}),
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
try {
|
|
175
|
+
onConnect?.(ws);
|
|
176
|
+
} catch (e) {
|
|
177
|
+
log.warn(`连接回调出错:${e.message}`);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
const pinger = setInterval(() => {
|
|
182
|
+
for (const ws of wss.clients) {
|
|
183
|
+
if (ws.isAlive === false) {
|
|
184
|
+
ws.terminate();
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
ws.isAlive = false;
|
|
188
|
+
try {
|
|
189
|
+
ws.ping();
|
|
190
|
+
} catch {
|
|
191
|
+
// 连接已经烂了,下一轮会被 terminate
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}, PING_MS);
|
|
195
|
+
pinger.unref?.();
|
|
196
|
+
|
|
197
|
+
return {
|
|
198
|
+
/** 实际监听端口(`port: 0` 时由系统分配) */
|
|
199
|
+
get port() {
|
|
200
|
+
return server.address()?.port ?? port;
|
|
201
|
+
},
|
|
202
|
+
get connections() {
|
|
203
|
+
return wss.clients.size;
|
|
204
|
+
},
|
|
205
|
+
broadcast(event) {
|
|
206
|
+
const text = JSON.stringify(event);
|
|
207
|
+
for (const ws of wss.clients) {
|
|
208
|
+
if (ws.readyState === 1) {
|
|
209
|
+
try {
|
|
210
|
+
ws.send(text);
|
|
211
|
+
} catch (e) {
|
|
212
|
+
log.warn(`推送失败:${e.message}`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
start() {
|
|
218
|
+
return new Promise((resolve, reject) => {
|
|
219
|
+
const onError = (e) => {
|
|
220
|
+
server.removeListener('listening', onListening);
|
|
221
|
+
reject(e);
|
|
222
|
+
};
|
|
223
|
+
const onListening = () => {
|
|
224
|
+
server.removeListener('error', onError);
|
|
225
|
+
resolve(server.address()?.port ?? port);
|
|
226
|
+
};
|
|
227
|
+
server.once('error', onError);
|
|
228
|
+
server.once('listening', onListening);
|
|
229
|
+
server.listen(port, host);
|
|
230
|
+
});
|
|
231
|
+
},
|
|
232
|
+
async stop() {
|
|
233
|
+
clearInterval(pinger);
|
|
234
|
+
for (const ws of wss.clients) {
|
|
235
|
+
try {
|
|
236
|
+
ws.close(1001, 'server closing');
|
|
237
|
+
} catch {
|
|
238
|
+
// 忽略:下面统一 terminate
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* `wss.close()` 的回调要等所有连接走完 close 握手,对端不理就是 30s。
|
|
243
|
+
* 面板保存配置 / 关机器人时会被这一下卡住,所以给 600ms 宽限后强拆。
|
|
244
|
+
*/
|
|
245
|
+
const hard = setTimeout(() => {
|
|
246
|
+
for (const ws of wss.clients) {
|
|
247
|
+
try {
|
|
248
|
+
ws.terminate();
|
|
249
|
+
} catch {
|
|
250
|
+
// 已经关了
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}, 600);
|
|
254
|
+
hard.unref?.();
|
|
255
|
+
await new Promise((resolve) => wss.close(() => resolve()));
|
|
256
|
+
clearTimeout(hard);
|
|
257
|
+
await new Promise((resolve) => {
|
|
258
|
+
const t = setTimeout(() => {
|
|
259
|
+
server.closeAllConnections?.();
|
|
260
|
+
resolve();
|
|
261
|
+
}, 2000);
|
|
262
|
+
t.unref?.();
|
|
263
|
+
server.close(() => {
|
|
264
|
+
clearTimeout(t);
|
|
265
|
+
resolve();
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
},
|
|
269
|
+
};
|
|
270
|
+
}
|
package/src/plugin/loader.js
CHANGED
|
@@ -512,6 +512,14 @@ export class PluginManager {
|
|
|
512
512
|
|
|
513
513
|
async #loadExternal(id, dir) {
|
|
514
514
|
const raw = JSON.parse(await fs.readFile(path.join(dir, 'plugin.json'), 'utf8'));
|
|
515
|
+
/**
|
|
516
|
+
* DSH 扩展不归框架加载。它们没有 `index.js`、runtime 是 mcp,旧逻辑会回落成
|
|
517
|
+
* python 去拉一个不存在的子进程。明确跳过并留一句可查的 warn,比静默失败好。
|
|
518
|
+
*/
|
|
519
|
+
if (raw?.kind === 'dsh-extension') {
|
|
520
|
+
log.warn(`目录 ${id} 是 DSH 扩展(kind=dsh-extension),不归框架插件加载;请用 dsh-roost 面板安装`);
|
|
521
|
+
return false;
|
|
522
|
+
}
|
|
515
523
|
if (raw.id && raw.id !== id) {
|
|
516
524
|
log.warn(`插件目录 ${id} 的 plugin.json 声明 id 为 ${raw.id},与目录名不一致,以目录名为准`);
|
|
517
525
|
}
|
|
@@ -796,6 +804,7 @@ export class PluginManager {
|
|
|
796
804
|
// 外部插件:plugin.json 就是元信息
|
|
797
805
|
try {
|
|
798
806
|
const raw = JSON.parse(await fs.readFile(path.join(dir, 'plugin.json'), 'utf8'));
|
|
807
|
+
if (raw?.kind === 'dsh-extension') return; // 扩展不进框架插件列表
|
|
799
808
|
this.#cacheMeta(id, raw, 'external', raw.runtime || 'python');
|
|
800
809
|
return;
|
|
801
810
|
} catch {
|
package/src/plugin/market.js
CHANGED
|
@@ -485,8 +485,25 @@ async function downloadRepoContents({
|
|
|
485
485
|
}
|
|
486
486
|
}
|
|
487
487
|
|
|
488
|
+
/**
|
|
489
|
+
* 索引里的 **DSH 扩展**条目。
|
|
490
|
+
*
|
|
491
|
+
* 扩展与框架插件共用 `index.json` / `plugin.json`,但运行形态完全不同:扩展是
|
|
492
|
+
* 标准 MCP server,由 DSH 按受管块 spawn,没有 `index.js`、没有 version 契约,
|
|
493
|
+
* 装在 `~/.dsh/dsh-roost/extensions/`,由 dsh-roost 自己的安装器管。
|
|
494
|
+
*
|
|
495
|
+
* 框架侧(这里)必须跳过它:装进 `plugins/` 后 loader 会按不认识的 runtime 处理,
|
|
496
|
+
* 旧逻辑还会回落成 `python` 去拉一个不存在的子进程,表现是「装上了但一点反应都没有」。
|
|
497
|
+
*/
|
|
498
|
+
export function isExtensionEntry(entry) {
|
|
499
|
+
return entry?.kind === 'dsh-extension';
|
|
500
|
+
}
|
|
501
|
+
|
|
488
502
|
export async function installPlugin({ entry, pluginsDir, repo, branch = 'main', exists, sha256 = null }) {
|
|
489
503
|
const id = String(entry?.id ?? '').trim();
|
|
504
|
+
if (isExtensionEntry(entry)) {
|
|
505
|
+
throw new Error(`「${id}」是 DSH 扩展,不归框架插件市场;请在 dsh-roost 面板的「设置 → 扩展」里安装`);
|
|
506
|
+
}
|
|
490
507
|
if (!/^[a-z0-9][a-z0-9_-]{0,31}$/.test(id)) throw new Error(`插件 id 不合法:${id}`);
|
|
491
508
|
if (!entry?.path) throw new Error('索引里缺少 path 字段');
|
|
492
509
|
if (!repo) throw new Error('索引里缺少 repo 字段');
|
|
@@ -586,6 +603,7 @@ export { compareVersion } from '../semver.js';
|
|
|
586
603
|
export function detectUpdates(indexPlugins, localVersions = {}) {
|
|
587
604
|
const out = [];
|
|
588
605
|
for (const entry of indexPlugins ?? []) {
|
|
606
|
+
if (isExtensionEntry(entry)) continue; // 扩展按目录哈希由 dsh-roost 更新
|
|
589
607
|
const cur = localVersions[entry.id];
|
|
590
608
|
// 没装、或本地版本号缺失/不合法 → 不提示,让用户自己决定
|
|
591
609
|
if (!cur) continue;
|
|
@@ -684,6 +702,7 @@ export async function updatePlugins({ tmp, index, pluginsDir, ids, localVersions
|
|
|
684
702
|
const result = { updated: [], skipped: [], failed: [] };
|
|
685
703
|
|
|
686
704
|
for (const entry of index.plugins ?? []) {
|
|
705
|
+
if (isExtensionEntry(entry)) { result.skipped.push({ id: entry.id, reason: 'DSH 扩展,不归框架市场更新' }); continue; }
|
|
687
706
|
if (want && !want.has(entry.id)) continue;
|
|
688
707
|
|
|
689
708
|
const dest = path.join(pluginsDir, entry.id);
|