[Server] Add ability to set password to server from config & scripts

This commit is contained in:
Koncord 2017-02-20 21:15:04 +08:00
parent 3e50cf60e7
commit 158e9b3b09
7 changed files with 24 additions and 2 deletions

View file

@ -16,6 +16,7 @@
#include "Networking.hpp" #include "Networking.hpp"
#include "MasterClient.hpp" #include "MasterClient.hpp"
#include "Cell.hpp" #include "Cell.hpp"
#include <components/openmw-mp/Version.hpp>
using namespace mwmp; using namespace mwmp;
using namespace std; using namespace std;
@ -43,6 +44,8 @@ Networking::Networking(RakNet::RakPeerInterface *peer)
exitCode = 0; exitCode = 0;
Script::Call<Script::CallbackIdentity("OnServerInit")>(); Script::Call<Script::CallbackIdentity("OnServerInit")>();
serverPassword = TES3MP_DEFAULT_PASSW;
} }
Networking::~Networking() Networking::~Networking()
@ -56,6 +59,11 @@ Networking::~Networking()
LOG_QUIT(); LOG_QUIT();
} }
void Networking::setServerPassword(std::string passw) noexcept
{
serverPassword = passw.empty() ? TES3MP_DEFAULT_PASSW : passw;
}
void Networking::processPlayerPacket(RakNet::Packet *packet) void Networking::processPlayerPacket(RakNet::Packet *packet)
{ {
Player *player = Players::getPlayer(packet->guid); Player *player = Players::getPlayer(packet->guid);
@ -64,7 +72,6 @@ void Networking::processPlayerPacket(RakNet::Packet *packet)
if (packet->data[0] == ID_HANDSHAKE) if (packet->data[0] == ID_HANDSHAKE)
{ {
string passw = "SuperPassword";
myPacket->Read(player); myPacket->Read(player);
@ -77,7 +84,7 @@ void Networking::processPlayerPacket(RakNet::Packet *packet)
return; return;
} }
if (player->passw != passw) if (player->passw != serverPassword)
{ {
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Wrong server password for player %d, name: %s (pass: %s)", LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Wrong server password for player %d, name: %s (pass: %s)",
player->getId(), player->getId(),

View file

@ -40,11 +40,13 @@ namespace mwmp
MasterClient *getMasterClient(); MasterClient *getMasterClient();
void InitQuery(std::string queryAddr, unsigned short queryPort, std::string serverAddr, unsigned short serverPort); void InitQuery(std::string queryAddr, unsigned short queryPort, std::string serverAddr, unsigned short serverPort);
void setServerPassword(std::string passw) noexcept;
static const Networking &get(); static const Networking &get();
static Networking *getPtr(); static Networking *getPtr();
private: private:
std::string serverPassword;
static Networking *sThis; static Networking *sThis;
RakNet::RakPeerInterface *peer; RakNet::RakPeerInterface *peer;
RakNet::BitStream bsOut; RakNet::BitStream bsOut;

View file

@ -139,3 +139,8 @@ void ScriptFunctions::SetHostname(const char *name) noexcept
{ {
mwmp::Networking::getPtr()->getMasterClient()->SetHostname(name); mwmp::Networking::getPtr()->getMasterClient()->SetHostname(name);
} }
void ScriptFunctions::SetServerPassword(const char *passw) noexcept
{
mwmp::Networking::getPtr()->setServerPassword(passw);
}

View file

@ -66,6 +66,7 @@ public:
static int GetAvgPing(unsigned short pid) noexcept; static int GetAvgPing(unsigned short pid) noexcept;
static void SetModname(const char* name) noexcept; static void SetModname(const char* name) noexcept;
static void SetHostname(const char* name) noexcept; static void SetHostname(const char* name) noexcept;
static void SetServerPassword(const char *passw) noexcept;
static constexpr ScriptFunctionData functions[]{ static constexpr ScriptFunctionData functions[]{
{"CreateTimer", ScriptFunctions::CreateTimer}, {"CreateTimer", ScriptFunctions::CreateTimer},
@ -88,6 +89,7 @@ public:
{"GetAvgPing", ScriptFunctions::GetAvgPing}, {"GetAvgPing", ScriptFunctions::GetAvgPing},
{"SetModname", ScriptFunctions::SetModname}, {"SetModname", ScriptFunctions::SetModname},
{"SetHostname", ScriptFunctions::SetHostname}, {"SetHostname", ScriptFunctions::SetHostname},
{"SetServerPassword", ScriptFunctions::SetServerPassword},
POSITIONAPI, POSITIONAPI,
CELLAPI, CELLAPI,

View file

@ -183,6 +183,8 @@ int main(int argc, char *argv[])
string addr = mgr.getString("address", "General"); string addr = mgr.getString("address", "General");
int port = mgr.getInt("port", "General"); int port = mgr.getInt("port", "General");
string passw = mgr.getString("password", "General");
string plugin_home = mgr.getString("home", "Plugins"); string plugin_home = mgr.getString("home", "Plugins");
string moddir = Utils::convertPath(plugin_home + "/data"); string moddir = Utils::convertPath(plugin_home + "/data");
@ -227,6 +229,7 @@ int main(int argc, char *argv[])
peer->SetMaximumIncomingConnections((unsigned short)(players)); peer->SetMaximumIncomingConnections((unsigned short)(players));
Networking networking(peer); Networking networking(peer);
networking.setServerPassword(passw);
if ( mgr.getBool("enabled", "MasterServer")) if ( mgr.getBool("enabled", "MasterServer"))
{ {

View file

@ -8,4 +8,6 @@
#define TES3MP_VERSION "0.5.0" #define TES3MP_VERSION "0.5.0"
#define TES3MP_PROTO_VERSION 6 #define TES3MP_PROTO_VERSION 6
#define TES3MP_DEFAULT_PASSW "SuperPassword"
#endif //OPENMW_VERSION_HPP #endif //OPENMW_VERSION_HPP

View file

@ -5,6 +5,7 @@ players = 64
hostname = My TES3MP server hostname = My TES3MP server
# 0 - Verbose (spam), 1 - Info, 2 - Warnings, 3 - Errors, 4 - Only fatal errors # 0 - Verbose (spam), 1 - Info, 2 - Warnings, 3 - Errors, 4 - Only fatal errors
loglevel = 1 loglevel = 1
password =
[Plugins] [Plugins]
#home = ~/local/openmw/tes3mp #home = ~/local/openmw/tes3mp