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
@@ -24,6 +24,11 @@
|
||||
// Platform-specific includes
|
||||
#ifdef __APPLE__
|
||||
#include <util.h> // macOS: openpty()
|
||||
#elif defined(__ANDROID__)
|
||||
// Android: bionic 的 <pty.h> 里 openpty() 是 API 23 才引入的,而客户端 minSdk 是 21。
|
||||
// 改用 API 21 就有的 posix_openpt/grantpt/unlockpt/ptsname_r 自己拼一个 openpty。
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#else
|
||||
#include <pty.h> // Linux: openpty()
|
||||
#endif
|
||||
@@ -44,6 +49,34 @@
|
||||
#include "commands.h"
|
||||
#include "../client/IOCPClient.h"
|
||||
|
||||
#ifdef __ANDROID__
|
||||
// openpty() 的 minSdk 21 兼容实现(API 21 起 posix_openpt/grantpt/unlockpt/ptsname_r 可用)。
|
||||
// 与 bionic API 23 的实现等价:开一对 master/slave,可选设置初始窗口大小。
|
||||
inline int android_openpty(int* master, int* slave, const struct winsize* ws)
|
||||
{
|
||||
int m = posix_openpt(O_RDWR | O_NOCTTY);
|
||||
if (m < 0) return -1;
|
||||
char name[128];
|
||||
if (grantpt(m) != 0 || unlockpt(m) != 0 || ptsname_r(m, name, sizeof(name)) != 0) {
|
||||
close(m);
|
||||
return -1;
|
||||
}
|
||||
int s = open(name, O_RDWR | O_NOCTTY);
|
||||
if (s < 0) {
|
||||
close(m);
|
||||
return -1;
|
||||
}
|
||||
if (ws && ioctl(m, TIOCSWINSZ, ws) != 0) {
|
||||
close(m);
|
||||
close(s);
|
||||
return -1;
|
||||
}
|
||||
*master = m;
|
||||
*slave = s;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* PTYHandler - Pseudo Terminal Handler
|
||||
*
|
||||
@@ -66,9 +99,15 @@ public:
|
||||
}
|
||||
|
||||
// Create pseudo terminal pair
|
||||
#ifdef __ANDROID__
|
||||
if (android_openpty(&m_master_fd, &m_slave_fd, nullptr) == -1) {
|
||||
throw std::runtime_error("Failed to create pseudo terminal");
|
||||
}
|
||||
#else
|
||||
if (openpty(&m_master_fd, &m_slave_fd, nullptr, nullptr, nullptr) == -1) {
|
||||
throw std::runtime_error("Failed to create pseudo terminal");
|
||||
}
|
||||
#endif
|
||||
|
||||
// Set master fd to non-blocking mode
|
||||
int flags = fcntl(m_master_fd, F_GETFL, 0);
|
||||
@@ -213,6 +252,14 @@ private:
|
||||
execl("/bin/zsh", "zsh", "-l", "-i", nullptr);
|
||||
}
|
||||
execl("/bin/bash", "bash", "-l", "-i", nullptr);
|
||||
#elif defined(__ANDROID__)
|
||||
// Android 没有 bash:/system/bin/sh 是 mksh,常用命令由 toybox 提供。
|
||||
// 不做 login shell(没有 /etc/profile);HOME 由 App 沙箱提供,写文件落在
|
||||
// /data/user/0/com.yama.client 下。
|
||||
setenv("LANG", "C.UTF-8", 1);
|
||||
setenv("LC_ALL", "C.UTF-8", 1);
|
||||
execl("/system/bin/sh", "sh", "-i", nullptr);
|
||||
execl("/system/bin/sh", "sh", nullptr);
|
||||
#else
|
||||
// Linux locale settings (C.UTF-8 is most portable)
|
||||
setenv("LANG", "C.UTF-8", 1);
|
||||
|
||||
Reference in new issue
Block a user