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

+6
View File
@@ -243,6 +243,11 @@ public:
// 成功 true:out=截断到哨兵前的原始字节、exitCode=0/1(进程退出无哨兵时 -1)、closed=是否进程退出。
bool WaitTerminalDone(uint64_t device_id, std::vector<BYTE>& out, int& exitCode, bool& closed, int timeoutMs);
// 工具线程:写命令前等 shell 启动完成(收到第一屏输出,通常是提示符)。
// 见 OnTerminalData 注释:Android 的 mksh 启动瞬间行规程仍开 ECHO,此时写入的
// 字节会被驱动额外回显一份,与服务端清洗逻辑(只剔一份回显)对不上。
void WaitTerminalBootstrap(uint64_t device_id, int timeoutMs);
// 工具线程:清理终端挂起(发送失败等提前退出路径)。
void ClearTermPending(uint64_t device_id);
@@ -816,6 +821,7 @@ private:
int exitCode = -1;
bool done = false; // 哨兵命中
bool closed = false; // TOKEN_TERMINAL_CLOSE(进程退出)
bool sawOutput = false; // 收到过任意 shell 输出(启动提示符到达 = shell 就绪)
// ===== 新增(持久终端)=====
std::string sessionId; // 持久会话 token(一次性 exec 为空)
bool persistent = false;