Feature: Support the Tier 1 MCP tools on Linux clients
All of these tools already had a working wire protocol on the Linux
client (SystemManager.h, common/PTYHandler.h, ScreenHandler.h); only the
server-side client-type gates rejected LNX hosts. kill_process,
exec_command, terminal_open/exec/close and remote_open/close/mouse/
keyboard/clipboard now accept LNX and reject only MAC (plus APK where it
applied). No client change, no capability bit.
exec_command and terminal_exec needed platform-aware plumbing because a
POSIX shell understands none of the Windows constructs: the sentinel is
now "PS1=" / the command / "echo __MCP_DONE_<nonce>__$?" over the Linux
PTY, with output decoded as UTF-8. The PS1= line exists because
interactive bash prints its prompt after every line it reads, which
otherwise leaks into stdout; its echo sits ahead of the command echo and
is removed together with it. The Windows ConPTY and legacy non-PTY forms
are unchanged.
The default command whitelist is now platform-specific: identically named
POSIX commands can write files or change system state (find -exec,
sort -o, hostname), so kDefaultCmdWhitelistPosix only lists commands that
stay read-only even with arguments. env is deliberately excluded, since
"env <program>" runs arbitrary programs and would defeat the whitelist
entirely. An explicit McpCmdWhitelist still overrides both platforms. The
forbidden metacharacter set is split the same way: on Linux it covers
& | < > ; backtick and $( / ${ / !, where ! triggers history expansion in
interactive bash. ^ is not a POSIX metacharacter, so read-only commands
like "grep -v ^# /etc/hosts" are accepted on Linux while Windows keeps
rejecting them.
Input injection had to be sent one MSG64 per COMMAND_SCREEN_CONTROL
packet. The protocol allows batching and the Windows client handles it,
but linux/ScreenHandler.h only processes the first message of a packet,
so a batch left key_press stuck down (X11 autorepeat), click
half-pressed and type emitting only its first character. Splitting the
packets is equivalent for Windows, which processes one message at a time
anyway, and keeps already-deployed Linux clients working without an
upgrade.
Verified on a Debug server against a WSL Ubuntu 24.04 client and a
Windows client: exec_command returned real exit codes (0/1/2) with clean
output, -32007/-32008 negatives, and the POSIX-only ^ allowance;
persistent terminals kept cwd and environment, returned 1 for false and
the last exit code for ";" chains, and closed idempotently; kill_process
killed a test process, returned an empty remaining list and skipped
pid 1; remote_open reported 3840x1140, remote_mouse mapped (0.5,0.5) to
(1920,570), xev showed balanced press/release pairs for keys and buttons,
and a Chinese clipboard round-trip came back as UTF-8. The Windows
regression of the same tools was unchanged, including the multi-monitor
gate on remote_open.
Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
3a158a241c
commit
4d2abf34d0
4 files changed
+155
-64
No files matched your search
+29
-2
@@ -682,7 +682,7 @@ token 只从 `-Token` / env 读取,不落仓库、不打印;无在线主机
|
||||
| `-32005` | Not supported on this host | 能力限制,改参数也修不好:需要 Windows 的工具用在 LNX/MAC/APK 主机上;`remote_open` 遇到多显示器(阶段一);主机无屏幕预览/控制能力位 | 换主机或换工具;不要改参数重试 |
|
||||
| `-32006` | Policy refused | 只读模式 / 对应 `Mcp*` 开关关闭 / 主机不支持该能力(如 V2 文件传输) | **不要重试**:这是配置决定,需管理员改服务端配置 |
|
||||
| `-32007` | Not in command whitelist | `exec_command` 的命令不在 `McpCmdWhitelist` 里,或词边界不匹配(`dir` 过、`dirx` 拒) | **不要重试**:换成白名单内的命令,或请管理员加白名单项 |
|
||||
| `-32008` | Rejected by validation | 只覆盖「参数被校验拒绝、改参数可修」:`exec_command` 含 shell 元字符 `&\|<>^` 或控制字符(含 `\r\n`);`terminal_exec` 含 `&`/`\|`/换行;路径/参数含非法字符 | 按 `message` 修正参数后重试 |
|
||||
| `-32008` | Rejected by validation | 只覆盖「参数被校验拒绝、改参数可修」:`exec_command` 含 shell 元字符(Windows `&\|<>^`;Linux `&\|<>;` 与反引号、`$(` `${` `!`)或控制字符(含 `\r\n`);`terminal_exec` 含 `&`/`\|`/换行;路径/参数含非法字符 | 按 `message` 修正参数后重试 |
|
||||
| `-32009` | Target not found | 远端路径/注册表键不存在(win32 `2`/`3`,由新客户端主动回报);`kill_network_connection` 的四元组在当前连接表里查不到(`dry_run` 传假四元组最常见) | 先用 `list_files`/`list_registry`(或 `host_list_network_state`)确认目标存在 |
|
||||
| `-32010` | Access denied | 目标存在但权限不足(win32 `5`):非提权客户端的服务枚举(`OpenSCManager`)、受保护目录(如 `System Volume Information`)、受保护注册表键(如 `HKLM\SAM`) | **不要重试**:需客户端提权(管理员运行)或换目标 |
|
||||
| `-32011` | Destination write failed | 文件传输的**目标端**已收到数据但写盘失败:`copy_files_c2c` / `upload_file` 的目标主机回了回执且 `status != 0`(`message` 带 status、首个 errno、实收文件数) | **不要原样重试**:按文案判断(errno 13/1→权限、2→路径不存在、28→磁盘满),先修目标端目录/权限 |
|
||||
@@ -700,13 +700,20 @@ token 只从 `-Token` / env 读取,不落仓库、不打印;无在线主机
|
||||
|
||||
### 12.1 命令哨兵与输出清洗(测试报告 P1/P3/P4 的修复)
|
||||
|
||||
`exec_command` 与 `terminal_exec` 都靠哨兵判断「命令已执行完、退出码是多少」。哨兵有两种形态:
|
||||
`exec_command` 与 `terminal_exec` 都靠哨兵判断「命令已执行完、退出码是多少」。哨兵有三种形态:
|
||||
|
||||
| 客户端 | 下发内容 | 退出码 |
|
||||
|---|---|---|
|
||||
| POSIX PTY(Linux,`RES_CLIENT_TYPE=LNX`) | 第一行 `PS1=`;第二行 `<cmd>`;第三行 `echo __MCP_DONE_<nonce>__$?` | 命令的真实退出码 |
|
||||
| ConPTY(`is_pty=true`,新版 Windows 客户端) | 第一行 `@echo off & <cmd>`;第二行单独 `echo __MCP_DONE_<nonce>__%errorlevel%` | 命令的真实退出码 |
|
||||
| 老 ShellManager 管道(`is_pty=false`) | 单行 `<cmd> 2>&1 && echo __MCP_DONE_<nonce>__0 || echo __MCP_DONE_<nonce>__1` | 仅 0 / 非 0 |
|
||||
|
||||
POSIX 形态与 Windows 的差别:
|
||||
|
||||
- **不能用 `@echo off & <cmd>` / `%errorlevel%`**:Linux 的 `bash -i` 把它们当字面量,哨兵永远不会出现,必然超时。
|
||||
- **先发一行 `PS1=`**:交互式 bash 每读一行都打印提示符(`user@host:cwd$`),提示符会混进 stdout(Windows ConPTY 不会)。清空 `PS1` 后提示符消失。这一行的回显排在下发的命令行回显之前,会被「从缓冲区开头删到命令行回显结束」那一步一并切掉,不进入返回文本。
|
||||
- **`$?` 是上一行的退出码**:`PS1=` 与命令各占一行,哨兵只取命令那一行的状态,未被 `PS1=` 干扰。
|
||||
|
||||
**为什么 PTY 要分两行下发**:`rem hello`、`cmd /c exit 0`、`if`、`for` 这类「吞行」命令会把同一行里的 `&& echo 哨兵` 一起吞掉(`rem`/`cmd /c` 把行内剩余文本当作自己的参数),哨兵永不出现,于是必然超时并销毁会话。哨兵独立成行后由 shell 自己执行,不再受命令行影响。老客户端只能维持单行形态:`client/ShellManager.cpp` 按「写入字节数」跳过回显,多写一行会吃掉上一行的输出。
|
||||
|
||||
**为什么用 `%errorlevel%`**:ConPTY 会把输入整行回显进输出流(80 列会折行)。回显文本里是字面 `%errorlevel%` 而不是数字,而 `FindSentinel` 只认「标记 + 数字」,折行回显再也不可能被误判成哨兵(评审 P3:`ver` 曾因回显里的 `__1` 被判成 `exit_code=1`),同时还能拿到真实退出码(如 `nosuchcmd` 的 9009)。
|
||||
@@ -858,3 +865,23 @@ Debug 实例(`Bin\Yama_x64d.exe`,临时端口)实测:`list_all_hosts`
|
||||
- **空文件**:worker 对 0 字节文件只发完成包(无路径字段),POSIX 接收端无从建文件。服务端只对 POSIX 目标补一个长度 0 的末块,接收端据此建空文件;Windows 目标的空文件仍不落地(V2 协议既有边界,未改)。
|
||||
- 顺带修掉一个下载完成判定的边界:条目顺序由客户端决定,目录可能排在文件之后(如空子目录),`OnFileChunkV2` 改为按 `filesDone + directoryCount >= totalFiles` 判完成,不再出现「文件全部收完还干等超时」。
|
||||
- 协议细节、POSIX 路径规则、错误码新语义与实测(含 3 MB 二进制、中文名、空文件、空目录的 SHA-256 往返)见 [Mcp_FileTransfer_Design.md](./Mcp_FileTransfer_Design.md) §15。
|
||||
|
||||
## 17. 增补(2026-10-27:档 1 工具支持 Linux 客户端)
|
||||
|
||||
Linux 客户端(`LNX`)早已实现下面这些工具的**线上协议**,只是服务端按客户端类型一律拒绝。本次只放开服务端的判定,**不改 Linux 客户端、不加能力位**:
|
||||
|
||||
| 工具 | 线上协议(已存在) | 服务端改动 |
|
||||
|---|---|---|
|
||||
| `kill_process` | 主连接 `COMMAND_SYSTEM` → 子连接 `TOKEN_PSLIST` → `COMMAND_KILLPROCESS` + PID 数组 → 刷新列表(`linux/SystemManager.h`,`kill(SIGKILL)`,忽略 `pid<=1`) | 只拒绝 `MAC`/`APK` |
|
||||
| `exec_command` | `COMMAND_SHELL` → `TOKEN_TERMINAL_START` → PTY(`common/PTYHandler.h`,`bash -i`) | 只拒绝 `MAC`;POSIX 白名单 / 元字符集 / 哨兵(§12.1) |
|
||||
| `terminal_open` / `terminal_exec` / `terminal_close` | 同上,`CMD_TERMINAL_RESIZE` + `COMMAND_NEXT` 启动读线程 | 只拒绝 `MAC`;`posix` 透传到哨兵与编码(UTF-8) |
|
||||
| `remote_open` / `remote_close` / `remote_mouse` / `remote_keyboard` / `remote_clipboard` | `COMMAND_SCREEN_SPY` → `TOKEN_BITMAPINFO` → `COMMAND_SCREEN_CONTROL` + `MSG64_LNX`(`linux/ScreenHandler.h`,XTest 注入);剪贴板 `COMMAND_SCREEN_SET_CLIPBOARD` | 只拒绝 `MAC`;输入事件改为**一条一包**(见下) |
|
||||
|
||||
要点:
|
||||
|
||||
- **POSIX 默认白名单是独立的一份**(`kDefaultCmdWhitelistPosix`):同名的 POSIX 命令语义与 Windows 不同(`find` 能 `-exec`、`hostname` 能改名、`sort -o` 能写文件),因此只收「参数也基本只读」的命令。`env` **刻意不收**——`env <程序>` 等于执行任意命令,会让白名单形同虚设。管理员显式配置 `McpCmdWhitelist` 时两种平台共用配置(配置者自负其责)。
|
||||
- **元字符集按平台分开**:Linux 拒绝 `& | < > ;` 与反引号、`$(` `${` `!`(`!` 在交互式 bash 里触发历史展开);`^` 在 POSIX 里不是元字符,因此 `grep -v ^# /etc/hosts` 这类只读命令在 Linux 上可用,Windows 上仍按原规则拒绝。
|
||||
- **输入事件一条一包**:协议允许 `[COMMAND_SCREEN_CONTROL][MSG64*N]` 批量,Windows 客户端(`CScreenManager::ProcessCommand`)也支持批量;但 `linux/ScreenHandler.h` 只处理包内第一条,批量会让 `key_press` 只按下不抬起(X11 自动重复)、`click` 只按下、`type` 只打首字符。服务端因此按消息拆包(`SendScreenCtrlEvents`),对 Windows 完全等价(它本来就一条一条处理),新老 Linux 客户端也都不用升级。
|
||||
- **`remote_keyboard` 的 `type` 仍是 ASCII-only**:Linux 侧 `VKtoKeySym` 把 VK 映射为 keysym 后走 XTest,IME 组合输入做不到;非 ASCII 走 `remote_clipboard` + `Ctrl+V`(Linux 客户端把服务端下发的 GBK 转回 UTF-8 再调 `xclip`/`xsel`,需要 X11 桌面)。
|
||||
- **限制**:纯 Wayland 会话无法抓屏/注入(要 X11 或 XWayland);多显示器仍按 §7.2 拒绝(Linux 客户端心跳里 `RES_RESOLUTION` 依赖 `xrandr`,没有 `xrandr` 的系统上报 `0:0*0`,按「无法判定」放行);`remote_*` 需要安装 `libX11` + `libXtst`,剪贴板需要 `xclip`/`xsel`。
|
||||
- **验收**(Debug 实例 + WSL Ubuntu 24.04 客户端,WSLg `DISPLAY=:0`):`exec_command` 真实退出码(`ls` 不存在路径 → 2)、`-32007`/`-32008` 负例、`grep -v ^#` 放行、输出无提示符噪声;持久终端 `cd`/环境变量保持、`false` → 1、`;` 串联取末条、`close` 幂等;`kill_process` 杀死自己起的 `sleep` 且 `remaining` 为空、`pid 1` 被跳过;`remote_open` 返回 3840x1140、`remote_mouse` 归一化 (0.5,0.5) 落在 (1920,570)、`xev` 观测到按键/按钮成对、剪贴板中文往返 UTF-8 一致。Windows 客户端同批工具回归无变化。
|
||||
@@ -1,7 +1,7 @@
|
||||
# MCP 远程控制(Remote Control)设计文档
|
||||
|
||||
> 版本:v1(设计基线) 状态:待开发 关联:`docs/Mcp_Design.md`、`docs/Mcp_Terminal_Design.md`
|
||||
> 定位:本文件是「AI 远程控制一台 Windows 客户端(像人一样点鼠标、敲键盘、看画面完成任务)」功能的**唯一参照设计**,后续开发一律以本文为准,改需求先改本文。
|
||||
> 定位:本文件是「AI 远程控制一台 Windows 或 Linux 客户端(像人一样点鼠标、敲键盘、看画面完成任务)」功能的**唯一参照设计**,后续开发一律以本文为准,改需求先改本文。Linux 支持范围与限制见 §6.5、§12.1。
|
||||
|
||||
---
|
||||
|
||||
@@ -172,6 +172,7 @@ AI 无法「看懂」视频流。把 25fps 的远程画面直接丢给视觉模
|
||||
|
||||
- **包格式**:`[cmd:1][MSG64:48]`,可批量重复(`commands.h` `COMMAND_SCREEN_CONTROL=20`,`MSG64` 1581–1623)。
|
||||
- **MSG64 布局**:`{uint64 hwnd, message, wParam, lParam, time; POINT pt;}` = 48 字节(`_WIN64` 下 `MYMSG` 即 `MSG`;客户端亦接受 28 字节 `MSG32`,由 `ulLength % 28/48` 判定)。
|
||||
- **一条一包(重要)**:协议允许一包多消息,Windows 客户端也支持(`ProcessCommand` 按 `ulLength % 48 / % 28` 算出条数后逐条处理);但 Linux 客户端(`linux/ScreenHandler.h` `OnReceive`)**只处理包内第一条**。服务端因此统一按「一条 MSG64 一个 `COMMAND_SCREEN_CONTROL` 包」下发(`SendScreenCtrlEvents`):批量下发会让 Linux 上的 `key_press` 只按下不抬起(X11 自动重复)、`click` 只按下、`type` 只打出首字符。对 Windows 客户端完全等价。
|
||||
- **客户端解码**:`ScreenManager.cpp:1175` → `ProcessCommand(1727)` → `SendInput`:
|
||||
- 鼠标:坐标取 `LOWORD/HIWORD(lParam)`,`WM_MOUSEMOVE`→`MOUSEEVENTF_MOVE`,`WM_LBUTTONDOWN/UP`→`LEFTDOWN/LEFTUP`,`WM_LBUTTONDBLCLK`→额外 `LEFTDOWN`,`WM_MBUTTONDOWN/UP`,`WM_MOUSEWHEEL`→`MOUSEEVENTF_WHEEL`(`mouseData=GET_WHEEL_DELTA_WPARAM`)。
|
||||
- 键盘:`wVk=(WORD)wParam`,`wScan=(lParam>>16)&0xFF`,`KEYEVENTF_EXTENDEDKEY` 由 `(lParam>>24)&1`。
|
||||
@@ -205,6 +206,16 @@ AI 无法「看懂」视频流。把 25fps 的远程画面直接丢给视觉模
|
||||
- 随后 `remote_keyboard` 注入 `Ctrl+V` 粘贴、`Enter` 发送。
|
||||
- **编码注意(已核实)**:`UpdateClientClipboard` 用 `SetClipboardData(CF_TEXT,...)`——ANSI/GBK 格式(`CF_TEXT`,非 `CF_UNICODETEXT`)。MVP 下中文可经 `ToAnsi(utf8, 936)` 转 GBK 后直发(与终端命令同模式);但 emoji/非 GBK 字符会丢、部分新式聊天软件粘贴偏好 Unicode。更稳做法是复用 `COMMAND_C2C_TEXT`(90) 的 UTF-8→Unicode→`CF_UNICODETEXT` 逻辑(`KernelManager.cpp:1573-1587`,且已内置「设完剪贴板后自动模拟 Ctrl+V」),或新增一个 UTF-8 设剪贴板变体。见 §17 实验案例。
|
||||
|
||||
### 6.5 Linux 客户端(`RES_CLIENT_TYPE=LNX`)
|
||||
|
||||
服务端路径与 Windows 完全一致(`COMMAND_SCREEN_SPY` → `TOKEN_BITMAPINFO` → `COMMAND_SCREEN_CONTROL`),差异都在客户端(`linux/ScreenHandler.h`,本轮**未改客户端**):
|
||||
|
||||
- **注入**:走 XTest(`XTestFakeMotionEvent` / `XTestFakeButtonEvent` / `XTestFakeKeyEvent`),需要 `libXtst` 与可用的 X 连接(X11 或 XWayland)。
|
||||
- **按键映射**:`VKtoKeySym` 把 Windows VK 映射为 X11 keysym 后 `XKeysymToKeycode`。支持 A–Z、0–9、小键盘、F1–F12、方向键/编辑键、修饰键(`VK_SHIFT→Shift_L`、`VK_CONTROL→Control_L`、`VK_MENU→Alt_L`、`VK_LWIN→Super_L`)与 OEM 标点;**F13–F24 与 `VK_APPS` 无映射**(消息被忽略,不报错)。
|
||||
- **滚轮**:客户端收到 `WM_MOUSEWHEEL` 后按 120 的倍数换算成 Button4/Button5 的按放对,单条消息即可(服务端 `scroll` 只发一条)。
|
||||
- **剪贴板**:服务端按 GBK 下发(与 Windows 相同),Linux 客户端 `ClipboardHandler::SetTextRaw` 检测编码并用 iconv 转 UTF-8 后写 `xclip -selection clipboard`;需要 `xclip` 或 `xsel`。非 GBK 字符(emoji)在服务端就已变成 `?`,Linux 侧无法挽回。
|
||||
- **分辨率**:`TOKEN_BITMAPINFO` 的尺寸取自 root window,与服务端归一化坐标一致;多显示器判定依赖客户端心跳的 `RES_RESOLUTION`(`xrandr` 才能拿到显示器数),无 `xrandr` 时上报 `0:0*0`,服务端按「无法判定」放行。
|
||||
|
||||
---
|
||||
|
||||
## 7. 坐标约定与映射
|
||||
@@ -317,6 +328,7 @@ remote_open ──► 建立子连接 ──► [remote_mouse/remote_keyboard ×
|
||||
|
||||
- **单显示器**:Observe 抓主屏,坐标空间与虚拟桌面重合;多显示器 `remote_open` 显式报错(`-32005`,能力限制而非参数错误)。
|
||||
- 注入基于**绝对坐标** `SendInput`,要求客户端处于可注入的桌面会话(已登录、非锁屏/非 UAC 安全桌面);锁屏/UAC 提示场景注入无效(客户端 `SendInput` 限制),设计文档记录为已知限制。
|
||||
- **Linux 专属限制**(§6.5):纯 Wayland 会话不支持(需 X11/XWayland);需 `libX11`+`libXtst`,剪贴板需 `xclip`/`xsel`;F13–F24、`VK_APPS` 无 keysym 映射;非 ASCII 文本仍只能走剪贴板。
|
||||
- **代聊实验使能项**:窗口抓屏(`COMMAND_SCREEN_WINDOW`)、UTF-8 剪贴板、窗口聚焦随阶段一落地(§17)。
|
||||
|
||||
### 12.2 阶段二(后续)
|
||||
|
||||
@@ -28,6 +28,7 @@ SimpleRemoter 的 Linux 客户端,支持远程桌面、远程终端、文件
|
||||
| V2 文件传输 | `FileTransferV2.h` | `file_upload.h` | ✅ 服务端↔Linux(含 MCP `upload_file`/`download_file`:发送完成包带流式 SHA-256,路径不存在/无权限回 `TOKEN_DOWNLOAD_ERROR`) |
|
||||
| C2C 文件接收 | `FileTransferV2.h` | `file_upload.h` | ✅ Windows/MCP → Linux(目标目录由服务端 `dst_dir` 指定,落盘后回执) |
|
||||
| 配置持久化 | `LinuxConfig` | INI 文件 | ✅ 完整 |
|
||||
| MCP 远程管理 | 复用 `SystemManager.h` / `PTYHandler.h` / `ScreenHandler.h` | 服务端 MCP | ✅ 档 1:`kill_process`、`exec_command`、`terminal_open/exec/close`、`remote_open/close/mouse/keyboard/clipboard`(服务端放开门禁,客户端协议本已实现) |
|
||||
|
||||
### 未实现
|
||||
|
||||
|
||||
+112
-61
@@ -1093,7 +1093,7 @@ Json::Value BuildExecCommandInputSchema() {
|
||||
|
||||
Json::Value cmdProp(Json::objectValue);
|
||||
cmdProp["type"] = "string";
|
||||
cmdProp["description"] = u8"要执行的命令(受只读模式与命令白名单约束,仅允许只读命令前缀)";
|
||||
cmdProp["description"] = u8"要执行的命令(受只读模式与命令白名单约束,仅允许只读命令前缀;Windows 与 Linux 使用各自的内置默认白名单)";
|
||||
props["command"] = cmdProp;
|
||||
|
||||
Json::Value timeoutProp(Json::objectValue);
|
||||
@@ -1122,7 +1122,7 @@ Json::Value BuildExecCommandOutputSchema() {
|
||||
|
||||
Json::Value exitProp(Json::objectValue);
|
||||
exitProp["type"] = "integer";
|
||||
exitProp["description"] = u8"退出码:ConPTY 客户端为命令的真实退出码;老客户端(非 PTY)仅区分 0/非 0;-1=未知(进程异常退出)";
|
||||
exitProp["description"] = u8"退出码:PTY 客户端(Windows ConPTY / Linux PTY)为命令的真实退出码;老客户端(非 PTY)仅区分 0/非 0;-1=未知(进程异常退出)";
|
||||
props["exit_code"] = exitProp;
|
||||
|
||||
Json::Value schema(Json::objectValue);
|
||||
@@ -1663,12 +1663,12 @@ std::string BuildToolsListResult(const Json::Value& id) {
|
||||
tools.append(tool);
|
||||
}
|
||||
|
||||
// 13) exec_command(P3:一次性远程命令,仅 Windows,安全门:只读默认 + 白名单 + 审计)
|
||||
// 13) exec_command(P3:一次性远程命令,Windows/Linux,安全门:只读默认 + 白名单 + 审计)
|
||||
// 只读模式下 tools/list 直接隐藏该工具;即便被绕过,BuildExecCommand 也会再次校验只读。
|
||||
if (!CMcpServer::Instance().IsReadonly()) {
|
||||
Json::Value tool(Json::objectValue);
|
||||
tool["name"] = "exec_command";
|
||||
tool["description"] = u8"在指定在线 Windows 主机上执行一条命令并返回 stdout 与退出码(受只读模式与命令白名单约束,默认仅允许只读命令前缀,见 McpReadonly / McpCmdWhitelist)。";
|
||||
tool["description"] = u8"在指定在线 Windows 或 Linux 主机上执行一条命令并返回 stdout 与退出码(受只读模式与命令白名单约束,默认仅允许只读命令前缀,见 McpReadonly / McpCmdWhitelist;Linux 有独立的 POSIX 默认白名单,禁用 ; 反引号 $( ${ ! 等串联/子命令字符)。";
|
||||
|
||||
tool["inputSchema"] = BuildExecCommandInputSchema();
|
||||
tool["outputSchema"] = BuildExecCommandOutputSchema();
|
||||
@@ -1676,13 +1676,13 @@ std::string BuildToolsListResult(const Json::Value& id) {
|
||||
tools.append(tool);
|
||||
}
|
||||
|
||||
// 14) terminal_open / terminal_exec / terminal_close(P4:持久远程终端,仅 Windows,
|
||||
// 14) terminal_open / terminal_exec / terminal_close(P4:持久远程终端,Windows/Linux,
|
||||
// 安全门:McpTerminal=1 且 McpReadonly=0;无白名单;全命令审计 + idle 回收)
|
||||
if (CMcpServer::Instance().IsTerminalEnabled() && !CMcpServer::Instance().IsReadonly()) {
|
||||
{
|
||||
Json::Value tool(Json::objectValue);
|
||||
tool["name"] = "terminal_open";
|
||||
tool["description"] = u8"在指定在线 Windows 主机上打开一个持久 shell 会话并返回 session_id 与终端模式。后续 terminal_exec 复用该会话(cwd/环境变量跨命令保持),用毕须 terminal_close。";
|
||||
tool["description"] = u8"在指定在线 Windows 或 Linux 主机上打开一个持久 shell 会话并返回 session_id 与终端模式。后续 terminal_exec 复用该会话(cwd/环境变量跨命令保持),用毕须 terminal_close。Linux 使用 POSIX PTY(UTF-8,is_pty=true)。";
|
||||
tool["inputSchema"] = BuildTerminalOpenInputSchema();
|
||||
tool["outputSchema"] = BuildTerminalOpenOutputSchema();
|
||||
tools.append(tool);
|
||||
@@ -1690,7 +1690,7 @@ std::string BuildToolsListResult(const Json::Value& id) {
|
||||
{
|
||||
Json::Value tool(Json::objectValue);
|
||||
tool["name"] = "terminal_exec";
|
||||
tool["description"] = u8"在已打开的持久会话中执行一条命令并返回 stdout 与退出码。命令不受白名单约束,但不能含 & 或 |(会破坏输出哨兵捕获),请拆成多条调用;重定向 > < 与转义 ^ 允许。";
|
||||
tool["description"] = u8"在已打开的持久会话中执行一条命令并返回 stdout 与退出码。命令不受白名单约束,但不能含 & 或 |(会破坏输出哨兵捕获),请拆成多条调用;Windows 下重定向 > < 与转义 ^ 允许,Linux 下可用 ; 串联(退出码取最后一条)。";
|
||||
tool["inputSchema"] = BuildTerminalExecInputSchema();
|
||||
tool["outputSchema"] = BuildTerminalExecOutputSchema();
|
||||
tools.append(tool);
|
||||
@@ -1705,13 +1705,14 @@ std::string BuildToolsListResult(const Json::Value& id) {
|
||||
}
|
||||
}
|
||||
|
||||
// 15) remote_open / remote_close(P5:MCP 远程控制,仅 Windows,
|
||||
// 15) remote_open / remote_close / remote_keyboard / remote_mouse / remote_clipboard
|
||||
// (P5:MCP 远程控制,Windows/Linux;Linux 需 X11 桌面,Wayland 纯会话不支持)
|
||||
// 安全门:McpRemoteControl=1 且 McpReadonly=0;全程审计 + idle 回收)
|
||||
if (CMcpServer::Instance().IsRemoteControlEnabled() && !CMcpServer::Instance().IsReadonly()) {
|
||||
{
|
||||
Json::Value tool(Json::objectValue);
|
||||
tool["name"] = "remote_open";
|
||||
tool["description"] = u8"在指定在线 Windows 主机上建立远程控制会话(隐藏屏幕子连接)并返回 session_id 与物理屏幕分辨率(screen_w/screen_h)。后续 remote_mouse/remote_keyboard 用归一化坐标(0..1)注入输入,用毕须 remote_close。";
|
||||
tool["description"] = u8"在指定在线 Windows 或 Linux 主机上建立远程控制会话(隐藏屏幕子连接)并返回 session_id 与物理屏幕分辨率(screen_w/screen_h)。后续 remote_mouse/remote_keyboard 用归一化坐标(0..1)注入输入,用毕须 remote_close。Linux 需要 X11/Xorg(或 XWayland)桌面,纯 Wayland 会话无法抓屏/注入;多显示器暂不支持。";
|
||||
tool["inputSchema"] = BuildRemoteOpenInputSchema();
|
||||
tool["outputSchema"] = BuildRemoteOpenOutputSchema();
|
||||
tools.append(tool);
|
||||
@@ -1727,7 +1728,7 @@ std::string BuildToolsListResult(const Json::Value& id) {
|
||||
{
|
||||
Json::Value tool(Json::objectValue);
|
||||
tool["name"] = "remote_keyboard";
|
||||
tool["description"] = u8"向已建立的远程控制会话注入键盘事件。action 可选 key_down / key_up / key_press / type:前三种需 key(Windows 虚拟键名,如 ENTER/TAB/F5/LEFT/CTRL/ALT/SHIFT/WIN),type 需 text(仅 ASCII 文本;非 ASCII 请用 remote_clipboard + Ctrl+V)。modifiers 可选(CTRL/ALT/SHIFT/WIN)。";
|
||||
tool["description"] = u8"向已建立的远程控制会话注入键盘事件。action 可选 key_down / key_up / key_press / type:前三种需 key(虚拟键名,如 ENTER/TAB/F5/LEFT/CTRL/ALT/SHIFT/WIN,Linux 侧映射为 X11 keysym),type 需 text(仅 ASCII 文本;非 ASCII 请用 remote_clipboard + Ctrl+V)。modifiers 可选(CTRL/ALT/SHIFT/WIN)。";
|
||||
tool["inputSchema"] = BuildRemoteKeyboardInputSchema();
|
||||
tool["outputSchema"] = BuildRemoteKeyboardOutputSchema();
|
||||
tools.append(tool);
|
||||
@@ -1743,7 +1744,7 @@ std::string BuildToolsListResult(const Json::Value& id) {
|
||||
{
|
||||
Json::Value tool(Json::objectValue);
|
||||
tool["name"] = "remote_clipboard";
|
||||
tool["description"] = u8"把文本写入远程主机的剪贴板(UTF-8 → GBK/ANSI,非 GBK 字符如 emoji 会丢失)。仅设置剪贴板,粘贴需随后用 remote_keyboard 注入 Ctrl+V。";
|
||||
tool["description"] = u8"把文本写入远程主机的剪贴板(服务端按 GBK/ANSI 下发,Linux 客户端自动转回 UTF-8;非 GBK 字符如 emoji 会丢失)。仅设置剪贴板,粘贴需随后用 remote_keyboard 注入 Ctrl+V;Linux 需要 X11 桌面与 xclip/xsel。";
|
||||
tool["inputSchema"] = BuildRemoteClipboardInputSchema();
|
||||
tool["outputSchema"] = BuildRemoteClipboardOutputSchema();
|
||||
tools.append(tool);
|
||||
@@ -1785,12 +1786,12 @@ std::string BuildToolsListResult(const Json::Value& id) {
|
||||
}
|
||||
|
||||
// 18) kill_process / operate_window / control_service / power_action(P7:设备管理控制,
|
||||
// 仅 Windows,安全门:McpManage=1 且 McpReadonly=0)
|
||||
// 除 kill_process 外仅 Windows,安全门:McpManage=1 且 McpReadonly=0)
|
||||
if (CMcpServer::Instance().IsManageEnabled() && !CMcpServer::Instance().IsReadonly()) {
|
||||
{
|
||||
Json::Value tool(Json::objectValue);
|
||||
tool["name"] = "kill_process";
|
||||
tool["description"] = u8"终止指定在线 Windows 主机上的一个或多个进程(TerminateProcess 强制结束,不保存进程状态,可能造成数据丢失)。返回 remaining(杀完后仍在运行的 PID)。不可撤销,请谨慎确认 PID。";
|
||||
tool["description"] = u8"终止指定在线 Windows 或 Linux 主机上的一个或多个进程(Windows 用 TerminateProcess,Linux 用 SIGKILL 强制结束,不保存进程状态,可能造成数据丢失;Linux 会忽略 pid<=1)。返回 remaining(杀完后仍在运行的 PID)。不可撤销,请谨慎确认 PID。";
|
||||
tool["inputSchema"] = BuildKillProcessInputSchema();
|
||||
tool["outputSchema"] = BuildKillProcessOutputSchema();
|
||||
tools.append(tool);
|
||||
@@ -2637,6 +2638,30 @@ static const char* kDefaultCmdWhitelist =
|
||||
"ping,tracert,nslookup,getmac,driverquery,query,gpresult,"
|
||||
"arp -a,route print,schtasks /query";
|
||||
|
||||
// Linux 默认白名单:同一条命令名的 POSIX 语义与 Windows 不同(find/sort/date/hostname
|
||||
// 都能写文件或改系统状态),故单独维护,只放参数也基本只读的命令。注意 env 不在列:
|
||||
// `env rm -rf /` 会执行任意程序,等于白名单失效。管理员配置了 McpCmdWhitelist 时以
|
||||
// 配置为准(两种平台共用同一份显式配置)。
|
||||
static const char* kDefaultCmdWhitelistPosix =
|
||||
"ls,cat,head,tail,grep,egrep,fgrep,wc,file,stat,pwd,id,whoami,uname,uptime,"
|
||||
"df,du,free,ps,ss,netstat,lsblk,lsusb,lspci,"
|
||||
"ip addr show,ip route show,ip link show,ip -s,"
|
||||
"systemctl status,systemctl list-units,journalctl,dmesg,"
|
||||
"last,lastb,w,who,printenv,getent,which,whereis";
|
||||
|
||||
// POSIX shell 元字符:& | < > 与 ; 都能在同一条命令行里追加命令,反引号与 $( / ${ 能
|
||||
// 执行子命令,! 在交互式 bash 里会触发历史展开。全部拒绝,防止绕过白名单前缀。
|
||||
static bool ContainsForbiddenPosixShellChars(const std::string& s) {
|
||||
for (size_t i = 0; i < s.size(); ++i) {
|
||||
char c = s[i];
|
||||
if (c == '&' || c == '|' || c == '<' || c == '>' || c == ';' || c == '`' || c == '!')
|
||||
return true;
|
||||
if (c == '$' && i + 1 < s.size() && (s[i + 1] == '(' || s[i + 1] == '{'))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 去首尾空白(空格/制表符/回车/换行)。
|
||||
static std::string Trim(const std::string& s) {
|
||||
size_t b = s.find_first_not_of(" \t\r\n");
|
||||
@@ -2789,15 +2814,24 @@ static std::string StripEchoedInput(const std::string& s, bool isPty,
|
||||
return out;
|
||||
}
|
||||
|
||||
// 哨兵行有两种形态(见 docs/Mcp_Design.md §12.1):
|
||||
// 哨兵行有三种形态(见 docs/Mcp_Design.md §12.1):
|
||||
// - POSIX PTY(posix=true,Linux):命令与哨兵**分两行**下发,哨兵用 echo 内置命令 +
|
||||
// $? 取上一行的真实退出码。不能用 Windows 的 "@echo off & ..." / %errorlevel%:
|
||||
// POSIX shell 会把它们当成字面量,FindSentinel 永远匹配不到。
|
||||
// - ConPTY(is_pty=true):命令与哨兵**分两行**下发。哨兵独立成行执行,于是 rem / cmd / c
|
||||
// exit / if / for 这类「吞行」命令不会连哨兵一起吞掉(P1);哨兵用 %errorlevel% 取真实
|
||||
// 退出码,回显文本里是字面 "%errorlevel%"、不是数字,FindSentinel 只认数字,折行回显再也
|
||||
// 不会被误判成哨兵(P3),也能返回真实退出码而非 0/1。
|
||||
// - 老 ShellManager(is_pty=false):客户端按写入字节数跳过回显,多写一行会吃掉上一行的
|
||||
// 输出,故仍用单行 && / || 形式(老客户端维持既有行为,已知不支持 rem / cmd /c)。
|
||||
static void BuildSentinelLines(bool isPty, const std::string& command, const std::string& nonce,
|
||||
static void BuildSentinelLines(bool isPty, bool posix, const std::string& command,
|
||||
const std::string& nonce,
|
||||
std::string& cmdLine, std::string& sentLine) {
|
||||
if (posix) {
|
||||
cmdLine = command;
|
||||
sentLine = "echo __MCP_DONE_" + nonce + "__$?";
|
||||
return;
|
||||
}
|
||||
if (!isPty) {
|
||||
cmdLine = command + " 2>&1 && echo __MCP_DONE_" + nonce + "__0 || echo __MCP_DONE_" + nonce + "__1";
|
||||
sentLine.clear();
|
||||
@@ -2808,11 +2842,16 @@ static void BuildSentinelLines(bool isPty, const std::string& command, const std
|
||||
}
|
||||
|
||||
// 组包下发;cmdLine/sentLine 回传给调用方用于剔除输入回显。sentLine 为空表示单行形态。
|
||||
static void SendSentinelLines(context* subCtx, bool isPty, const std::string& command,
|
||||
static void SendSentinelLines(context* subCtx, bool isPty, bool posix, const std::string& command,
|
||||
const std::string& nonce, std::string& cmdLine, std::string& sentLine) {
|
||||
BuildSentinelLines(isPty, command, nonce, cmdLine, sentLine);
|
||||
UINT cp = isPty ? CP_UTF8 : 936;
|
||||
std::string wire = ToAnsi(cmdLine, cp) + "\r\n";
|
||||
BuildSentinelLines(isPty, posix, command, nonce, cmdLine, sentLine);
|
||||
UINT cp = (posix || isPty) ? CP_UTF8 : 936;
|
||||
// Linux 的交互式 bash 每读一行都打印 PS1,提示符会混进 stdout(Windows ConPTY 不会),
|
||||
// 故先清空 PS1 再执行。这一行的回显在命令行回显之前,会被 CutThroughEchoedLine 连同
|
||||
// 启动画面一起切掉,不进入返回文本。
|
||||
std::string wire;
|
||||
if (posix) wire = "PS1=\r\n";
|
||||
wire += ToAnsi(cmdLine, cp) + "\r\n";
|
||||
if (!sentLine.empty()) wire += ToAnsi(sentLine, cp) + "\r\n";
|
||||
subCtx->Send2Client((BYTE*)wire.data(), (ULONG)wire.size());
|
||||
}
|
||||
@@ -2825,7 +2864,7 @@ static bool ContainsControlChar(const std::string& s) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// tools/call:exec_command(Windows 一次性远程命令:主连接 COMMAND_SHELL → 子连接终端 →
|
||||
// tools/call:exec_command(Windows/Linux 一次性远程命令:主连接 COMMAND_SHELL → 子连接终端 →
|
||||
// 哨兵命令行 → 收集 stdout + exit_code → 关子链接)。安全门:只读默认 + 白名单 + 审计。
|
||||
std::string BuildExecCommand(const Json::Value& id, const Json::Value& args, CMy2015RemoteDlg* parent) {
|
||||
uint64_t devId = 0;
|
||||
@@ -2837,10 +2876,11 @@ std::string BuildExecCommand(const Json::Value& id, const Json::Value& args, CMy
|
||||
if (!ctx)
|
||||
return BuildError(id, -32002, "Host not found or offline: " + std::to_string(devId));
|
||||
|
||||
// 终端仅 Windows 客户端实现(ConPTY / 老 cmd 管道);LNX/MAC 远期再考虑。
|
||||
// Linux 客户端同样走 PTY + POSIX 哨兵;macOS 本轮不开放。
|
||||
CString clientType = ctx->GetAdditionalData(RES_CLIENT_TYPE);
|
||||
if (clientType == "LNX" || clientType == "MAC")
|
||||
return BuildError(id, -32005, "exec_command is only supported on Windows hosts");
|
||||
const bool posix = (clientType == "LNX");
|
||||
if (clientType == "MAC")
|
||||
return BuildError(id, -32005, "exec_command is only supported on Windows and Linux hosts");
|
||||
|
||||
std::string command = Trim(GetStringArg(args, "command"));
|
||||
if (command.empty())
|
||||
@@ -2853,12 +2893,16 @@ std::string BuildExecCommand(const Json::Value& id, const Json::Value& args, CMy
|
||||
return BuildError(id, -32006, "exec_command is disabled: MCP is in read-only mode (set McpReadonly=0 to enable)");
|
||||
|
||||
// 安全门 2:拒绝 shell 元字符与控制字符(\r\n 会把命令拆成多行绕过白名单),防注入。
|
||||
if (command.find_first_of("&|<>^") != std::string::npos || ContainsControlChar(command))
|
||||
// POSIX 下还要挡 ; 反引号 $( ${ !;^ 在 POSIX 里不是元字符,不能沿用 Windows 的字符集
|
||||
// (否则 grep -v '^#' 这类只读命令会被误拒)。
|
||||
const bool badMeta = posix ? ContainsForbiddenPosixShellChars(command)
|
||||
: (command.find_first_of("&|<>^") != std::string::npos);
|
||||
if (badMeta || ContainsControlChar(command))
|
||||
return BuildError(id, -32008, "Command contains forbidden shell characters: " + command);
|
||||
|
||||
// 安全门 3:命令白名单前缀校验。
|
||||
// 安全门 3:命令白名单前缀校验(显式配置优先;否则按平台用内置默认表)。
|
||||
std::string whitelist = mcp.GetCmdWhitelist();
|
||||
if (whitelist.empty()) whitelist = kDefaultCmdWhitelist;
|
||||
if (whitelist.empty()) whitelist = posix ? kDefaultCmdWhitelistPosix : kDefaultCmdWhitelist;
|
||||
if (!IsCommandAllowed(command, whitelist))
|
||||
return BuildError(id, -32007, "Command not allowed by whitelist: " + command);
|
||||
|
||||
@@ -2883,12 +2927,12 @@ std::string BuildExecCommand(const Json::Value& id, const Json::Value& args, CMy
|
||||
if (!mcp.WaitTerminalReady(devId, subCtx, isPty, timeoutMs))
|
||||
return BuildError(id, -32001, "Timeout waiting for shell to start");
|
||||
|
||||
// 编码:ConPTY(UTF-8) / 老 cmd 管道(GBK)。哨兵为纯 ASCII,两种编码下字节一致。
|
||||
UINT cp = isPty ? CP_UTF8 : 936;
|
||||
// 哨兵形态见 BuildSentinelLines:PTY 下命令与哨兵分两行下发(吞行命令不再超时),
|
||||
// 老 ShellManager 保持单行 && / || 形式(其客户端按写入长度跳回显)。
|
||||
// 编码:PTY(UTF-8) / 老 cmd 管道(GBK)。哨兵为纯 ASCII,两种编码下字节一致。
|
||||
UINT cp = (posix || isPty) ? CP_UTF8 : 936;
|
||||
// 哨兵形态见 BuildSentinelLines:POSIX PTY / ConPTY 下命令与哨兵分两行下发(吞行命令
|
||||
// 不再超时),老 ShellManager 保持单行 && / || 形式(其客户端按写入长度跳回显)。
|
||||
std::string cmdLine, sentLine;
|
||||
SendSentinelLines(subCtx, isPty, command, nonce, cmdLine, sentLine);
|
||||
SendSentinelLines(subCtx, isPty, posix, command, nonce, cmdLine, sentLine);
|
||||
|
||||
std::vector<BYTE> raw;
|
||||
int exitCode = -1;
|
||||
@@ -3158,7 +3202,7 @@ std::string BuildGetServerStatus(const Json::Value& id, CMy2015RemoteDlg* parent
|
||||
static const int kTermIdleTimeoutSec = 300; // 持久终端 idle 回收超时(秒)
|
||||
static const int kTermTombstoneSec = 600; // 「终端被销毁」墓碑保留时长(秒),供后续报错文案用
|
||||
|
||||
// 共享前置:校验 McpTerminal && !readonly → 解析 id → FindMainContext → 拒绝 LNX/MAC。
|
||||
// 共享前置:校验 McpTerminal && !readonly → 解析 id → FindMainContext → 拒绝 macOS。
|
||||
// 成功返回 true 并输出 devId/ctx;失败时 errJson 已写入对应 JSON 错误串。
|
||||
static bool ResolveTerminalSessionHost(const Json::Value& id, const Json::Value& args,
|
||||
CMy2015RemoteDlg* parent,
|
||||
@@ -3183,19 +3227,19 @@ static bool ResolveTerminalSessionHost(const Json::Value& id, const Json::Value&
|
||||
}
|
||||
|
||||
CString clientType = ctx->GetAdditionalData(RES_CLIENT_TYPE);
|
||||
if (clientType == "LNX" || clientType == "MAC") {
|
||||
errJson = BuildError(id, -32005, "Persistent terminal is only supported on Windows hosts");
|
||||
if (clientType == "MAC") {
|
||||
errJson = BuildError(id, -32005, "Persistent terminal is only supported on Windows and Linux hosts");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// 发送哨兵命令行(复用 exec_command 的哨兵逻辑);回传所发送的两行,
|
||||
// 供调用方剔除 ConPTY 把输入整行回显造成的噪声。cp 由 isPty 决定。
|
||||
static void SendTerminalCommandLine(context* subCtx, bool isPty, const std::string& command,
|
||||
const std::string& nonce,
|
||||
// 供调用方剔除 PTY 把输入整行回显造成的噪声。cp 由平台/isPty 决定。
|
||||
static void SendTerminalCommandLine(context* subCtx, bool isPty, bool posix,
|
||||
const std::string& command, const std::string& nonce,
|
||||
std::string& cmdLine, std::string& sentLine) {
|
||||
SendSentinelLines(subCtx, isPty, command, nonce, cmdLine, sentLine);
|
||||
SendSentinelLines(subCtx, isPty, posix, command, nonce, cmdLine, sentLine);
|
||||
}
|
||||
|
||||
// 输出清洗(复用 exec_command 尾部逻辑):raw → UTF-8 → 剥 ANSI → CRLF 归一 →
|
||||
@@ -3244,7 +3288,7 @@ Json::Value BuildTerminalOpenOutputSchema() {
|
||||
props["session_id"] = sid;
|
||||
Json::Value pty(Json::objectValue);
|
||||
pty["type"] = "boolean";
|
||||
pty["description"] = u8"true=ConPTY(UTF-8);false=老 cmd 管道(GBK)";
|
||||
pty["description"] = u8"true=PTY(UTF-8)(Windows ConPTY / Linux PTY);false=老 cmd 管道(GBK)";
|
||||
props["is_pty"] = pty;
|
||||
Json::Value schema(Json::objectValue);
|
||||
schema["type"] = "object";
|
||||
@@ -3268,7 +3312,7 @@ Json::Value BuildTerminalExecInputSchema() {
|
||||
props["session_id"] = sid;
|
||||
Json::Value cmdProp(Json::objectValue);
|
||||
cmdProp["type"] = "string";
|
||||
cmdProp["description"] = u8"要执行的命令(完整 shell,不受白名单约束;但不能含 & 或 |,请拆成多条调用)";
|
||||
cmdProp["description"] = u8"要执行的命令(完整 shell,不受白名单约束;但不能含 & 或 |,请拆成多条调用)。Linux 下通过 POSIX shell 执行,可用 ; 串联且退出码取最后一条命令";
|
||||
props["command"] = cmdProp;
|
||||
Json::Value timeoutProp(Json::objectValue);
|
||||
timeoutProp["type"] = "integer";
|
||||
@@ -3293,7 +3337,7 @@ Json::Value BuildTerminalExecOutputSchema() {
|
||||
props["stdout"] = stdoutProp;
|
||||
Json::Value exitProp(Json::objectValue);
|
||||
exitProp["type"] = "integer";
|
||||
exitProp["description"] = u8"退出码:ConPTY 客户端为命令的真实退出码;老客户端(非 PTY)仅区分 0/非 0;-1=未知(进程异常退出)";
|
||||
exitProp["description"] = u8"退出码:PTY 客户端(Windows ConPTY / Linux PTY)为命令的真实退出码;老客户端(非 PTY)仅区分 0/非 0;-1=未知(进程异常退出)";
|
||||
props["exit_code"] = exitProp;
|
||||
Json::Value schema(Json::objectValue);
|
||||
schema["type"] = "object";
|
||||
@@ -3405,6 +3449,7 @@ std::string BuildTerminalExec(const Json::Value& id, const Json::Value& args, CM
|
||||
std::string errJson;
|
||||
if (!ResolveTerminalSessionHost(id, args, parent, devId, ctx, errJson))
|
||||
return errJson;
|
||||
const bool posix = (ctx->GetAdditionalData(RES_CLIENT_TYPE) == "LNX");
|
||||
|
||||
std::string sessionId = GetStringArg(args, "session_id");
|
||||
if (sessionId.empty())
|
||||
@@ -3446,7 +3491,7 @@ std::string BuildTerminalExec(const Json::Value& id, const Json::Value& args, CM
|
||||
}
|
||||
|
||||
std::string cmdLine, sentLine;
|
||||
SendTerminalCommandLine(subCtx, isPty, command, nonce, cmdLine, sentLine);
|
||||
SendTerminalCommandLine(subCtx, isPty, posix, command, nonce, cmdLine, sentLine);
|
||||
|
||||
std::vector<BYTE> raw;
|
||||
int exitCode = -1;
|
||||
@@ -3460,8 +3505,8 @@ std::string BuildTerminalExec(const Json::Value& id, const Json::Value& args, CM
|
||||
subCtx->CancelIO();
|
||||
}
|
||||
|
||||
std::string stdoutStr = CleanTerminalOutput(raw, isPty ? CP_UTF8 : 936, isPty, cmdLine, sentLine,
|
||||
nonce);
|
||||
std::string stdoutStr = CleanTerminalOutput(raw, (posix || isPty) ? CP_UTF8 : 936, isPty,
|
||||
cmdLine, sentLine, nonce);
|
||||
|
||||
// 审计:命令执行(不可关闭)。
|
||||
if (parent) {
|
||||
@@ -3542,7 +3587,7 @@ std::string BuildTerminalClose(const Json::Value& id, const Json::Value& args, C
|
||||
|
||||
static const int kScreenCtrlIdleTimeoutSec = 300; // 远程控制 idle 回收超时(秒)
|
||||
|
||||
// 共享前置:校验 McpRemoteControl && !readonly → 解析 id → FindMainContext → 拒绝 LNX/MAC。
|
||||
// 共享前置:校验 McpRemoteControl && !readonly → 解析 id → FindMainContext → 拒绝 macOS。
|
||||
// 成功返回 true 并输出 devId/ctx;失败时 errJson 已写入对应 JSON 错误串。
|
||||
static bool ResolveScreenCtrlSessionHost(const Json::Value& id, const Json::Value& args,
|
||||
CMy2015RemoteDlg* parent,
|
||||
@@ -3567,8 +3612,8 @@ static bool ResolveScreenCtrlSessionHost(const Json::Value& id, const Json::Valu
|
||||
}
|
||||
|
||||
CString clientType = ctx->GetAdditionalData(RES_CLIENT_TYPE);
|
||||
if (clientType == "LNX" || clientType == "MAC") {
|
||||
errJson = BuildError(id, -32005, "Remote control is only supported on Windows hosts");
|
||||
if (clientType == "MAC") {
|
||||
errJson = BuildError(id, -32005, "Remote control is only supported on Windows and Linux hosts");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -3969,6 +4014,22 @@ Json::Value BuildRemoteKeyboardOutputSchema() {
|
||||
return schema;
|
||||
}
|
||||
|
||||
// 下发一批输入事件:每条 MSG64 单独一包([COMMAND_SCREEN_CONTROL][MSG64])。
|
||||
// 协议上本可一包多条,但 Linux 客户端(ScreenHandler::OnReceive)只处理包内第一条,
|
||||
// 多消息批量会让 key_press 只剩按下(触发键盘自动重复)、click 只剩按下、type 只打出
|
||||
// 首字符。按消息拆包对 Windows 客户端完全等价(它本来就一次处理一条),且新老客户端
|
||||
// 行为一致,故不改客户端。任一条发送失败即返回 false。
|
||||
static bool SendScreenCtrlEvents(context* subCtx, const std::vector<MSG64>& batch) {
|
||||
std::vector<BYTE> packet(1 + sizeof(MSG64));
|
||||
packet[0] = COMMAND_SCREEN_CONTROL;
|
||||
for (size_t i = 0; i < batch.size(); ++i) {
|
||||
memcpy(packet.data() + 1, &batch[i], sizeof(MSG64));
|
||||
if (!subCtx->Send2Client(packet.data(), (ULONG)packet.size()))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// tools/call:remote_keyboard(键盘注入;成功返回空对象)
|
||||
std::string BuildRemoteKeyboard(const Json::Value& id, const Json::Value& args, CMy2015RemoteDlg* parent) {
|
||||
CMcpServer& mcp = CMcpServer::Instance();
|
||||
@@ -4038,12 +4099,7 @@ std::string BuildRemoteKeyboard(const Json::Value& id, const Json::Value& args,
|
||||
if (r == 2) return BuildError(id, -32003, "Device busy: another injection is in flight for this session");
|
||||
if (r == 1) return BuildError(id, -32002, "Remote control session not found, not ready, or closed: " + sessionId);
|
||||
|
||||
// 组包 [COMMAND_SCREEN_CONTROL][MSG64*N],经屏幕子连接发送(主连接无效,设计 P2)。
|
||||
const int len = (int)(1 + batch.size() * sizeof(MSG64));
|
||||
std::vector<BYTE> packet((size_t)len);
|
||||
packet[0] = COMMAND_SCREEN_CONTROL;
|
||||
memcpy(packet.data() + 1, batch.data(), batch.size() * sizeof(MSG64));
|
||||
bool ok = subCtx->Send2Client(packet.data(), (ULONG)len) != FALSE;
|
||||
bool ok = SendScreenCtrlEvents(subCtx, batch);
|
||||
|
||||
mcp.EndScreenCtrlAction(devId, sessionId);
|
||||
|
||||
@@ -4298,12 +4354,7 @@ std::string BuildRemoteMouse(const Json::Value& id, const Json::Value& args, CMy
|
||||
batch.push_back(BuildMouseMsg64(px, py, WM_MOUSEWHEEL, MAKEWPARAM(0, wheelDelta)));
|
||||
}
|
||||
|
||||
// 组包 [COMMAND_SCREEN_CONTROL][MSG64*N],经屏幕子连接发送(主连接无效,设计 P2)。
|
||||
const int len = (int)(1 + batch.size() * sizeof(MSG64));
|
||||
std::vector<BYTE> packet((size_t)len);
|
||||
packet[0] = COMMAND_SCREEN_CONTROL;
|
||||
memcpy(packet.data() + 1, batch.data(), batch.size() * sizeof(MSG64));
|
||||
bool ok = subCtx->Send2Client(packet.data(), (ULONG)len) != FALSE;
|
||||
bool ok = SendScreenCtrlEvents(subCtx, batch);
|
||||
|
||||
mcp.EndScreenCtrlAction(devId, sessionId);
|
||||
|
||||
@@ -5499,8 +5550,8 @@ std::string BuildKillProcess(const Json::Value& id, const Json::Value& args, CMy
|
||||
return BuildError(id, -32002, "Host not found or offline: " + std::to_string(devId));
|
||||
|
||||
CString clientType = ctx->GetAdditionalData(RES_CLIENT_TYPE);
|
||||
if (clientType == "LNX" || clientType == "MAC" || clientType == "APK")
|
||||
return BuildError(id, -32005, "kill_process is only supported on Windows hosts");
|
||||
if (clientType == "MAC" || clientType == "APK")
|
||||
return BuildError(id, -32005, "kill_process is only supported on Windows and Linux hosts");
|
||||
|
||||
CMcpServer& mcp = CMcpServer::Instance();
|
||||
if (mcp.IsReadonly())
|
||||
|
||||
Reference in new issue
Block a user