feat: 新增多插件 UI 支持及运行日志集中回传功能

本次提交包含多项功能更新:
1. 为多个插件(xq0002/0003/0004/0008/0010/0011/0020/0031_xq0032/0033/0035/0037/0045_xq0046/0047/0048)新增 UI 界面及配置支持
2. 新增运行日志集中回传功能,客户端自动上传运行日志到中心机集中存储
3. 新增插件分类字段支持,用于场景库分组管理
4. 优化 xq0034/0037/0048 等插件的功能及描述
5. 修复控制面响应兼容问题,支持新旧版本数据格式
6. 新增 xq0033 插件的状态存储模块,用于历史记录管理
7. 优化 CRM 登录窗口模式,支持全屏置顶
This commit is contained in:
2026-10-26 15:42:50 +08:00
parent 8bf33b65cd
commit c73c113721
453 changed files with 243002 additions and 4493 deletions
@@ -14,8 +14,9 @@ import { FileSourceEntity, FileserverStatusEntity } from '../../entity/resource'
import { ClientCredEntity } from '../../entity/account';
import { CenterAuthService, ApiError } from '../../service/auth';
import { rsaEncrypt } from '../../service/crypto';
import { CenterLogService } from '../../service/logstore';
import { PKG_DIR, SHELL_DIR, AVATAR_DIR } from '../../comm/conf';
import { pageBounds } from '../../comm/util';
import { pageBounds, readUploadFile } from '../../comm/util';
const ALLOWED_CREDS_KEYS = ['DINGTALK_APPKEY', 'DINGTALK_APPSECRET', 'DINGTALK_AGENT_ID', 'DINGTALK_CORP_ID'];
const SAFE_NAME_RE = /^[A-Za-z0-9._-]+$/;
@@ -35,6 +36,9 @@ export class AdminOpsController extends BaseController {
@Inject()
centerAuthService: CenterAuthService;
@Inject()
centerLogService: CenterLogService;
@InjectEntityModel(PluginEntity) pluginRepo: Repository<PluginEntity>;
@InjectEntityModel(PluginVersionEntity) pluginVersionRepo: Repository<PluginVersionEntity>;
@InjectEntityModel(AccountPluginEntity) accountPluginRepo: Repository<AccountPluginEntity>;
@@ -53,7 +57,7 @@ export class AdminOpsController extends BaseController {
@Headers('X-Admin-Token') xAdminToken: string = '',
@Headers('X-API-Token') xApiToken: string = ''
) {
await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
await this.centerAuthService.requirePerms('center:overview:view', xAdminToken, xApiToken);
const plugins = await this.pluginRepo.find();
const accounts = await this.accountRepo.find();
const admins = (await this.accountRepo
@@ -86,7 +90,7 @@ export class AdminOpsController extends BaseController {
@Headers('X-Admin-Token') xAdminToken: string = '',
@Headers('X-API-Token') xApiToken: string = ''
) {
await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
await this.centerAuthService.requirePerms('center:stats:view', xAdminToken, xApiToken);
const m = this.runtimeRepo.manager;
const users: any = (await m.query(
`SELECT (SELECT COUNT(*) FROM accounts) AS total,
@@ -152,7 +156,7 @@ export class AdminOpsController extends BaseController {
@Query('action') action: string = '', @Query('q') q: string = '',
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
await this.centerAuthService.requirePerms('center:audit:list', xAdminToken, xApiToken);
const [limit, offset] = pageBounds(page, pageSize);
const cond: string[] = [];
const args: any[] = [];
@@ -180,7 +184,7 @@ export class AdminOpsController extends BaseController {
@Query('status') status: string = '', @Query('q') q: string = '',
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
await this.centerAuthService.requirePerms('center:report:list', xAdminToken, xApiToken);
const [limit, offset] = pageBounds(page, pageSize);
const cond: string[] = [];
const args: any[] = [];
@@ -216,7 +220,7 @@ export class AdminOpsController extends BaseController {
@Query('page') page: number = 1, @Query('page_size') pageSize: number = 20,
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
await this.centerAuthService.requirePerms('center:device:list', xAdminToken, xApiToken);
const [limit, offset] = pageBounds(page, pageSize);
const m = this.deviceRepo.manager;
const total: any = (await m.query('SELECT COUNT(*) AS c FROM devices'))[0];
@@ -232,7 +236,7 @@ export class AdminOpsController extends BaseController {
async devicesSummary(
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
await this.centerAuthService.requirePerms('center:device:list', xAdminToken, xApiToken);
const m = this.deviceRepo.manager;
const total: any = (await m.query('SELECT COUNT(*) AS c FROM devices'))[0];
const byVersion: any[] = await m.query(
@@ -305,11 +309,11 @@ export class AdminOpsController extends BaseController {
@Headers('X-Admin-Token') xAdminToken: string = '',
@Headers('X-API-Token') xApiToken: string = ''
) {
const login = await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
const login = await this.centerAuthService.requirePerms('center:device:summary', xAdminToken, xApiToken);
version = (version || '').trim();
if (!version) throw new ApiError(400, 'version 不能为空');
if (!SAFE_NAME_RE.test(version)) throw new ApiError(400, 'version 仅允许字母数字及 . _ -');
const content: Buffer = file?.data || (file?.filepath ? fs.readFileSync(file.filepath) : null);
const content: Buffer = readUploadFile(file);
if (!content || !content.length) throw new ApiError(400, '上传内容为空');
if (content.length > 1024 * 1024 * 1024) throw new ApiError(413, '壳子安装包超过大小上限(1GB)');
const filename = `setup-37du-host-${version}.exe`;
@@ -330,7 +334,7 @@ export class AdminOpsController extends BaseController {
@Query('page_size') pageSize: number = 20,
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
await this.centerAuthService.requirePerms('center:fileSource:list', xAdminToken, xApiToken);
const qb = this.fileSourceRepo.createQueryBuilder('f');
if ((category || '').trim()) qb.where('f.category = :c', { c: category.trim() });
const rows = await qb.orderBy('f.id', 'DESC').getMany();
@@ -348,7 +352,7 @@ export class AdminOpsController extends BaseController {
@Body() b: any,
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
const login = await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
const login = await this.centerAuthService.requirePerms('center:fileSource:list', xAdminToken, xApiToken);
const category = (b?.category || '').trim();
const version = (b?.version || '').trim();
const url = (b?.url || '').trim();
@@ -375,7 +379,7 @@ export class AdminOpsController extends BaseController {
@Body() b: any, @Param('fid') fid: any,
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
const login = await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
const login = await this.centerAuthService.requirePerms('center:fileSource:list', xAdminToken, xApiToken);
const id = Number(fid || 0);
if (b?.enabled === undefined && b?.enabled === null && b?.notes === undefined) {
throw new ApiError(400, '无更新字段');
@@ -398,7 +402,7 @@ export class AdminOpsController extends BaseController {
@Param('fid') fid: any,
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
const login = await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
const login = await this.centerAuthService.requirePerms('center:fileSource:list', xAdminToken, xApiToken);
const id = Number(fid || 0);
const m = this.fileSourceRepo.manager;
const r: any = await m.query('DELETE FROM file_sources WHERE id = ?', [id]);
@@ -413,7 +417,7 @@ export class AdminOpsController extends BaseController {
@Query('page') page: number = 1, @Query('page_size') pageSize: number = 20,
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
await this.centerAuthService.requirePerms('center:creds:list', xAdminToken, xApiToken);
const [limit, offset] = pageBounds(page, pageSize);
const m = this.clientCredRepo.manager;
const total: any = (await m.query('SELECT COUNT(*) AS c FROM client_creds'))[0];
@@ -436,7 +440,7 @@ export class AdminOpsController extends BaseController {
@Body() b: any,
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
const login = await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
const login = await this.centerAuthService.requirePerms('center:creds:list', xAdminToken, xApiToken);
const account = (b?.account || '').trim();
if (!account) throw new ApiError(400, 'account 不能为空');
const allowed: Record<string, string> = {};
@@ -461,7 +465,7 @@ export class AdminOpsController extends BaseController {
@Query('limit') limit: number = 15,
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
await this.centerAuthService.requireLogin(xAdminToken, xApiToken);
const n = Math.max(1, Math.min(50, Number(limit) || 15));
const rows: any[] = await this.notificationRepo.manager.query(
`SELECT id, title, content, level, target, account, created_at, expires_at
@@ -482,7 +486,7 @@ export class AdminOpsController extends BaseController {
@Query('q') q: string = '',
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
await this.centerAuthService.requirePerms('center:notify:list', xAdminToken, xApiToken);
const [limit, offset] = pageBounds(page, pageSize);
const cond: string[] = [];
const args: any[] = [];
@@ -507,7 +511,7 @@ export class AdminOpsController extends BaseController {
@Body() b: any,
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
const login = await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
const login = await this.centerAuthService.requirePerms('center:notify:list', xAdminToken, xApiToken);
const title = (b?.title || '').trim();
if (!title) throw new ApiError(400, '标题不能为空');
if (!NOTIF_LEVELS.has(b?.level)) throw new ApiError(400, 'level 仅允许 info/warning/important');
@@ -525,7 +529,7 @@ export class AdminOpsController extends BaseController {
@Param('nid') nid: any, @Query('active') active: string = '',
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
const login = await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
const login = await this.centerAuthService.requirePerms('center:notify:list', xAdminToken, xApiToken);
const id = Number(nid || 0);
await this.notificationRepo.manager.query('UPDATE notifications SET active = ? WHERE id = ?', [Number(active === 'true'), id]);
await this.centerAuthService.audit(login, 'notify-toggle', `通知 #${id} active=${active}`);
@@ -537,7 +541,7 @@ export class AdminOpsController extends BaseController {
@Param('nid') nid: any,
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
const login = await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
const login = await this.centerAuthService.requirePerms('center:notify:list', xAdminToken, xApiToken);
const id = Number(nid || 0);
await this.notificationRepo.manager.query('DELETE FROM notifications WHERE id = ?', [id]);
await this.centerAuthService.audit(login, 'notify-delete', `通知 #${id}`);
@@ -549,7 +553,7 @@ export class AdminOpsController extends BaseController {
async fileserverStatus(
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
await this.centerAuthService.requirePerms('center:fileSource:list', xAdminToken, xApiToken);
const rows = await this.fileserverRepo.find({ order: { lastSeen: 'DESC' } as any });
return {
// 契约对齐旧 FastAPI:键名 fileservers;status/offline_notified 布尔;total_size 数值
@@ -582,11 +586,11 @@ export class AdminOpsController extends BaseController {
account = (account || '').trim();
if (!account) throw new ApiError(400, 'account 不能为空');
if (!SAFE_NAME_RE.test(account)) throw new ApiError(400, 'account 含非法字符(仅允许字母数字及 . _ -)');
if (account !== login) await this.centerAuthService.requireAdmin(xAdminToken, xApiToken);
if (account !== login) await this.centerAuthService.requirePerms('center:sys:user', xAdminToken, xApiToken);
const filename = (file?.filename || '').toLowerCase();
const ext = path.extname(filename);
if (!AVATAR_EXT.includes(ext)) throw new ApiError(400, '仅支持图片格式(jpg/png/gif/webp)');
const content: Buffer = file?.data || (file?.filepath ? fs.readFileSync(file.filepath) : null);
const content: Buffer = readUploadFile(file);
if (!content || !content.length || content.length > 2 * 1024 * 1024) throw new ApiError(400, '头像文件为空或超过 2MB');
const acc = await this.accountRepo.findOneBy({ account });
if (!acc) throw new ApiError(400, `账号 ${account} 不存在`);
@@ -599,6 +603,94 @@ export class AdminOpsController extends BaseController {
await this.accountRepo.update({ account }, { avatar: fname });
return { ok: true, account, avatar: fname };
}
// ==================== 集中日志(运行日志 / 客户端壳日志) ====================
/** GET /api/admin/run-logs — 运行日志列表(分页 + 账号/设备/插件/状态/关键字/近N天筛选) */
@Get('/run-logs')
async runLogs(
@Query('page') page: number = 1, @Query('page_size') pageSize: number = 20,
@Query('account') account: string = '', @Query('device') device: string = '',
@Query('plugin') plugin: string = '', @Query('status') status: string = '',
@Query('q') q: string = '', @Query('days') days: number = 0,
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
await this.centerAuthService.requirePerms('center:log:list', xAdminToken, xApiToken);
const r = await this.centerLogService.queryRunLogs({ page, pageSize, account, device, plugin, status, q, days });
// 关联账号昵称(对齐旧接口)
const nick: Record<string, string> = {};
for (const a of await this.accountRepo.find()) nick[a.account] = a.nickname || '';
return {
items: r.items.map((x: any) => ({ ...x, nickname: nick[x.account] || '' })),
total: r.total,
};
}
/** GET /api/admin/run-logs/:runId — 单次运行日志详情(含日志正文) */
@Get('/run-logs/:runId')
async runLogDetail(
@Param('runId') runId: string,
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
await this.centerAuthService.requirePerms('center:log:list', xAdminToken, xApiToken);
const row = await this.centerLogService.getRunLog(runId);
if (!row) throw new ApiError(404, '未找到该运行日志');
return row;
}
/** GET /api/admin/device-logs — 已回传壳日志的设备列表 */
@Get('/device-logs')
async deviceLogs(
@Query('page') page: number = 1, @Query('page_size') pageSize: number = 20,
@Query('account') account: string = '', @Query('q') q: string = '',
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
await this.centerAuthService.requirePerms('center:log:list', xAdminToken, xApiToken);
const r = await this.centerLogService.queryDeviceLogs({ page, pageSize, account, q });
const nick: Record<string, string> = {};
for (const a of await this.accountRepo.find()) nick[a.account] = a.nickname || '';
return {
items: r.items.map((x: any) => ({ ...x, nickname: nick[x.account] || '' })),
total: r.total,
};
}
/** GET /api/admin/device-logs/:deviceId — 设备壳日志尾部内容(最多尾部 512KB) */
@Get('/device-logs/:deviceId')
async deviceLogDetail(
@Param('deviceId') deviceId: string,
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
await this.centerAuthService.requirePerms('center:log:list', xAdminToken, xApiToken);
const row = await this.centerLogService.getDeviceLog(deviceId);
if (!row) throw new ApiError(404, '未找到该设备的壳日志');
return row;
}
/** GET /api/admin/device-logs/:deviceId/download — 设备壳日志全文下载 */
@Get('/device-logs/:deviceId/download')
async deviceLogDownload(
@Param('deviceId') deviceId: string,
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
await this.centerAuthService.requirePerms('center:log:list', xAdminToken, xApiToken);
const file = this.centerLogService.deviceLogFilePath(deviceId);
if (!file) throw new ApiError(404, '未找到该设备的壳日志文件');
this.ctx.attachment(`shell-${deviceId}.log`);
this.ctx.type = 'text/plain; charset=utf-8';
this.ctx.body = fs.createReadStream(file);
}
/** POST /api/admin/logs/cleanup — 手动触发一次保留期清理 */
@Post('/logs/cleanup')
async logsCleanup(
@Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = ''
) {
const login = await this.centerAuthService.requirePerms('center:log:list', xAdminToken, xApiToken);
const result = await this.centerLogService.cleanupExpired();
await this.centerAuthService.audit(login, 'logs-cleanup', 'run-logs',
`deleted_run_logs=${result.deleted_run_logs}, deleted_device_logs=${result.deleted_device_logs}`);
return { ok: true, ...result };
}
}
function versionKey(v: string): number {