Skip to content

Commit

Permalink
improve version checks
Browse files Browse the repository at this point in the history
  • Loading branch information
zpl-zak committed Jan 30, 2024
1 parent 450fc17 commit 9b00a50
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 46 deletions.
8 changes: 4 additions & 4 deletions code/framework/src/integrations/client/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include "instance.h"

#include <networking/messages/client_connection_finalized.h>
#include <networking/messages/client_initialise_player.h>
#include <networking/messages/client_handshake.h>
#include <networking/messages/client_initialise_player.h>
#include <networking/messages/client_kick.h>

#include <world/game_rpc/set_transform.h>
Expand Down Expand Up @@ -116,7 +116,7 @@ namespace Framework::Integrations::Client {
case Graphics::RendererBackend::BACKEND_D3D_9: _renderer->GetD3D9Backend()->Init(_opts.rendererOptions.d3d9.device, nullptr, nullptr, nullptr); break;
case Graphics::RendererBackend::BACKEND_D3D_11: _renderer->GetD3D11Backend()->Init(_opts.rendererOptions.d3d11.device, _opts.rendererOptions.d3d11.deviceContext, nullptr, nullptr); break;
case Graphics::RendererBackend::BACKEND_D3D_12: {
const auto& ctx = _opts.rendererOptions.d3d12;
const auto &ctx = _opts.rendererOptions.d3d12;
_renderer->GetD3D12Backend()->Init(ctx.device, nullptr, ctx.swapchain, ctx.commandQueue);
} break;
default: Logging::GetLogger(FRAMEWORK_INNER_GRAPHICS)->info("[renderDevice] Device not implemented"); break;
Expand Down Expand Up @@ -214,7 +214,7 @@ namespace Framework::Integrations::Client {
Logging::GetLogger(FRAMEWORK_INNER_CLIENT)->debug("Connection accepted by server, sending handshake");

ClientHandshake msg;
msg.FromParameters(_currentState._nickname, "MY_SUPER_ID_1", "MY_SUPER_ID_2", Utils::Version::rel, _opts.gameVersion, _opts.gameName);
msg.FromParameters(_currentState._nickname, "MY_SUPER_ID_1", "MY_SUPER_ID_2", _opts.modVersion, Utils::Version::rel, _opts.gameVersion, _opts.gameName);

net->Send(msg, SLNet::UNASSIGNED_RAKNET_GUID);
});
Expand Down Expand Up @@ -259,7 +259,7 @@ namespace Framework::Integrations::Client {
}

auto tr = e.get_mut<Framework::World::Modules::Base::Transform>();
*tr = msg->GetTransform();
*tr = msg->GetTransform();
});
net->SetOnPlayerDisconnectedCallback([this](SLNet::Packet *packet, uint32_t reasonId) {
_worldEngine->OnDisconnect();
Expand Down
4 changes: 3 additions & 1 deletion code/framework/src/integrations/client/instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ namespace Framework::Integrations::Client {
// networked game metadata (required)
std::string gameName;
std::string gameVersion;
std::string modVersion;

bool initRendererManually = false;

Graphics::RendererConfiguration rendererOptions = {};
};

Expand All @@ -55,7 +57,7 @@ namespace Framework::Integrations::Client {

class Instance {
private:
bool _initialized = false;
bool _initialized = false;
bool _renderInitialized = false;
InstanceOptions _opts;

Expand Down
23 changes: 17 additions & 6 deletions code/framework/src/integrations/server/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ namespace Framework::Integrations::Server {
nlohmann::json root;
root["mod_name"] = _opts.modName;
root["mod_slug"] = _opts.modSlug;
root["mod_version"] = _opts.modVersion;
root["mod_version"] = Utils::Version::rel;
root["host"] = _opts.bindHost;
root["port"] = _opts.bindPort;
root["password_required"] = !_opts.bindPassword.empty();
Expand Down Expand Up @@ -242,7 +242,7 @@ namespace Framework::Integrations::Server {
return;
}

if (msg->GetMPClientGame() != _opts.gameName) {
if (msg->GetGameName() != _opts.gameName) {
Logging::GetLogger(FRAMEWORK_INNER_SERVER)->error("Client has invalid game, force-disconnecting peer");
Framework::Networking::Messages::ClientKick kick;
kick.FromParameters(Framework::Networking::Messages::DisconnectionReason::WRONG_VERSION);
Expand All @@ -251,9 +251,20 @@ namespace Framework::Integrations::Server {
return;
}

const auto fwVersion = msg->GetFWVersion();

if (!Utils::Version::VersionSatisfies(fwVersion.c_str(), Utils::Version::rel)) {
Logging::GetLogger(FRAMEWORK_INNER_SERVER)->error("Client has invalid Framework version, force-disconnecting peer");
Framework::Networking::Messages::ClientKick kick;
kick.FromParameters(Framework::Networking::Messages::DisconnectionReason::WRONG_VERSION);
net->Send(kick, guid);
net->GetPeer()->CloseConnection(guid, true);
return;
}

const auto clientVersion = msg->GetClientVersion();

if (!Utils::Version::VersionSatisfies(clientVersion.c_str(), Utils::Version::rel)) {
if (!Utils::Version::VersionSatisfies(clientVersion.c_str(), _opts.modVersion.c_str())) {
Logging::GetLogger(FRAMEWORK_INNER_SERVER)->error("Client has invalid version, force-disconnecting peer");
Framework::Networking::Messages::ClientKick kick;
kick.FromParameters(Framework::Networking::Messages::DisconnectionReason::WRONG_VERSION);
Expand All @@ -262,9 +273,9 @@ namespace Framework::Integrations::Server {
return;
}

const auto mpClientVersion = msg->GetMPClientVersion();
const auto mpClientVersion = msg->GetGameVersion();

if (!Utils::Version::VersionSatisfies(mpClientVersion.c_str(), _opts.gameVersion.c_str())) {
if (mpClientVersion != _opts.gameVersion) {
Logging::GetLogger(FRAMEWORK_INNER_SERVER)->error("Client has invalid game version, force-disconnecting peer");
Framework::Networking::Messages::ClientKick kick;
kick.FromParameters(Framework::Networking::Messages::DisconnectionReason::WRONG_VERSION);
Expand Down Expand Up @@ -371,7 +382,7 @@ namespace Framework::Integrations::Server {
if (_masterlist->IsInitialized()) {
Services::ServerInfo info {};
info.gameMode = _scriptingEngine->GetEngine()->GetGameModeName();
info.version = _opts.modVersion;
info.version = Utils::Version::rel;
info.maxPlayers = _opts.maxPlayers;
info.currentPlayers = _networkingEngine->GetNetworkServer()->GetPeer()->NumberOfConnections();
_masterlist->Ping(info);
Expand Down
39 changes: 23 additions & 16 deletions code/framework/src/networking/messages/client_handshake.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,66 @@ namespace Framework::Networking::Messages {
SLNet::RakString _playerSteamId = "";
SLNet::RakString _playerDiscordId = "";
SLNet::RakString _clientVersion = "";
SLNet::RakString _mpClientVersion = "";
SLNet::RakString _mpClientGame = "";
SLNet::RakString _fwVersion = "";
SLNet::RakString _gameVersion = "";
SLNet::RakString _gameName = "";

public:
uint8_t GetMessageID() const override {
return GAME_CONNECTION_HANDSHAKE;
}

void FromParameters(const std::string &playerName, const std::string &playerSteamId, const std::string &playerDiscordId, const std::string &clientVersion, const std::string &mpClientVersion, const std::string &mpClientGame) {
void FromParameters(const std::string &playerName, const std::string &playerSteamId, const std::string &playerDiscordId, const std::string &clientVersion, const std::string &fwVersion, const std::string &gameVersion, const std::string &gameName) {
Framework::Logging::GetLogger("dbg")->debug(playerName);
_playerName = SLNet::RakString(playerName.c_str());
_playerSteamId = SLNet::RakString(playerSteamId.c_str());
_playerDiscordId = SLNet::RakString(playerDiscordId.c_str());
_fwVersion = SLNet::RakString(fwVersion.c_str());
_clientVersion = SLNet::RakString(clientVersion.c_str());
_mpClientVersion = SLNet::RakString(mpClientVersion.c_str());
_mpClientGame = SLNet::RakString(mpClientGame.c_str());
_gameVersion = SLNet::RakString(gameVersion.c_str());
_gameName = SLNet::RakString(gameName.c_str());
}

void Serialize(SLNet::BitStream *bs, bool write) override {
bs->Serialize(write, _playerName);
bs->Serialize(write, _playerSteamId);
bs->Serialize(write, _playerDiscordId);
bs->Serialize(write, _fwVersion);
bs->Serialize(write, _clientVersion);
bs->Serialize(write, _mpClientVersion);
bs->Serialize(write, _mpClientGame);
bs->Serialize(write, _gameVersion);
bs->Serialize(write, _gameName);
}

bool Valid() const override {
return _playerName.GetLength() > 0 && (_playerSteamId.GetLength() > 0 || _playerDiscordId.GetLength() > 0) && _clientVersion.GetLength() > 0 && _mpClientVersion.GetLength() > 0 && _mpClientGame.GetLength() > 0;
return _playerName.GetLength() > 0 && (_playerSteamId.GetLength() > 0 || _playerDiscordId.GetLength() > 0) && _fwVersion.GetLength() > 0 && _clientVersion.GetLength() > 0 && _gameVersion.GetLength() > 0 && _gameName.GetLength() > 0;
}

std::string GetPlayerName() {
std::string GetPlayerName() const {
return _playerName.C_String();
}

std::string GetPlayerSteamID() {
std::string GetPlayerSteamID() const {
return _playerSteamId.C_String();
}

std::string GetPlayerDiscordID() {
std::string GetPlayerDiscordID() const {
return _playerDiscordId.C_String();
}

std::string GetClientVersion() {
std::string GetFWVersion() const {
return _fwVersion.C_String();
}

std::string GetClientVersion() const {
return _clientVersion.C_String();
}

std::string GetMPClientVersion() {
return _mpClientVersion.C_String();
std::string GetGameVersion() const {
return _gameVersion.C_String();
}

std::string GetMPClientGame() {
return _mpClientGame.C_String();
std::string GetGameName() const {
return _gameName.C_String();
}
};
} // namespace Framework::Networking::Messages
39 changes: 20 additions & 19 deletions code/framework/src/services/masterlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,34 @@ namespace Framework::Services {
}
void MasterlistConnector::PingThread() {
while (_isInitialized) {
std::lock_guard lock(_mutex);

// Only ping every 5 seconds
if (Utils::Time::GetDifference(Utils::Time::GetTimePoint(), _lastPingAt) < 5000) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
continue;
}

// Update the last ping time
_lastPingAt = Utils::Time::GetTimePoint();
{
std::lock_guard lock(_mutex);
// Update the last ping time
_lastPingAt = Utils::Time::GetTimePoint();

// Build the payload
httplib::Params params {
{"gamemode", _storedInfo.gameMode},
{"version", _storedInfo.version},
{"max_players", std::to_string(_storedInfo.maxPlayers)},
{"current_players", std::to_string(_storedInfo.currentPlayers)},
};
// Build the payload
httplib::Params params {
{"gamemode", _storedInfo.gameMode},
{"version", _storedInfo.version},
{"max_players", std::to_string(_storedInfo.maxPlayers)},
{"current_players", std::to_string(_storedInfo.currentPlayers)},
};

// Send the request
// Make sure we can only handle valid responses
const auto res = _client->Post("/rcon/ping", params);
if (!res) {
Logging::GetLogger(FRAMEWORK_INNER_SERVER)->error("Failed to ping masterlist server: {}", res.error());
}
if (res->status != 200 && res->status != 201) {
Logging::GetLogger(FRAMEWORK_INNER_SERVER)->error("Failed to ping masterlist server: {} {}", res->status, res->body);
// Send the request
// Make sure we can only handle valid responses
const auto res = _client->Post("/rcon/ping", params);
if (!res) {
Logging::GetLogger(FRAMEWORK_INNER_SERVER)->error("Failed to ping masterlist server: {}", res.error());
}
if (res->status != 200 && res->status != 201) {
Logging::GetLogger(FRAMEWORK_INNER_SERVER)->error("Failed to ping masterlist server: {} {}", res->status, res->body);
}
}
}
}
Expand Down

0 comments on commit 9b00a50

Please sign in to comment.