Feature: Support host uninstall on Android and expose uninstall_host

The server's "uninstall host" action is the single-byte TOKEN_UNINSTALL on
the main connection. Windows clients have handled it for a long time; the
Android client ignored it, so the menu entry silently did nothing there.

Handle it in the Android client the only way Android allows: ask the
system to uninstall the package. The client first replies with COMMAND_BYE
and drops the connection, then opens the package installer's confirmation
dialog through the accessibility service, or posts a notification that
resumes the dialog from MainActivity when accessibility is off. The user
still has to confirm on the device; if they cancel, the client has already
left the server and must be reopened by hand. The manifest gains
REQUEST_DELETE_PACKAGES, without which the installer activity refuses the
request outright.

New APKs advertise CLIENT_CAP_UNINSTALL so the server can reject old
clients instead of sending a command nobody handles. The new MCP tool
uninstall_host is gated on McpManage and McpReadonly=0, rejects Linux and
macOS hosts, and offers the same wait=true verification as power_action,
reporting went_offline once the host disappears from the online list.

Verified on the emulator end to end: the tool returned went_offline after
200 ms, the system dialog appeared, confirming it removed the package.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-11 20:59:10 +01:00
1 parent fd1b085279
commit 29d7ee1a18
12 files changed
+362 -8

No files matched your search

+162 -2
View File
@@ -335,7 +335,7 @@ const char* kServerInstructions =
u8"lnx(LNX):不支持服务、注册表、键日志、文件搜索/删除/移动/建目录、"
u8"窗口列表(list_windows)与窗口信息(get_foreground_window)、网络连接列表与断开"
u8"(list_network_connections / kill_network_connection)、"
u8"块输入、host_* 全族、chat_* 全族、operate_window、power_action。远程控制与截屏要求目标机"
u8"块输入、host_* 全族、chat_* 全族、operate_window、power_action、uninstall_host。远程控制与截屏要求目标机"
u8"有 X11/Xorg(或 XWayland)桌面,纯 Wayland 或无桌面的服务器不行。\n"
u8"mac(MAC):在 lnx 的基础上,还不支持远程控制全族(remote_open / remote_close / "
u8"remote_mouse / remote_keyboard / remote_clipboard)、get_screenshot、"
@@ -350,7 +350,8 @@ const char* kServerInstructions =
u8"(remote_open / remote_close / remote_mouse / remote_keyboard / remote_clipboard,"
u8"输入被翻译成触摸手势与 ESC(返回)/ HOME(桌面)/ APPS(任务列表)/ PRINTSCREEN(截屏)"
u8"等全局动作,只支持整屏目标;remote_lock_screen 用系统的锁屏动作;注入依赖"
u8"无障碍服务、抓屏依赖已授权录屏,两者都没有能力位可查,命令下发成功不代表生效),以及 "
u8"无障碍服务、抓屏依赖已授权录屏,两者都没有能力位可查,命令下发成功不代表生效),"
u8"卸载主机(uninstall_host,会拉起系统的卸载确认框,需要有人在设备上点确认),以及 "
u8"host_set_remark / host_set_watch_level 这两个只改服务端本地数据的工具。其余一律不支持"
u8"(客户端未实现 SYSTEM 与 SHELL)。\n"
u8"copy_files_c2c 是唯一有方向性的工具:源端 src_id 必须是 win,目标端 dst_id 可以是 "
@@ -2074,6 +2075,9 @@ std::string BuildKillProcess(const Json::Value& id, const Json::Value& args, CMy
std::string BuildOperateWindow(const Json::Value& id, const Json::Value& args, CMy2015RemoteDlg* parent);
std::string BuildControlService(const Json::Value& id, const Json::Value& args, CMy2015RemoteDlg* parent);
std::string BuildPowerAction(const Json::Value& id, const Json::Value& args, CMy2015RemoteDlg* parent);
Json::Value BuildUninstallHostInputSchema();
Json::Value BuildUninstallHostOutputSchema();
std::string BuildUninstallHost(const Json::Value& id, const Json::Value& args, CMy2015RemoteDlg* parent);
// ===== B1 前置声明(主机库元数据,定义见「tools/call 分派」前)=====
Json::Value BuildHostSetRemarkInputSchema();
@@ -2648,6 +2652,14 @@ std::string BuildToolsListResult(const Json::Value& id) {
tool["outputSchema"] = BuildPowerActionOutputSchema();
tools.append(tool);
}
{
Json::Value tool(Json::objectValue);
tool["name"] = "uninstall_host";
tool["description"] = u8"卸载指定在线主机上的被控程序(不可撤销)。报文是单字节 TOKEN_UNINSTALL,与主控在线列表右键「卸载主机」完全一样:Windows 客户端会退出并延时删除自身;Android 客户端会先回 BYE,再拉起系统的卸载确认框(Android 不允许静默卸载,必须有人在设备上点「确定」;没人确认就不会真的删除,Android 客户端却已按卸载退出连接,需要人工重开 App)。Linux/macOS 客户端没有该分支,返回 -32005。没有回执:ok/sent 只代表命令已送达;要确认主机确实消失就传 wait=true,工具会等它从在线列表下线。卸载后主机不会自动回来。";
tool["inputSchema"] = BuildUninstallHostInputSchema();
tool["outputSchema"] = BuildUninstallHostOutputSchema();
tools.append(tool);
}
}
// 18b) 主机库元数据(B1:host_set_remark / host_set_watch_level)。
@@ -8603,6 +8615,151 @@ std::string BuildPowerAction(const Json::Value& id, const Json::Value& args, CMy
return BuildResult(id, result);
}
// ===== 卸载主机:uninstall_host(TOKEN_UNINSTALL,单字节,无回执)=====
//
// 与主控在线列表右键「卸载主机」完全相同:单字节 TOKEN_UNINSTALL 走主连接。Windows
// 客户端一直有该分支(回 BYE、退出、self_del),Android 新版映射为「拉起系统卸载
// 确认框」,Linux/macOS 没有分支。老 APK 收到也是静默丢弃,所以 Android 必须先有
// CLIENT_CAP_UNINSTALL 才放行;Windows 没有能力位体系,按平台直接允许。
// 结果验证同 power_action:wait=true 等主机从在线列表消失(卸载后不会回线)。
Json::Value BuildUninstallHostInputSchema() {
Json::Value props(Json::objectValue);
AddIdRequiredProp(props);
Json::Value waitProp(Json::objectValue);
waitProp["type"] = "boolean";
waitProp["description"] = u8"true 时等主机从在线列表消失再返回(默认 false,发完即走)。卸载后主机不会回线。";
props["wait"] = waitProp;
Json::Value waitMsProp(Json::objectValue);
waitMsProp["type"] = "integer";
waitMsProp["minimum"] = 1000;
waitMsProp["maximum"] = 600000;
waitMsProp["description"] = u8"wait=true 时的最长等待(毫秒,默认 30000)";
props["wait_ms"] = waitMsProp;
return FinalizeIdSchema(props, {});
}
// 输出:ok/sent 只代表命令写进了 socket;wait=true 时用在线列表观察 went_offline。
Json::Value BuildUninstallHostOutputSchema() {
Json::Value props(Json::objectValue);
Json::Value ok(Json::objectValue);
ok["type"] = "boolean";
ok["description"] = u8"命令已下发(不代表已经卸载)";
props["ok"] = ok;
Json::Value sent(Json::objectValue);
sent["type"] = "boolean";
sent["description"] = u8"命令已送达主机";
props["sent"] = sent;
Json::Value off(Json::objectValue);
off["type"] = "boolean";
off["description"] = u8"主机是否已从在线列表消失(wait=true 时观察到的;wait=false 恒为 false)";
props["went_offline"] = off;
Json::Value elapsed(Json::objectValue);
elapsed["type"] = "integer";
elapsed["description"] = u8"本次等待实际耗时(毫秒;wait=false 时为 0)";
props["elapsed_ms"] = elapsed;
Json::Value schema(Json::objectValue);
schema["type"] = "object";
schema["properties"] = props;
return schema;
}
std::string BuildUninstallHost(const Json::Value& id, const Json::Value& args, CMy2015RemoteDlg* parent) {
uint64_t devId = 0;
std::string err;
if (!ParseHostIdArg(args, devId, err))
return BuildError(id, -32602, err);
context* ctx = FindMainContext(parent, devId);
if (!ctx)
return BuildError(id, -32002, "Host not found or offline: " + std::to_string(devId));
CString clientType = ctx->GetAdditionalData(RES_CLIENT_TYPE);
if (clientType == "LNX" || clientType == "MAC")
return BuildError(id, -32005, "uninstall_host is not supported on Linux/macOS hosts: "
"the client has no TOKEN_UNINSTALL handler");
if (clientType == "APK" && !ctx->SupportsUninstall())
return BuildError(id, -32005, "Android client is too old for uninstall_host: "
"the APK has no TOKEN_UNINSTALL handler, upgrade the client first");
CMcpServer& mcp = CMcpServer::Instance();
if (mcp.IsReadonly())
return BuildError(id, -32006, "uninstall_host is disabled: MCP is in read-only mode (set McpReadonly=0 to enable)");
if (!mcp.IsManageEnabled())
return BuildError(id, -32006, "uninstall_host is disabled: management operations are off (set McpManage=1 to enable)");
bool wait = false;
if (args.isMember("wait")) {
if (!args["wait"].isBool())
return BuildError(id, -32602, "wait must be a boolean");
wait = args["wait"].asBool();
}
int waitMs = 30000;
if (args.isMember("wait_ms")) {
int v = 0;
if (!GetIntArg(args, "wait_ms", v) || v < 1000 || v > 600000)
return BuildError(id, -32602, "wait_ms must be in range 1000..600000");
waitMs = v;
}
BYTE token = (BYTE)TOKEN_UNINSTALL;
if (!ctx->Send2Client(&token, sizeof(token)))
return BuildError(id, -32004, "Failed to send uninstall command to host");
bool wentOffline = false;
long long elapsedMs = 0;
if (wait) {
const auto t0 = std::chrono::steady_clock::now();
wentOffline = WaitHostOnlineState(parent, devId, false, waitMs);
elapsedMs = (long long)std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - t0).count();
}
AuditManageAction(parent, devId, "uninstall_host",
std::string("wait=") + (wait ? "1" : "0") +
(wait ? (wentOffline ? " went_offline=1" : " went_offline=0") : ""));
if (wait && !wentOffline && McpCallCancelled())
return BuildError(id, kErrCancelled,
"Cancelled while waiting for the host to go offline; the uninstall command "
"was already sent, so check the host list before retrying");
Json::Value result(Json::objectValue);
Json::Value structuredContent(Json::objectValue);
structuredContent["ok"] = true;
structuredContent["sent"] = true;
structuredContent["went_offline"] = wentOffline;
structuredContent["elapsed_ms"] = (Json::Int64)elapsedMs;
result["structuredContent"] = structuredContent;
std::string text = std::string(u8"已下发卸载命令");
if (!wait) {
text += u8"(未等待结果;要确认主机真的下线请传 wait=true)";
} else if (!wentOffline) {
text += std::string(u8";在 ") + std::to_string(waitMs) +
u8" ms 内主机仍在在线列表里——命令可能没有生效,或 Android 端在等人工点确认框。";
} else {
text += std::string(u8";主机已从在线列表下线(") + std::to_string(elapsedMs) + u8" ms)。";
}
Json::Value content(Json::arrayValue);
Json::Value item(Json::objectValue);
item["type"] = "text";
item["text"] = text;
content.append(item);
result["content"] = content;
result["isError"] = false;
return BuildResult(id, result);
}
// ===== B1:主机库元数据(备注 / 关注等级)=====
//
// 写的是服务端本机的 _ClientList(MAP_NOTE / MAP_LEVEL),只影响主控端自己的展示:
@@ -10120,6 +10277,8 @@ const ToolPlatform kToolPlatforms[] = {
{ "operate_window", kPlatWin },
{ "control_service", kPlatWin },
{ "power_action", kPlatWin },
// TOKEN_UNINSTALL:Windows 客户端一直有该分支;Android 新版映射为系统卸载框
{ "uninstall_host", kPlatWin|kPlatApk },
{ "search_file", kPlatWin },
{ "delete_path", kPlatWin },
{ "move_path", kPlatWin },
@@ -10470,6 +10629,7 @@ std::string DispatchSingleHostTool(const Json::Value& id, const std::string& too
if (toolName == "operate_window") return BuildOperateWindow(id, args, parent);
if (toolName == "control_service") return BuildControlService(id, args, parent);
if (toolName == "power_action") return BuildPowerAction(id, args, parent);
if (toolName == "uninstall_host") return BuildUninstallHost(id, args, parent);
if (toolName == "host_set_remark") return BuildHostSetRemark(id, args, parent);
if (toolName == "host_set_watch_level") return BuildHostSetWatchLevel(id, args, parent);
if (toolName == "search_file") return BuildSearchFile(id, args, parent);