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:
1 parent
cdd9c56b3d
commit
36618b0ad0
17 files changed
+955
-8
No files matched your search
@@ -195,6 +195,9 @@ State g_bExit{S_CLIENT_NORMAL};
|
||||
uint64_t g_myClientID = 0; // sub_conn_thread.h 需要的外部符号
|
||||
static std::atomic<bool> g_running{false};
|
||||
static std::atomic<uint64_t> g_lastHeartbeatAckMs{0};
|
||||
// 主连接句柄:COMMAND_UPDATE 失败回报要往主连接发 ClientMsg(与 Linux 端同一套路)。
|
||||
// 只在 ConnectionThread 里赋值/置空,其他线程只读。
|
||||
static IOCPClient* g_pMainClient = nullptr;
|
||||
|
||||
// ---- JNI 反向调用(DataProcess → ControlService / CaptureService)----
|
||||
static JavaVM* g_jvm = nullptr;
|
||||
@@ -207,6 +210,7 @@ static jmethodID g_requestIdrMid = nullptr;
|
||||
static jmethodID g_forceFirstFrameMid = nullptr;
|
||||
static jmethodID g_statusMid = nullptr; // CaptureService.onNativeStatus(String)
|
||||
static jmethodID g_nativeExitMid = nullptr; // CaptureService.onNativeExit() — 服务端主动断开时停止服务
|
||||
static jmethodID g_updateReceivedMid = nullptr; // CaptureService.onUpdateReceived(String) — 升级 APK 已落盘
|
||||
|
||||
// 心跳日志限流:每 60 秒最多打一次
|
||||
static std::atomic<uint64_t> g_lastHbLogMs{0};
|
||||
@@ -276,6 +280,8 @@ static std::string g_screenRes;
|
||||
static std::string g_username;
|
||||
static std::string g_apkPath;
|
||||
static std::string g_filesDir; // context.filesDir,无需权限,用于持久化分组名
|
||||
static std::string g_versionName = "0"; // BuildConfig.VERSION_NAME,登录时上报到版本列
|
||||
static int g_versionCode = 0; // BuildConfig.VERSION_CODE
|
||||
|
||||
static void SaveGroupName() {
|
||||
if (g_filesDir.empty()) return;
|
||||
@@ -334,9 +340,30 @@ static void CallCaptureStaticVoid(jmethodID mid)
|
||||
if (attached) g_jvm->DetachCurrentThread();
|
||||
}
|
||||
|
||||
// 带一个 String 参数的静态方法调用(onUpdateReceived(path))
|
||||
static void CallCaptureStaticString(jmethodID mid, const char* s)
|
||||
{
|
||||
if (!g_jvm || !g_captureClass || !mid || !s) return;
|
||||
JNIEnv* jenv = nullptr;
|
||||
bool attached = false;
|
||||
jint st = g_jvm->GetEnv((void**)&jenv, JNI_VERSION_1_6);
|
||||
if (st == JNI_EDETACHED) {
|
||||
if (g_jvm->AttachCurrentThread(&jenv, nullptr) != JNI_OK || !jenv) return;
|
||||
attached = true;
|
||||
} else if (st != JNI_OK || !jenv) {
|
||||
return;
|
||||
}
|
||||
jstring js = jenv->NewStringUTF(s);
|
||||
if (js) {
|
||||
jenv->CallStaticVoidMethod(g_captureClass, mid, js);
|
||||
jenv->DeleteLocalRef(js);
|
||||
}
|
||||
if (jenv->ExceptionOccurred()) jenv->ExceptionClear();
|
||||
if (attached) g_jvm->DetachCurrentThread();
|
||||
}
|
||||
|
||||
// 触发编码器输出关键帧(setParameters REQUEST_SYNC_FRAME)
|
||||
static void RequestIdrFromJava() { CallCaptureStaticVoid(g_requestIdrMid); }
|
||||
|
||||
// 强制 VirtualDisplay 推一帧(静止屏幕绕过 SurfaceFlinger 空闲优化)
|
||||
static void ForceFirstFrameFromJava() { CallCaptureStaticVoid(g_forceFirstFrameMid); }
|
||||
|
||||
@@ -440,6 +467,103 @@ static void ScreenSpyThread()
|
||||
LOGI("ScreenSpyThread exit");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ 在线升级
|
||||
// 载荷布局:[COMMAND_UPDATE:1][APK 大小:8 LE][APK:size][xxh64:8 LE]
|
||||
// 校验通过后写到 filesDir/update.apk,交给 Kotlin 的 PackageInstaller 会话去装
|
||||
// (必须系统安装器 + 人工确认,APP 不能静默自替换)。
|
||||
// 任何失败都只回报、不退出:当前版本继续跑,服务端能看到原因。
|
||||
|
||||
// ClientMsg 的 text/title 是定长数组,strcpy_s 遇到超长会触发无效参数处理器直接崩,
|
||||
// 所以统一截断后 memcpy。
|
||||
static void sendClientMsg(const char* title, const char* text)
|
||||
{
|
||||
if (!g_pMainClient || !g_pMainClient->IsConnected()) return;
|
||||
ClientMsg msg;
|
||||
msg.cmd = TOKEN_CLIENT_MSG;
|
||||
strncpy(msg.title, title ? title : "YAMA", sizeof(msg.title) - 1);
|
||||
if (text) {
|
||||
size_t n = strlen(text);
|
||||
if (n > sizeof(msg.text) - 1) n = sizeof(msg.text) - 1;
|
||||
memcpy(msg.text, text, n);
|
||||
}
|
||||
g_pMainClient->Send2Server((char*)&msg, sizeof(msg));
|
||||
}
|
||||
|
||||
static void reportUpdateFailure(const std::string& reason)
|
||||
{
|
||||
LOGI("UPDATE failed: %s", reason.c_str());
|
||||
PostStatus(("update: " + reason).c_str());
|
||||
sendClientMsg("Android update failed", reason.c_str());
|
||||
}
|
||||
|
||||
static void ApplyUpdateApk(const PBYTE szBuffer, ULONG ulLength)
|
||||
{
|
||||
const size_t headerLen = 1 + sizeof(uint64_t);
|
||||
const size_t trailerLen = sizeof(uint64_t);
|
||||
if (ulLength < headerLen + trailerLen) {
|
||||
reportUpdateFailure("payload too short");
|
||||
return;
|
||||
}
|
||||
uint64_t size = 0;
|
||||
memcpy(&size, szBuffer + 1, sizeof(size));
|
||||
if (size < 1024 || size > (uint64_t)(ulLength - headerLen - trailerLen)) {
|
||||
reportUpdateFailure("payload size invalid");
|
||||
return;
|
||||
}
|
||||
const unsigned char* payload = szBuffer + headerLen;
|
||||
uint64_t expect = 0;
|
||||
memcpy(&expect, payload + size, sizeof(expect));
|
||||
uint64_t actual = (uint64_t)XXH64(payload, (size_t)size, 0);
|
||||
if (actual != expect) {
|
||||
char buf[96];
|
||||
snprintf(buf, sizeof(buf), "xxh64 mismatch (payload corrupted: %u bytes)", (unsigned)size);
|
||||
reportUpdateFailure(buf);
|
||||
return;
|
||||
}
|
||||
if (payload[0] != 'P' || payload[1] != 'K' || payload[2] != 0x03 || payload[3] != 0x04) {
|
||||
reportUpdateFailure("payload is not an APK");
|
||||
return;
|
||||
}
|
||||
if (g_filesDir.empty()) {
|
||||
reportUpdateFailure("filesDir not set");
|
||||
return;
|
||||
}
|
||||
std::string path = g_filesDir + "/update.apk";
|
||||
int fd = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
||||
if (fd < 0) {
|
||||
reportUpdateFailure("cannot create " + path + ": " + strerror(errno));
|
||||
return;
|
||||
}
|
||||
bool ok = true;
|
||||
size_t off = 0;
|
||||
while (off < (size_t)size) {
|
||||
ssize_t n = write(fd, payload + off, (size_t)size - off);
|
||||
if (n <= 0) {
|
||||
if (n < 0 && errno == EINTR) continue;
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
off += (size_t)n;
|
||||
}
|
||||
if (ok) ok = (fsync(fd) == 0);
|
||||
if (close(fd) != 0) ok = false;
|
||||
if (!ok) {
|
||||
std::string err = strerror(errno);
|
||||
unlink(path.c_str());
|
||||
reportUpdateFailure("cannot write " + path + ": " + err);
|
||||
return;
|
||||
}
|
||||
|
||||
LOGI("UPDATE: saved %s (%llu bytes), handing to installer", path.c_str(),
|
||||
(unsigned long long)size);
|
||||
char msg[128];
|
||||
snprintf(msg, sizeof(msg), "APK received (%.1f MB), opening system installer",
|
||||
(double)size / (1024.0 * 1024.0));
|
||||
PostStatus(msg);
|
||||
// 拉不起来(无障碍没开/系统限制)时 Kotlin 侧会改发通知,failure 只回报真正的前置错误
|
||||
CallCaptureStaticString(g_updateReceivedMid, path.c_str());
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ DataProcess
|
||||
int DataProcess(void* /*user*/, PBYTE szBuffer, ULONG ulLength)
|
||||
{
|
||||
@@ -531,6 +655,11 @@ int DataProcess(void* /*user*/, PBYTE szBuffer, ULONG ulLength)
|
||||
LOGI("COMMAND_SHELL (not implemented)");
|
||||
break;
|
||||
|
||||
case COMMAND_UPDATE:
|
||||
LOGI("cmd: COMMAND_UPDATE len=%lu", (unsigned long)ulLength);
|
||||
ApplyUpdateApk(szBuffer, ulLength);
|
||||
break;
|
||||
|
||||
case COMMAND_SYSTEM:
|
||||
LOGI("COMMAND_SYSTEM (not implemented)");
|
||||
break;
|
||||
@@ -582,7 +711,7 @@ static void ConnectionThread()
|
||||
logInfo.AddReserved("");
|
||||
logInfo.AddReserved(g_location.c_str()); // [10] 地理位置
|
||||
logInfo.AddReserved(g_pubIp.c_str()); // [11] 公网 IP
|
||||
logInfo.AddReserved("v1.0.0");
|
||||
logInfo.AddReserved(("v" + g_versionName + " (" + std::to_string(g_versionCode) + ")").c_str());
|
||||
logInfo.AddReserved(g_username.c_str());
|
||||
logInfo.AddReserved(0); // IsRunningAsAdmin
|
||||
logInfo.AddReserved(g_screenRes.c_str());
|
||||
@@ -598,6 +727,7 @@ static void ConnectionThread()
|
||||
|
||||
std::unique_ptr<IOCPClient> client(new IOCPClient(g_bExit, false));
|
||||
client->setManagerCallBack(nullptr, DataProcess, nullptr);
|
||||
g_pMainClient = client.get(); // 升级失败回报用;线程退出前置空
|
||||
// 同 Linux/macOS:不传 conn 的构造路径拿不到注入的 CONNECT_ADDRESS,协议必须显式喂进去,
|
||||
// 否则配成 WS/WSS 也只会静默按明文 TCP 直连。Android 没有 TLS 通道,WSS 会 fail-closed。
|
||||
client->SetDefaultProto((int)(unsigned char)g_SETTINGS.protoType);
|
||||
@@ -688,6 +818,7 @@ static void ConnectionThread()
|
||||
}
|
||||
|
||||
g_running.store(false);
|
||||
g_pMainClient = nullptr; // client 马上析构,别让别的线程拿到悬空指针
|
||||
LOGI("ConnectionThread exit");
|
||||
|
||||
if (g_bExit == S_CLIENT_EXIT && g_jvm && g_captureClass && g_nativeExitMid) {
|
||||
@@ -714,7 +845,8 @@ Java_com_yama_client_YamaBridge_nativeInit(
|
||||
jstring serverIp, jint serverPort,
|
||||
jstring androidId, jstring deviceModel,
|
||||
jstring androidVersion, jstring screenRes,
|
||||
jstring username, jstring apkPath, jstring filesDir)
|
||||
jstring username, jstring apkPath, jstring filesDir,
|
||||
jstring versionName, jint versionCode)
|
||||
{
|
||||
// CPP-01 fix: CAS 原子地完成"检查+设置",消除 check-then-act 竞争
|
||||
bool expected = false;
|
||||
@@ -739,6 +871,9 @@ Java_com_yama_client_YamaBridge_nativeInit(
|
||||
g_username = toStr(username);
|
||||
g_apkPath = toStr(apkPath);
|
||||
g_filesDir = toStr(filesDir);
|
||||
std::string verName = toStr(versionName);
|
||||
if (!verName.empty()) g_versionName = verName;
|
||||
g_versionCode = (int)versionCode;
|
||||
LoadGroupName(); // 若文件存在则覆盖 g_SETTINGS.szGroupName(优先级高于编译时 patch)
|
||||
|
||||
// 缓存 JVM 和 Class/Method 引用。必须在 Java 线程(nativeInit 调用栈)
|
||||
@@ -775,6 +910,9 @@ Java_com_yama_client_YamaBridge_nativeInit(
|
||||
g_nativeExitMid = env->GetStaticMethodID(g_captureClass, "onNativeExit", "()V");
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); g_nativeExitMid = nullptr; }
|
||||
if (!g_nativeExitMid) LOGE("CaptureService.onNativeExit not found");
|
||||
g_updateReceivedMid = env->GetStaticMethodID(g_captureClass, "onUpdateReceived", "(Ljava/lang/String;)V");
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); g_updateReceivedMid = nullptr; }
|
||||
if (!g_updateReceivedMid) LOGE("CaptureService.onUpdateReceived not found");
|
||||
UseAndroidLog(PostStatus);
|
||||
} else {
|
||||
LOGE("CaptureService class not found");
|
||||
@@ -797,6 +935,18 @@ Java_com_yama_client_YamaBridge_nativeStop(JNIEnv*, jobject)
|
||||
g_running.store(false);
|
||||
}
|
||||
|
||||
// 由 Kotlin 侧(安装流程)回报结果到服务端消息栏,走 TOKEN_CLIENT_MSG
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_yama_client_YamaBridge_nativeReportMessage(JNIEnv* env, jobject, jstring msg)
|
||||
{
|
||||
if (!msg) return;
|
||||
const char* c = env->GetStringUTFChars(msg, nullptr);
|
||||
if (!c) return;
|
||||
LOGI("REPORT: %s", c);
|
||||
sendClientMsg("Android update", c);
|
||||
env->ReleaseStringUTFChars(msg, c);
|
||||
}
|
||||
|
||||
// 由 CaptureService 在 MediaCodec 配置完成后调用,告知 C++ 侧实际捕获尺寸
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_yama_client_YamaBridge_nativeSetScreenSize(JNIEnv*, jobject, jint width, jint height)
|
||||
|
||||
Reference in new issue
Block a user