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
+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