Move Server ban check to different point

This commit is contained in:
sfan5 2024-01-24 19:39:28 +01:00
parent 5dbc1d4c08
commit 89f3502b56
3 changed files with 25 additions and 25 deletions

@ -95,6 +95,9 @@ void Server::handleCommand_Init(NetworkPacket* pkt)
return; return;
} }
if (denyIfBanned(peer_id))
return;
// First byte after command is maximum supported // First byte after command is maximum supported
// serialization version // serialization version
u8 client_max; u8 client_max;

@ -1160,31 +1160,6 @@ void Server::ProcessData(NetworkPacket *pkt)
ScopeProfiler sp(g_profiler, "Server: Process network packet (sum)"); ScopeProfiler sp(g_profiler, "Server: Process network packet (sum)");
u32 peer_id = pkt->getPeerId(); u32 peer_id = pkt->getPeerId();
try {
Address address = getPeerAddress(peer_id);
std::string addr_s = address.serializeString();
// FIXME: Isn't it a bit excessive to check this for every packet?
if (m_banmanager->isIpBanned(addr_s)) {
std::string ban_name = m_banmanager->getBanName(addr_s);
infostream << "Server: A banned client tried to connect from "
<< addr_s << "; banned name was " << ban_name << std::endl;
DenyAccess(peer_id, SERVER_ACCESSDENIED_CUSTOM_STRING,
"Your IP is banned. Banned name was " + ban_name);
return;
}
} catch (con::PeerNotFoundException &e) {
/*
* no peer for this packet found
* most common reason is peer timeout, e.g. peer didn't
* respond for some time, your server was overloaded or
* things like that.
*/
infostream << "Server::ProcessData(): Canceling: peer "
<< peer_id << " not found" << std::endl;
return;
}
try { try {
ToServerCommand command = (ToServerCommand) pkt->getCommand(); ToServerCommand command = (ToServerCommand) pkt->getCommand();
@ -3287,6 +3262,11 @@ void Server::reportFormspecPrependModified(const std::string &name)
void Server::setIpBanned(const std::string &ip, const std::string &name) void Server::setIpBanned(const std::string &ip, const std::string &name)
{ {
m_banmanager->add(ip, name); m_banmanager->add(ip, name);
auto clients = m_clients.getClientIDs(CS_Created);
for (const auto peer_id : clients) {
denyIfBanned(peer_id);
}
} }
void Server::unsetIpBanned(const std::string &ip_or_name) void Server::unsetIpBanned(const std::string &ip_or_name)
@ -3299,6 +3279,22 @@ std::string Server::getBanDescription(const std::string &ip_or_name)
return m_banmanager->getBanDescription(ip_or_name); return m_banmanager->getBanDescription(ip_or_name);
} }
bool Server::denyIfBanned(session_t peer_id)
{
Address address = getPeerAddress(peer_id);
std::string addr_s = address.serializeString();
if (m_banmanager->isIpBanned(addr_s)) {
std::string ban_name = m_banmanager->getBanName(addr_s);
actionstream << "Server: A banned client tried to connect from "
<< addr_s << "; banned name was " << ban_name << std::endl;
DenyAccess(peer_id, SERVER_ACCESSDENIED_CUSTOM_STRING,
"Your IP is banned. Banned name was " + ban_name);
return true;
}
return false;
}
void Server::notifyPlayer(const char *name, const std::wstring &msg) void Server::notifyPlayer(const char *name, const std::wstring &msg)
{ {
// m_env will be NULL if the server is initializing // m_env will be NULL if the server is initializing

@ -245,6 +245,7 @@ public:
void setIpBanned(const std::string &ip, const std::string &name); void setIpBanned(const std::string &ip, const std::string &name);
void unsetIpBanned(const std::string &ip_or_name); void unsetIpBanned(const std::string &ip_or_name);
std::string getBanDescription(const std::string &ip_or_name); std::string getBanDescription(const std::string &ip_or_name);
bool denyIfBanned(session_t peer_id);
void notifyPlayer(const char *name, const std::wstring &msg); void notifyPlayer(const char *name, const std::wstring &msg);
void notifyPlayers(const std::wstring &msg); void notifyPlayers(const std::wstring &msg);