From e40da2341cfcd6580075571d325105b9a9fe783a Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Fri, 12 Aug 2011 13:11:27 +0300 Subject: [PATCH] Fixed/extended/modified ban stuff to be good for inclusion --- src/ban.cpp | 69 +++++++++++++++++++++++++++++++++++-------- src/ban.h | 13 ++++---- src/server.cpp | 4 ++- src/server.h | 13 +++++--- src/servercommand.cpp | 32 ++++++++++++-------- 5 files changed, 96 insertions(+), 35 deletions(-) diff --git a/src/ban.cpp b/src/ban.cpp index 0d66e8041..398976260 100644 --- a/src/ban.cpp +++ b/src/ban.cpp @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include #include +#include #include "strfnd.h" #include "debug.h" @@ -59,9 +60,14 @@ void BanManager::load() { if(is.eof() || is.good() == false) break; - std::string ip; - std::getline(is, ip, '\n'); - m_ips.insert(ip); + std::string line; + std::getline(is, line, '\n'); + Strfnd f(line); + std::string ip = trim(f.next("|")); + std::string name = trim(f.next("|")); + if(ip.empty()) + continue; + m_ips[ip] = name; } m_modified = false; } @@ -78,34 +84,73 @@ void BanManager::save() throw SerializationError("BanManager::load(): Couldn't open file"); } - for(std::set::iterator + for(std::map::iterator i = m_ips.begin(); i != m_ips.end(); i++) { - if(*i == "") - continue; - os<<*i<<"\n"; + os<first<<"|"<second<<"\n"; } m_modified = false; } -bool BanManager::isIpBanned(std::string ip) +bool BanManager::isIpBanned(const std::string &ip) { JMutexAutoLock lock(m_mutex); return m_ips.find(ip) != m_ips.end(); } -void BanManager::add(std::string ip) +std::string BanManager::getBanDescription(const std::string &ip_or_name) { JMutexAutoLock lock(m_mutex); - m_ips.insert(ip); + std::string s = ""; + for(std::map::iterator + i = m_ips.begin(); + i != m_ips.end(); i++) + { + if(i->first == ip_or_name || i->second == ip_or_name + || ip_or_name == "") + s += i->first + "|" + i->second + ", "; + } + s = s.substr(0, s.size()-2); + return s; +} + +std::string BanManager::getBanName(const std::string &ip) +{ + JMutexAutoLock lock(m_mutex); + std::map::iterator i = m_ips.find(ip); + if(i == m_ips.end()) + return ""; + return i->second; +} + +void BanManager::add(const std::string &ip, const std::string &name) +{ + JMutexAutoLock lock(m_mutex); + m_ips[ip] = name; m_modified = true; } -void BanManager::remove(std::string ip) +void BanManager::remove(const std::string &ip_or_name) { JMutexAutoLock lock(m_mutex); - m_ips.erase(m_ips.find(ip)); + //m_ips.erase(m_ips.find(ip)); + // Find out all ip-name pairs that match the ip or name + std::set ips_to_delete; + for(std::map::iterator + i = m_ips.begin(); + i != m_ips.end(); i++) + { + if(i->first == ip_or_name || i->second == ip_or_name) + ips_to_delete.insert(i->first); + } + // Erase them + for(std::set::iterator + i = ips_to_delete.begin(); + i != ips_to_delete.end(); i++) + { + m_ips.erase(*i); + } m_modified = true; } diff --git a/src/ban.h b/src/ban.h index 38b08b604..8fdcf4c6a 100644 --- a/src/ban.h +++ b/src/ban.h @@ -20,7 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef BAN_HEADER #define BAN_HEADER -#include +#include #include #include #include @@ -34,14 +34,17 @@ public: ~BanManager(); void load(); void save(); - void add(std::string ip); - void remove(std::string ip); - bool isIpBanned(std::string ip); + bool isIpBanned(const std::string &ip); + // Supplying ip_or_name = "" lists all bans. + std::string getBanDescription(const std::string &ip_or_name); + std::string getBanName(const std::string &ip); + void add(const std::string &ip, const std::string &name); + void remove(const std::string &ip_or_name); bool isModified(); private: JMutex m_mutex; std::string m_banfilepath; - std::set m_ips; + std::map m_ips; bool m_modified; }; diff --git a/src/server.cpp b/src/server.cpp index 65ea45330..383381c9f 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1932,7 +1932,9 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) // drop player if is ip is banned if(m_banmanager.isIpBanned(peer->address.serializeString())){ SendAccessDenied(m_con, peer_id, - L"Your ip is banned!"); + L"Your ip is banned. Banned name was " + +narrow_to_wide(m_banmanager.getBanName( + peer->address.serializeString()))); m_con.deletePeer(peer_id, false); return; } diff --git a/src/server.h b/src/server.h index df2d38e65..7065efacf 100644 --- a/src/server.h +++ b/src/server.h @@ -456,18 +456,23 @@ public: g_settings.updateConfigFile(m_configpath.c_str()); } - void setIpBanned(std::string ip) + void setIpBanned(const std::string &ip, const std::string &name) { - m_banmanager.add(ip); + m_banmanager.add(ip, name); return; } - void unsetIpBanned(std::string ip) + void unsetIpBanned(const std::string &ip_or_name) { - m_banmanager.remove(ip); + m_banmanager.remove(ip_or_name); return; } + std::string getBanDescription(const std::string &ip_or_name) + { + return m_banmanager.getBanDescription(ip_or_name); + } + con::Peer* getPeerNoEx(u16 peer_id) { return m_con.GetPeerNoEx(peer_id); diff --git a/src/servercommand.cpp b/src/servercommand.cpp index 87d7cd363..bf174497d 100644 --- a/src/servercommand.cpp +++ b/src/servercommand.cpp @@ -183,7 +183,7 @@ void cmd_teleport(std::wostringstream &os, os<< L"-!- Teleported."; } -void cmd_ipbanunban(std::wostringstream &os, ServerCommandContext *ctx) +void cmd_banunban(std::wostringstream &os, ServerCommandContext *ctx) { if((ctx->privs && PRIV_BAN) == 0) { @@ -191,33 +191,39 @@ void cmd_ipbanunban(std::wostringstream &os, ServerCommandContext *ctx) return; } - if(ctx->parms.size() != 2) + if(ctx->parms.size() < 2) { - os<server->getBanDescription(""); + os<parms[0] == L"ipban") + if(ctx->parms[0] == L"ban") { Player *player = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str()); if(player == NULL) { - os<server->getPeerNoEx(player->peer_id); if(peer == NULL) { - dstream<<"peer was not found!"<server->setIpBanned(peer->address.serializeString()); - os<address.serializeString())<address.serializeString(); + ctx->server->setIpBanned(ip_string, player->getName()); + os<getName()); } else { - ctx->server->unsetIpBanned(wide_to_narrow(ctx->parms[1])); - os<parms[1]<parms[1]); + std::string desc = ctx->server->getBanDescription(ip_or_name); + ctx->server->unsetIpBanned(ip_or_name); + os<parms[0] == L"status") { @@ -273,9 +279,9 @@ std::wstring processServerCommand(ServerCommandContext *ctx) { cmd_teleport(os, ctx); } - else if(ctx->parms[0] == L"ipban" || ctx->parms[0] == L"ipunban") + else if(ctx->parms[0] == L"ban" || ctx->parms[0] == L"unban") { - cmd_ipbanunban(os, ctx); + cmd_banunban(os, ctx); } else {