Adding IP address bans.
This commit is contained in:
@@ -265,40 +265,66 @@ ServerLobbyThread::BanPlayerRegex(const string &playerRegex)
|
||||
m_banPlayerNameMap[++m_curBanId] = boost::regex(playerRegex, boost::regex_constants::no_except);
|
||||
}
|
||||
|
||||
void
|
||||
ServerLobbyThread::BanIPAddress(const string &ipAddress)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_banMutex);
|
||||
m_banIPAddressMap[++m_curBanId] = ipAddress;
|
||||
}
|
||||
|
||||
bool
|
||||
ServerLobbyThread::UnBanPlayerRegex(unsigned banId)
|
||||
ServerLobbyThread::UnBan(unsigned banId)
|
||||
{
|
||||
bool retVal = false;
|
||||
boost::mutex::scoped_lock lock(m_banMutex);
|
||||
RegexMap::iterator pos = m_banPlayerNameMap.find(banId);
|
||||
if (pos != m_banPlayerNameMap.end())
|
||||
RegexMap::iterator posNick = m_banPlayerNameMap.find(banId);
|
||||
if (posNick != m_banPlayerNameMap.end())
|
||||
{
|
||||
m_banPlayerNameMap.erase(pos);
|
||||
m_banPlayerNameMap.erase(posNick);
|
||||
retVal = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
IPAddressMap::iterator posIP = m_banIPAddressMap.find(banId);
|
||||
if (posIP != m_banIPAddressMap.end())
|
||||
{
|
||||
m_banIPAddressMap.erase(posIP);
|
||||
retVal = true;
|
||||
}
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
void
|
||||
ServerLobbyThread::GetBanPlayerList(list<string> &list) const
|
||||
ServerLobbyThread::GetBanList(list<string> &list) const
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_banMutex);
|
||||
RegexMap::const_iterator i = m_banPlayerNameMap.begin();
|
||||
RegexMap::const_iterator end = m_banPlayerNameMap.end();
|
||||
while (i != end)
|
||||
RegexMap::const_iterator i_nick = m_banPlayerNameMap.begin();
|
||||
RegexMap::const_iterator end_nick = m_banPlayerNameMap.end();
|
||||
while (i_nick != end_nick)
|
||||
{
|
||||
ostringstream banText;
|
||||
banText << (*i).first << ": " << (*i).second.str();
|
||||
banText << (*i_nick).first << " (nick): " << (*i_nick).second.str();
|
||||
list.push_back(banText.str());
|
||||
++i;
|
||||
++i_nick;
|
||||
}
|
||||
IPAddressMap::const_iterator i_ip = m_banIPAddressMap.begin();
|
||||
IPAddressMap::const_iterator end_ip = m_banIPAddressMap.end();
|
||||
while (i_ip != end_ip)
|
||||
{
|
||||
ostringstream banText;
|
||||
banText << (*i_ip).first << " (IP): " << (*i_ip).second;
|
||||
list.push_back(banText.str());
|
||||
++i_ip;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ServerLobbyThread::ClearBanPlayerList()
|
||||
ServerLobbyThread::ClearBanList()
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_banMutex);
|
||||
m_banPlayerNameMap.clear();
|
||||
m_banIPAddressMap.clear();
|
||||
}
|
||||
|
||||
string
|
||||
@@ -575,6 +601,12 @@ ServerLobbyThread::HandleNetPacketInit(SessionWrapper session, const NetPacketIn
|
||||
SessionError(session, ERR_NET_PLAYER_BANNED);
|
||||
return;
|
||||
}
|
||||
// Check whether the peer IP address is banned.
|
||||
if (IsIPAddressBanned(session.sessionData->GetClientAddr()))
|
||||
{
|
||||
SessionError(session, ERR_NET_PLAYER_BANNED);
|
||||
return;
|
||||
}
|
||||
|
||||
// Create player data object.
|
||||
boost::shared_ptr<PlayerData> tmpPlayerData(
|
||||
@@ -1441,6 +1473,26 @@ ServerLobbyThread::IsPlayerBanned(const std::string &name) const
|
||||
return retVal;
|
||||
}
|
||||
|
||||
bool
|
||||
ServerLobbyThread::IsIPAddressBanned(const std::string &ipAddress) const
|
||||
{
|
||||
bool retVal = false;
|
||||
boost::mutex::scoped_lock lock(m_banMutex);
|
||||
IPAddressMap::const_iterator i = m_banIPAddressMap.begin();
|
||||
IPAddressMap::const_iterator end = m_banIPAddressMap.end();
|
||||
while (i != end)
|
||||
{
|
||||
if (ipAddress == (*i).second)
|
||||
{
|
||||
retVal = true;
|
||||
break;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
boost::shared_ptr<NetPacket>
|
||||
ServerLobbyThread::CreateNetPacketGameListNew(const ServerGameThread &game)
|
||||
{
|
||||
|
||||
@@ -121,12 +121,12 @@ ServerManager::SignalIrcChatMsg(const std::string &nickName, const std::string &
|
||||
{
|
||||
string ipAddress(GetLobbyThread().GetPlayerIPAddress(playerName));
|
||||
if (!ipAddress.empty())
|
||||
m_ircThread->SendChatMessage(nickName + ": The IP address of player \"" + playerName + "\" is: " + ipAddress);
|
||||
m_ircThread->SendChatMessage(nickName + ": The IP address of player \"" + playerName + "\" is: \"" + ipAddress + "\"");
|
||||
else
|
||||
m_ircThread->SendChatMessage(nickName + ": The IP address of player \"" + playerName + "\" is unknown.");
|
||||
}
|
||||
}
|
||||
else if (command == "nickban")
|
||||
else if (command == "bannick")
|
||||
{
|
||||
while (msgStream.peek() == ' ')
|
||||
msgStream.get();
|
||||
@@ -137,10 +137,21 @@ ServerManager::SignalIrcChatMsg(const std::string &nickName, const std::string &
|
||||
m_ircThread->SendChatMessage(nickName + ": The regex \"" + playerRegex + "\" was added to the player ban list.");
|
||||
}
|
||||
}
|
||||
else if (command == "banip")
|
||||
{
|
||||
while (msgStream.peek() == ' ')
|
||||
msgStream.get();
|
||||
string ipAddress(msgStream.str().substr(msgStream.tellg()));
|
||||
if (!ipAddress.empty())
|
||||
{
|
||||
GetLobbyThread().BanIPAddress(ipAddress);
|
||||
m_ircThread->SendChatMessage(nickName + ": The IP address \"" + ipAddress + "\" was added to the IP address ban list.");
|
||||
}
|
||||
}
|
||||
else if (command == "listban")
|
||||
{
|
||||
list<string> banList;
|
||||
GetLobbyThread().GetBanPlayerList(banList);
|
||||
GetLobbyThread().GetBanList(banList);
|
||||
list<string>::const_iterator i = banList.begin();
|
||||
list<string>::const_iterator end = banList.end();
|
||||
while (i != end)
|
||||
@@ -157,14 +168,14 @@ ServerManager::SignalIrcChatMsg(const std::string &nickName, const std::string &
|
||||
{
|
||||
unsigned banId = 0;
|
||||
msgStream >> banId;
|
||||
if (GetLobbyThread().UnBanPlayerRegex(banId))
|
||||
if (GetLobbyThread().UnBan(banId))
|
||||
m_ircThread->SendChatMessage(nickName + ": The nick ban was successfully removed.");
|
||||
else
|
||||
m_ircThread->SendChatMessage(nickName + ": This nick ban does not exist.");
|
||||
}
|
||||
else if (command == "clearban")
|
||||
{
|
||||
GetLobbyThread().ClearBanPlayerList();
|
||||
GetLobbyThread().ClearBanList();
|
||||
m_ircThread->SendChatMessage(nickName + ": All ban lists were cleared.");
|
||||
}
|
||||
else if (command == "stat")
|
||||
|
||||
@@ -70,9 +70,10 @@ public:
|
||||
|
||||
bool KickPlayerByName(const std::string &playerName);
|
||||
void BanPlayerRegex(const std::string &playerRegex);
|
||||
bool UnBanPlayerRegex(unsigned banId);
|
||||
void GetBanPlayerList(std::list<std::string> &list) const;
|
||||
void ClearBanPlayerList();
|
||||
void BanIPAddress(const std::string &ipAddress);
|
||||
bool UnBan(unsigned banId);
|
||||
void GetBanList(std::list<std::string> &list) const;
|
||||
void ClearBanList();
|
||||
std::string GetPlayerIPAddress(const std::string &playerName) const;
|
||||
void RemovePlayer(unsigned playerId, unsigned errorCode);
|
||||
|
||||
@@ -106,6 +107,7 @@ protected:
|
||||
typedef std::map<std::string, boost::timers::portable::microsec_timer> TimerClientAddressMap;
|
||||
typedef std::list<unsigned> RemoveGameList;
|
||||
typedef std::map<unsigned, boost::regex> RegexMap;
|
||||
typedef std::map<unsigned, std::string> IPAddressMap;
|
||||
|
||||
// Main function of the thread.
|
||||
virtual void Main();
|
||||
@@ -165,6 +167,7 @@ protected:
|
||||
|
||||
bool IsPlayerConnected(const std::string &name) const;
|
||||
bool IsPlayerBanned(const std::string &name) const;
|
||||
bool IsIPAddressBanned(const std::string &ipAddress) const;
|
||||
|
||||
static boost::shared_ptr<NetPacket> CreateNetPacketGameListNew(const ServerGameThread &game);
|
||||
static boost::shared_ptr<NetPacket> CreateNetPacketGameListUpdate(unsigned gameId, GameMode mode);
|
||||
@@ -196,6 +199,7 @@ private:
|
||||
mutable boost::mutex m_resubscribeListMutex;
|
||||
|
||||
RegexMap m_banPlayerNameMap;
|
||||
IPAddressMap m_banIPAddressMap;
|
||||
unsigned m_curBanId;
|
||||
mutable boost::mutex m_banMutex;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user