aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDedicatedTest/masterserver.cpp
diff options
context:
space:
mode:
authorBobTheBob <32057864+BobTheBob9@users.noreply.github.com>2022-01-02 22:32:36 +0000
committerGitHub <noreply@github.com>2022-01-02 22:32:36 +0000
commit3f629680f71b4694bd4eb78726537a91e10a2352 (patch)
tree137e44e28bbe372adeb616e3f20156ba23c08655 /NorthstarDedicatedTest/masterserver.cpp
parentff875c0551c54bb187b5320b81029f8fe7f010fb (diff)
parentf7b96d1048acddc343244ddec87a996ebaf7cb5b (diff)
downloadNorthstarLauncher-3f629680f71b4694bd4eb78726537a91e10a2352.tar.gz
NorthstarLauncher-3f629680f71b4694bd4eb78726537a91e10a2352.zip
Merge pull request #19 from p0358/main
Big refactor of launching process
Diffstat (limited to 'NorthstarDedicatedTest/masterserver.cpp')
-rw-r--r--NorthstarDedicatedTest/masterserver.cpp149
1 files changed, 92 insertions, 57 deletions
diff --git a/NorthstarDedicatedTest/masterserver.cpp b/NorthstarDedicatedTest/masterserver.cpp
index 0913be11..01f4bc8e 100644
--- a/NorthstarDedicatedTest/masterserver.cpp
+++ b/NorthstarDedicatedTest/masterserver.cpp
@@ -3,7 +3,6 @@
#include "concommand.h"
#include "gameutils.h"
#include "hookutils.h"
-#include "libcurl/include/curl/curl.h"
#include "serverauthentication.h"
#include "gameutils.h"
#include "rapidjson/document.h"
@@ -38,6 +37,39 @@ CHostState__State_ChangeLevelSPType CHostState__State_ChangeLevelSP;
typedef void(*CHostState__State_GameShutdownType)(CHostState* hostState);
CHostState__State_GameShutdownType CHostState__State_GameShutdown;
+const char* HttplibErrorToString(httplib::Error error)
+{
+ switch (error)
+ {
+ case httplib::Error::Success:
+ return "httplib::Error::Success";
+ case httplib::Error::Unknown:
+ return "httplib::Error::Unknown";
+ case httplib::Error::Connection:
+ return "httplib::Error::Connection";
+ case httplib::Error::BindIPAddress:
+ return "httplib::Error::BindIPAddress";
+ case httplib::Error::Read:
+ return "httplib::Error::Read";
+ case httplib::Error::Write:
+ return "httplib::Error::Write";
+ case httplib::Error::ExceedRedirectCount:
+ return "httplib::Error::ExceedRedirectCount";
+ case httplib::Error::Canceled:
+ return "httplib::Error::Canceled";
+ case httplib::Error::SSLConnection:
+ return "httplib::Error::SSLConnection";
+ case httplib::Error::SSLLoadingCerts:
+ return "httplib::Error::SSLLoadingCerts";
+ case httplib::Error::SSLServerVerification:
+ return "httplib::Error::SSLServerVerification";
+ case httplib::Error::UnsupportedMultipartBoundaryChars:
+ return "httplib::Error::UnsupportedMultipartBoundaryChars";
+ }
+
+ return "";
+}
+
RemoteServerInfo::RemoteServerInfo(const char* newId, const char* newName, const char* newDescription, const char* newMap, const char* newPlaylist, int newPlayerCount, int newMaxPlayers, bool newRequiresPassword)
{
// passworded servers don't have public ips
@@ -59,6 +91,18 @@ RemoteServerInfo::RemoteServerInfo(const char* newId, const char* newName, const
maxPlayers = newMaxPlayers;
}
+void MasterServerManager::SetCommonHttpClientOptions(CURL* curl)
+{
+ curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
+ curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
+ //curl_easy_setopt(curl, CURLOPT_STDERR, stdout);
+ if (CommandLine()->FindParm("-msinsecure")) // TODO: this check doesn't seem to work
+ {
+ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
+ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
+ }
+}
+
void MasterServerManager::ClearServerList()
{
// this doesn't really do anything lol, probably isn't threadsafe
@@ -90,12 +134,11 @@ void MasterServerManager::AuthenticateOriginWithMasterServer(char* uid, char* or
spdlog::info("Trying to authenticate with northstar masterserver for user {}", uidStr);
CURL* curl = curl_easy_init();
+ SetCommonHttpClientOptions(curl);
std::string readBuffer;
curl_easy_setopt(curl, CURLOPT_URL, fmt::format("{}/client/origin_auth?id={}&token={}", Cvar_ns_masterserver_hostname->m_pszString, uidStr, tokenStr).c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
- curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteToStringBufferCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
@@ -105,12 +148,12 @@ void MasterServerManager::AuthenticateOriginWithMasterServer(char* uid, char* or
{
m_successfullyConnected = true;
- rapidjson::Document originAuthInfo;
+ rapidjson_document originAuthInfo;
originAuthInfo.Parse(readBuffer.c_str());
if (originAuthInfo.HasParseError())
{
- spdlog::error("Failed reading origin auth info response: encountered parse error \{}\"", rapidjson::GetParseError_En(originAuthInfo.GetParseError()));
+ spdlog::error("Failed reading origin auth info response: encountered parse error \"{}\"", rapidjson::GetParseError_En(originAuthInfo.GetParseError()));
goto REQUEST_END_CLEANUP;
}
@@ -163,12 +206,11 @@ void MasterServerManager::RequestServerList()
spdlog::info("Requesting server list from {}", Cvar_ns_masterserver_hostname->m_pszString);
CURL* curl = curl_easy_init();
+ SetCommonHttpClientOptions(curl);
std::string readBuffer;
curl_easy_setopt(curl, CURLOPT_URL, fmt::format("{}/client/servers", Cvar_ns_masterserver_hostname->m_pszString).c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
- curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteToStringBufferCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
@@ -178,7 +220,7 @@ void MasterServerManager::RequestServerList()
{
m_successfullyConnected = true;
- rapidjson::Document serverInfoJson;
+ rapidjson_document serverInfoJson;
serverInfoJson.Parse(readBuffer.c_str());
if (serverInfoJson.HasParseError())
@@ -200,7 +242,7 @@ void MasterServerManager::RequestServerList()
goto REQUEST_END_CLEANUP;
}
- rapidjson::GenericArray<false, rapidjson::Value> serverArray = serverInfoJson.GetArray();
+ rapidjson::GenericArray<false, rapidjson_document::GenericValue> serverArray = serverInfoJson.GetArray();
spdlog::info("Got {} servers", serverArray.Size());
@@ -273,7 +315,7 @@ void MasterServerManager::RequestServerList()
std::sort(m_remoteServers.begin(), m_remoteServers.end(), [](RemoteServerInfo& a, RemoteServerInfo& b)
{
return a.playerCount > b.playerCount;
- });
+ });
}
else
{
@@ -301,12 +343,11 @@ void MasterServerManager::RequestMainMenuPromos()
Sleep(500);
CURL* curl = curl_easy_init();
+ SetCommonHttpClientOptions(curl);
std::string readBuffer;
curl_easy_setopt(curl, CURLOPT_URL, fmt::format("{}/client/mainmenupromos", Cvar_ns_masterserver_hostname->m_pszString).c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
- curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteToStringBufferCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
@@ -316,7 +357,7 @@ void MasterServerManager::RequestMainMenuPromos()
{
m_successfullyConnected = true;
- rapidjson::Document mainMenuPromoJson;
+ rapidjson_document mainMenuPromoJson;
mainMenuPromoJson.Parse(readBuffer.c_str());
if (mainMenuPromoJson.HasParseError())
@@ -412,12 +453,11 @@ void MasterServerManager::AuthenticateWithOwnServer(char* uid, char* playerToken
std::thread requestThread([this, uidStr, tokenStr]()
{
CURL* curl = curl_easy_init();
+ SetCommonHttpClientOptions(curl);
std::string readBuffer;
curl_easy_setopt(curl, CURLOPT_URL, fmt::format("{}/client/auth_with_self?id={}&playerToken={}", Cvar_ns_masterserver_hostname->m_pszString, uidStr, tokenStr).c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
- curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteToStringBufferCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
@@ -427,7 +467,7 @@ void MasterServerManager::AuthenticateWithOwnServer(char* uid, char* playerToken
{
m_successfullyConnected = true;
- rapidjson::Document authInfoJson;
+ rapidjson_document authInfoJson;
authInfoJson.Parse(readBuffer.c_str());
if (authInfoJson.HasParseError())
@@ -480,7 +520,7 @@ void MasterServerManager::AuthenticateWithOwnServer(char* uid, char* playerToken
goto REQUEST_END_CLEANUP;
}
- newAuthData.pdata[i++] = (char)byte.GetUint();
+ newAuthData.pdata[i++] = static_cast<char>(byte.GetUint());
}
std::lock_guard<std::mutex> guard(g_ServerAuthenticationManager->m_authDataMutex);
@@ -538,11 +578,10 @@ void MasterServerManager::AuthenticateWithServer(char* uid, char* playerToken, c
spdlog::info("Attempting authentication with server of id \"{}\"", serverIdStr);
CURL* curl = curl_easy_init();
+ SetCommonHttpClientOptions(curl);
std::string readBuffer;
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
- curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteToStringBufferCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
@@ -560,7 +599,7 @@ void MasterServerManager::AuthenticateWithServer(char* uid, char* playerToken, c
{
m_successfullyConnected = true;
- rapidjson::Document connectionInfoJson;
+ rapidjson_document connectionInfoJson;
connectionInfoJson.Parse(readBuffer.c_str());
if (connectionInfoJson.HasParseError())
@@ -595,7 +634,7 @@ void MasterServerManager::AuthenticateWithServer(char* uid, char* playerToken, c
}
m_pendingConnectionInfo.ip.S_un.S_addr = inet_addr(connectionInfoJson["ip"].GetString());
- m_pendingConnectionInfo.port = connectionInfoJson["port"].GetInt();
+ m_pendingConnectionInfo.port = (unsigned short)connectionInfoJson["port"].GetUint();
strncpy(m_pendingConnectionInfo.authToken, connectionInfoJson["authToken"].GetString(), 31);
m_pendingConnectionInfo.authToken[31] = 0;
@@ -645,37 +684,36 @@ void MasterServerManager::AddSelfToServerList(int port, int authPort, char* name
m_ownServerAuthToken[0] = 0;
// build modinfo obj
- rapidjson::Document modinfoDoc;
+ rapidjson_document modinfoDoc;
modinfoDoc.SetObject();
- modinfoDoc.AddMember("Mods", rapidjson::Value(rapidjson::kArrayType), modinfoDoc.GetAllocator());
+ modinfoDoc.AddMember("Mods", rapidjson_document::GenericValue(rapidjson::kArrayType), modinfoDoc.GetAllocator());
- int currentModIndex = 0;
- for (Mod& mod : g_ModManager->m_loadedMods)
- {
- if (!mod.Enabled || (!mod.RequiredOnClient && !mod.Pdiff.size()))
- continue;
+ int currentModIndex = 0;
+ for (Mod& mod : g_ModManager->m_loadedMods)
+ {
+ if (!mod.Enabled || (!mod.RequiredOnClient && !mod.Pdiff.size()))
+ continue;
- modinfoDoc["Mods"].PushBack(rapidjson::Value(rapidjson::kObjectType), modinfoDoc.GetAllocator());
- modinfoDoc["Mods"][currentModIndex].AddMember("Name", rapidjson::StringRef(&mod.Name[0]), modinfoDoc.GetAllocator());
- modinfoDoc["Mods"][currentModIndex].AddMember("Version", rapidjson::StringRef(&mod.Version[0]), modinfoDoc.GetAllocator());
- modinfoDoc["Mods"][currentModIndex].AddMember("RequiredOnClient", mod.RequiredOnClient, modinfoDoc.GetAllocator());
- modinfoDoc["Mods"][currentModIndex].AddMember("Pdiff", rapidjson::StringRef(&mod.Pdiff[0]), modinfoDoc.GetAllocator());
+ modinfoDoc["Mods"].PushBack(rapidjson_document::GenericValue(rapidjson::kObjectType), modinfoDoc.GetAllocator());
+ modinfoDoc["Mods"][currentModIndex].AddMember("Name", rapidjson::StringRef(&mod.Name[0]), modinfoDoc.GetAllocator());
+ modinfoDoc["Mods"][currentModIndex].AddMember("Version", rapidjson::StringRef(&mod.Version[0]), modinfoDoc.GetAllocator());
+ modinfoDoc["Mods"][currentModIndex].AddMember("RequiredOnClient", mod.RequiredOnClient, modinfoDoc.GetAllocator());
+ modinfoDoc["Mods"][currentModIndex].AddMember("Pdiff", rapidjson::StringRef(&mod.Pdiff[0]), modinfoDoc.GetAllocator());
- currentModIndex++;
- }
+ currentModIndex++;
+ }
- rapidjson::StringBuffer buffer;
- buffer.Clear();
- rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
- modinfoDoc.Accept(writer);
- const char* modInfoString = buffer.GetString();
+ rapidjson::StringBuffer buffer;
+ buffer.Clear();
+ rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
+ modinfoDoc.Accept(writer);
+ const char* modInfoString = buffer.GetString();
CURL* curl = curl_easy_init();
+ SetCommonHttpClientOptions(curl);
std::string readBuffer;
curl_easy_setopt(curl, CURLOPT_POST, 1L);
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
- curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteToStringBufferCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
@@ -712,7 +750,7 @@ void MasterServerManager::AddSelfToServerList(int port, int authPort, char* name
{
m_successfullyConnected = true;
- rapidjson::Document serverAddedJson;
+ rapidjson_document serverAddedJson;
serverAddedJson.Parse(readBuffer.c_str());
if (serverAddedJson.HasParseError())
@@ -746,8 +784,8 @@ void MasterServerManager::AddSelfToServerList(int port, int authPort, char* name
goto REQUEST_END_CLEANUP;
}
- strncpy(m_ownServerId, serverAddedJson["id"].GetString(), sizeof(m_ownServerId));
- m_ownServerId[sizeof(m_ownServerId) - 1] = 0;
+ strncpy(m_ownServerId, serverAddedJson["id"].GetString(), sizeof(m_ownServerId));
+ m_ownServerId[sizeof(m_ownServerId) - 1] = 0;
strncpy(m_ownServerAuthToken, serverAddedJson["serverAuthToken"].GetString(), sizeof(m_ownServerAuthToken));
m_ownServerAuthToken[sizeof(m_ownServerAuthToken) - 1] = 0;
@@ -760,13 +798,14 @@ void MasterServerManager::AddSelfToServerList(int port, int authPort, char* name
do
{
CURL* curl = curl_easy_init();
+ SetCommonHttpClientOptions(curl);
std::string readBuffer;
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, fmt::format("{}/server/heartbeat?id={}&playerCount={}", Cvar_ns_masterserver_hostname->m_pszString, m_ownServerId, g_ServerAuthenticationManager->m_additionalPlayerData.size()).c_str());
- curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteToStringBufferCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
+ curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
CURLcode result = curl_easy_perform(curl);
if (result != CURLcode::CURLE_OK)
@@ -805,11 +844,10 @@ void MasterServerManager::UpdateServerMapAndPlaylist(char* map, char* playlist,
std::thread requestThread([this, strMap, strPlaylist, maxPlayers]
{
CURL* curl = curl_easy_init();
+ SetCommonHttpClientOptions(curl);
std::string readBuffer;
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
- curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteToStringBufferCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
@@ -846,11 +884,10 @@ void MasterServerManager::UpdateServerPlayerCount(int playerCount)
std::thread requestThread([this, playerCount]
{
CURL* curl = curl_easy_init();
+ SetCommonHttpClientOptions(curl);
std::string readBuffer;
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
- curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteToStringBufferCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_URL, fmt::format("{}/server/update_values?id={}&playerCount={}", Cvar_ns_masterserver_hostname->m_pszString, m_ownServerId, playerCount).c_str());
@@ -884,12 +921,11 @@ void MasterServerManager::WritePlayerPersistentData(char* playerId, char* pdata,
std::thread requestThread([this, strPlayerId, strPdata, pdataSize]
{
CURL* curl = curl_easy_init();
+ SetCommonHttpClientOptions(curl);
std::string readBuffer;
curl_easy_setopt(curl, CURLOPT_URL, fmt::format("{}/accounts/write_persistence?id={}&serverId={}", Cvar_ns_masterserver_hostname->m_pszString, strPlayerId, m_ownServerId).c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1L);
- curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteToStringBufferCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
@@ -912,7 +948,7 @@ void MasterServerManager::WritePlayerPersistentData(char* playerId, char* pdata,
curl_easy_cleanup(curl);
- m_savingPersistentData = false;
+ m_savingPersistentData = false;
});
requestThread.detach();
@@ -926,11 +962,10 @@ void MasterServerManager::RemoveSelfFromServerList()
std::thread requestThread([this] {
CURL* curl = curl_easy_init();
+ SetCommonHttpClientOptions(curl);
std::string readBuffer;
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
- curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteToStringBufferCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_URL, fmt::format("{}/server/remove_server?id={}", Cvar_ns_masterserver_hostname->m_pszString, m_ownServerId).c_str());
@@ -1004,9 +1039,9 @@ void CHostState__State_GameShutdownHook(CHostState* hostState)
CHostState__State_GameShutdown(hostState);
}
-MasterServerManager::MasterServerManager()
+MasterServerManager::MasterServerManager() : m_pendingConnectionInfo{}, m_ownServerId{ "" }, m_ownClientAuthToken{ "" }
{
- curl_global_init(CURL_GLOBAL_DEFAULT);
+
}
void InitialiseSharedMasterServer(HMODULE baseAddress)