mirror of
https://github.com/minetest/minetest.git
synced 2024-12-28 00:47:29 +01:00
Fixed/extended/modified ban stuff to be good for inclusion
This commit is contained in:
parent
7aa72c56b6
commit
e40da2341c
69
src/ban.cpp
69
src/ban.cpp
@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
#include <fstream>
|
||||
#include <jmutexautolock.h>
|
||||
#include <sstream>
|
||||
#include <set>
|
||||
#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<std::string>::iterator
|
||||
for(std::map<std::string, std::string>::iterator
|
||||
i = m_ips.begin();
|
||||
i != m_ips.end(); i++)
|
||||
{
|
||||
if(*i == "")
|
||||
continue;
|
||||
os<<*i<<"\n";
|
||||
os<<i->first<<"|"<<i->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<std::string, std::string>::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<std::string, std::string>::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<std::string> ips_to_delete;
|
||||
for(std::map<std::string, std::string>::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<std::string>::iterator
|
||||
i = ips_to_delete.begin();
|
||||
i != ips_to_delete.end(); i++)
|
||||
{
|
||||
m_ips.erase(*i);
|
||||
}
|
||||
m_modified = true;
|
||||
}
|
||||
|
||||
|
13
src/ban.h
13
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 <set>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <jthread.h>
|
||||
#include <jmutex.h>
|
||||
@ -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<std::string> m_ips;
|
||||
std::map<std::string, std::string> m_ips;
|
||||
bool m_modified;
|
||||
|
||||
};
|
||||
|
@ -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;
|
||||
}
|
||||
|
13
src/server.h
13
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);
|
||||
|
@ -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<<L"-!- Missing parameter";
|
||||
std::string desc = ctx->server->getBanDescription("");
|
||||
os<<L"-!- Ban list: "<<narrow_to_wide(desc);
|
||||
return;
|
||||
}
|
||||
if(ctx->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<<L"-!- No such Player!";
|
||||
os<<L"-!- No such player";
|
||||
return;
|
||||
}
|
||||
|
||||
con::Peer *peer = ctx->server->getPeerNoEx(player->peer_id);
|
||||
if(peer == NULL)
|
||||
{
|
||||
dstream<<"peer was not found!"<<std::endl;
|
||||
dstream<<__FUNCTION_NAME<<": peer was not found"<<std::endl;
|
||||
return;
|
||||
}
|
||||
ctx->server->setIpBanned(peer->address.serializeString());
|
||||
os<<L"-!- IP: "<<narrow_to_wide(peer->address.serializeString())<<L" was banned!";
|
||||
std::string ip_string = peer->address.serializeString();
|
||||
ctx->server->setIpBanned(ip_string, player->getName());
|
||||
os<<L"-!- Banned "<<narrow_to_wide(ip_string)<<L"|"
|
||||
<<narrow_to_wide(player->getName());
|
||||
}
|
||||
else
|
||||
{
|
||||
ctx->server->unsetIpBanned(wide_to_narrow(ctx->parms[1]));
|
||||
os<<L"-!- IP: "<<ctx->parms[1]<<L" was unbanned!";
|
||||
std::string ip_or_name = wide_to_narrow(ctx->parms[1]);
|
||||
std::string desc = ctx->server->getBanDescription(ip_or_name);
|
||||
ctx->server->unsetIpBanned(ip_or_name);
|
||||
os<<L"-!- Unbanned "<<narrow_to_wide(desc);
|
||||
}
|
||||
}
|
||||
|
||||
@ -243,7 +249,7 @@ std::wstring processServerCommand(ServerCommandContext *ctx)
|
||||
if(privs & PRIV_PRIVS)
|
||||
os<<L" grant revoke";
|
||||
if(privs & PRIV_BAN)
|
||||
os<<L" ipban ipunban";
|
||||
os<<L" ban unban";
|
||||
}
|
||||
else if(ctx->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
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user