IP address bans are now time limited and will be removed automatically (minimum duration: 1 hour).
This commit is contained in:
@@ -22,8 +22,8 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
ServerBanManager::ServerBanManager()
|
ServerBanManager::ServerBanManager(boost::shared_ptr<boost::asio::io_service> ioService)
|
||||||
: m_curBanId(0)
|
: m_ioService(ioService), m_curBanId(0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,10 +39,18 @@ ServerBanManager::BanPlayerRegex(const string &playerRegex)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
ServerBanManager::BanIPAddress(const string &ipAddress)
|
ServerBanManager::BanIPAddress(const string &ipAddress, unsigned durationHours)
|
||||||
{
|
{
|
||||||
boost::mutex::scoped_lock lock(m_banMutex);
|
boost::mutex::scoped_lock lock(m_banMutex);
|
||||||
m_banIPAddressMap[GetNextBanId()] = ipAddress;
|
unsigned banId = GetNextBanId();
|
||||||
|
boost::shared_ptr<TimedIPAddress> tmpItem(new TimedIPAddress(ipAddress, *m_ioService));
|
||||||
|
tmpItem->timer.expires_from_now(
|
||||||
|
boost::posix_time::hours(durationHours));
|
||||||
|
tmpItem->timer.async_wait(
|
||||||
|
boost::bind(
|
||||||
|
&ServerBanManager::TimerRemoveIPBan, shared_from_this(), boost::asio::placeholders::error, banId, tmpItem));
|
||||||
|
|
||||||
|
m_banIPAddressMap[banId] = tmpItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@@ -61,6 +69,7 @@ ServerBanManager::UnBan(unsigned banId)
|
|||||||
IPAddressMap::iterator posIP = m_banIPAddressMap.find(banId);
|
IPAddressMap::iterator posIP = m_banIPAddressMap.find(banId);
|
||||||
if (posIP != m_banIPAddressMap.end())
|
if (posIP != m_banIPAddressMap.end())
|
||||||
{
|
{
|
||||||
|
posIP->second->timer.cancel();
|
||||||
m_banIPAddressMap.erase(posIP);
|
m_banIPAddressMap.erase(posIP);
|
||||||
retVal = true;
|
retVal = true;
|
||||||
}
|
}
|
||||||
@@ -86,7 +95,7 @@ ServerBanManager::GetBanList(list<string> &list) const
|
|||||||
while (i_ip != end_ip)
|
while (i_ip != end_ip)
|
||||||
{
|
{
|
||||||
ostringstream banText;
|
ostringstream banText;
|
||||||
banText << (*i_ip).first << ": (IP) - " << (*i_ip).second;
|
banText << (*i_ip).first << ": (IP) - " << (*i_ip).second->ipAddress << " duration: " << (*i_ip).second->timer.expires_from_now().hours() << "h";
|
||||||
list.push_back(banText.str());
|
list.push_back(banText.str());
|
||||||
++i_ip;
|
++i_ip;
|
||||||
}
|
}
|
||||||
@@ -129,7 +138,7 @@ ServerBanManager::IsIPAddressBanned(const std::string &ipAddress) const
|
|||||||
IPAddressMap::const_iterator end = m_banIPAddressMap.end();
|
IPAddressMap::const_iterator end = m_banIPAddressMap.end();
|
||||||
while (i != end)
|
while (i != end)
|
||||||
{
|
{
|
||||||
if (ipAddress == (*i).second)
|
if (ipAddress == (*i).second->ipAddress)
|
||||||
{
|
{
|
||||||
retVal = true;
|
retVal = true;
|
||||||
break;
|
break;
|
||||||
@@ -140,6 +149,13 @@ ServerBanManager::IsIPAddressBanned(const std::string &ipAddress) const
|
|||||||
return retVal;
|
return retVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ServerBanManager::TimerRemoveIPBan(const boost::system::error_code &ec, unsigned timerId, boost::shared_ptr<TimedIPAddress> item)
|
||||||
|
{
|
||||||
|
if (!ec && item)
|
||||||
|
UnBan(timerId);
|
||||||
|
}
|
||||||
|
|
||||||
unsigned
|
unsigned
|
||||||
ServerBanManager::GetNextBanId()
|
ServerBanManager::GetNextBanId()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ ServerLobbyThread::ServerLobbyThread(GuiInterface &gui, ConfigFile *playerConfig
|
|||||||
m_senderCallback.reset(new ServerSenderCallback(*this));
|
m_senderCallback.reset(new ServerSenderCallback(*this));
|
||||||
m_sender.reset(new SenderHelper(*m_senderCallback, m_ioService));
|
m_sender.reset(new SenderHelper(*m_senderCallback, m_ioService));
|
||||||
m_receiver.reset(new ReceiverHelper);
|
m_receiver.reset(new ReceiverHelper);
|
||||||
m_banManager.reset(new ServerBanManager);
|
m_banManager.reset(new ServerBanManager(m_ioService));
|
||||||
}
|
}
|
||||||
|
|
||||||
ServerLobbyThread::~ServerLobbyThread()
|
ServerLobbyThread::~ServerLobbyThread()
|
||||||
|
|||||||
@@ -141,11 +141,19 @@ ServerManager::SignalIrcChatMsg(const std::string &nickName, const std::string &
|
|||||||
{
|
{
|
||||||
while (msgStream.peek() == ' ')
|
while (msgStream.peek() == ' ')
|
||||||
msgStream.get();
|
msgStream.get();
|
||||||
string ipAddress(msgStream.str().substr(msgStream.tellg()));
|
string ipAddress;
|
||||||
|
unsigned durationHours = 12;
|
||||||
|
msgStream >> ipAddress;
|
||||||
|
while (msgStream.peek() == ' ')
|
||||||
|
msgStream.get();
|
||||||
|
if (!msgStream.eof())
|
||||||
|
msgStream >> durationHours;
|
||||||
if (!ipAddress.empty())
|
if (!ipAddress.empty())
|
||||||
{
|
{
|
||||||
GetLobbyThread().GetBanManager().BanIPAddress(ipAddress);
|
ostringstream durationStr;
|
||||||
m_ircThread->SendChatMessage(nickName + ": The IP address \"" + ipAddress + "\" was added to the IP address ban list.");
|
durationStr << durationHours;
|
||||||
|
GetLobbyThread().GetBanManager().BanIPAddress(ipAddress, durationHours);
|
||||||
|
m_ircThread->SendChatMessage(nickName + ": The IP address \"" + ipAddress + "\" was added to the IP address ban list for " + durationStr.str() + (durationHours == 1 ? " hour." : " hours."));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (command == "listban")
|
else if (command == "listban")
|
||||||
|
|||||||
@@ -23,17 +23,19 @@
|
|||||||
|
|
||||||
#include <boost/regex.hpp>
|
#include <boost/regex.hpp>
|
||||||
#include <boost/thread.hpp>
|
#include <boost/thread.hpp>
|
||||||
|
#include <boost/asio.hpp>
|
||||||
|
#include <boost/enable_shared_from_this.hpp>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class ServerBanManager
|
class ServerBanManager : public boost::enable_shared_from_this<ServerBanManager>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ServerBanManager();
|
ServerBanManager(boost::shared_ptr<boost::asio::io_service> ioService);
|
||||||
virtual ~ServerBanManager();
|
virtual ~ServerBanManager();
|
||||||
|
|
||||||
void BanPlayerRegex(const std::string &playerRegex);
|
void BanPlayerRegex(const std::string &playerRegex);
|
||||||
void BanIPAddress(const std::string &ipAddress);
|
void BanIPAddress(const std::string &ipAddress, unsigned durationHours);
|
||||||
bool UnBan(unsigned banId);
|
bool UnBan(unsigned banId);
|
||||||
void GetBanList(std::list<std::string> &list) const;
|
void GetBanList(std::list<std::string> &list) const;
|
||||||
void ClearBanList();
|
void ClearBanList();
|
||||||
@@ -42,8 +44,20 @@ public:
|
|||||||
bool IsIPAddressBanned(const std::string &ipAddress) const;
|
bool IsIPAddressBanned(const std::string &ipAddress) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
struct TimedIPAddress
|
||||||
|
{
|
||||||
|
TimedIPAddress(const std::string &i, boost::asio::io_service &s)
|
||||||
|
: ipAddress(i), timer(s) {}
|
||||||
|
std::string ipAddress;
|
||||||
|
boost::asio::deadline_timer timer;
|
||||||
|
};
|
||||||
|
|
||||||
|
void TimerRemoveIPBan(const boost::system::error_code &ec, unsigned timerId, boost::shared_ptr<TimedIPAddress> item);
|
||||||
|
|
||||||
|
boost::shared_ptr<boost::asio::io_service> m_ioService;
|
||||||
|
|
||||||
typedef std::map<unsigned, boost::regex> RegexMap;
|
typedef std::map<unsigned, boost::regex> RegexMap;
|
||||||
typedef std::map<unsigned, std::string> IPAddressMap;
|
typedef std::map<unsigned, boost::shared_ptr<TimedIPAddress> > IPAddressMap;
|
||||||
|
|
||||||
unsigned GetNextBanId();
|
unsigned GetNextBanId();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user