490 lines
17 KiB
C++
Raw Normal View History

//=====================================================================================//
//
// Purpose: Implementation of the pylon server backend.
//
// $NoKeywords: $
//=====================================================================================//
#include <core/stdafx.h>
#include <tier1/cvar.h>
#include <tier2/curlutils.h>
#include <networksystem/pylon.h>
#ifndef CLIENT_DLL
#include <engine/server/server.h>
#endif // !CLIENT_DLL
2022-07-01 10:29:27 +02:00
//-----------------------------------------------------------------------------
// Purpose: returns a vector of hosted servers.
// Input : &svOutMessage -
// Output : vector<NetGameServer_t>
2022-07-01 10:29:27 +02:00
//-----------------------------------------------------------------------------
vector<NetGameServer_t> CPylon::GetServerList(string& svOutMessage) const
2022-07-01 10:29:27 +02:00
{
vector<NetGameServer_t> vslList;
2022-07-01 10:29:27 +02:00
nlohmann::json jsRequestBody = nlohmann::json::object();
jsRequestBody["version"] = SDK_VERSION;
string svRequestBody = jsRequestBody.dump(4);
string svResponse;
2022-07-01 10:29:27 +02:00
if (pylon_showdebuginfo->GetBool())
2022-07-01 10:29:27 +02:00
{
DevMsg(eDLL_T::ENGINE, "%s - Sending server list request to comp-server:\n%s\n", __FUNCTION__, svRequestBody.c_str());
}
CURLINFO status;
if (!QueryMasterServer(pylon_matchmaking_hostname->GetString(), "/servers", svRequestBody, svResponse, svOutMessage, status))
2022-07-01 10:29:27 +02:00
{
return vslList;
2022-07-01 10:29:27 +02:00
}
try
2022-07-01 10:29:27 +02:00
{
if (status == 200) // STATUS_OK
2022-07-01 10:29:27 +02:00
{
nlohmann::json jsResultBody = nlohmann::json::parse(svResponse);
if (jsResultBody["success"].is_boolean() && jsResultBody["success"].get<bool>())
2022-07-01 10:29:27 +02:00
{
for (auto& obj : jsResultBody["servers"])
{
vslList.push_back(
NetGameServer_t
{
obj.value("name",""),
obj.value("description",""),
obj.value("hidden","false") == "true",
obj.value("map",""),
obj.value("playlist",""),
obj.value("ip",""),
obj.value("port", ""),
obj.value("key",""),
obj.value("checksum",""),
obj.value("version", SDK_VERSION),
obj.value("playerCount", ""),
obj.value("maxPlayers", ""),
obj.value("timeStamp", 0),
obj.value("publicRef", ""),
obj.value("cachedId", ""),
}
);
}
2022-07-01 10:29:27 +02:00
}
else
{
if (jsResultBody["error"].is_string())
2022-07-01 10:29:27 +02:00
{
svOutMessage = jsResultBody["error"].get<string>();
2022-07-01 10:29:27 +02:00
}
else
{
svOutMessage = string("Unknown error with status: ") + std::to_string(status);
}
}
}
else
{
if (status)
{
if (!svResponse.empty())
{
nlohmann::json jsResultBody = nlohmann::json::parse(svResponse);
if (jsResultBody["error"].is_string())
{
svOutMessage = jsResultBody["error"].get<string>();
}
else
{
svOutMessage = string("Failed HTTP request: ") + std::to_string(status);
}
return vslList;
2022-07-01 10:29:27 +02:00
}
svOutMessage = string("Failed HTTP request: ") + std::to_string(status);
2022-07-01 10:29:27 +02:00
return vslList;
}
2022-08-29 00:29:32 +02:00
svOutMessage = "Failed to reach comp-server: connection timed-out";
2022-07-01 10:29:27 +02:00
return vslList;
}
}
catch (const std::exception& ex)
{
Warning(eDLL_T::ENGINE, "%s - Exception while parsing comp-server response:\n%s\n", __FUNCTION__, ex.what());
2022-07-01 10:29:27 +02:00
}
return vslList;
}
//-----------------------------------------------------------------------------
// Purpose: Gets the server by token string.
// Input : &slOutServer -
// &svOutMessage -
// &svToken -
2022-07-01 10:29:27 +02:00
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CPylon::GetServerByToken(NetGameServer_t& slOutServer, string& svOutMessage, const string& svToken) const
2022-07-01 10:29:27 +02:00
{
nlohmann::json jsRequestBody = nlohmann::json::object();
jsRequestBody["token"] = svToken;
2022-07-01 10:29:27 +02:00
string svRequestBody = jsRequestBody.dump(4);
string svResponseBuf;
const bool bDebugLog = pylon_showdebuginfo->GetBool();
if (bDebugLog)
2022-07-01 10:29:27 +02:00
{
DevMsg(eDLL_T::ENGINE, "%s - Sending token connect request to comp-server:\n%s\n", __FUNCTION__, svRequestBody.c_str());
2022-07-01 10:29:27 +02:00
}
CURLINFO status;
if (!QueryMasterServer(pylon_matchmaking_hostname->GetString(), "/server/byToken", svRequestBody, svResponseBuf, svOutMessage, status))
{
return false;
}
if (bDebugLog)
2022-07-01 10:29:27 +02:00
{
DevMsg(eDLL_T::ENGINE, "%s - Comp-server replied with status: '%d'\n", __FUNCTION__, status);
try
{
string jsResultBody = nlohmann::json::parse(svResponseBuf).dump(4);
DevMsg(eDLL_T::ENGINE, "%s - Comp-server response body:\n%s\n", __FUNCTION__, jsResultBody.c_str());
}
catch (const std::exception& ex)
{
DevMsg(eDLL_T::ENGINE, "%s - Encountered error parsing Comp-server response body: '%s'\n", __FUNCTION__, ex.what());
}
2022-07-01 10:29:27 +02:00
}
try
2022-07-01 10:29:27 +02:00
{
if (status == 200) // STATUS_OK
2022-07-01 10:29:27 +02:00
{
if (!svResponseBuf.empty())
2022-07-01 10:29:27 +02:00
{
nlohmann::json jsResultBody = nlohmann::json::parse(svResponseBuf);
2022-07-01 10:29:27 +02:00
if (jsResultBody["success"].is_boolean() && jsResultBody["success"])
2022-07-01 10:29:27 +02:00
{
slOutServer = NetGameServer_t
{
jsResultBody["server"].value("name",""),
jsResultBody["server"].value("description",""),
jsResultBody["server"].value("hidden","false") == "true",
jsResultBody["server"].value("map",""),
jsResultBody["server"].value("playlist",""),
jsResultBody["server"].value("ip",""),
jsResultBody["server"].value("port", ""),
jsResultBody["server"].value("key",""),
jsResultBody["server"].value("checksum",""),
jsResultBody["server"].value("version", SDK_VERSION),
jsResultBody["server"].value("playerCount", ""),
jsResultBody["server"].value("maxPlayers", ""),
jsResultBody["server"].value("timeStamp", 0),
jsResultBody["server"].value("publicRef", ""),
jsResultBody["server"].value("cachedId", ""),
};
return true;
2022-07-01 10:29:27 +02:00
}
else
{
if (jsResultBody["error"].is_string())
{
svOutMessage = jsResultBody["error"].get<string>();
}
else
{
svOutMessage = string("Unknown error with status: ") + std::to_string(status);
}
slOutServer = NetGameServer_t{};
return false;
}
}
}
else
{
if (!svResponseBuf.empty())
{
nlohmann::json jsResultBody = nlohmann::json::parse(svResponseBuf);
if (jsResultBody["error"].is_string())
{
svOutMessage = jsResultBody["error"].get<string>();
}
else
{
svOutMessage = string("Server not found: ") + std::to_string(status);
2022-07-01 10:29:27 +02:00
}
return false;
}
svOutMessage = string("Failed HTTP request: ") + std::to_string(status);
return false;
2022-08-29 00:29:32 +02:00
svOutMessage = "Failed to reach comp-server: connection timed-out";
slOutServer = NetGameServer_t{};
2022-07-01 10:29:27 +02:00
return false;
}
}
catch (const std::exception& ex)
{
Warning(eDLL_T::ENGINE, "%s - Exception while parsing comp-server response:\n%s\n", __FUNCTION__, ex.what());
2022-07-01 10:29:27 +02:00
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Sends host server POST request.
// Input : &svOutMessage -
// &svOutToken -
// &netGameServer -
2022-07-01 10:29:27 +02:00
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CPylon::PostServerHost(string& svOutMessage, string& svOutToken, const NetGameServer_t& netGameServer) const
2022-07-01 10:29:27 +02:00
{
nlohmann::json jsRequestBody = nlohmann::json::object();
jsRequestBody["name"] = netGameServer.m_svHostName;
jsRequestBody["description"] = netGameServer.m_svDescription;
jsRequestBody["hidden"] = netGameServer.m_bHidden;
jsRequestBody["map"] = netGameServer.m_svHostMap;
jsRequestBody["playlist"] = netGameServer.m_svPlaylist;
jsRequestBody["ip"] = netGameServer.m_svIpAddress;
jsRequestBody["port"] = netGameServer.m_svGamePort;
jsRequestBody["key"] = netGameServer.m_svEncryptionKey;
jsRequestBody["checksum"] = netGameServer.m_svRemoteChecksum;
jsRequestBody["version"] = netGameServer.m_svSDKVersion;
jsRequestBody["playerCount"] = netGameServer.m_svPlayerCount;
jsRequestBody["maxPlayers"] = netGameServer.m_svMaxPlayers;
jsRequestBody["timeStamp"] = netGameServer.m_nTimeStamp;
jsRequestBody["publicRef"] = netGameServer.m_svPublicRef;
jsRequestBody["cachedId"] = netGameServer.m_svCachedId;
2022-07-01 10:29:27 +02:00
string svRequestBody = jsRequestBody.dump(4);
string svResponseBuf;
const bool bDebugLog = pylon_showdebuginfo->GetBool();
if (bDebugLog)
2022-07-01 10:29:27 +02:00
{
DevMsg(eDLL_T::ENGINE, "%s - Sending post host request to comp-server:\n%s\n", __FUNCTION__, svRequestBody.c_str());
2022-07-01 10:29:27 +02:00
}
CURLINFO status;
if (!QueryMasterServer(pylon_matchmaking_hostname->GetString(), "/servers/add", svRequestBody, svResponseBuf, svOutMessage, status))
{
return false;
}
2022-07-01 10:29:27 +02:00
if (bDebugLog)
2022-07-01 10:29:27 +02:00
{
DevMsg(eDLL_T::ENGINE, "%s - Comp-server replied with status: '%d'\n", __FUNCTION__, status);
try
{
string svResultBody = nlohmann::json::parse(svResponseBuf).dump(4);
DevMsg(eDLL_T::ENGINE, "%s - Comp-server response body:\n%s\n", __FUNCTION__, svResultBody.c_str());
}
catch (const std::exception& ex)
{
DevMsg(eDLL_T::ENGINE, "%s - Encountered error parsing Comp-server response body: '%s'\n", __FUNCTION__, ex.what());
}
2022-07-01 10:29:27 +02:00
}
try
2022-07-01 10:29:27 +02:00
{
if (status == 200) // STATUS_OK
2022-07-01 10:29:27 +02:00
{
nlohmann::json jsResultBody = nlohmann::json::parse(svResponseBuf);
if (jsResultBody["success"].is_boolean() && jsResultBody["success"].get<bool>())
2022-07-01 10:29:27 +02:00
{
if (jsResultBody["token"].is_string())
2022-07-01 10:29:27 +02:00
{
svOutToken = jsResultBody["token"].get<string>();
2022-07-01 10:29:27 +02:00
}
else
{
svOutToken = string();
}
2022-07-01 10:29:27 +02:00
return true;
}
else
{
if (jsResultBody["error"].is_string())
{
svOutMessage = jsResultBody["error"].get<string>();
}
else
{
svOutMessage = string("Unknown error with status: ") + std::to_string(status);
}
return false;
2022-07-01 10:29:27 +02:00
}
}
else
2022-07-01 10:29:27 +02:00
{
if (!svResponseBuf.empty())
2022-07-01 10:29:27 +02:00
{
nlohmann::json jsResultBody = nlohmann::json::parse(svResponseBuf);
if (jsResultBody["error"].is_string())
{
svOutMessage = jsResultBody["error"].get<string>();
}
else
{
svOutMessage = string("Failed HTTP request: ") + std::to_string(status);
2022-07-01 10:29:27 +02:00
}
svOutToken = string();
2022-07-01 10:29:27 +02:00
return false;
}
svOutToken = string();
svOutMessage = string("Failed HTTP request: ") + std::to_string(status);
return false;
svOutToken = string();
2022-08-29 00:29:32 +02:00
svOutMessage = "Failed to reach comp-server: connection timed-out";
2022-07-01 10:29:27 +02:00
return false;
}
}
catch (const std::exception& ex)
{
Warning(eDLL_T::ENGINE, "%s - Exception while parsing comp-server response:\n%s\n", __FUNCTION__, ex.what());
2022-07-01 10:29:27 +02:00
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Send keep alive request to Pylon Master Server.
// Input : &netGameServer -
// Output : Returns true on success, false otherwise.
//-----------------------------------------------------------------------------
bool CPylon::KeepAlive(const NetGameServer_t& netGameServer) const
{
#ifndef CLIENT_DLL
if (g_pServer->IsActive() && sv_pylonVisibility->GetBool()) // Check for active game.
{
string m_szHostToken;
string m_szHostRequestMessage;
bool result = PostServerHost(m_szHostRequestMessage, m_szHostToken, netGameServer);
return result;
}
#endif // !CLIENT_DLL
2022-09-09 21:05:10 +02:00
return false;
}
2022-07-01 10:29:27 +02:00
//-----------------------------------------------------------------------------
// Purpose: Checks if client is banned on the comp server.
// Input : &svIpAddress -
// nNucleusID -
// &svOutReason -
2022-07-01 10:29:27 +02:00
// Output : Returns true if banned, false if not banned.
//-----------------------------------------------------------------------------
bool CPylon::CheckForBan(const string& svIpAddress, const uint64_t nNucleusID, string& svOutReason) const
2022-07-01 10:29:27 +02:00
{
nlohmann::json jsRequestBody = nlohmann::json::object();
jsRequestBody["id"] = nNucleusID;
2022-07-01 10:29:27 +02:00
jsRequestBody["ip"] = svIpAddress;
string svRequestBody = jsRequestBody.dump(4);
string svResponseBuf;
string svOutMessage;
CURLINFO status;
const bool bDebugLog = pylon_showdebuginfo->GetBool();
if (bDebugLog)
{
DevMsg(eDLL_T::ENGINE, "%s - Sending ban check request to comp-server:\n%s\n", __FUNCTION__, svRequestBody.c_str());
}
if (!QueryMasterServer(pylon_matchmaking_hostname->GetString(), "/banlist/isBanned", svRequestBody, svResponseBuf, svOutMessage, status))
{
Error(eDLL_T::ENGINE, NO_ERROR, "%s - Failed to query comp-server: %s\n", __FUNCTION__, svOutMessage.c_str());
return false;
}
if (bDebugLog)
{
DevMsg(eDLL_T::ENGINE, "%s - Comp-server replied with status: '%d'\n", __FUNCTION__, status);
}
try
2022-07-01 10:29:27 +02:00
{
if (status == 200)
2022-07-01 10:29:27 +02:00
{
nlohmann::json jsResultBody = nlohmann::json::parse(svResponseBuf);
if (bDebugLog)
{
string svResultBody = jsResultBody.dump(4);
DevMsg(eDLL_T::ENGINE, "%s - Comp-server response body:\n%s\n", __FUNCTION__, svResultBody.c_str());
}
if (jsResultBody["success"].is_boolean() && jsResultBody["success"].get<bool>())
2022-07-01 10:29:27 +02:00
{
if (jsResultBody["banned"].is_boolean() && jsResultBody["banned"].get<bool>())
{
svOutReason = jsResultBody.value("reason", "#DISCONNECT_BANNED");
return true;
}
2022-07-01 10:29:27 +02:00
}
}
else
{
Error(eDLL_T::ENGINE, NO_ERROR, "%s - Failed to query comp-server: status code = %d\n", __FUNCTION__, status);
}
2022-07-01 10:29:27 +02:00
}
catch (const std::exception& ex)
{
Warning(eDLL_T::ENGINE, "%s - Exception while parsing comp-server response:\n%s\n", __FUNCTION__, ex.what());
}
2022-07-01 10:29:27 +02:00
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Sends query to master server.
// Input : &svHostName -
// &svApi -
// &svInRequest -
// &svResponse -
// &svOutMessage -
// &outStatus -
// Output : Returns true if successful, false otherwise.
//-----------------------------------------------------------------------------
bool CPylon::QueryMasterServer(const string& svHostName, const string& svApi, const string& svInRequest,
string& svOutResponse, string& svOutMessage, CURLINFO& outStatus) const
{
string svUrl;
CURLFormatUrl(svUrl, svHostName, svApi);
curl_slist* sList = nullptr;
CURL* curl = CURLInitRequest(svUrl, svInRequest, svOutResponse, sList);
if (!curl)
{
return false;
}
CURLcode res = CURLSubmitRequest(curl, sList);
if (!CURLHandleError(curl, res, svOutMessage))
{
return false;
}
outStatus = CURLRetrieveInfo(curl);
return true;
}
2022-07-01 10:29:27 +02:00
///////////////////////////////////////////////////////////////////////////////
CPylon* g_pMasterServer(new CPylon());