Feature: Upgrade a Linux client from the server
The Linux client is a standalone ELF with no in-memory loader and no COMMAND_UPDATE handler, so the server's upgrade menu silently did nothing for an LNX host. It can now push a new binary and have the client replace and restart itself. The server grows an LNX branch next to EXE/DLL/SC. It takes the ELF from an explicit settings.ini LinuxClientPath, then ghost_linux_amd64 in the program directory, then res\ghost_linux, then IDR_LINUX_GHOST, the copy the server has always embedded for the client-build dialog, so a server with nothing on disk can still upgrade; a configured path that cannot be read is an error rather than a silent fallback. Only x86_64 ELF64 is accepted. The payload adds a 32-byte MD5 trailer to the existing [cmd][size:8][payload] layout, which the Windows branches keep unchanged. linux/ghost in the tree is still the build from before this change and is not updated here. It has to be refreshed before release: the embedded default is copied from it at build time, and a stale copy would install a client with no COMMAND_UPDATE handler, which can never be upgraded remotely again. The client verifies the declared size, elf magic, e_machine against its own ELF header and the MD5 before touching anything, so a miscopied or corrupted file never lands on disk. It then writes a same-directory temp file with fsync and 0755 (a running executable cannot be truncated in place), renames it over its own path atomically, keeps the replaced inode as .old, removes the PID file, forks a detached child that waits for the old process to exit and execs the new binary with the original argv, and finally exits with S_CLIENT_UPDATE. Doing the rename and the exec in that order avoids ETXTBSY, double connections under one client id, and a new instance that isAlreadyRunning() would refuse to start. Any failure (unwritable install directory, bad payload, fork) leaves the client running on the old version; the reason goes to the client log and to the server's message log through TOKEN_CLIENT_MSG, which required moving that struct out of the Windows-only block in common/commands.h. The shared client source had also stopped compiling off Windows: the unknown-header diagnostic in IOCPClient::OnServerReceiving printed TlsEnabled() and m_tls.IsReady() unconditionally, but TlsChannel and m_tls only exist on Windows. That diagnostic now keeps the TLS details where the channel exists and prints just the buffer dump everywhere else. Verified end to end in WSL against the Debug server, foreground and -d daemon: the PID changes, the installed file matches the pushed MD5, the host reconnects, and ghost.old holds the previous binary. A read-only install directory reports "cannot create ...: Permission denied" in the server log with the client still online. With an invalid file at ghost_linux_amd64 the server refuses it and names that path, which shows the disk source wins; with nothing on disk the update goes through from the embedded ELF. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
bfb17d0d25
commit
4c0e86dcc5
5 files changed
+358
-2
No files matched your search
@@ -4899,6 +4899,7 @@ void CMy2015RemoteDlg::OnOnlineUpdate()
|
||||
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);
|
||||
@@ -4943,10 +4944,95 @@ void CMy2015RemoteDlg::OnOnlineUpdate()
|
||||
fileSize = 0;
|
||||
buffer = new BYTE[fileSize + 9]();
|
||||
buffer[0] = COMMAND_UPDATE;
|
||||
} else if (clientType == "LNX") {
|
||||
// Linux 客户端是独立 ELF(不经过内存加载器),载荷布局与 Windows 不同:
|
||||
// [cmd:1][size:8][ELF:size][md5:32 ASCII 小写 hex]
|
||||
// 客户端把新版本写到同目录临时文件、校验后 rename 覆盖自己再 execv 重启。
|
||||
// ELF 来源(优先级从高到低):
|
||||
// 1. ini [settings] LinuxClientPath —— 显式指定,读不到就直接报错,不悄悄换成别的版本
|
||||
// 2. 程序目录 ghost_linux_amd64 —— 磁盘上换个文件即可,不用重编服务端
|
||||
// 3. res\ghost_linux → 内嵌资源 IDR_LINUX_GHOST —— 服务端自带的兜底,什么都不用放
|
||||
// MVP 只支持 x86_64(客户端也会用自身 ELF 头的 e_machine 再校验一次)。
|
||||
std::string iniPath = THIS_CFG.GetStr("settings", "LinuxClientPath", "");
|
||||
std::string defaultPath;
|
||||
{
|
||||
// 程序所在目录(注意 GetParentDir() 是再上一级,不能直接用)
|
||||
char exePath[MAX_PATH] = {};
|
||||
GetModuleFileNameA(NULL, exePath, MAX_PATH);
|
||||
std::string progDir(exePath);
|
||||
size_t pos = progDir.find_last_of("\\/");
|
||||
if (pos != std::string::npos) progDir = progDir.substr(0, pos);
|
||||
defaultPath = progDir + "\\ghost_linux_amd64";
|
||||
}
|
||||
std::vector<BYTE> elf;
|
||||
std::string source; // 实际用到的来源,只用于日志
|
||||
// 从磁盘读;不存在/为空/超过 64MB 都算失败,由调用方决定要不要往下一级找
|
||||
auto readDisk = [&](const std::string& p) {
|
||||
std::ifstream in(p.c_str(), std::ios::binary | std::ios::ate);
|
||||
std::streamoff len = in ? (std::streamoff)in.tellg() : -1;
|
||||
if (len <= 0 || len > 64 * 1024 * 1024) return false;
|
||||
elf.resize((size_t)len);
|
||||
in.seekg(0);
|
||||
if (!in.read((char*)elf.data(), len)) {
|
||||
elf.clear();
|
||||
return false;
|
||||
}
|
||||
source = p;
|
||||
return true;
|
||||
};
|
||||
bool loaded = false;
|
||||
if (!iniPath.empty()) {
|
||||
loaded = readDisk(iniPath);
|
||||
} else {
|
||||
loaded = readDisk(defaultPath);
|
||||
if (!loaded) {
|
||||
DWORD resSize = 0;
|
||||
LPBYTE data = ReadResource(IDR_LINUX_GHOST, resSize, ResFileName::GHOST_LINUX);
|
||||
if (data && resSize) {
|
||||
elf.assign(data, data + resSize);
|
||||
source = "res\\ghost_linux 或内嵌资源";
|
||||
loaded = true;
|
||||
}
|
||||
delete[] data;
|
||||
}
|
||||
}
|
||||
auto reject = [&](LPCTSTR why) {
|
||||
CString msg;
|
||||
msg.Format(_L("无法使用 Linux 客户端文件:\n%s\n\n%s"),
|
||||
(iniPath.empty() ? defaultPath : iniPath).c_str(), why);
|
||||
MessageBoxL(msg, _T("提示"), MB_ICONWARNING);
|
||||
};
|
||||
if (!loaded || elf.size() < 64) {
|
||||
reject(_L("文件不存在或过小(可在 settings.ini 设置 LinuxClientPath)"));
|
||||
return;
|
||||
}
|
||||
Mprintf("[Update] Linux client ELF: %s (%u bytes)\n", source.c_str(), (unsigned)elf.size());
|
||||
if (elf[0] != 0x7f || elf[1] != 'E' || elf[2] != 'L' || elf[3] != 'F') {
|
||||
reject(_L("不是 ELF 可执行文件"));
|
||||
return;
|
||||
}
|
||||
int machine = elf[18] | (elf[19] << 8);
|
||||
if (elf[4] != 2 || machine != 0x3E) { // ELFCLASS64 + EM_X86_64
|
||||
reject(_L("只支持 x86_64 ELF(ELFCLASS64 / EM_X86_64)"));
|
||||
return;
|
||||
}
|
||||
std::string md5 = CalcMD5FromBytes(elf.data(), (DWORD)elf.size());
|
||||
if (md5.size() != 32) {
|
||||
reject(_L("无法计算校验值"));
|
||||
return;
|
||||
}
|
||||
fileSize = elf.size();
|
||||
packetSize = (ULONG)(9 + fileSize + 32);
|
||||
buffer = new BYTE[packetSize];
|
||||
buffer[0] = COMMAND_UPDATE;
|
||||
memset(buffer + 1, 0, 8);
|
||||
memcpy(buffer + 1, &fileSize, 8);
|
||||
memcpy(buffer + 9, elf.data(), (size_t)fileSize);
|
||||
memcpy(buffer + 9 + fileSize, md5.c_str(), 32);
|
||||
}
|
||||
|
||||
if (buffer) {
|
||||
SendSelectedCommand((PBYTE)buffer, 9 + fileSize);
|
||||
SendSelectedCommand((PBYTE)buffer, packetSize ? packetSize : (ULONG)(9 + fileSize));
|
||||
delete[] buffer;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user