Feature: update zlib to 1.4 and pick wire compression at runtime

The client selected zlib or zstd for the wire protocol at compile time
(USING_ZLIB), so changing the mode required a rebuild, and the vendored
zlib was still 1.3.2.

Vendored zlib is now 1.4 (headers and both static libs rebuilt from the
upstream source root). The client links zlib and zstd together and reads
settings\compress from its own registry key when a connection is
created: "zlib" selects zlib, anything else keeps the zstd default.
Every connection reads it once, so sub-connections follow the main link.

The online list context menu gains "Experimental -> Wire compression:
ZSTD / ZLIB". It sends COMMAND_SET_COMPRESS (181) only to hosts that
declare CLIENT_CAP_COMPRESS_SWITCH; the client persists the choice and
restarts its connection thread, so the new mode applies in about two
seconds without restarting the process. The online-info tooltip now
marks the negotiated algorithm with [ZSTD] / [ZLIB] / [NONE].

Also fixes zlib buffer sizing on both ends: 1.001n+12 is one byte short
of compressBound for packets under about 1 KB, which silently dropped
small commands.

Verified: both modes connect and receive MasterSettings; menu switching
to zlib and back reconnects in each mode; Debug/Release x64, Release
x86, ServerDll and server builds pass.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-23 17:46:36 +01:00
1 parent 340fb20518
commit 0e20e0bbdb
20 files changed
+303 -84

No files matched your search

+49 -1
View File
@@ -1177,6 +1177,8 @@ BEGIN_MESSAGE_MAP(CMy2015RemoteDlg, CDialogEx)
ON_COMMAND(ID_EXPERIMENT_DRAWING_BOARD, &CMy2015RemoteDlg::OnExperimentDrawingBoard)
ON_COMMAND(ID_EXPERIMENT_TLS_ENABLE, &CMy2015RemoteDlg::OnExperimentTlsEnable)
ON_COMMAND(ID_EXPERIMENT_TLS_DISABLE, &CMy2015RemoteDlg::OnExperimentTlsDisable)
ON_COMMAND(ID_EXPERIMENT_COMPRESS_ZSTD, &CMy2015RemoteDlg::OnExperimentCompressZstd)
ON_COMMAND(ID_EXPERIMENT_COMPRESS_ZLIB, &CMy2015RemoteDlg::OnExperimentCompressZlib)
ON_COMMAND(ID_PROTO_SWITCH_TCP, &CMy2015RemoteDlg::OnProtoSwitchTcp)
ON_COMMAND(ID_PROTO_SWITCH_WS, &CMy2015RemoteDlg::OnProtoSwitchWs)
ON_COMMAND(ID_PROTO_SWITCH_WSS, &CMy2015RemoteDlg::OnProtoSwitchWss)
@@ -4979,6 +4981,13 @@ void CMy2015RemoteDlg::OnNMRClickOnline(NMHDR *pNMHDR, LRESULT *pResult)
pExperimentMenu->DeleteMenu(ID_EXPERIMENT_TLS_ENABLE, MF_BYCOMMAND);
if (SHOULD_HIDE_CTX(HIDE_CTX_EXPERIMENT_TLS_DISABLE, CF_EXPERIMENT_TLS_DISABLE))
pExperimentMenu->DeleteMenu(ID_EXPERIMENT_TLS_DISABLE, MF_BYCOMMAND);
// 线缆压缩切换:放在实验功能末尾。仅当该组仍有可见项时追加——被功能位整组
// 隐藏时既不泄露,也不影响下面「空组摘除」的逻辑。
if (!IsPopupEmptyOfItems(pExperimentMenu)) {
pExperimentMenu->AppendMenu(MF_SEPARATOR);
pExperimentMenu->AppendMenuL(MF_STRING, ID_EXPERIMENT_COMPRESS_ZSTD, "线缆压缩:ZSTD");
pExperimentMenu->AppendMenuL(MF_STRING, ID_EXPERIMENT_COMPRESS_ZLIB, "线缆压缩:ZLIB");
}
for (UINT i = 0; i < SubMenu->GetMenuItemCount(); i++) {
if (SubMenu->GetSubMenu(i) == pExperimentMenu) {
protoInsertPos = (int)i + 1;
@@ -5326,6 +5335,36 @@ void CMy2015RemoteDlg::OnExperimentTlsDisable()
SendSelectedCommand(&bToken, sizeof(BYTE));
}
// ---- 线缆压缩切换(在线列表右键 → 实验功能 → 线缆压缩:ZSTD / ZLIB)----
void CMy2015RemoteDlg::OnExperimentCompressZstd() { SwitchSelectedCompression("zstd"); }
void CMy2015RemoteDlg::OnExperimentCompressZlib() { SwitchSelectedCompression("zlib"); }
VOID CMy2015RemoteDlg::SwitchSelectedCompression(const char* mode)
{
// 老客户端不认识 COMMAND_SET_COMPRESS,收到只会丢弃;先按能力位过滤。
int selected = 0, unsupported = 0;
EnterCriticalSection(&m_cs);
for (context* ctx : ResolveCommandTargets()) {
if (!ctx->IsLogin()) continue;
++selected;
if (!ctx->SupportsCompressSwitch()) ++unsupported;
}
LeaveCriticalSection(&m_cs);
if (selected == 0) return;
if (unsupported > 0) {
MessageBoxL(_T("选中的主机里有客户端版本不支持线缆压缩切换(需要新版 Windows 客户端)"),
_T("提示"), MB_ICONWARNING);
return;
}
// 载荷 [cmd]["zstd"|"zlib"]。客户端写入 settings\compress 后重启 StartClient 线程,
// 新连接按新模式重连;服务端按每个连接的首包自动识别算法,不影响其它主机。
std::string pkt(1, (char)COMMAND_SET_COMPRESS);
pkt += mode;
Mprintf("[compress-switch] send %s to %d host(s)\n", mode, selected);
SendSelectedCommand((PBYTE)&pkt[0], (ULONG)pkt.size());
}
// ---- 协议切换(在线列表右键 → 协议切换 → 直连 TCP / WS / WSS)----
//
// 服务端只负责下发目标入口和协议:客户端把入口写进本机配置后断开重连,在线升级换 DLL
@@ -10357,6 +10396,15 @@ void CMy2015RemoteDlg::OnListClick(NMHDR* pNMHDR, LRESULT* pResult)
{FLAG_WINOS, "WinOS"}, {FLAG_UNKNOWN, "Unknown"}, {FLAG_SHINE, "Shine"},
{FLAG_FUCK, "FUCK"}, {FLAG_HELLO, "Hello"}, {FLAG_HELL, "HELL"},
};
// 压缩算法标注:服务端按连接自动识别(首个压缩包解压时定型),追加在加密协议后面。
// 不改 FormatL 的格式串,避免影响多语言翻译 key。
CString typeText = typMap[type].c_str();
switch (ctx->GetCompressMethod()) {
case COMPRESS_ZSTD: typeText += " [ZSTD]"; break;
case COMPRESS_ZLIB: typeText += " [ZLIB]"; break;
case COMPRESS_NONE: typeText += " [NONE]"; break;
default: break; // COMPRESS_UNKNOWN:不标注
}
CString processInfo;
if (!res[RES_PID].IsEmpty() || !res[RES_FILESIZE].IsEmpty()) {
processInfo.FormatL("\r\n进程 PID: %s %s", res[RES_PID].IsEmpty() ? "" : res[RES_PID], res[RES_FILESIZE].IsEmpty() ? "" : res[RES_FILESIZE]);
@@ -10377,7 +10425,7 @@ void CMy2015RemoteDlg::OnListClick(NMHDR* pNMHDR, LRESULT* pResult)
res[RES_PROGRAM_BITS].IsEmpty() ? "" : res[RES_PROGRAM_BITS] + _L(" 位 "), res[RES_FILE_PATH], res[RES_EXE_VERSION], processInfo,
res[RES_SYSTEM_BITS], res[RES_SYSTEM_CPU], res[RES_SYSTEM_MEM], res[RES_RESOLUTION], startTime, expired.c_str(),
res[RES_USERNAME], res[RES_ISADMIN] == "1" ? _L("[管理员]") : res[RES_ISADMIN].IsEmpty() ? "" : _L("[非管理员]"), elapsed,
ctx->GetProtocol().c_str(), ctx->GetServerPort(), typMap[type].c_str(), ctx->GetMasterID().c_str(), res[RES_CLIENT_ID]);
ctx->GetProtocol().c_str(), ctx->GetServerPort(), typeText.GetString(), ctx->GetMasterID().c_str(), res[RES_CLIENT_ID]);
// 地理信息只在加载了本地 IP 库(QQWry/ip2region)时才查询并展示。
// 未装库时 m_IPConverter 是网络版 GetGeoLocation(同步外网 HTTP,可达数秒),
// 且结果不会被显示——跳过可消除双击浮窗的弹出延时。
+4
View File
@@ -597,6 +597,10 @@ public:
afx_msg void OnExperimentDrawingBoard();
afx_msg void OnExperimentTlsEnable();
afx_msg void OnExperimentTlsDisable();
// 线缆压缩切换(实验功能子菜单):写客户端配置并触发立即重连(zstd / zlib)
afx_msg void OnExperimentCompressZstd();
afx_msg void OnExperimentCompressZlib();
VOID SwitchSelectedCompression(const char* mode);
// 协议切换(在线列表右键菜单,与「实验功能」平级):直连 TCP / WS / WSS
afx_msg void OnProtoSwitchTcp();
afx_msg void OnProtoSwitchWs();
@@ -107,6 +107,7 @@
<AdditionalOptions>/source-charset:utf-8 /execution-charset:.936 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<ModuleDefinitionFile>server_exports.def</ModuleDefinitionFile>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>zlib\zlib.lib;%(AdditionalDependencies)</AdditionalDependencies>
@@ -142,6 +143,7 @@
<AdditionalOptions>/source-charset:utf-8 /execution-charset:.936 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<ModuleDefinitionFile>server_exports.def</ModuleDefinitionFile>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>zlib\zlib_x64.lib;%(AdditionalDependencies)</AdditionalDependencies>
@@ -177,6 +179,7 @@
<AdditionalOptions>/source-charset:utf-8 /execution-charset:.936 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<ModuleDefinitionFile>server_exports.def</ModuleDefinitionFile>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
@@ -214,6 +217,7 @@
<AdditionalOptions>/source-charset:utf-8 /execution-charset:.936 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<ModuleDefinitionFile>server_exports.def</ModuleDefinitionFile>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
@@ -239,6 +243,7 @@
<None Include="..\..\macos\ghost" />
<None Include="..\..\Release\ghost.exe" />
<None Include="..\..\Release\SCLoader.exe" />
<None Include="server_exports.def" />
<None Include="..\..\Release\ServerDll.dll" />
<None Include="..\..\Release\TestRun.exe" />
<None Include="..\..\Release\TinyRun.dll" />
+3 -1
View File
@@ -1410,8 +1410,10 @@ BOOL WriteContextData(CONTEXT_OBJECT* ContextObject, PBYTE szBuffer, size_t ulOr
break;
}
bool usingZstd = ContextObject->CompressMethod == COMPRESS_ZSTD;
// zlib 用 compressBound:1.001n+12 对小于约 1KB 的包会比 compressBound 少
// 1 字节,compress() 返回 Z_BUF_ERROR,小包(心跳 ACK、控制命令)被静默丢弃。
unsigned long ulCompressedLength = usingZstd ?
ZSTD_compressBound(ulOriginalLength) : (unsigned long)((double)ulOriginalLength * 1.001 + 12);
ZSTD_compressBound(ulOriginalLength) : compressBound(ulOriginalLength);
// 使用预分配缓冲区替代每次 new
LPBYTE CompressedBuffer = ContextObject->GetSendCompressBuffer(ulCompressedLength);
Buffer tmp(szBuffer, ulOriginalLength);
+4
View File
@@ -741,6 +741,10 @@ public:
{
return Parser.m_nFlagType;
}
virtual int GetCompressMethod() const override
{
return CompressMethod;
}
// Write compressed buffer.
void WriteBuffer(LPBYTE data, ULONG dataLen, ULONG originLen, int cmd = -1)
{
+9
View File
@@ -41,6 +41,7 @@ public:
virtual std::string GetProtocol() const = 0;
virtual int GetServerPort() const = 0;
virtual FlagType GetFlagType() const = 0;
virtual int GetCompressMethod() const = 0; // COMPRESS_ZSTD / COMPRESS_ZLIB / COMPRESS_NONE
virtual std::string GetGroupName() const = 0;
virtual uint64_t GetAliveTime()const = 0;
virtual void SetLastHeartbeat(uint64_t time) = 0;
@@ -199,6 +200,14 @@ public:
return (strtoul(caps.GetString(), nullptr, 16) & CLIENT_CAP_PROTO_SWITCH) != 0;
}
// 检查客户端是否支持远程切换线缆压缩(COMMAND_SET_COMPRESS,zstd / zlib)。
// 只有声明 CLIENT_CAP_COMPRESS_SWITCH 的 Windows 客户端认识。
bool SupportsCompressSwitch() const {
CString caps = GetClientData(ONLINELIST_CAPABILITIES);
if (caps.IsEmpty()) return false;
return (strtoul(caps.GetString(), nullptr, 16) & CLIENT_CAP_COMPRESS_SWITCH) != 0;
}
// 检查客户端是否支持收到 COMMAND_UPDATE 下发的 APK 后拉起系统安装器。
// 只有新版 Android 客户端声明;老 APK 收到命令会走 default 静默丢弃,
// 服务端据此提示「先手动装一次新版」。
+3 -1
View File
@@ -1123,6 +1123,8 @@
#define ID_PROTO_SWITCH_WSS 33100
// 高级菜单开关:把服务端 TLS 证书内嵌进下发给客户端的 ServerDll
#define ID_TOOL_EMBED_TLS_ANCHOR 33101
#define ID_EXPERIMENT_COMPRESS_ZSTD 33107
#define ID_EXPERIMENT_COMPRESS_ZLIB 33108
// 高级 → 运行测试程序:把内存里的 ServerDll 在主控进程内加载,本机上线一台测试主机
#define ID_TOOL_RUN_TEST 33106
#define ID_EXIT_FULLSCREEN 40001
@@ -1132,7 +1134,7 @@
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 418
#define _APS_NEXT_COMMAND_VALUE 33107
#define _APS_NEXT_COMMAND_VALUE 33109
#define _APS_NEXT_CONTROL_VALUE 2621
#define _APS_NEXT_SYMED_VALUE 105
#endif
+3
View File
@@ -0,0 +1,3 @@
; Host identity block for auxiliary tooling/diagnostics.
EXPORTS
HostIdentityBlock=?g_MasterID@@3PADA