Feature: Support the remote terminal on Android hosts

The terminal tools were Windows/Linux only because the Android client had
no COMMAND_SHELL handler: the server opened a shell sub-connection and
the command was silently dropped, leaving exec_command to time out. This
adds the PTY path on the client and the matching server gating.

Client: COMMAND_SHELL on the main connection spawns a terminal thread
that opens an authenticated sub-connection, sends TOKEN_TERMINAL_START
and keeps the PTY alive until the server closes it. common/PTYHandler.h
is reused, with a minSdk-21 openpty built from posix_openpt/grantpt and
/system/bin/sh (mksh) as the shell. A new CLIENT_CAP_TERMINAL bit lets
the server reject old APKs up front with -32005.

Server: the tool masks, instructions and docs matrix accept APK. While
verifying, exec_command left a second echoed copy of the command line in
stdout: mksh forks with ECHO still on, so input written before its line
editor takes over is echoed by the line discipline in addition to the
shell's own redraw. The server now waits for the shell's first output
(the prompt) before sending the command and sentinel, which restores the
single-echo assumption the cleaning step relies on. terminal_exec on a
ready session is unaffected.

Verified on the Android 15 emulator: exec_command returns clean stdout
and real exit codes for id/uname/df/ls (including a failing command),
and terminal_open keeps cwd across commands, pipes work and close is
clean. The committed ghost.apk is stale and must be rebuilt before
release; a rebuilt APK was used for this testing.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-11 21:58:54 +01:00
1 parent 915c587964
commit 445bd8bbb9
9 files changed
+205 -32

No files matched your search

+9 -6
View File
@@ -108,13 +108,15 @@ GATE = {
#
# 判定依据是**服务端实际放行**,不是客户端有没有实现。两者在 macOS 上会分叉:
# exec_command / terminal_* / kill_process 的客户端实现是有的,但服务端刻意拦着
# (McpServer.cpp:3652「macOS 本轮不开放」、6870),所以标 win/lnx。
# (McpServer.cpp:3652「macOS 本轮不开放」、6870),所以标 win/lnx(终端族另有 apk)。
# Android 同理:COMMAND_SHELL 只有声明 CLIENT_CAP_TERMINAL 的新版 APK 才处理,
# 老 APK 由工具内部按能力位返回 -32005。
#
# 分组依据(全部经代码核对):
# 全平台 —— 只读写服务端本地数据,与客户端平台无关
# win/lnx/mac —— client/、linux/、macos/ 都有该命令的处理分支,且服务端不拦
# win/lnx —— 远程控制只实现于 Windows/Linux(McpServer.cpp:2728);
# 以及 macOS 客户端有实现但服务端未开放的 exec_command / terminal_* / kill_process
# 以及 macOS 客户端有实现但服务端未开放的 kill_process
# 仅 win —— 服务/注册表/键日志/活动历史/文件写/块输入/host_* /power_action
# (power_action 实际走 TOKEN_MACHINE_MANAGE,不是通用电源命令)
#
@@ -160,10 +162,11 @@ PLATFORMS = {
# macOS 客户端有 PTY 实现(macos/main.mm:753 起 ShellworkingThread),但服务端
# 本轮刻意不开放(McpServer.cpp:3652 注释「macOS 本轮不开放」),故实际不可用。
"terminal_open": _WL,
"terminal_exec": _WL,
"terminal_close": _WL,
"exec_command": _WL,
# Android 新版复用 common/PTYHandler.h,声明 CLIENT_CAP_TERMINAL 后服务端才放行。
"terminal_open": _WLA,
"terminal_exec": _WLA,
"terminal_close": _WLA,
"exec_command": _WLA,
# macOS 客户端有 COMMAND_KILLPROCESS 分支,但服务端同样拦着(McpServer.cpp:6870)
"kill_process": _WL,