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 <fstream>
|
||||||
#include <jmutexautolock.h>
|
#include <jmutexautolock.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <set>
|
||||||
#include "strfnd.h"
|
#include "strfnd.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
|
||||||
|
@ -59,9 +60,14 @@ void BanManager::load()
|
||||||
{
|
{
|
||||||
if(is.eof() || is.good() == false)
|
if(is.eof() || is.good() == false)
|
||||||
break;
|
break;
|
||||||
std::string ip;
|
std::string line;
|
||||||
std::getline(is, ip, '\n');
|
std::getline(is, line, '\n');
|
||||||
m_ips.insert(ip);
|
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;
|
m_modified = false;
|
||||||
}
|
}
|
||||||
|
@ -78,34 +84,73 @@ void BanManager::save()
|
||||||
throw SerializationError("BanManager::load(): Couldn't open file");
|
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.begin();
|
||||||
i != m_ips.end(); i++)
|
i != m_ips.end(); i++)
|
||||||
{
|
{
|
||||||
if(*i == "")
|
os<<i->first<<"|"<<i->second<<"\n";
|
||||||
continue;
|
|
||||||
os<<*i<<"\n";
|
|
||||||
}
|
}
|
||||||
m_modified = false;
|
m_modified = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BanManager::isIpBanned(std::string ip)
|
bool BanManager::isIpBanned(const std::string &ip)
|
||||||
{
|
{
|
||||||
JMutexAutoLock lock(m_mutex);
|
JMutexAutoLock lock(m_mutex);
|
||||||
return m_ips.find(ip) != m_ips.end();
|
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);
|
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;
|
m_modified = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BanManager::remove(std::string ip)
|
void BanManager::remove(const std::string &ip_or_name)
|
||||||
{
|
{
|
||||||
JMutexAutoLock lock(m_mutex);
|
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;
|
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
|
#ifndef BAN_HEADER
|
||||||
#define BAN_HEADER
|
#define BAN_HEADER
|
||||||
|
|
||||||
#include <set>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <jthread.h>
|
#include <jthread.h>
|
||||||
#include <jmutex.h>
|
#include <jmutex.h>
|
||||||
|
@ -34,14 +34,17 @@ public:
|
||||||
~BanManager();
|
~BanManager();
|
||||||
void load();
|
void load();
|
||||||
void save();
|
void save();
|
||||||
void add(std::string ip);
|
bool isIpBanned(const std::string &ip);
|
||||||
void remove(std::string ip);
|
// Supplying ip_or_name = "" lists all bans.
|
||||||
bool isIpBanned(std::string ip);
|
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();
|
bool isModified();
|
||||||
private:
|
private:
|
||||||
JMutex m_mutex;
|
JMutex m_mutex;
|
||||||
std::string m_banfilepath;
|
std::string m_banfilepath;
|
||||||
std::set<std::string> m_ips;
|
std::map<std::string, std::string> m_ips;
|
||||||
bool m_modified;
|
bool m_modified;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -1932,7 +1932,9 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
|
||||||
// drop player if is ip is banned
|
// drop player if is ip is banned
|
||||||
if(m_banmanager.isIpBanned(peer->address.serializeString())){
|
if(m_banmanager.isIpBanned(peer->address.serializeString())){
|
||||||
SendAccessDenied(m_con, peer_id,
|
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);
|
m_con.deletePeer(peer_id, false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
13
src/server.h
13
src/server.h
|
@ -456,18 +456,23 @@ public:
|
||||||
g_settings.updateConfigFile(m_configpath.c_str());
|
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;
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string getBanDescription(const std::string &ip_or_name)
|
||||||
|
{
|
||||||
|
return m_banmanager.getBanDescription(ip_or_name);
|
||||||
|
}
|
||||||
|
|
||||||
con::Peer* getPeerNoEx(u16 peer_id)
|
con::Peer* getPeerNoEx(u16 peer_id)
|
||||||
{
|
{
|
||||||
return m_con.GetPeerNoEx(peer_id);
|
return m_con.GetPeerNoEx(peer_id);
|
||||||
|
|
|
@ -183,7 +183,7 @@ void cmd_teleport(std::wostringstream &os,
|
||||||
os<< L"-!- Teleported.";
|
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)
|
if((ctx->privs && PRIV_BAN) == 0)
|
||||||
{
|
{
|
||||||
|
@ -191,33 +191,39 @@ void cmd_ipbanunban(std::wostringstream &os, ServerCommandContext *ctx)
|
||||||
return;
|
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;
|
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());
|
Player *player = ctx->env->getPlayer(wide_to_narrow(ctx->parms[1]).c_str());
|
||||||
|
|
||||||
if(player == NULL)
|
if(player == NULL)
|
||||||
{
|
{
|
||||||
os<<L"-!- No such Player!";
|
os<<L"-!- No such player";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
con::Peer *peer = ctx->server->getPeerNoEx(player->peer_id);
|
con::Peer *peer = ctx->server->getPeerNoEx(player->peer_id);
|
||||||
if(peer == NULL)
|
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());
|
std::string ip_string = peer->address.serializeString();
|
||||||
os<<L"-!- IP: "<<narrow_to_wide(peer->address.serializeString())<<L" was banned!";
|
ctx->server->setIpBanned(ip_string, player->getName());
|
||||||
|
os<<L"-!- Banned "<<narrow_to_wide(ip_string)<<L"|"
|
||||||
|
<<narrow_to_wide(player->getName());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ctx->server->unsetIpBanned(wide_to_narrow(ctx->parms[1]));
|
std::string ip_or_name = wide_to_narrow(ctx->parms[1]);
|
||||||
os<<L"-!- IP: "<<ctx->parms[1]<<L" was unbanned!";
|
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)
|
if(privs & PRIV_PRIVS)
|
||||||
os<<L" grant revoke";
|
os<<L" grant revoke";
|
||||||
if(privs & PRIV_BAN)
|
if(privs & PRIV_BAN)
|
||||||
os<<L" ipban ipunban";
|
os<<L" ban unban";
|
||||||
}
|
}
|
||||||
else if(ctx->parms[0] == L"status")
|
else if(ctx->parms[0] == L"status")
|
||||||
{
|
{
|
||||||
|
@ -273,9 +279,9 @@ std::wstring processServerCommand(ServerCommandContext *ctx)
|
||||||
{
|
{
|
||||||
cmd_teleport(os, 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
|
else
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue