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