[General] Add server script functions for banning & unbanning IPs

Additionally, use a more informative message for the client when trying to connecting to a server that it is banned from.
This commit is contained in:
David Cernat 2017-07-27 19:29:17 +03:00
parent 500bff9911
commit 9d05063af4
5 changed files with 40 additions and 10 deletions

View file

@ -477,6 +477,16 @@ void Networking::kickPlayer(RakNet::RakNetGUID guid)
peer->CloseConnection(guid, true);
}
void Networking::banAddress(const char *ipAddress)
{
peer->AddToBanList(ipAddress);
}
void Networking::unbanAddress(const char *ipAddress)
{
peer->RemoveFromBanList(ipAddress);
}
unsigned short Networking::numberOfConnections() const
{
return peer->NumberOfConnections();

View file

@ -23,6 +23,9 @@ namespace mwmp
void newPlayer(RakNet::RakNetGUID guid);
void disconnectPlayer(RakNet::RakNetGUID guid);
void kickPlayer(RakNet::RakNetGUID guid);
void banAddress(const char *ipAddress);
void unbanAddress(const char *ipAddress);
RakNet::SystemAddress getSystemAddress(RakNet::RakNetGUID guid);
void processPlayerPacket(RakNet::Packet *packet);

View file

@ -108,6 +108,16 @@ void ScriptFunctions::Kick(unsigned short pid) noexcept
mwmp::Networking::getPtr()->kickPlayer(player->guid);
}
void ScriptFunctions::BanAddress(const char *ipAddress) noexcept
{
mwmp::Networking::getPtr()->banAddress(ipAddress);
}
void ScriptFunctions::UnbanAddress(const char *ipAddress) noexcept
{
mwmp::Networking::getPtr()->unbanAddress(ipAddress);
}
const char *ScriptFunctions::GetServerVersion() noexcept
{
return TES3MP_VERSION;
@ -126,6 +136,14 @@ int ScriptFunctions::GetAvgPing(unsigned short pid) noexcept
return mwmp::Networking::get().getAvgPing(player->guid);
}
const char *ScriptFunctions::GetIP(unsigned short pid) noexcept
{
Player *player;
GET_PLAYER(pid, player, "");
RakNet::SystemAddress addr = mwmp::Networking::getPtr()->getSystemAddress(player->guid);
return addr.ToString(false);
}
void ScriptFunctions::SetModname(const char *name) noexcept
{
mwmp::Networking::getPtr()->getMasterClient()->SetModname(name);
@ -154,11 +172,3 @@ void ScriptFunctions::SetRuleValue(const char *key, double value) noexcept
if (mc)
mc->SetRuleValue(key, value);
}
const char *ScriptFunctions::GetIP(unsigned short pid) noexcept
{
Player *player;
GET_PLAYER(pid, player, "");
RakNet::SystemAddress addr = mwmp::Networking::getPtr()->getSystemAddress(player->guid);
return addr.ToString(false);
}

View file

@ -69,11 +69,14 @@ public:
static void FreeTimer(int timerId) noexcept;
static bool IsTimerElapsed(int timerId) noexcept;
static const char* GetIP(unsigned short pid) noexcept;
static void Kick(unsigned short pid) noexcept;
static void BanAddress(const char *ipAddress) noexcept;
static void UnbanAddress(const char *ipAddress) noexcept;
static const char *GetServerVersion() noexcept;
static const char *GetProtocolVersion() noexcept;
static int GetAvgPing(unsigned short pid) noexcept;
static const char* GetIP(unsigned short pid) noexcept;
static void SetModname(const char* name) noexcept;
static void SetHostname(const char* name) noexcept;
static void SetServerPassword(const char *passw) noexcept;
@ -95,7 +98,11 @@ public:
{"StopServer", ScriptFunctions::StopServer},
{"SendMessage", ScriptFunctions::SendMessage},
{"Kick", ScriptFunctions::Kick},
{"BanAddress", ScriptFunctions::BanAddress},
{"UnbanAddress", ScriptFunctions::UnbanAddress},
{"GetServerVersion", ScriptFunctions::GetServerVersion},
{"GetProtocolVersion", ScriptFunctions::GetProtocolVersion},
{"GetAvgPing", ScriptFunctions::GetAvgPing},

View file

@ -295,7 +295,7 @@ void Networking::connect(const std::string &ip, unsigned short port, std::vector
case ID_DISCONNECTION_NOTIFICATION:
throw runtime_error("ID_DISCONNECTION_NOTIFICATION.\n");
case ID_CONNECTION_BANNED:
throw runtime_error("ID_CONNECTION_BANNED.\n");
throw runtime_error("You have been banned from this server.\n");
case ID_CONNECTION_LOST:
throw runtime_error("ID_CONNECTION_LOST.\n");
default: