From 78002840b32ac2f571c89f36ead6e692dc3e000f Mon Sep 17 00:00:00 2001 From: ycxom Date: Sat, 4 Jan 2025 15:58:18 +0800 Subject: [PATCH] =?UTF-8?q?refactor(bym):=20=E7=A7=BB=E9=99=A4=20SystemCom?= =?UTF-8?q?mandTool=20-=20=E8=B0=A2=E8=B0=A2=E4=BD=A0vscode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/bym.js | 3 -- utils/tools/SystemCommandTool.js | 86 -------------------------------- 2 files changed, 89 deletions(-) delete mode 100644 utils/tools/SystemCommandTool.js diff --git a/apps/bym.js b/apps/bym.js index 4620d6b..8f466ad 100644 --- a/apps/bym.js +++ b/apps/bym.js @@ -20,7 +20,6 @@ import { SerpTool } from '../utils/tools/SerpTool.js' import { initializeImageTool } from '../utils/tools/ImageTool.js' import { DailyNewsTool } from '../utils/tools/DailyNewsTool.js' import { SendMessageToSpecificGroupOrUserTool } from '../utils/tools/SendMessageToSpecificGroupOrUserTool.js' -import { SystemCommandTool } from '../utils/tools/SystemCommandTool.js' const DefaultConfig = { returnQQ: [], @@ -323,8 +322,6 @@ export class bym extends plugin { tools.push(new SetTitleTool()) } - // 你没见过bot能控制系统吗( - tools.push(new SystemCommandTool()) const imageTool = await initializeImageTool(e, previousRole, bymGo) if (Config.AutoToDownImg) { tools.push(imageTool) diff --git a/utils/tools/SystemCommandTool.js b/utils/tools/SystemCommandTool.js deleted file mode 100644 index 0ce5405..0000000 --- a/utils/tools/SystemCommandTool.js +++ /dev/null @@ -1,86 +0,0 @@ -// SystemCommandTool.js -import { AbstractTool } from './AbstractTool.js' -import { exec } from 'child_process' -import { promisify } from 'util' -import os from 'os' -import cfg from '../../../../lib/config/config.js' - -const execAsync = promisify(exec) - -export class SystemCommandTool extends AbstractTool { - name = 'systemCommand' - - #safeCommands = { - windows: [ - 'dir', 'echo', 'type', 'systeminfo', - 'tasklist', 'ver', 'hostname', 'time', - 'date', 'ping', 'ipconfig' - ], - linux: [ - 'ls', 'echo', 'cat', 'uname', 'ps', - 'pwd', 'date', 'uptime', 'free', - 'df', 'ping', 'ifconfig', 'ip','lspci' - ] - } - - parameters = { - properties: { - command: { - type: 'string', - description: 'The command to execute' - } - }, - required: ['command'] - } - - description = `Execute system commands. Commands: ${ - os.platform() === 'win32' - ? this.#safeCommands.windows.join(', ') - : this.#safeCommands.linux.join(', ') - }` - - #isWindows = os.platform() === 'win32' - - func = async (opts, e) => { - if (!cfg.masterQQ?.includes(e.user_id)) return `用户权限不足` - try { - const { command } = opts - if (!command) { - return '命令不能为空' - } - - // 安全性检查 - const mainCommand = command.split(' ')[0].toLowerCase() - const platform = this.#isWindows ? 'windows' : 'linux' - if (!this.#safeCommands[platform].includes(mainCommand)) { - return `命令不在允许列表中。\n可用命令:\n${this.#safeCommands[platform].join(', ')}` - } - - // 执行命令 - logger.info(`[SystemCommandTool] Executing: ${command}`) - const { stdout, stderr } = await execAsync(command, { - timeout: 10000, - maxBuffer: 1024 * 1024 - }) - - // 格式化输出 - let output = '' - if (stdout) output += stdout - if (stderr) output += `\nErrors:\n${stderr}` - - return output.trim() || '命令执行成功,无输出' - - } catch (error) { - logger.error('[SystemCommandTool] Error:', error) - return `命令执行失败: ${error.message}` - } - } - - async processText(text, e) { - const cmdMatch = text.match(/^[!/](cmd|系统|命令)\s+(.+)$/i) - if (cmdMatch) { - return await this.func({ command: cmdMatch[2].trim() }, e) - } - return null - } -} \ No newline at end of file