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);
|
||||
|
||||
@@ -148,6 +148,7 @@ inline int isValid_10s()
|
||||
#define CLIENT_CAP_LOCK_SCREEN 0x1000 // 屏幕子连接处理 COMMAND_SCREEN_CTRL_ALT_DEL(Android 映射为锁屏 GLOBAL_ACTION_LOCK_SCREEN)
|
||||
#define CLIENT_CAP_UNINSTALL 0x2000 // 主连接处理 TOKEN_UNINSTALL(Android 拉起系统卸载确认框;Windows 客户端一直支持但不声明)
|
||||
#define CLIENT_CAP_REMOTE_CHAT 0x4000 // 主连接处理 COMMAND_CHAT(远程交谈子连接;Windows 一直支持但不声明,目前只有新版 Android 声明)
|
||||
#define CLIENT_CAP_TERMINAL 0x8000 // 主连接处理 COMMAND_SHELL(PTY 终端子连接:exec_command / terminal_*;Linux 一直支持但不声明,目前只有新版 Android 声明)
|
||||
|
||||
#define TALK_DLG_MAXLEN 1024 // 最大输入字符长度
|
||||
|
||||
@@ -1314,6 +1315,9 @@ typedef struct LOGIN_INFOR {
|
||||
caps |= CLIENT_CAP_UNINSTALL;
|
||||
// 远程交谈(COMMAND_CHAT → 子连接 + 应用内聊天窗口):同上,老 APK 不认识这条命令。
|
||||
caps |= CLIENT_CAP_REMOTE_CHAT;
|
||||
// 远程终端(COMMAND_SHELL → PTY 子连接):老 APK 里 COMMAND_SHELL 是空实现,
|
||||
// 服务端据此提前拒绝 exec_command / terminal_*,而不是等子连接超时。
|
||||
caps |= CLIENT_CAP_TERMINAL;
|
||||
#endif
|
||||
return caps;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user