Feature: Install Android APK updates pushed from the master

The master now sends COMMAND_UPDATE to Android hosts: it picks a
"Build Client -> Android" APK, checks the ZIP magic and that the package
carries this master's fingerprint, then sends [cmd][size][APK][xxh64].
The client verifies length and hash, writes filesDir/update.apk and
commits a PackageInstaller session, so the system installer still asks
the user to confirm - an app cannot replace itself silently. Write and
hash failures come back over TOKEN_CLIENT_MSG, so the master sees why
nothing happened instead of a silently dropped packet.

Sending the wrong file is what the fingerprint check guards against: a
template without the master id installs fine and then cannot connect
back. "Send anyway" is offered only after spelling that out, and the
client advertises CLIENT_CAP_APK_UPDATE (0x0400) so an old APK gets
"install a new build once by hand" instead of a packet it would discard.

Also report the real versionName/versionCode at login instead of the
hardcoded v1.0.0, add android/tools/verify_update_apk.py (pre-send check
of size, ZIP_STORED libs, ABI, package, versionCode and the decrypted
CONNECT_ADDRESS, which it still locates after BuildDlg rewrote szFlag),
and translate the nine new strings in en_US, zh_TW and de_DE.

Verified on the emulator and against the live release server: generate,
sign, send, install, and the device reconnects and re-grants screen
capture. Language packs are GBK and were appended as bytes.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-11 00:53:29 +01:00
1 parent cdd9c56b3d
commit 36618b0ad0
17 files changed
+955 -8

No files matched your search

+118 -1
View File
@@ -23,6 +23,7 @@
#include "ConnectionsDlg.h"
#include "VideoDlg.h"
#include <vector>
#include <algorithm>
#include <map>
#include <mutex>
#include <filesystem>
@@ -5424,13 +5425,18 @@ void CMy2015RemoteDlg::OnOnlineUpdate()
MessageBoxL(_T("请选择一个被控程序进行升级!"), _T("提示"), MB_ICONWARNING);
return;
}
CString clientType = ContextObject->GetAdditionalData(RES_CLIENT_TYPE).GetString();
if (clientType == "APK") {
// Android 有自己的确认流程(选包/校验/人工安装提示),普通的「是否升级」框对它没有信息量
SendApkUpdate(ContextObject);
return;
}
if (IDYES != MessageBoxL(_T("确定升级选定的被控程序吗?\n需受控程序支持方可生效!"),
_T("提示"), MB_ICONQUESTION | MB_YESNO))
return;
PBYTE buffer = nullptr;
ULONGLONG fileSize = 0;
ULONG packetSize = 0; // LNX 载荷尾部带 32 字节 MD5,故不总是 9 + fileSize
CString clientType = ContextObject->GetAdditionalData(RES_CLIENT_TYPE).GetString();
if (clientType == "EXE") {
CUpdateDlg dlg(this);
if (dlg.DoModal() != IDOK)
@@ -5567,6 +5573,117 @@ void CMy2015RemoteDlg::OnOnlineUpdate()
}
}
// 安卓在线升级:把「生成客户端导出、并用 android/sign_apk.bat 重签过」的 APK 整包
// 发给客户端,由客户端的系统安装器弹框、人工点安装。
// 载荷布局:[cmd:1][size:8 LE][APK:size][xxh64:8 LE]
//
// 为什么要扫主控指纹:APK 里的 CONNECT_ADDRESS(服务器地址/端口/主控 ID)是
// 「生成客户端」写进去的。直接发模板(未 patch、未重签)装上去会连错服务器并失联,
// 所以这里用 GetMasterHash() 的高 8 字节在 APK 里扫一遍,扫不到默认取消,防手滑。
VOID CMy2015RemoteDlg::SendApkUpdate(context* ContextObject)
{
// 后面全是模态框;期间主机可能下线被销毁,先把手头要用的信息取出来
CString targetIp = ContextObject->GetClientData(ONLINELIST_IP);
if (!ContextObject->SupportsApkUpdate()) {
MessageBoxL(_L("该安卓客户端版本过旧,不认识在线升级命令(收到也会丢弃)。\n\n"
"请先手动在同一台设备上安装一次新版 APK,之后即可在线升级。"),
_L("提示"), MB_ICONWARNING);
return;
}
std::string lastPath = THIS_CFG.GetStr("settings", "AndroidClientPath", "");
CFileDialog dlg(TRUE, _T("apk"), lastPath.empty() ? _T("ghost.apk") : CString(lastPath.c_str()),
OFN_FILEMUSTEXIST | OFN_HIDEREADONLY,
_T("Android 安装包 (*.apk)|*.apk|所有文件 (*.*)|*.*||"), this);
if (dlg.DoModal() != IDOK)
return;
CString apkPath = dlg.GetPathName();
// 客户端 IOCPClient 的 MAX_PACKET_SIZE 是 50MB,留点余量
const ULONGLONG kMaxApkSize = 40ull * 1024 * 1024;
std::ifstream in(apkPath.GetString(), std::ios::binary | std::ios::ate);
if (!in) {
CString msg;
msg.Format(_L("无法打开文件:\n%s"), apkPath.GetString());
MessageBoxL(msg, _L("提示"), MB_ICONWARNING);
return;
}
std::streamoff len = (std::streamoff)in.tellg();
if (len <= 0 || (ULONGLONG)len > kMaxApkSize) {
CString msg;
msg.Format(_L("APK 是空文件或超过 %llu 字节上限,不能下发。\n\n文件:\n%s"),
(unsigned long long)kMaxApkSize, apkPath.GetString());
MessageBoxL(msg, _L("提示"), MB_ICONWARNING);
return;
}
std::vector<BYTE> apk((size_t)len);
in.seekg(0);
if (!in.read((char*)apk.data(), len)) {
CString msg;
msg.Format(_L("读取文件失败:\n%s"), apkPath.GetString());
MessageBoxL(msg, _L("提示"), MB_ICONWARNING);
return;
}
// 路径记下来,下次默认打开同一目录(和 LinuxClientPath 一样放在 settings 段)
THIS_CFG.SetStr("settings", "AndroidClientPath", std::string(apkPath.GetString()));
auto reject = [&](LPCTSTR why) {
CString msg;
msg.Format(_L("无法下发该 APK:\n%s\n\n%s"), apkPath.GetString(), why);
MessageBoxL(msg, _L("提示"), MB_ICONWARNING);
};
if (apk.size() < 1024 || apk[0] != 'P' || apk[1] != 'K' || apk[2] != 0x03 || apk[3] != 0x04) {
reject(_L("不是 APK(ZIP)文件"));
return;
}
// CONNECT_ADDRESS.superAdmin = GetMasterHash() 前 16 个 hex 字符的 uint64,生成客户端时
// 原样写进 libyama.so;useLegacyPackaging=false 让它以 ZIP_STORED 存放,可直接搜到。
bool patched = false;
{
std::string hash = GetMasterHash();
if (hash.size() >= 16) {
uint64_t adminId = strtoull(hash.substr(0, 16).c_str(), NULL, 16);
// 必须用 BYTE:std::search 逐元素比较 *it1 == *it2,apk 是 vector<BYTE>(unsigned char),
// 若 needle 是 char 则 >=0x80 的字节(如 0xE3)会被当负数提升,永远比不上 227。
BYTE needle[8] = {};
memcpy(needle, &adminId, sizeof(needle));
patched = std::search(apk.begin(), apk.end(), needle, needle + sizeof(needle)) != apk.end();
}
}
if (!patched) {
CString msg;
msg.Format(_L("这个 APK 里没有找到本主控的标识,装上去很可能连不回本服务器。\n\n"
"请确认它是「生成客户端 → 安卓」导出的包,并且已经用 android/sign_apk.bat "
"重新签名(未重签的包签名对不上,系统会拒绝覆盖安装)。\n\n"
"文件:\n%s\n\n仍然下发吗?"),
apkPath.GetString());
if (IDYES != MessageBoxL(msg, _L("确认"), MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON2))
return;
}
ULONGLONG fileSize = (ULONGLONG)apk.size();
ULONG packetSize = (ULONG)(9 + fileSize + 8);
std::vector<BYTE> packet(packetSize);
packet[0] = COMMAND_UPDATE;
memcpy(&packet[1], &fileSize, 8);
memcpy(&packet[9], apk.data(), (size_t)fileSize);
uint64_t digest = XXH64(apk.data(), (size_t)fileSize, 0);
memcpy(&packet[9 + fileSize], &digest, 8);
Mprintf("[Update] Android APK %s (%llu bytes, xxh64=%016llx) → %s\n",
apkPath.GetString(), (unsigned long long)fileSize,
(unsigned long long)digest, targetIp.GetString());
SendSelectedCommand(packet.data(), packetSize);
MessageBoxL(_L("APK 已下发。\n\n"
"接下来的操作需要在设备上完成:\n"
"1. 若没弹出安装框,可能是系统未授权「安装未知应用」——在设备的应用设置里给 YAMA 打开;\n"
"2. 点「安装」确认覆盖安装;\n"
"3. 安装完成后打开 YAMA,重新授权录屏(升级后授权一定会失效)。"),
_L("提示"), MB_ICONINFORMATION);
}
std::string floatToString(float f)
{
char buf[32];