]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_connflood.cpp
9bc8277eeb0914afafc53a0558e2b1dc44494838
[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         int seconds, timeout, 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         void init()
42         {
43                 InitConf();
44                 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister };
45                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
46         }
47
48         virtual Version GetVersion()
49         {
50                 return Version("Connection throttle", VF_VENDOR);
51         }
52
53         void InitConf()
54         {
55                 /* read configuration variables */
56                 ConfigTag* tag = ServerInstance->Config->ConfValue("connflood");
57                 /* throttle configuration */
58                 seconds = tag->getInt("seconds");
59                 maxconns = tag->getInt("maxconns");
60                 timeout = tag->getInt("timeout");
61                 quitmsg = tag->getString("quitmsg");
62
63                 /* seconds to wait when the server just booted */
64                 boot_wait = tag->getInt("bootwait");
65
66                 first = ServerInstance->Time();
67         }
68
69         virtual ModResult OnUserRegister(LocalUser* user)
70         {
71                 if (user->exempt)
72                         return MOD_RES_PASSTHRU;
73
74                 time_t next = ServerInstance->Time();
75
76                 if ((ServerInstance->startup_time + boot_wait) > next)
77                         return MOD_RES_PASSTHRU;
78
79                 /* time difference between first and latest connection */
80                 time_t tdiff = next - first;
81
82                 /* increase connection count */
83                 conns++;
84
85                 if (throttled)
86                 {
87                         if (tdiff > seconds + timeout)
88                         {
89                                 /* expire throttle */
90                                 throttled = false;
91                                 ServerInstance->SNO->WriteGlobalSno('a', "Connection throttle deactivated");
92                                 return MOD_RES_PASSTHRU;
93                         }
94
95                         ServerInstance->Users->QuitUser(user, quitmsg);
96                         return MOD_RES_DENY;
97                 }
98
99                 if (tdiff <= seconds)
100                 {
101                         if (conns >= maxconns)
102                         {
103                                 throttled = true;
104                                 ServerInstance->SNO->WriteGlobalSno('a', "Connection throttle activated");
105                                 ServerInstance->Users->QuitUser(user, quitmsg);
106                                 return MOD_RES_DENY;
107                         }
108                 }
109                 else
110                 {
111                         conns = 1;
112                         first = next;
113                 }
114                 return MOD_RES_PASSTHRU;
115         }
116
117         virtual void OnRehash(User* user)
118         {
119                 InitConf();
120         }
121
122 };
123
124 MODULE_INIT(ModuleConnFlood)