Feature: Add window info RPC for foreground checks and window activation

Add a capability-gated main-connection RPC (COMMAND_MCP_WINDOW_INFO=179 /
TOKEN_MCP_WINDOW_INFO=180) that returns visible top-level windows with UTF-8
titles, screen rectangles, foreground flag and z-order, so MCP can verify
focus before injecting input. New tool get_foreground_window; host_list_windows
switches to the richer fields; host_control_window and operate_window gain
action=activate; remote_keyboard and remote_mouse gain expect_hwnd,
require_foreground and report_foreground. Old clients keep the previous
behavior: the RPC is refused up front via CLIENT_CAP_WINDOW_INFO.

The client caps a response at 96 windows (about 101 KB, under MAX_SEND_BUFFER).
Signal the cut-off in the mode byte (0x80) instead of silently dropping windows:
host_list_windows now reports truncated=true and says the list is incomplete,
and activate verification reports state=unknown with reason=list_truncated
rather than claiming the window is gone. expect_hwnd is checked only after the
session is validated, so an invalid session_id no longer burns a window RPC and
no longer reports a window error for a session that does not exist. Nullable
output fields (foreground, window) are declared as ["object","null"] so strict
clients do not reject responses.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-10 15:09:24 +01:00
1 parent c7e23f750d
commit b2c0d81396
18 files changed
+1298 -68

No files matched your search

+26 -6
View File
@@ -144,6 +144,16 @@ EndManagedSession(dev, cancelIo=true) // 关闭子连接,不留长
- 动作 10–30;多数动作客户端会自动回一份刷新后的列表(服务 = 40,任务 = 41),
进程冻结/解冻(16/17)与窗口操作(11)**不回包**。
窗口信息(新客户端,`CLIENT_CAP_WINDOW_INFO=0x0100`)另走**主连接** RPC,不建子连接、
不弹框:请求 `[COMMAND_MCP_WINDOW_INFO(179)][mode=0]`,客户端回
`[TOKEN_MCP_WINDOW_INFO(180)][mode][u16 LE count][WINDOWINFO_MCP × count]`。
`WINDOWINFO_MCP` 为 1056 字节 packed(标题 UTF-8、屏幕坐标、可见/最小化/最大化/前台标志、
z 序),最多 96 个窗口(按 z 序从上到下截取,超出的窗口不返回;被截断时响应把 `mode`
最高位 `0x80` 置 1,服务端据此在 `host_list_windows` 里返回 `truncated=true`)。主连接响应由 `2015RemoteDlg::MessageHandle` 的
`TOKEN_MCP_WINDOW_INFO` 分支交给 `TakeMainResponse`。
能力位未声明时,`host_list_windows` 自动回落到旧的 tab 1;`get_foreground_window`、
`activate` 与 `remote_keyboard/remote_mouse` 的 `expect_hwnd` 直接返回 `-32005`。
`MachineManager` 枚举(`common/commands.h`):请求 0 PROCESS / 1 WINDOWS / 2 NETSTATE /
3 SOFTWARE / 4 HTML / 5 FAVORITES / 6 WIN32SERVICE / 7 DRIVERSERVICE / 8 TASK / 9 HOSTS;
响应 31 PROCESS / 32 WINDOWS / 33 NETSTATE / 34 SOFTWARE / 35 HTML / 36 FAVORITES /
@@ -154,7 +164,7 @@ EndManagedSession(dev, cancelIo=true) // 关闭子连接,不留长
| 工具 | tab | 响应 | 主要过滤 |
|---|---|---|---|
| `host_list_processes` | 0 | 31 | `pid` / `name` / `arch` |
| `host_list_windows` | 1 | 32 | `pid` / `title` |
| `host_list_windows` | 1 | 32;新客户端为 180 | `pid` / `title` |
| `host_list_network_state` | 2 | 33 | `pid` / `process` / `protocol` / `local_port` / `remote_port` / `state` |
| `host_list_software` | 3 | 34 | `name` / `publisher` |
| `host_list_browser_history` | 4 | 35 | `title` / `url` |
@@ -173,6 +183,7 @@ EndManagedSession(dev, cancelIo=true) // 关闭子连接,不留长
|---|---|
| 进程 31 | `DWORD pid`、`bool is64`、`title[260]`、`exe`+pad、`path`+pad、`DWORD priClass`、`DWORD threads`、`user`+pad、`DWORD workingSetKb`、`DWORD fileSizeKb`;CP936 |
| 窗口 32 | `WINDOWSINFO` 定长 1044 字节;**只含可见窗口**;空列表 = 1 字节 |
| 窗口 180 | `[180][mode][u16 LE count][WINDOWINFO_MCP × count]`;1056 字节 packed,UTF-8 标题,含屏幕矩形、flags、z 序;空列表为 count=0 |
| 网络状态 33 | 进程名、`DWORD pid`、`[TCP]/[UDP]`、`ip:port`、远端、状态,各串 +2;UDP 远端 `*.*.*.*:*`、状态为空;按最后一个 `:` 拆地址 |
| 软件 34 | 5 个串各 +2:显示名、发布者、版本、安装日期、卸载命令 |
| 浏览记录 35 | 定长 2148:`time[100]` + `title[1024]` + `url[1024]` |
@@ -189,7 +200,7 @@ EndManagedSession(dev, cancelIo=true) // 关闭子连接,不留长
| 工具 | action | 额外门控 | 动作包 | 复核 |
|---|---|---|---|---|
| `host_control_process` | `freeze` / `thaw` | `McpMachineDangerous` | `[16/17][DWORD pid]`(无回包) | 重新取 tab 0:PID 仍在 |
| `host_control_window` | `restore/hide/maximize/minimize` | — | `[11][DWORD hwnd][DWORD SW_*]` + 零填充到 20 字节(客户端只读前 9 字节) | 重新取 tab 1:hide 后 hwnd 应消失,其余应仍在 |
| `host_control_window` | `restore/hide/maximize/minimize/activate` | — | `[11][DWORD hwnd][DWORD SW_*]` + 零填充到 20 字节;`activate` 用哨兵 `CMD_WINDOW_ACTIVATE_FLAG(0xFFFFFFFF)` 代替 `SW_*` | 重新取 tab 1:hide 后 hwnd 应消失,其余应仍在;`activate` 额外用窗口信息 RPC 复核 `GetForegroundWindow` |
| `host_control_service` | `start`/`pause`/`continue` | — | `[22/24/25][服务名 CP936][NUL]` | 客户端自动回 40 |
| `host_control_service` | `stop`/`restart` | `McpMachineDangerous` | stop = `[23]`;restart = stop → 等 40 → start | 等 40(12 s),状态在期望集合内 |
| `host_control_task` | `run` / `stop` | — | `[29/28][folder CP936][NUL][name CP936][NUL]` | 客户端自动回 41:任务仍在 + 当前状态 |
@@ -199,8 +210,13 @@ EndManagedSession(dev, cancelIo=true) // 关闭子连接,不留长
- 服务动作前先取一次服务列表(prime),既校验目标,也把客户端 `m_dwServiceType`
钉到正确类型(Win32 / 驱动)。
- 已是目标状态时不下发动作,返回 `ok=true, changed=false, reason=already_*`。
- 冻结/解冻、窗口动作、任务动作**没有回执**:`verified` 只表示「已用动作后的快照复核」,
不表示「动作一定生效」。冻结不改变进程存在性,因此只能确认目标仍在。
- 冻结/解冻、窗口动作、任务动作**没有来自客户端的操作回执**:`verified` 只表示「已用动作后的快照复核」,
不表示「动作一定生效」。冻结不改变进程存在性,因此只能确认目标仍在。`activate` 例外:
它在动作后另发一次窗口信息 RPC 复核 `GetForegroundWindow`,未成为前台时返回
`ok=false, reason=not_foreground`。
- 注入键鼠(`remote_keyboard` / `remote_mouse`)可带 `expect_hwnd`:注入前用同一 RPC 确认窗口
仍在可见列表里;`require_foreground=true` 还要求它已是前台,否则不发注入(`-32000`)。
未显式给 `report_foreground` 时默认在结果里回报注入后的前台窗口。
- 写工具输入串(服务名、任务 folder/name)必须能转成 CP936,否则 `-32602`,不发包。
- 审计格式:`host <id> <tool>: action=… target=… result=…`。
@@ -250,10 +266,14 @@ EndManagedSession(dev, cancelIo=true) // 关闭子连接,不留长
9. **`host_control_window {action:"hide"}` 是单向的**:客户端只枚举可见窗口,隐藏后该 hwnd
从快照中消失,再发 `restore` 会在发包前被 `-32009` 拦下(MFC 对话框同样无法再操作它)。
实测确认:`hide` 复核为 `state:"hidden"`,随后 `restore` 返回 `-32009`。
10. **`host_control_service {action:"pause"}` 依赖服务自身支持 PAUSE**:实测虚拟机上的
新协议的窗口信息同样只返回可见顶层窗口,这一限制不变。
10. **`activate` 不是强制的**:Windows 前台锁定策略(如用户正在操作、目标进程有模态框)可能
让 `SetForegroundWindow` 失败;工具会如实返回 `ok=false, reason=not_foreground`,可重试。
老客户端没有该实现,返回 `-32005`,应退回 `restore`。
11. **`host_control_service {action:"pause"}` 依赖服务自身支持 PAUSE**:实测虚拟机上的
`Spooler` 下发 PAUSE 后状态仍为 `Running`,工具如实返回 `ok:false, reason:"unexpected_state"`
(不谎报成功)。`start/stop/continue/restart` 在同一台机器上均通过。
11. **开关只在进程启动时读取**(Release 版配置来自注册表 `HKCU\Software\YAMA\settings`,
12. **开关只在进程启动时读取**(Release 版配置来自注册表 `HKCU\Software\YAMA\settings`,
不是 `Bin\settings.ini`);在「MCP设置」里改动 `McpMachine` / `McpMachineDangerous` /
`McpReadonly` 等必须重启服务端才生效。未重启时 `tools/list` 里看不到新工具,
直接调用返回 `-32006`。