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