From e779fb0b510027f32e3878bda1305efb6341837a Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Wed, 13 Aug 2025 04:54:33 +0800 Subject: [PATCH] Improve: Enable zstd multi-thread compression for client --- Dependencies.md | 2 +- client/ClientDll_vs2015.vcxproj | 2 ++ client/IOCPClient.cpp | 19 +++++++++---- client/ScreenManager.cpp | 2 ++ client/ghost_vs2015.vcxproj | 2 ++ common/zstd_wrapper.c | 42 +++++++++++++++++++++++++++++ common/zstd_wrapper.h | 31 +++++++++++++++++++++ server/2015Remote/2015RemoteDlg.cpp | 2 +- 8 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 common/zstd_wrapper.c create mode 100644 common/zstd_wrapper.h diff --git a/Dependencies.md b/Dependencies.md index c502e68..09535a8 100644 --- a/Dependencies.md +++ b/Dependencies.md @@ -12,5 +12,5 @@ ## lib -- [HPSocket vv6.0.3](https://github.com/ldcsaa/HP-Socket) +- [HPSocket v6.0.3](https://github.com/ldcsaa/HP-Socket) - [shrink v0.0.1](https://github.com/yuanyuanxiang/PrivateRemoter/tree/master/shrink) diff --git a/client/ClientDll_vs2015.vcxproj b/client/ClientDll_vs2015.vcxproj index fcd97e0..577cc85 100644 --- a/client/ClientDll_vs2015.vcxproj +++ b/client/ClientDll_vs2015.vcxproj @@ -162,6 +162,7 @@ + @@ -196,6 +197,7 @@ + diff --git a/client/IOCPClient.cpp b/client/IOCPClient.cpp index 3700f32..e72f1a9 100644 --- a/client/IOCPClient.cpp +++ b/client/IOCPClient.cpp @@ -23,7 +23,7 @@ inline int WSAGetLastError() { return -1; } #define Z_FAILED(p) (Z_OK != (p)) #define Z_SUCCESS(p) (!Z_FAILED(p)) #else -#include "zstd/zstd.h" +#include "common/zstd_wrapper.h" #ifdef _WIN64 #pragma comment(lib, "zstd/zstd_x64.lib") #else @@ -31,9 +31,9 @@ inline int WSAGetLastError() { return -1; } #endif #define Z_FAILED(p) ZSTD_isError(p) #define Z_SUCCESS(p) (!Z_FAILED(p)) -#define ZSTD_CLEVEL 5 +#define ZSTD_CLEVEL ZSTD_CLEVEL_DEFAULT #if USING_CTX -#define compress(dest, destLen, source, sourceLen) ZSTD_compress2(m_Cctx, dest, *(destLen), source, sourceLen) +#define compress(dest, destLen, source, sourceLen) zstd_compress_auto(m_Cctx, dest, *(destLen), source, sourceLen, 1024*1024) #define uncompress(dest, destLen, source, sourceLen) ZSTD_decompressDCtx(m_Dctx, dest, *(destLen), source, sourceLen) #else #define compress(dest, destLen, source, sourceLen) ZSTD_compress(dest, *(destLen), source, sourceLen, ZSTD_CLEVEL_DEFAULT) @@ -114,7 +114,15 @@ IOCPClient::IOCPClient(const State&bExit, bool exit_while_disconnect, int mask, #if USING_CTX m_Cctx = ZSTD_createCCtx(); m_Dctx = ZSTD_createDCtx(); + auto n = ZSTD_CCtx_setParameter(m_Cctx, ZSTD_c_nbWorkers, 4); + if (Z_FAILED(n)) { + ZSTD_CCtx_setParameter(m_Cctx, ZSTD_c_nbWorkers, 0); + } ZSTD_CCtx_setParameter(m_Cctx, ZSTD_c_compressionLevel, ZSTD_CLEVEL); + ZSTD_CCtx_setParameter(m_Cctx, ZSTD_c_hashLog, 15); + ZSTD_CCtx_setParameter(m_Cctx, ZSTD_c_chainLog, 16); + ZSTD_CCtx_setParameter(m_Cctx, ZSTD_c_searchLog, 1); + ZSTD_CCtx_setParameter(m_Cctx, ZSTD_c_windowLog, 19); #endif } @@ -446,7 +454,7 @@ VOID IOCPClient::OnServerReceiving(CBuffer* m_CompressedBuffer, char* szBuffer, // 关闭压缩开关时,SendWithSplit比较耗时。 BOOL IOCPClient::OnServerSending(const char* szBuffer, ULONG ulOriginalLength, PkgMask* mask) //Hello { - AUTO_TICK(50); + AUTO_TICK(40); assert (ulOriginalLength > 0); { int cmd = BYTE(szBuffer[0]); @@ -487,6 +495,7 @@ BOOL IOCPClient::OnServerSending(const char* szBuffer, ULONG ulOriginalLength, P if (CompressedBuffer != buf) delete [] CompressedBuffer; + STOP_TICK; // 分块发送 return SendWithSplit((char*)m_WriteBuffer.GetBuffer(), m_WriteBuffer.GetBufferLength(), MAX_SEND_BUFFER, cmd, mask); } @@ -495,6 +504,7 @@ BOOL IOCPClient::OnServerSending(const char* szBuffer, ULONG ulOriginalLength, P // 5 2 // 2 2 1 BOOL IOCPClient::SendWithSplit(const char* src, ULONG srcSize, ULONG ulSplitLength, int cmd, PkgMask* mask) { + AUTO_TICK(50); if (src == nullptr || srcSize == 0 || ulSplitLength == 0) return FALSE; // Mask @@ -505,7 +515,6 @@ BOOL IOCPClient::SendWithSplit(const char* src, ULONG srcSize, ULONG ulSplitLeng if(szBuffer != src && srcSize > ulSplitLength){ Mprintf("SendWithSplit: %d bytes large packet may causes issues.\n", srcSize); } - AUTO_TICK(25); bool isFail = false; int iReturn = 0; //真正发送了多少 const char* Travel = szBuffer; diff --git a/client/ScreenManager.cpp b/client/ScreenManager.cpp index 2856179..038f305 100644 --- a/client/ScreenManager.cpp +++ b/client/ScreenManager.cpp @@ -232,6 +232,7 @@ DWORD WINAPI CScreenManager::WorkThreadProc(LPVOID lParam) s0 = (s0 <= sleep*4) ? s0*alpha : s0; c1 = 0; #ifdef _DEBUG + if (1000./s0>1.0) Mprintf("[+]SendScreen Span= %dms, s0= %f, fps= %f\n", span, s0, 1000./s0); #endif } @@ -241,6 +242,7 @@ DWORD WINAPI CScreenManager::WorkThreadProc(LPVOID lParam) s0 = (s0 >= sleep/4) ? s0/alpha : s0; c2 = 0; #ifdef _DEBUG + if (1000./s0<20.0) Mprintf("[-]SendScreen Span= %dms, s0= %f, fps= %f\n", span, s0, 1000./s0); #endif } diff --git a/client/ghost_vs2015.vcxproj b/client/ghost_vs2015.vcxproj index f0909ca..bd47ba0 100644 --- a/client/ghost_vs2015.vcxproj +++ b/client/ghost_vs2015.vcxproj @@ -172,6 +172,7 @@ + @@ -206,6 +207,7 @@ + diff --git a/common/zstd_wrapper.c b/common/zstd_wrapper.c new file mode 100644 index 0000000..61502e5 --- /dev/null +++ b/common/zstd_wrapper.c @@ -0,0 +1,42 @@ +#include "zstd_wrapper.h" +#include // memcpy + +size_t zstd_compress_auto( + ZSTD_CCtx* cctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + size_t threshold +) { + // 妫鏌ヨ緭鍏ユ湁鏁堟 + if (!cctx || !dst || !src) { + return ZSTD_error_GENERIC; + } + + // --- 灏忔暟鎹垨搴撲笉鏀寔澶氱嚎绋 鈫 閫鍥炲埌鍗曠嚎绋 ZSTD_compress2 --- + if (srcSize < threshold) { + return ZSTD_compress2(cctx, dst, dstCapacity, src, srcSize); + } + + // --- 澶氱嚎绋嬫祦寮忓帇缂 --- + ZSTD_inBuffer input = {src, srcSize, 0}; + ZSTD_outBuffer output = {dst, dstCapacity, 0}; + + // 寰幆鍘嬬缉杈撳叆鏁版嵁 + size_t ret = 0; + while (input.pos < input.size) { + ret = ZSTD_compressStream2(cctx, &output, &input, ZSTD_e_continue); + if (ZSTD_isError(ret)) break; + + // 杈撳嚭缂撳啿鍖哄凡婊★紙鐞嗚涓婁笉搴斿彂鐢燂紝鍥 dstCapacity >= ZSTD_compressBound锛 + if (output.pos == output.size) { + return ZSTD_error_dstSize_tooSmall; + } + } + + // 缁撴潫鍘嬬缉锛堢‘淇濇墍鏈夌嚎绋嬪畬鎴愶級 + if (!ZSTD_isError(ret)) { + ret = ZSTD_compressStream2(cctx, &output, &input, ZSTD_e_end); + } + + return ZSTD_isError(ret) ? ret : output.pos; +} diff --git a/common/zstd_wrapper.h b/common/zstd_wrapper.h new file mode 100644 index 0000000..cdc61b6 --- /dev/null +++ b/common/zstd_wrapper.h @@ -0,0 +1,31 @@ +#ifndef ZSTD_WRAPPER_H +#define ZSTD_WRAPPER_H + +#include "zstd/zstd.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * 鏅鸿兘鍘嬬缉鍑芥暟锛堣嚜鍔ㄩ夋嫨鍗曠嚎绋/澶氱嚎绋嬶級 + * @param cctx 鍘嬬缉涓婁笅鏂囷紙闇鎻愬墠鍒涘缓锛 + * @param dst 杈撳嚭缂撳啿鍖 + * @param dstCapacity 杈撳嚭缂撳啿鍖哄ぇ灏 + * @param src 杈撳叆鏁版嵁 + * @param srcSize 杈撳叆鏁版嵁澶у皬 + * @param threshold 瑙﹀彂澶氱嚎绋嬬殑鏈灏忔暟鎹ぇ灏忥紙寤鸿 >= 1MB锛 + * @return 鍘嬬缉鍚庣殑鏁版嵁澶у皬锛堥敊璇爜閫氳繃 ZSTD_isError() 妫鏌ワ級 + */ +size_t zstd_compress_auto( + ZSTD_CCtx* cctx, + void* dst, size_t dstCapacity, + const void* src, size_t srcSize, + size_t threshold +); + +#ifdef __cplusplus +} +#endif + +#endif // ZSTD_WRAPPER_H diff --git a/server/2015Remote/2015RemoteDlg.cpp b/server/2015Remote/2015RemoteDlg.cpp index 3b48278..8e91393 100644 --- a/server/2015Remote/2015RemoteDlg.cpp +++ b/server/2015Remote/2015RemoteDlg.cpp @@ -3071,7 +3071,7 @@ int main() { */ void CMy2015RemoteDlg::OnToolGenShellcode() { - CFileDialog fileDlg(TRUE, _T("dll"), "ServerDll.dll", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, + CFileDialog fileDlg(TRUE, _T("dll"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("DLL Files (*.dll)|*.dll|All Files (*.*)|*.*||"), AfxGetMainWnd()); int ret = 0; try {