]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_connflood.cpp
Raise <connflood:bootwait> from 10 seconds to 2 minutes.
[user/henk/code/inspircd.git] / src / modules / m_connflood.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
5  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "inspircd.h"
23
24 class ModuleConnFlood : public Module
25 {
26         unsigned int seconds;
27         unsigned int timeout;
28         unsigned int boot_wait;
29         unsigned int conns;
30         unsigned int maxconns;
31         bool throttled;
32         time_t first;
33         std::string quitmsg;
34
35 public:
36         ModuleConnFlood()
37                 : conns(0), throttled(false)
38         {
39         }
40
41         Version GetVersion() CXX11_OVERRIDE
42         {
43                 return Version("Connection throttle", VF_VENDOR);
44         }
45
46         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
47         {
48                 /* read configuration variables */
49                 ConfigTag* tag = ServerInstance->Config->ConfValue("connflood");
50                 /* throttle configuration */
51                 seconds = tag->getDuration("period", tag->getDuration("seconds", 30));
52                 maxconns = tag->getUInt("maxconns", 3);
53                 timeout = tag->getDuration("timeout", 30);
54                 quitmsg = tag->getString("quitmsg");
55
56                 /* seconds to wait when the server just booted */
57                 boot_wait = tag->getDuration("bootwait", 60*2);
58
59                 first = ServerInstance->Time();
60         }
61
62         ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
63         {
64                 if (user->exempt)
65                         return MOD_RES_PASSTHRU;
66
67                 time_t next = ServerInstance->Time();
68
69                 if ((ServerInstance->startup_time + boot_wait) > next)
70                         return MOD_RES_PASSTHRU;
71
72                 /* time difference between first and latest connection */
73                 time_t tdiff = next - first;
74
75                 /* increase connection count */
76                 conns++;
77
78                 if (throttled)
79                 {
80                         if (tdiff > seconds + timeout)
81                         {
82                                 /* expire throttle */
83                                 throttled = false;
84                                 ServerInstance->SNO->WriteGlobalSno('a', "Connection throttle deactivated");
85                                 return MOD_RES_PASSTHRU;
86                         }
87
88                         ServerInstance->Users->QuitUser(user, quitmsg);
89                         return MOD_RES_DENY;
90                 }
91
92                 if (tdiff <= seconds)
93                 {
94                         if (conns >= maxconns)
95                         {
96                                 throttled = true;
97                                 ServerInstance->SNO->WriteGlobalSno('a', "Connection throttle activated");
98                                 ServerInstance->Users->QuitUser(user, quitmsg);
99                                 return MOD_RES_DENY;
100                         }
101                 }
102                 else
103                 {
104                         conns = 1;
105                         first = next;
106                 }
107                 return MOD_RES_PASSTHRU;
108         }
109 };
110
111 MODULE_INIT(ModuleConnFlood)