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