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:
1 parent
915c587964
commit
445bd8bbb9
9 files changed
+205
-32
No files matched your search
@@ -26,6 +26,7 @@
|
||||
#include "client/IOCPClient.h"
|
||||
#include "ScreenHandler.h"
|
||||
#include "ChatHandler.h"
|
||||
#include "common/PTYHandler.h" // 远程终端(COMMAND_SHELL):与 Linux/macOS 共用 PTY 实现
|
||||
#define XXH_INLINE_ALL
|
||||
#include "common/xxhash.h"
|
||||
#include "common/logger.h"
|
||||
@@ -781,6 +782,49 @@ static void ChatThread()
|
||||
LOGI("ChatThread exit");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ 远程终端
|
||||
// 服务端 COMMAND_SHELL → 子连接 → 首包 TOKEN_TERMINAL_START → 服务端接管后发
|
||||
// CMD_TERMINAL_RESIZE + COMMAND_NEXT → PTY 读线程开始回传 shell 输出。
|
||||
// 与 ChatThread 同一套子连接要求(ConnAuth / 传输协议),但不依赖任何 Android 权限。
|
||||
static std::atomic<bool> g_termInFlight{false};
|
||||
|
||||
static void TerminalThread()
|
||||
{
|
||||
// detached 线程:异常逃出去会 std::terminate;在途标志必须无条件还回去
|
||||
struct InFlightGuard {
|
||||
~InFlightGuard() { g_termInFlight.store(false); }
|
||||
} guard;
|
||||
|
||||
LOGI("TerminalThread start → %s:%d", g_serverIp.c_str(), g_serverPort);
|
||||
try {
|
||||
std::unique_ptr<IOCPClient> client(new IOCPClient(g_bExit, true));
|
||||
client->EnableSubConnAuth(true, g_myClientID);
|
||||
// 与屏幕/日志/交谈子连接一致:继承配置的传输方式,WS/WSS 部署下直连会连不上反代
|
||||
client->SetDefaultProto((int)(unsigned char)g_SETTINGS.protoType);
|
||||
if (!client->ConnectServer(g_serverIp.c_str(), g_serverPort)) {
|
||||
LOGE("TerminalThread: connect failed");
|
||||
return;
|
||||
}
|
||||
// 构造即 openpty + fork shell;失败会抛异常,服务端按等待超时处理
|
||||
std::unique_ptr<PTYHandler> handler(new PTYHandler(client.get()));
|
||||
client->setManagerCallBack(handler.get(),
|
||||
IOCPManager::DataProcess,
|
||||
IOCPManager::ReconnectProcess);
|
||||
// 首包告诉服务端这是 PTY 会话(决定 UTF-8 编码与 POSIX 哨兵形态)。
|
||||
// PTY 读线程要等服务端的 COMMAND_NEXT 才开始回传,初始提示符不会混进命令输出。
|
||||
BYTE bToken = TOKEN_TERMINAL_START;
|
||||
client->Send2Server((char*)&bToken, 1);
|
||||
while (client->IsRunning() && client->IsConnected() && S_CLIENT_NORMAL == g_bExit)
|
||||
Sleep(200);
|
||||
// 先摘回调再让 handler 析构(析构会停 PTY 读线程并结束 shell)
|
||||
client->setManagerCallBack(nullptr, nullptr, nullptr);
|
||||
LOGI("TerminalThread: sub-conn closed");
|
||||
} catch (const std::exception& e) {
|
||||
LOGE("TerminalThread exception: %s", e.what());
|
||||
}
|
||||
LOGI("TerminalThread exit");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ 历史活动上报
|
||||
// 主连接一次性快照(与 Windows/Linux 一致,服务端 TOKEN_REPORT_ACTIVITY 分支不 CancelIO)。
|
||||
// 采样机每秒问一次 ControlService 的前台应用;这里只取 Dump(),几十微秒的活。
|
||||
@@ -1259,9 +1303,23 @@ int DataProcess(void* /*user*/, PBYTE szBuffer, ULONG ulLength)
|
||||
break;
|
||||
}
|
||||
|
||||
case COMMAND_SHELL:
|
||||
LOGI("COMMAND_SHELL (not implemented)");
|
||||
case COMMAND_SHELL: {
|
||||
// 远程终端(MCP exec_command / terminal_open,或主控菜单「终端」):
|
||||
// 子连接 + PTY,没有任何 Android 权限依赖。同一时刻只允许一个会话:
|
||||
// 服务端本身按主机去重,这里挡住用户连点造成的重复子连接。
|
||||
if (g_termInFlight.exchange(true)) {
|
||||
LOGI("cmd: COMMAND_SHELL ignored (terminal already in flight)");
|
||||
break;
|
||||
}
|
||||
LOGI("cmd: COMMAND_SHELL → PTY sub-conn");
|
||||
try {
|
||||
std::thread(TerminalThread).detach();
|
||||
} catch (const std::exception& e) {
|
||||
g_termInFlight.store(false);
|
||||
LOGE("Terminal thread failed: %s", e.what());
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case COMMAND_UPDATE:
|
||||
LOGI("cmd: COMMAND_UPDATE len=%lu", (unsigned long)ulLength);
|
||||
|
||||
Reference in new issue
Block a user