2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "modules/webirc.h"
24 class ModuleConnectBan
26 , public WebIRC::EventListener
28 typedef std::map<irc::sockets::cidr_mask, unsigned int> ConnectMap;
30 unsigned int threshold;
31 unsigned int banduration;
32 unsigned int ipv4_cidr;
33 unsigned int ipv6_cidr;
34 std::string banmessage;
36 unsigned char GetRange(LocalUser* user)
38 int family = user->client_sa.family();
48 // Ranges for UNIX sockets are ignored entirely.
52 // If we have reached this point then we have encountered a bug.
53 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "BUG: ModuleConnectBan::GetRange(): socket type %d is unknown!", family);
59 : WebIRC::EventListener(this)
63 Version GetVersion() CXX11_OVERRIDE
65 return Version("Throttles the connections of IP ranges who try to connect flood", VF_VENDOR);
68 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
70 ConfigTag* tag = ServerInstance->Config->ConfValue("connectban");
72 ipv4_cidr = tag->getUInt("ipv4cidr", 32, 1, 32);
73 ipv6_cidr = tag->getUInt("ipv6cidr", 128, 1, 128);
74 threshold = tag->getUInt("threshold", 10, 1);
75 banduration = tag->getDuration("duration", 10*60, 1);
76 banmessage = tag->getString("banmessage", "Your IP range has been attempting to connect too many times in too short a duration. Wait a while, and you will be able to connect.");
79 void OnWebIRCAuth(LocalUser* user, const WebIRC::FlagMap* flags) CXX11_OVERRIDE
84 // HACK: Lower the connection attempts for the gateway IP address. The user
85 // will be rechecked for connect spamming shortly after when their IP address
86 // is changed and OnSetUserIP is called.
87 irc::sockets::cidr_mask mask(user->client_sa, GetRange(user));
88 ConnectMap::iterator iter = connects.find(mask);
89 if (iter != connects.end() && iter->second)
93 void OnSetUserIP(LocalUser* u) CXX11_OVERRIDE
98 irc::sockets::cidr_mask mask(u->client_sa, GetRange(u));
99 ConnectMap::iterator i = connects.find(mask);
101 if (i != connects.end())
105 if (i->second >= threshold)
107 // Create Z-line for set duration.
108 ZLine* zl = new ZLine(ServerInstance->Time(), banduration, ServerInstance->Config->ServerName, banmessage, mask.str());
109 if (!ServerInstance->XLines->AddLine(zl, NULL))
114 ServerInstance->XLines->ApplyLines();
115 std::string maskstr = mask.str();
116 ServerInstance->SNO->WriteGlobalSno('x', "Z-line added by module m_connectban on %s to expire in %s (on %s): Connect flooding",
117 maskstr.c_str(), InspIRCd::DurationString(zl->duration).c_str(), InspIRCd::TimeString(zl->expiry).c_str());
118 ServerInstance->SNO->WriteGlobalSno('a', "Connect flooding from IP range %s (%d)", maskstr.c_str(), threshold);
128 void OnGarbageCollect() CXX11_OVERRIDE
130 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Clearing map.");
135 MODULE_INIT(ModuleConnectBan)