@michengai/dsh-im-connect 0.1.37 → 0.1.39
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 +17 -1
- package/CHANGELOG.zh-CN.md +17 -1
- package/README.en.md +60 -65
- package/README.md +60 -65
- package/SECURITY.md +7 -2
- package/client.js +31 -298
- package/lib/channels/channel-image-download.d.ts +22 -0
- package/lib/channels/channel-image-download.d.ts.map +1 -0
- package/lib/channels/channel-image-download.js +179 -0
- package/lib/channels/channel-image-download.js.map +1 -0
- package/lib/channels/dingtalk-token-cache.d.ts +18 -0
- package/lib/channels/dingtalk-token-cache.d.ts.map +1 -0
- package/lib/channels/dingtalk-token-cache.js +48 -0
- package/lib/channels/dingtalk-token-cache.js.map +1 -0
- package/lib/channels/dingtalk.d.ts +17 -4
- package/lib/channels/dingtalk.d.ts.map +1 -1
- package/lib/channels/dingtalk.js +111 -18
- package/lib/channels/dingtalk.js.map +1 -1
- package/lib/channels/factory.d.ts.map +1 -1
- package/lib/channels/factory.js +6 -3
- package/lib/channels/factory.js.map +1 -1
- package/lib/channels/feishu.d.ts +30 -2
- package/lib/channels/feishu.d.ts.map +1 -1
- package/lib/channels/feishu.js +141 -26
- package/lib/channels/feishu.js.map +1 -1
- package/lib/channels/image-host-policy.d.ts +4 -0
- package/lib/channels/image-host-policy.d.ts.map +1 -0
- package/lib/channels/image-host-policy.js +24 -0
- package/lib/channels/image-host-policy.js.map +1 -0
- package/lib/channels/qq.d.ts +1 -0
- package/lib/channels/qq.d.ts.map +1 -1
- package/lib/channels/qq.js +89 -18
- package/lib/channels/qq.js.map +1 -1
- package/lib/channels/telegram.d.ts.map +1 -1
- package/lib/channels/telegram.js +121 -16
- package/lib/channels/telegram.js.map +1 -1
- package/lib/channels/wecom.d.ts +8 -2
- package/lib/channels/wecom.d.ts.map +1 -1
- package/lib/channels/wecom.js +60 -11
- package/lib/channels/wecom.js.map +1 -1
- package/lib/channels/weixin.d.ts.map +1 -1
- package/lib/channels/weixin.js +169 -17
- package/lib/channels/weixin.js.map +1 -1
- package/lib/client.js +31 -298
- package/lib/engine/gateway.d.ts +2 -0
- package/lib/engine/gateway.d.ts.map +1 -1
- package/lib/engine/gateway.js +48 -11
- package/lib/engine/gateway.js.map +1 -1
- package/lib/engine/image-input.d.ts +8 -0
- package/lib/engine/image-input.d.ts.map +1 -0
- package/lib/engine/image-input.js +31 -0
- package/lib/engine/image-input.js.map +1 -0
- package/lib/engine/router.d.ts.map +1 -1
- package/lib/engine/router.js +29 -19
- package/lib/engine/router.js.map +1 -1
- package/lib/manager.d.ts +0 -10
- package/lib/manager.d.ts.map +1 -1
- package/lib/manager.js +231 -203
- package/lib/manager.js.map +1 -1
- package/package.json +1 -1
- package/src/channels/channel-image-download.ts +154 -0
- package/src/channels/dingtalk-token-cache.ts +39 -0
- package/src/channels/dingtalk.ts +106 -27
- package/src/channels/factory.ts +6 -3
- package/src/channels/feishu.ts +126 -24
- package/src/channels/image-host-policy.ts +21 -0
- package/src/channels/qq.ts +80 -26
- package/src/channels/telegram.ts +105 -16
- package/src/channels/wecom.ts +57 -12
- package/src/channels/weixin.ts +124 -14
- package/src/engine/gateway.ts +35 -6
- package/src/engine/image-input.ts +26 -0
- package/src/engine/router.ts +28 -18
- package/src/manager.ts +116 -84
package/lib/manager.js
CHANGED
|
@@ -2,6 +2,7 @@ import { createHash, randomUUID } from 'node:crypto';
|
|
|
2
2
|
import { readFileSync } from 'node:fs';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import { createChannelAdapter } from './channels/factory.js';
|
|
5
|
+
import { parseAdditionalImageHosts } from './channels/image-host-policy.js';
|
|
5
6
|
import { CHANNEL_META, CHANNEL_ORDER, supportsQr } from './channels/meta.js';
|
|
6
7
|
import { PairingHub } from './channels/qr/hub.js';
|
|
7
8
|
import { SessionMapStore } from './engine/session-store.js';
|
|
@@ -22,17 +23,8 @@ class ApiRequestError extends Error {
|
|
|
22
23
|
this.status = status;
|
|
23
24
|
}
|
|
24
25
|
}
|
|
25
|
-
/**
|
|
26
|
-
|
|
27
|
-
const remote = request.remoteAddress ?? '';
|
|
28
|
-
if (!(remote === '::1' || /^127\./.test(remote) || /^::ffff:127\./i.test(remote))) {
|
|
29
|
-
return { status: 403, error: 'forbidden client address' };
|
|
30
|
-
}
|
|
31
|
-
const hostValue = request.headers.host;
|
|
32
|
-
const host = String(Array.isArray(hostValue) ? hostValue[0] ?? '' : hostValue ?? '').toLowerCase();
|
|
33
|
-
if (!/^(localhost|127\.0\.0\.1|\[::1\])(:\d+)?$/.test(host)) {
|
|
34
|
-
return { status: 403, error: 'forbidden host' };
|
|
35
|
-
}
|
|
26
|
+
/** 宿主负责身份认证;插件仍限制写请求格式,避免简单跨站表单与错误载荷。 */
|
|
27
|
+
function validateApiMutation(request) {
|
|
36
28
|
const method = (request.method ?? 'GET').toUpperCase();
|
|
37
29
|
if (method === 'GET' || method === 'HEAD')
|
|
38
30
|
return undefined;
|
|
@@ -233,6 +225,16 @@ export class ChannelManager {
|
|
|
233
225
|
const hadAnotherIdentity = !existed && Object.entries(this.store.channels)
|
|
234
226
|
.some(([existingId, state]) => existingId !== accountId && this.platformOf(existingId, state) === id);
|
|
235
227
|
const prev = this.store.channels[accountId] ?? {};
|
|
228
|
+
if (['qq', 'wecom', 'dingtalk'].includes(id)) {
|
|
229
|
+
const hosts = Object.hasOwn(incoming, 'additionalImageHosts') ? incoming.additionalImageHosts : prev.config?.additionalImageHosts;
|
|
230
|
+
try {
|
|
231
|
+
if (hosts !== undefined)
|
|
232
|
+
incoming.additionalImageHosts = parseAdditionalImageHosts(hosts).join('\n');
|
|
233
|
+
}
|
|
234
|
+
catch {
|
|
235
|
+
return { ok: false, error: '图片扩展主机配置无效:请填写精确域名,不要包含通配符、URL、端口或 IP 地址。' };
|
|
236
|
+
}
|
|
237
|
+
}
|
|
236
238
|
const normalized = this.normalizeAccountSettings(id, settings, prev);
|
|
237
239
|
if (!normalized.ok)
|
|
238
240
|
return normalized;
|
|
@@ -356,7 +358,7 @@ export class ChannelManager {
|
|
|
356
358
|
if (!webServer)
|
|
357
359
|
return;
|
|
358
360
|
const send = (res, status, body) => {
|
|
359
|
-
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8' });
|
|
361
|
+
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
360
362
|
res.end(JSON.stringify(body));
|
|
361
363
|
};
|
|
362
364
|
const readJson = async (req) => {
|
|
@@ -367,228 +369,254 @@ export class ChannelManager {
|
|
|
367
369
|
throw new ApiRequestError(400, '请求体不是合法 JSON');
|
|
368
370
|
return body;
|
|
369
371
|
};
|
|
370
|
-
const
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
372
|
+
const dispatch = async (method, path, readBody, send) => {
|
|
373
|
+
const payload = () => ({
|
|
374
|
+
ok: true,
|
|
375
|
+
channels: this.list(),
|
|
376
|
+
groups: this.channelSessions(),
|
|
377
|
+
pending: this.pendingRequests(),
|
|
378
|
+
assistant: this.currentAssistant(),
|
|
379
|
+
});
|
|
380
|
+
try {
|
|
381
|
+
// 路由段统一从插件前缀之后开始,身份认证只由下方入口负责。
|
|
382
|
+
const parts = ('/dsh-im-connect/api' + path).split('/').filter(Boolean);
|
|
383
|
+
if (parts[2] === 'assistant' && parts.length === 3) {
|
|
384
|
+
if (method === 'GET') {
|
|
385
|
+
send(200, {
|
|
386
|
+
ok: true,
|
|
387
|
+
assistant: this.currentAssistant(),
|
|
388
|
+
cwd: this.currentWorkspace(),
|
|
389
|
+
permission: this.currentPermission(),
|
|
390
|
+
permissions: this.permissionOptions(),
|
|
391
|
+
providers: await this.listModelCatalog(),
|
|
392
|
+
});
|
|
385
393
|
return;
|
|
386
394
|
}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
send(res, 200, {
|
|
392
|
-
ok: true,
|
|
393
|
-
assistant: this.currentAssistant(),
|
|
394
|
-
cwd: this.currentWorkspace(),
|
|
395
|
-
permission: this.currentPermission(),
|
|
396
|
-
permissions: this.permissionOptions(),
|
|
397
|
-
providers: await this.listModelCatalog(),
|
|
398
|
-
});
|
|
399
|
-
return;
|
|
400
|
-
}
|
|
401
|
-
if (req.method === 'POST') {
|
|
402
|
-
const body = await readJson(req);
|
|
403
|
-
const result = this.setAssistant(body);
|
|
404
|
-
send(res, result.ok ? 200 : 400, result);
|
|
405
|
-
return;
|
|
406
|
-
}
|
|
407
|
-
send(res, 405, { ok: false, error: 'method not allowed' });
|
|
395
|
+
if (method === 'POST') {
|
|
396
|
+
const body = await readBody();
|
|
397
|
+
const result = this.setAssistant(body);
|
|
398
|
+
send(result.ok ? 200 : 400, result);
|
|
408
399
|
return;
|
|
409
400
|
}
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
send(res, ok ? 200 : 404, ok ? { ok: true, groups: this.channelSessions() } : { ok: false, error: '会话不存在' });
|
|
426
|
-
return;
|
|
427
|
-
}
|
|
428
|
-
if (action === 'remove') {
|
|
429
|
-
const ok = await this.engine.removeSession(sessionId);
|
|
430
|
-
send(res, ok ? 200 : 404, ok ? { ok: true, groups: this.channelSessions() } : { ok: false, error: '会话不存在' });
|
|
431
|
-
return;
|
|
432
|
-
}
|
|
433
|
-
if (action === 'ensure') {
|
|
434
|
-
const ok = await this.engine.ensureSession(sessionId);
|
|
435
|
-
send(res, ok ? 200 : 404, ok ? { ok: true, sessionId } : { ok: false, error: '会话不存在' });
|
|
401
|
+
send(405, { ok: false, error: 'method not allowed' });
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
if (parts[2] === 'sessions' && parts.length === 4 && method === 'POST') {
|
|
405
|
+
const action = parts[3];
|
|
406
|
+
const body = await readBody();
|
|
407
|
+
const sessionId = String(body.sessionId ?? '');
|
|
408
|
+
if (!sessionId) {
|
|
409
|
+
send(400, { ok: false, error: '缺少 sessionId' });
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
if (action === 'rename') {
|
|
413
|
+
const title = String(body.title ?? '').trim();
|
|
414
|
+
if (!title) {
|
|
415
|
+
send(400, { ok: false, error: '缺少标题' });
|
|
436
416
|
return;
|
|
437
417
|
}
|
|
438
|
-
|
|
418
|
+
const ok = this.engine.renameSession(sessionId, title);
|
|
419
|
+
send(ok ? 200 : 404, ok ? { ok: true, groups: this.channelSessions() } : { ok: false, error: '会话不存在' });
|
|
439
420
|
return;
|
|
440
421
|
}
|
|
441
|
-
if (
|
|
442
|
-
|
|
422
|
+
if (action === 'remove') {
|
|
423
|
+
const ok = await this.engine.removeSession(sessionId);
|
|
424
|
+
send(ok ? 200 : 404, ok ? { ok: true, groups: this.channelSessions() } : { ok: false, error: '会话不存在' });
|
|
443
425
|
return;
|
|
444
426
|
}
|
|
445
|
-
if (
|
|
446
|
-
const
|
|
447
|
-
|
|
448
|
-
if (!CHANNEL_META[id]) {
|
|
449
|
-
send(res, 404, { ok: false, error: '未知渠道' });
|
|
450
|
-
return;
|
|
451
|
-
}
|
|
452
|
-
if (!supportsQr(id)) {
|
|
453
|
-
send(res, 400, { ok: false, error: '该渠道不支持扫码绑定' });
|
|
454
|
-
return;
|
|
455
|
-
}
|
|
456
|
-
if (action === 'status' && req.method === 'GET') {
|
|
457
|
-
send(res, 200, { ok: true, pairing: this.pairing.view(id), channel: this.list().find((item) => item.id === id) });
|
|
458
|
-
return;
|
|
459
|
-
}
|
|
460
|
-
if (req.method !== 'POST') {
|
|
461
|
-
send(res, 405, { ok: false, error: 'method not allowed' });
|
|
462
|
-
return;
|
|
463
|
-
}
|
|
464
|
-
const body = await readJson(req);
|
|
465
|
-
if (action === 'start') {
|
|
466
|
-
const pairing = await this.pairing.start(id, pairingSettings(body.settings));
|
|
467
|
-
send(res, pairing.status === 'failed' ? 400 : 200, { ok: pairing.status !== 'failed', pairing, error: pairing.error });
|
|
468
|
-
return;
|
|
469
|
-
}
|
|
470
|
-
if (action === 'refresh') {
|
|
471
|
-
const pairing = await this.pairing.refresh(id);
|
|
472
|
-
send(res, pairing.status === 'failed' ? 400 : 200, { ok: pairing.status !== 'failed', pairing, error: pairing.error });
|
|
473
|
-
return;
|
|
474
|
-
}
|
|
475
|
-
if (action === 'cancel') {
|
|
476
|
-
send(res, 200, { ok: true, pairing: this.pairing.cancel(id) });
|
|
477
|
-
return;
|
|
478
|
-
}
|
|
479
|
-
send(res, 404, { ok: false, error: `未知扫码操作 ${action}` });
|
|
427
|
+
if (action === 'ensure') {
|
|
428
|
+
const ok = await this.engine.ensureSession(sessionId);
|
|
429
|
+
send(ok ? 200 : 404, ok ? { ok: true, sessionId } : { ok: false, error: '会话不存在' });
|
|
480
430
|
return;
|
|
481
431
|
}
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
return;
|
|
495
|
-
}
|
|
496
|
-
if (action === 'disconnect') {
|
|
497
|
-
await this.disconnect(id);
|
|
498
|
-
send(res, 200, { ok: true, channel: this.list().find((item) => item.id === id) });
|
|
499
|
-
return;
|
|
500
|
-
}
|
|
501
|
-
if (action === 'remove') {
|
|
502
|
-
await this.remove(id);
|
|
503
|
-
send(res, 200, { ok: true, channel: this.list().find((item) => item.id === id) });
|
|
504
|
-
return;
|
|
505
|
-
}
|
|
506
|
-
if (action === 'approve' || action === 'deny') {
|
|
507
|
-
const userId = String(body.userId ?? '');
|
|
508
|
-
if (!userId) {
|
|
509
|
-
send(res, 400, { ok: false, error: '缺少 userId' });
|
|
510
|
-
return;
|
|
511
|
-
}
|
|
512
|
-
const accountId = this.resolveAccountId(id);
|
|
513
|
-
if (!accountId) {
|
|
514
|
-
send(res, 404, { ok: false, error: '账号不存在或该渠道包含多个账号' });
|
|
515
|
-
return;
|
|
516
|
-
}
|
|
517
|
-
if (action === 'approve')
|
|
518
|
-
this.approve(accountId, userId);
|
|
519
|
-
else
|
|
520
|
-
this.deny(accountId, userId);
|
|
521
|
-
send(res, 200, { ok: true, pending: this.pendingRequests() });
|
|
522
|
-
return;
|
|
523
|
-
}
|
|
524
|
-
send(res, 404, { ok: false, error: `未知操作 ${action}` });
|
|
432
|
+
send(404, { ok: false, error: `未知会话操作 ${action}` });
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
if (parts[2] === 'channels' && parts.length === 3 && method === 'GET') {
|
|
436
|
+
send(200, payload());
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
if (parts[2] === 'channels' && parts.length === 6 && parts[4] === 'qr') {
|
|
440
|
+
const id = parts[3];
|
|
441
|
+
const action = parts[5];
|
|
442
|
+
if (!CHANNEL_META[id]) {
|
|
443
|
+
send(404, { ok: false, error: '未知渠道' });
|
|
525
444
|
return;
|
|
526
445
|
}
|
|
527
|
-
if (
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
446
|
+
if (!supportsQr(id)) {
|
|
447
|
+
send(400, { ok: false, error: '该渠道不支持扫码绑定' });
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
if (action === 'status' && method === 'GET') {
|
|
451
|
+
send(200, { ok: true, pairing: this.pairing.view(id), channel: this.list().find((item) => item.id === id) });
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
if (method !== 'POST') {
|
|
455
|
+
send(405, { ok: false, error: 'method not allowed' });
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
const body = await readBody();
|
|
459
|
+
if (action === 'start') {
|
|
460
|
+
const pairing = await this.pairing.start(id, pairingSettings(body.settings));
|
|
461
|
+
send(pairing.status === 'failed' ? 400 : 200, { ok: pairing.status !== 'failed', pairing, error: pairing.error });
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
if (action === 'refresh') {
|
|
465
|
+
const pairing = await this.pairing.refresh(id);
|
|
466
|
+
send(pairing.status === 'failed' ? 400 : 200, { ok: pairing.status !== 'failed', pairing, error: pairing.error });
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
if (action === 'cancel') {
|
|
470
|
+
send(200, { ok: true, pairing: this.pairing.cancel(id) });
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
send(404, { ok: false, error: `未知扫码操作 ${action}` });
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
if (parts[2] === 'channels' && parts.length === 5 && method === 'POST') {
|
|
477
|
+
const id = parts[3];
|
|
478
|
+
const action = parts[4];
|
|
479
|
+
const body = await readBody();
|
|
480
|
+
if (action === 'connect') {
|
|
481
|
+
const result = await this.connect(id, body.config, body.settings);
|
|
482
|
+
send(result.ok ? 200 : 400, result.ok ? { ...result, channel: this.list().find((item) => item.id === id) } : result);
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
if (action === 'receive') {
|
|
486
|
+
const result = await this.setReceive(id, body.receiveEnabled !== false);
|
|
487
|
+
send(result.ok ? 200 : 400, result.ok ? { ok: true, channel: this.list().find((item) => item.id === id) } : result);
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
if (action === 'disconnect') {
|
|
491
|
+
await this.disconnect(id);
|
|
492
|
+
send(200, { ok: true, channel: this.list().find((item) => item.id === id) });
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
if (action === 'remove') {
|
|
496
|
+
await this.remove(id);
|
|
497
|
+
send(200, { ok: true, channel: this.list().find((item) => item.id === id) });
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
if (action === 'approve' || action === 'deny') {
|
|
501
|
+
const userId = String(body.userId ?? '');
|
|
502
|
+
if (!userId) {
|
|
503
|
+
send(400, { ok: false, error: '缺少 userId' });
|
|
555
504
|
return;
|
|
556
505
|
}
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
send(
|
|
506
|
+
const accountId = this.resolveAccountId(id);
|
|
507
|
+
if (!accountId) {
|
|
508
|
+
send(404, { ok: false, error: '账号不存在或该渠道包含多个账号' });
|
|
560
509
|
return;
|
|
561
510
|
}
|
|
562
|
-
if (action === 'approve'
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
511
|
+
if (action === 'approve')
|
|
512
|
+
this.approve(accountId, userId);
|
|
513
|
+
else
|
|
514
|
+
this.deny(accountId, userId);
|
|
515
|
+
send(200, { ok: true, pending: this.pendingRequests() });
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
send(404, { ok: false, error: `未知操作 ${action}` });
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
if (parts[2] === 'accounts' && parts.length === 5 && method === 'POST') {
|
|
522
|
+
const accountId = parts[3];
|
|
523
|
+
const action = parts[4];
|
|
524
|
+
const body = await readBody();
|
|
525
|
+
if (!this.store.channels[accountId]) {
|
|
526
|
+
send(404, { ok: false, error: '账号不存在' });
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
if (action === 'settings') {
|
|
530
|
+
const result = await this.updateAccount(accountId, body);
|
|
531
|
+
send(result.ok ? 200 : 400, result);
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
if (action === 'receive') {
|
|
535
|
+
const result = await this.setReceive(accountId, body.receiveEnabled !== false);
|
|
536
|
+
send(result.ok ? 200 : 400, result);
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
if (action === 'reconnect') {
|
|
540
|
+
const result = await this.reconnect(accountId);
|
|
541
|
+
send(result.ok ? 200 : 400, result);
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
if (action === 'check') {
|
|
545
|
+
const state = this.store.channels[accountId];
|
|
546
|
+
state.lastCheckedAt = new Date().toISOString();
|
|
547
|
+
this.flush();
|
|
548
|
+
send(200, { ok: true, account: this.accountView(accountId, state) });
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
if (action === 'remove') {
|
|
552
|
+
await this.remove(accountId);
|
|
553
|
+
send(200, { ok: true });
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
if (action === 'approve' || action === 'deny') {
|
|
557
|
+
const userId = String(body.userId ?? '');
|
|
558
|
+
if (!userId) {
|
|
559
|
+
send(400, { ok: false, error: '缺少 userId' });
|
|
570
560
|
return;
|
|
571
561
|
}
|
|
572
|
-
|
|
562
|
+
action === 'approve' ? this.approve(accountId, userId) : this.deny(accountId, userId);
|
|
563
|
+
send(200, { ok: true, pending: this.pendingRequests() });
|
|
573
564
|
return;
|
|
574
565
|
}
|
|
575
|
-
send(
|
|
566
|
+
send(404, { ok: false, error: `未知账号操作 ${action}` });
|
|
567
|
+
return;
|
|
576
568
|
}
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
569
|
+
send(404, { ok: false, error: 'not found' });
|
|
570
|
+
}
|
|
571
|
+
catch (error) {
|
|
572
|
+
// 单个路由异常不能让 HTTP 连接悬死,统一回 500 并落日志。
|
|
573
|
+
if (error instanceof ApiRequestError) {
|
|
574
|
+
send(error.status, { ok: false, error: error.message });
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
578
|
+
this.log(`[manager] API 处理失败: ${detail}`);
|
|
579
|
+
send(500, { ok: false, error: '操作失败,请查看本机日志' });
|
|
580
|
+
}
|
|
581
|
+
};
|
|
582
|
+
const basePath = '/api/dsh-im-connect';
|
|
583
|
+
const dispose = webServer.register({
|
|
584
|
+
kind: 'prefix',
|
|
585
|
+
path: basePath,
|
|
586
|
+
handler: async (req, res) => {
|
|
587
|
+
try {
|
|
588
|
+
// 动态读取活动服务,并保留 requestRejection 的 this 绑定。
|
|
589
|
+
const connection = (typeof ctx.get === 'function' ? ctx.get('connection') : undefined);
|
|
590
|
+
if (typeof connection?.requestRejection !== 'function') {
|
|
591
|
+
send(res, 503, { ok: false, error: '宿主认证服务尚未就绪或版本不支持;请稍后重试或升级宿主。 / Retry or upgrade DSH to enable authenticated management.' });
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
const rejection = connection.requestRejection(req);
|
|
595
|
+
if (rejection !== undefined) {
|
|
596
|
+
send(res, rejection, { ok: false, error: rejection === 401 ? '请先登录 DSH / Sign in to DSH first' : '宿主拒绝此 Host 或跨站请求 / Host or cross-site request forbidden' });
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
const requestError = validateApiMutation(req);
|
|
600
|
+
if (requestError) {
|
|
601
|
+
send(res, requestError.status, { ok: false, error: requestError.error });
|
|
581
602
|
return;
|
|
582
603
|
}
|
|
583
|
-
const
|
|
584
|
-
|
|
604
|
+
const url = new URL(req.url ?? '/', 'http://localhost');
|
|
605
|
+
if (url.pathname !== basePath && !url.pathname.startsWith(basePath + '/')) {
|
|
606
|
+
send(res, 404, { ok: false, error: 'not found' });
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
await dispatch(req.method ?? 'GET', url.pathname.slice(basePath.length), () => readJson(req), (status, body) => send(res, status, body));
|
|
610
|
+
}
|
|
611
|
+
catch (error) {
|
|
612
|
+
this.log(`[manager] API 认证失败: ${error instanceof Error ? error.message : String(error)}`);
|
|
585
613
|
send(res, 500, { ok: false, error: '操作失败,请查看本机日志' });
|
|
586
614
|
}
|
|
587
615
|
},
|
|
588
616
|
});
|
|
589
617
|
if (typeof dispose === 'function')
|
|
590
618
|
this.apiDisposers.push(dispose);
|
|
591
|
-
this.log('[manager] API 已注册 /dsh-im-connect
|
|
619
|
+
this.log('[manager] API 已注册 /api/dsh-im-connect');
|
|
592
620
|
}
|
|
593
621
|
disposeApi() {
|
|
594
622
|
this.disposed = true;
|