2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
// RCONServer.cpp
|
|
|
|
|
|
|
|
// Implements the cRCONServer class representing the RCON server
|
|
|
|
|
|
|
|
#include "Globals.h"
|
2014-10-23 09:15:10 -04:00
|
|
|
#include "IniFile.h"
|
2013-07-29 07:13:03 -04:00
|
|
|
#include "RCONServer.h"
|
|
|
|
#include "Server.h"
|
|
|
|
#include "Root.h"
|
|
|
|
#include "CommandOutput.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Disable MSVC warnings:
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:4355) // 'this' : used in base member initializer list
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
// Client -> Server:
|
|
|
|
RCON_PACKET_COMMAND = 2,
|
|
|
|
RCON_PACKET_LOGIN = 3,
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
// Server -> Client:
|
|
|
|
RCON_PACKET_RESPONSE = 2,
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-24 07:31:42 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cRCONListenCallbacks:
|
|
|
|
|
|
|
|
class cRCONListenCallbacks:
|
|
|
|
public cNetwork::cListenCallbacks
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cRCONListenCallbacks(cRCONServer & a_RCONServer, UInt16 a_Port):
|
|
|
|
m_RCONServer(a_RCONServer),
|
|
|
|
m_Port(a_Port)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/** The RCON server instance that we're attached to. */
|
|
|
|
cRCONServer & m_RCONServer;
|
|
|
|
|
|
|
|
/** The port for which this instance is responsible. */
|
|
|
|
UInt16 m_Port;
|
|
|
|
|
|
|
|
// cNetwork::cListenCallbacks overrides:
|
|
|
|
virtual cTCPLink::cCallbacksPtr OnIncomingConnection(const AString & a_RemoteIPAddress, UInt16 a_RemotePort) override
|
|
|
|
{
|
|
|
|
LOG("RCON Client \"%s\" connected!", a_RemoteIPAddress.c_str());
|
|
|
|
return std::make_shared<cRCONServer::cConnection>(m_RCONServer, a_RemoteIPAddress);
|
|
|
|
}
|
|
|
|
virtual void OnAccepted(cTCPLink & a_Link) override {}
|
|
|
|
virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override
|
|
|
|
{
|
2015-01-24 09:08:11 -05:00
|
|
|
LOGWARNING("RCON server error on port %d: %d (%s)", m_Port, a_ErrorCode, a_ErrorMsg.c_str());
|
2015-01-24 07:31:42 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-17 16:15:34 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2013-07-29 07:13:03 -04:00
|
|
|
// cRCONCommandOutput:
|
|
|
|
|
|
|
|
class cRCONCommandOutput :
|
|
|
|
public cCommandOutputCallback
|
|
|
|
{
|
|
|
|
public:
|
2015-01-25 10:33:49 -05:00
|
|
|
cRCONCommandOutput(cRCONServer::cConnection & a_Connection, UInt32 a_RequestID) :
|
2013-07-29 07:13:03 -04:00
|
|
|
m_Connection(a_Connection),
|
|
|
|
m_RequestID(a_RequestID)
|
|
|
|
{
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
// cCommandOutputCallback overrides:
|
|
|
|
virtual void Out(const AString & a_Text) override
|
|
|
|
{
|
|
|
|
m_Buffer.append(a_Text);
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
virtual void Finished(void) override
|
|
|
|
{
|
2015-01-25 10:33:49 -05:00
|
|
|
m_Connection.SendResponse(m_RequestID, RCON_PACKET_RESPONSE, static_cast<UInt32>(m_Buffer.size()), m_Buffer.c_str());
|
2013-07-29 07:13:03 -04:00
|
|
|
delete this;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
protected:
|
|
|
|
cRCONServer::cConnection & m_Connection;
|
2015-01-25 10:33:49 -05:00
|
|
|
UInt32 m_RequestID;
|
2013-07-29 07:13:03 -04:00
|
|
|
AString m_Buffer;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-17 16:15:34 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2013-07-29 07:13:03 -04:00
|
|
|
// cRCONServer:
|
|
|
|
|
|
|
|
cRCONServer::cRCONServer(cServer & a_Server) :
|
2015-01-24 07:31:42 -05:00
|
|
|
m_Server(a_Server)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cRCONServer::~cRCONServer()
|
|
|
|
{
|
2015-01-24 07:31:42 -05:00
|
|
|
for (auto srv: m_ListenServers)
|
|
|
|
{
|
|
|
|
srv->Close();
|
|
|
|
}
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-14 10:47:51 -04:00
|
|
|
void cRCONServer::Initialize(cSettingsRepositoryInterface & a_Settings)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2015-05-14 10:47:51 -04:00
|
|
|
if (!a_Settings.GetValueSetB("RCON", "Enabled", false))
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the password, don't allow an empty one:
|
2015-05-14 10:47:51 -04:00
|
|
|
m_Password = a_Settings.GetValueSet("RCON", "Password", "");
|
2013-07-29 07:13:03 -04:00
|
|
|
if (m_Password.empty())
|
|
|
|
{
|
|
|
|
LOGWARNING("RCON is requested, but the password is not set. RCON is now disabled.");
|
|
|
|
return;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-01-24 07:31:42 -05:00
|
|
|
// Read the listening ports for RCON from config:
|
2015-05-14 10:47:51 -04:00
|
|
|
AStringVector Ports = ReadUpgradeIniPorts(a_Settings, "RCON", "Ports", "PortsIPv4", "PortsIPv6", "25575");
|
2015-01-24 07:31:42 -05:00
|
|
|
|
|
|
|
// Start listening on each specified port:
|
|
|
|
for (auto port: Ports)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2015-01-25 10:25:15 -05:00
|
|
|
UInt16 PortNum;
|
|
|
|
if (!StringToInteger(port, PortNum))
|
2015-01-24 07:31:42 -05:00
|
|
|
{
|
|
|
|
LOGINFO("Invalid RCON port value: \"%s\". Ignoring.", port.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto Handle = cNetwork::Listen(PortNum, std::make_shared<cRCONListenCallbacks>(*this, PortNum));
|
|
|
|
if (Handle->IsListening())
|
|
|
|
{
|
|
|
|
m_ListenServers.push_back(Handle);
|
|
|
|
}
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
2015-01-24 07:31:42 -05:00
|
|
|
|
|
|
|
if (m_ListenServers.empty())
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2015-01-24 07:31:42 -05:00
|
|
|
LOGWARNING("RCON is enabled but no valid ports were found. RCON is not accessible.");
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-24 07:31:42 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cRCONServer::cConnection:
|
2013-07-29 07:13:03 -04:00
|
|
|
|
2015-01-24 07:31:42 -05:00
|
|
|
cRCONServer::cConnection::cConnection(cRCONServer & a_RCONServer, const AString & a_IPAddress) :
|
|
|
|
m_IsAuthenticated(false),
|
|
|
|
m_RCONServer(a_RCONServer),
|
|
|
|
m_IPAddress(a_IPAddress)
|
|
|
|
{
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-24 07:31:42 -05:00
|
|
|
void cRCONServer::cConnection::OnLinkCreated(cTCPLinkPtr a_Link)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2015-01-24 07:31:42 -05:00
|
|
|
m_Link = a_Link;
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-24 07:31:42 -05:00
|
|
|
void cRCONServer::cConnection::OnReceivedData(const char * a_Data, size_t a_Size)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2015-01-24 07:31:42 -05:00
|
|
|
ASSERT(m_Link != nullptr);
|
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
// Append data to the buffer:
|
|
|
|
m_Buffer.append(a_Data, a_Size);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
// Process the packets in the buffer:
|
|
|
|
while (m_Buffer.size() >= 14)
|
|
|
|
{
|
2015-01-25 10:33:49 -05:00
|
|
|
UInt32 Length = UIntFromBuffer(m_Buffer.data());
|
2013-07-29 07:13:03 -04:00
|
|
|
if (Length > 1500)
|
|
|
|
{
|
|
|
|
// Too long, drop the connection
|
|
|
|
LOGWARNING("Received an invalid RCON packet length (%d), dropping RCON connection to %s.",
|
|
|
|
Length, m_IPAddress.c_str()
|
|
|
|
);
|
2015-01-24 07:31:42 -05:00
|
|
|
m_Link->Close();
|
|
|
|
m_Link.reset();
|
|
|
|
return;
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
2015-01-25 12:07:51 -05:00
|
|
|
if (Length > static_cast<UInt32>(m_Buffer.size() + 4))
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
// Incomplete packet yet, wait for more data to come
|
2015-01-24 07:31:42 -05:00
|
|
|
return;
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-01-25 10:33:49 -05:00
|
|
|
UInt32 RequestID = UIntFromBuffer(m_Buffer.data() + 4);
|
|
|
|
UInt32 PacketType = UIntFromBuffer(m_Buffer.data() + 8);
|
2013-07-29 07:13:03 -04:00
|
|
|
if (!ProcessPacket(RequestID, PacketType, Length - 10, m_Buffer.data() + 12))
|
|
|
|
{
|
2015-01-24 07:31:42 -05:00
|
|
|
m_Link->Close();
|
|
|
|
m_Link.reset();
|
|
|
|
return;
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
m_Buffer.erase(0, Length + 4);
|
|
|
|
} // while (m_Buffer.size() >= 14)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-24 07:31:42 -05:00
|
|
|
void cRCONServer::cConnection::OnRemoteClosed(void)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2015-01-24 07:31:42 -05:00
|
|
|
m_Link.reset();
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-24 07:31:42 -05:00
|
|
|
void cRCONServer::cConnection::OnError(int a_ErrorCode, const AString & a_ErrorMsg)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2015-01-24 07:31:42 -05:00
|
|
|
LOGD("Error in RCON connection %s: %d (%s)", m_IPAddress.c_str(), a_ErrorCode, a_ErrorMsg.c_str());
|
|
|
|
m_Link.reset();
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-25 10:33:49 -05:00
|
|
|
bool cRCONServer::cConnection::ProcessPacket(UInt32 a_RequestID, UInt32 a_PacketType, UInt32 a_PayloadLength, const char * a_Payload)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
switch (a_PacketType)
|
|
|
|
{
|
|
|
|
case RCON_PACKET_LOGIN:
|
|
|
|
{
|
|
|
|
if (strncmp(a_Payload, m_RCONServer.m_Password.c_str(), a_PayloadLength) != 0)
|
|
|
|
{
|
|
|
|
LOGINFO("RCON: Invalid password from client %s, dropping connection.", m_IPAddress.c_str());
|
2015-01-25 10:33:49 -05:00
|
|
|
SendResponse(0xffffffffU, RCON_PACKET_RESPONSE, 0, nullptr);
|
2013-07-29 07:13:03 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_IsAuthenticated = true;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
LOGD("RCON: Client at %s has successfully authenticated", m_IPAddress.c_str());
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
// Send OK response:
|
2014-10-20 16:55:07 -04:00
|
|
|
SendResponse(a_RequestID, RCON_PACKET_RESPONSE, 0, nullptr);
|
2013-07-29 07:13:03 -04:00
|
|
|
return true;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
case RCON_PACKET_COMMAND:
|
|
|
|
{
|
|
|
|
if (!m_IsAuthenticated)
|
|
|
|
{
|
|
|
|
char AuthNeeded[] = "You need to authenticate first!";
|
|
|
|
SendResponse(a_RequestID, RCON_PACKET_RESPONSE, sizeof(AuthNeeded), AuthNeeded);
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
AString cmd(a_Payload, a_PayloadLength);
|
|
|
|
LOGD("RCON command from %s: \"%s\"", m_IPAddress.c_str(), cmd.c_str());
|
|
|
|
cRoot::Get()->ExecuteConsoleCommand(cmd, *(new cRCONCommandOutput(*this, a_RequestID)));
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
// Send an empty response:
|
2014-10-20 16:55:07 -04:00
|
|
|
SendResponse(a_RequestID, RCON_PACKET_RESPONSE, 0, nullptr);
|
2013-07-29 07:13:03 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
// Unknown packet type, drop the connection:
|
|
|
|
LOGWARNING("RCON: Client at %s has sent an unknown packet type %d, dropping connection.",
|
|
|
|
m_IPAddress.c_str(), a_PacketType
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-25 10:33:49 -05:00
|
|
|
UInt32 cRCONServer::cConnection::UIntFromBuffer(const char * a_Buffer)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2015-01-25 10:33:49 -05:00
|
|
|
const Byte * Buffer = reinterpret_cast<const Byte *>(a_Buffer);
|
2015-07-29 11:04:03 -04:00
|
|
|
return static_cast<UInt32>((Buffer[3] << 24) | (Buffer[2] << 16) | (Buffer[1] << 8) | Buffer[0]);
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-25 10:33:49 -05:00
|
|
|
void cRCONServer::cConnection::UIntToBuffer(UInt32 a_Value, char * a_Buffer)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2015-01-25 10:33:49 -05:00
|
|
|
a_Buffer[0] = static_cast<char>(a_Value & 0xff);
|
|
|
|
a_Buffer[1] = static_cast<char>((a_Value >> 8) & 0xff);
|
|
|
|
a_Buffer[2] = static_cast<char>((a_Value >> 16) & 0xff);
|
|
|
|
a_Buffer[3] = static_cast<char>((a_Value >> 24) & 0xff);
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-25 10:33:49 -05:00
|
|
|
void cRCONServer::cConnection::SendResponse(UInt32 a_RequestID, UInt32 a_PacketType, UInt32 a_PayloadLength, const char * a_Payload)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
ASSERT((a_PayloadLength == 0) || (a_Payload != nullptr)); // Either zero data to send, or a valid payload ptr
|
2015-01-24 07:31:42 -05:00
|
|
|
ASSERT(m_Link != nullptr);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-01-25 10:33:49 -05:00
|
|
|
char Buffer[12];
|
|
|
|
UInt32 Length = a_PayloadLength + 10;
|
|
|
|
UIntToBuffer(Length, Buffer);
|
|
|
|
UIntToBuffer(a_RequestID, Buffer + 4);
|
|
|
|
UIntToBuffer(a_PacketType, Buffer + 8);
|
|
|
|
m_Link->Send(Buffer, 12);
|
2013-07-29 07:13:03 -04:00
|
|
|
if (a_PayloadLength > 0)
|
|
|
|
{
|
2015-01-24 07:31:42 -05:00
|
|
|
m_Link->Send(a_Payload, a_PayloadLength);
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
2015-01-24 07:31:42 -05:00
|
|
|
m_Link->Send("\0", 2); // Send two zero chars as the padding
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|