]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_connflood.cpp
Merge pull request #251 from Shawn-Smith/insp20+extbanU
[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 /* $ModDesc: Connection throttle */
25
26 class ModuleConnFlood : public Module
27 {
28 private:
29         int seconds, timeout, boot_wait;
30         unsigned int conns;
31         unsigned int maxconns;
32         bool throttled;
33         time_t first;
34         std::string quitmsg;
35
36 public:
37         ModuleConnFlood()
38                 : conns(0), throttled(false)
39         {
40                 InitConf();
41                 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister };
42                 ServerInstance->Modules->Attach(eventlist, this, 2);
43         }
44
45         virtual Version GetVersion()
46         {
47                 return Version("Connection throttle", VF_VENDOR);
48         }
49
50         void InitConf()
51         {
52                 /* read configuration variables */
53                 ConfigReader conf;
54                 /* throttle configuration */
55                 seconds = conf.ReadInteger("connflood", "seconds", 0, true);
56                 maxconns = conf.ReadInteger("connflood", "maxconns", 0, true);
57                 timeout = conf.ReadInteger("connflood", "timeout", 0, true);
58                 quitmsg = conf.ReadValue("connflood", "quitmsg", 0);
59
60                 /* seconds to wait when the server just booted */
61                 boot_wait = conf.ReadInteger("connflood", "bootwait", 0, true);
62
63                 first = ServerInstance->Time();
64         }
65
66         virtual ModResult OnUserRegister(LocalUser* user)
67         {
68                 time_t next = ServerInstance->Time();
69
70                 if ((ServerInstance->startup_time + boot_wait) > next)
71                         return MOD_RES_PASSTHRU;
72
73                 /* time difference between first and latest connection */
74                 time_t tdiff = next - first;
75
76                 /* increase connection count */
77                 conns++;
78
79                 if (throttled)
80                 {
81                         if (tdiff > seconds + timeout)
82                         {
83                                 /* expire throttle */
84                                 throttled = false;
85                                 ServerInstance->SNO->WriteGlobalSno('a', "Connection throttle deactivated");
86                                 return MOD_RES_PASSTHRU;
87                         }
88
89                         ServerInstance->Users->QuitUser(user, quitmsg);
90                         return MOD_RES_DENY;
91                 }
92
93                 if (tdiff <= seconds)
94                 {
95                         if (conns >= maxconns)
96                         {
97                                 throttled = true;
98                                 ServerInstance->SNO->WriteGlobalSno('a', "Connection throttle activated");
99                                 ServerInstance->Users->QuitUser(user, quitmsg);
100                                 return MOD_RES_DENY;
101                         }
102                 }
103                 else
104                 {
105                         conns = 1;
106                         first = next;
107                 }
108                 return MOD_RES_PASSTHRU;
109         }
110
111         virtual void OnRehash(User* user)
112         {
113                 InitConf();
114         }
115
116 };
117
118 MODULE_INIT(ModuleConnFlood)