Connect RCON to localhost by machine name

Connect to local host using machine name when ip is set to 'localhost'.
This commit is contained in:
Kawe Mazidjatari 2023-01-29 17:12:33 +01:00
parent 5f56c23af2
commit 8ed5a3a695
4 changed files with 32 additions and 11 deletions

View File

@ -83,6 +83,17 @@ bool CRConClient::Connect(void)
//-----------------------------------------------------------------------------
bool CRConClient::Connect(const char* szInAdr)
{
string svLocalHost;
if (strcmp(szInAdr, "localhost") == 0)
{
char szHostName[512];
if (!gethostname(szHostName, sizeof(szHostName)))
{
svLocalHost = fmt::format("[{:s}]:{:s}", szHostName, hostport->GetString());
szInAdr = svLocalHost.c_str();
}
}
if (!m_Address.SetFromString(szInAdr, true))
{
Warning(eDLL_T::CLIENT, "Failed to set RCON address: %s\n", szInAdr);
@ -113,9 +124,9 @@ void CRConClient::Disconnect(void)
// Purpose: send message
// Input : *svMessage -
//-----------------------------------------------------------------------------
void CRConClient::Send(const std::string& svMessage) const
void CRConClient::Send(const string& svMessage) const
{
std::ostringstream ssSendBuf;
ostringstream ssSendBuf;
const u_long nLen = htonl(svMessage.size());
ssSendBuf.write(reinterpret_cast<const char*>(&nLen), sizeof(u_long));
@ -195,7 +206,7 @@ void CRConClient::ProcessBuffer(const char* pRecvBuf, int nRecvLen, CConnectedNe
}
if (pData->m_nPayloadRead == pData->m_nPayloadLen)
{
this->ProcessMessage(this->Deserialize(std::string(
this->ProcessMessage(this->Deserialize(string(
reinterpret_cast<char*>(pData->m_RecvBuffer.data()), pData->m_nPayloadLen)));
pData->m_nPayloadLen = 0;
@ -275,7 +286,7 @@ void CRConClient::ProcessMessage(const sv_rcon::response& sv_response) const
// request_t -
// Output : serialized results as string
//-----------------------------------------------------------------------------
std::string CRConClient::Serialize(const std::string& svReqBuf, const std::string& svReqVal, const cl_rcon::request_t request_t) const
string CRConClient::Serialize(const string& svReqBuf, const string& svReqVal, const cl_rcon::request_t request_t) const
{
cl_rcon::request cl_request;
@ -305,7 +316,7 @@ std::string CRConClient::Serialize(const std::string& svReqBuf, const std::strin
// Input : *svBuf -
// Output : de-serialized object
//-----------------------------------------------------------------------------
sv_rcon::response CRConClient::Deserialize(const std::string& svBuf) const
sv_rcon::response CRConClient::Deserialize(const string& svBuf) const
{
sv_rcon::response sv_response;
sv_response.ParseFromArray(svBuf.data(), static_cast<int>(svBuf.size()));

View File

@ -19,14 +19,14 @@ public:
bool Connect(const char* szInAdr);
void Disconnect(void);
void Send(const std::string& svMessage) const;
void Send(const string& svMessage) const;
void Recv(void);
void ProcessBuffer(const char* pRecvBuf, int nRecvLen, CConnectedNetConsoleData* pData);
void ProcessMessage(const sv_rcon::response& sv_response) const;
std::string Serialize(const std::string& svReqBuf, const std::string& svReqVal, const cl_rcon::request_t request_t) const;
sv_rcon::response Deserialize(const std::string& svBuf) const;
string Serialize(const string& svReqBuf, const string& svReqVal, const cl_rcon::request_t request_t) const;
sv_rcon::response Deserialize(const string& svBuf) const;
bool IsInitialized(void) const;
bool IsConnected(void) const;

View File

@ -239,7 +239,17 @@ bool CNetCon::Connect(const std::string& svInAdr, const std::string& svInPort)
{
if (!svInAdr.empty() && !svInPort.empty()) // Construct from ip port
{
const string svFull = fmt::format("[{:s}]:{:s}", svInAdr, svInPort).c_str();
string svLocalHost;
if (svInAdr.compare("localhost") == 0)
{
char szHostName[512];
if (!gethostname(szHostName, sizeof(szHostName)))
{
svLocalHost = szHostName;
}
}
const string svFull = fmt::format("[{:s}]:{:s}", svLocalHost.empty() ? svInAdr : svLocalHost, svInPort);
if (!m_Address.SetFromString(svFull.c_str(), true))
{
Warning(eDLL_T::CLIENT, "Failed to set RCON address: %s\n", svFull.c_str());

View File

@ -72,8 +72,8 @@ void ConVar::Init(void)
cm_unset_all_cmdquery = ConVar::Create("cm_unset_all_cmdquery" , "0", FCVAR_DEVELOPMENTONLY | FCVAR_REPLICATED, "Returns false on every ConVar/ConCommand query ( !warning! ).", false, 0.f, false, 0.f, nullptr, nullptr);
rcon_address = ConVar::Create("rcon_address", "::", FCVAR_SERVER_CANNOT_QUERY | FCVAR_DONTRECORD | FCVAR_RELEASE, "Remote server access address.", false, 0.f, false, 0.f, nullptr, nullptr);
rcon_password = ConVar::Create("rcon_password", "" , FCVAR_SERVER_CANNOT_QUERY | FCVAR_DONTRECORD | FCVAR_RELEASE, "Remote server access password (rcon is disabled if empty).", false, 0.f, false, 0.f, &RCON_PasswordChanged_f, nullptr);
rcon_address = ConVar::Create("rcon_address", "localhost", FCVAR_SERVER_CANNOT_QUERY | FCVAR_DONTRECORD | FCVAR_RELEASE, "Remote server access address.", false, 0.f, false, 0.f, nullptr, nullptr);
rcon_password = ConVar::Create("rcon_password", "" , FCVAR_SERVER_CANNOT_QUERY | FCVAR_DONTRECORD | FCVAR_RELEASE, "Remote server access password (rcon is disabled if empty).", false, 0.f, false, 0.f, &RCON_PasswordChanged_f, nullptr);
r_debug_overlay_nodecay = ConVar::Create("r_debug_overlay_nodecay" , "0", FCVAR_DEVELOPMENTONLY | FCVAR_CHEAT, "Keeps all debug overlays alive regardless of their lifetime. Use command 'clear_debug_overlays' to clear everything.", false, 0.f, false, 0.f, nullptr, nullptr);
r_debug_overlay_invisible = ConVar::Create("r_debug_overlay_invisible" , "1", FCVAR_DEVELOPMENTONLY | FCVAR_CHEAT, "Show invisible debug overlays (alpha < 1 = 255).", false, 0.f, false, 0.f, nullptr, nullptr);