From f059202ff2a5fb6a7ff959515a899080f9ec05b0 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Tue, 16 Apr 2024 16:51:51 +0200 Subject: [PATCH] NetConsole: fix command line bug Recently, encryption was implemented in the RCON system, but the command line initialization code wasn't adapted to this new logic. Code has now been adapted. --- src/netconsole/netconsole.cpp | 97 ++++++++++++++++++++++++----------- src/netconsole/netconsole.h | 7 ++- 2 files changed, 72 insertions(+), 32 deletions(-) diff --git a/src/netconsole/netconsole.cpp b/src/netconsole/netconsole.cpp index 0543388a..4503e22b 100644 --- a/src/netconsole/netconsole.cpp +++ b/src/netconsole/netconsole.cpp @@ -24,7 +24,7 @@ CNetCon::CNetCon(void) : m_bInitialized(false) , m_bQuitting(false) , m_bPromptConnect(true) - , m_bEncryptFrames(true) + , m_bEncryptFrames(false) , m_flTickInterval(0.05f) { // Empty character set used for ip addresses if we still need to initiate a @@ -44,7 +44,7 @@ CNetCon::~CNetCon(void) // Purpose: WSA and NETCON systems init // Output : true on success, false otherwise //----------------------------------------------------------------------------- -bool CNetCon::Init(const bool bAnsiColor, const char* pHostName, const int nPort) +bool CNetCon::Init(const bool bAnsiColor, const char* pAdr, const char* pKey) { std::lock_guard l(m_Mutex); @@ -60,12 +60,20 @@ bool CNetCon::Init(const bool bAnsiColor, const char* pHostName, const int nPort return false; } - // Try to connect from given parameters, these are passed in from the - // command line. If we fail, return out as this allows the user to - // quickly retry again. - if (pHostName && nPort != SOCKET_ERROR) + // Install the encryption key and enable encryption if this has been passed + // in by the user. + if (pKey) { - if (!Connect(pHostName, nPort)) + SetKey(pKey, true); + m_bEncryptFrames = true; + } + + // Try and connect to the giver remote address if this has been passed in + // by the user. If we fail, return out as this allows the user to quickly + // try again. + if (pAdr) + { + if (!Connect(pAdr)) { return false; } @@ -176,6 +184,24 @@ void CNetCon::TermSetup(const bool bAnsiColor) SpdLog_InstallSupplementalLogger("supplemental_logger_mt", "netconsole.log"); } +//----------------------------------------------------------------------------- +// Purpose: tries to set the passed in netkey, falls back to default on failure +//----------------------------------------------------------------------------- +void CNetCon::TrySetKey(const char* const pKey) +{ + if (!*pKey) + { + Warning(eDLL_T::CLIENT, "No key provided; using default %s'%s%s%s'\n", + g_svReset, g_svGreyB, DEFAULT_NET_ENCRYPTION_KEY, g_svReset); + + SetKey(DEFAULT_NET_ENCRYPTION_KEY, true); + } + else + { + SetKey(pKey, true); + } +} + //----------------------------------------------------------------------------- // Purpose: gets input IP and port for initialization //----------------------------------------------------------------------------- @@ -254,23 +280,9 @@ void CNetCon::RunInput(const string& lineInput) return; } - if (!*inKey) - { - Warning(eDLL_T::CLIENT, "No key provided; using default %s'%s%s%s'\n", - g_svReset, g_svGreyB, DEFAULT_NET_ENCRYPTION_KEY, g_svReset); - - SetKey(DEFAULT_NET_ENCRYPTION_KEY, true); - } - else - { - SetKey(inKey, true); - } - + TrySetKey(inKey); m_bEncryptFrames = true; - Msg(eDLL_T::CLIENT, "Attempting connection to '%s' with key %s'%s%s%s'\n", - inAdr, g_svReset, g_svGreyB, GetKey(), g_svReset); - if (!Connect(inAdr)) { SetPrompting(true); @@ -282,8 +294,6 @@ void CNetCon::RunInput(const string& lineInput) const char* inAdr = cmd.GetCommandString(); m_bEncryptFrames = false; - Msg(eDLL_T::CLIENT, "Attempting connection to '%s'\n", inAdr); - if (!Connect(inAdr)) { SetPrompting(true); @@ -354,6 +364,28 @@ void CNetCon::SetPrompting(const bool bPrompt) m_bPromptConnect = bPrompt; } +//----------------------------------------------------------------------------- +// Purpose: connect to remote +// Output : true on success, false otherwise +//----------------------------------------------------------------------------- +bool CNetCon::Connect(const char* pHostName, const int nPort) +{ + Assert(nPort == SOCKET_ERROR, "Port should be part of the address on the CNetCon implementation!"); + NOTE_UNUSED(nPort); + + if (m_bEncryptFrames) + { + Msg(eDLL_T::CLIENT, "Attempting connection to '%s' with key %s'%s%s%s'\n", + pHostName, g_svReset, g_svGreyB, GetKey(), g_svReset); + } + else + { + Msg(eDLL_T::CLIENT, "Attempting connection to '%s'\n", pHostName); + } + + return CL_NetConConnect(this, pHostName, SOCKET_ERROR); +} + //----------------------------------------------------------------------------- // Purpose: disconnect from current session // Input : *szReason - @@ -487,16 +519,21 @@ int main(int argc, char* argv[]) } } - const char* pHostName = nullptr; - int nPort = SOCKET_ERROR; + // The address and key from command line if passed in. + const char* pAdr = nullptr; + const char* pKey = nullptr; - if (argc >= 3) // Get IP and Port from command line. + if (argc >= 2) { - pHostName = argv[1]; - nPort = atoi(argv[2]); + pAdr = argv[1]; } - if (!NetConsole()->Init(bEnableColor, pHostName, nPort)) + if (argc >= 3) + { + pKey = argv[2]; + } + + if (!NetConsole()->Init(bEnableColor, pAdr, pKey)) { return EXIT_FAILURE; } diff --git a/src/netconsole/netconsole.h b/src/netconsole/netconsole.h index 6a9f8b73..ef2f5f91 100644 --- a/src/netconsole/netconsole.h +++ b/src/netconsole/netconsole.h @@ -16,7 +16,7 @@ public: CNetCon(void); ~CNetCon(void); - bool Init(const bool bAnsiColor, const char* pHostName = nullptr, const int nPort = SOCKET_ERROR); + bool Init(const bool bAnsiColor, const char* pAdr = nullptr, const char* pKey = nullptr); bool Shutdown(void); void TermSetup(const bool bAnsiColor); @@ -32,9 +32,12 @@ public: inline float GetTickInterval() const { return m_flTickInterval; } static BOOL WINAPI CloseHandler(DWORD eventCode); - virtual void Disconnect(const char* szReason = nullptr); + virtual bool Connect(const char* pHostName, const int nHostPort = SOCKET_ERROR) override; + virtual void Disconnect(const char* szReason = nullptr) override; + virtual bool ProcessMessage(const char* pMsgBuf, const int nMsgLen) override; + void TrySetKey(const char* const pKey); bool Serialize(vector& vecBuf, const char* szReqBuf, const char* szReqVal, const netcon::request_e requestType) const;