]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_connflood.cpp
04fe456275d659b9a259c91a5dc8084318c096d8
[user/henk/code/inspircd.git] / src / modules / m_connflood.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Connection throttle */
17
18 int conns = 0, throttled = 0;
19
20 class ModuleConnFlood : public Module
21 {
22 private:
23         int seconds, maxconns, timeout, boot_wait;
24         time_t first;
25         std::string quitmsg;
26
27         ConfigReader* conf;
28
29
30 public:
31         ModuleConnFlood(InspIRCd* Me) : Module(Me)
32         {
33
34                 InitConf();
35                 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister };
36                 ServerInstance->Modules->Attach(eventlist, this, 2);
37         }
38
39         virtual ~ModuleConnFlood()
40         {
41         }
42
43         virtual Version GetVersion()
44         {
45                 return Version("Connection throttle", VF_VENDOR,API_VERSION);
46         }
47
48         void InitConf()
49         {
50                 /* read configuration variables */
51                 conf = new ConfigReader(ServerInstance);
52                 /* throttle configuration */
53                 seconds = conf->ReadInteger("connflood", "seconds", 0, true);
54                 maxconns = conf->ReadInteger("connflood", "maxconns", 0, true);
55                 timeout = conf->ReadInteger("connflood", "timeout", 0, true);
56                 quitmsg = conf->ReadValue("connflood", "quitmsg", 0);
57
58                 /* seconds to wait when the server just booted */
59                 boot_wait = conf->ReadInteger("connflood", "bootwait", 0, true);
60
61                 first = ServerInstance->Time();
62         }
63
64         virtual ModResult OnUserRegister(User* user)
65         {
66                 time_t next = ServerInstance->Time();
67
68                 if ((ServerInstance->startup_time + boot_wait) > next)
69                         return MOD_RES_PASSTHRU;
70
71                 /* time difference between first and latest connection */
72                 time_t tdiff = next - first;
73
74                 /* increase connection count */
75                 conns++;
76
77                 if (throttled == 1)
78                 {
79                         if (tdiff > seconds + timeout)
80                         {
81                                 /* expire throttle */
82                                 throttled = 0;
83                                 ServerInstance->SNO->WriteGlobalSno('a', "Connection throttle deactivated");
84                                 return MOD_RES_PASSTHRU;
85                         }
86
87                         ServerInstance->Users->QuitUser(user, quitmsg);
88                         return MOD_RES_DENY;
89                 }
90
91                 if (tdiff <= seconds)
92                 {
93                         if (conns >= maxconns)
94                         {
95                                 throttled = 1;
96                                 ServerInstance->SNO->WriteGlobalSno('a', "Connection throttle activated");
97                                 ServerInstance->Users->QuitUser(user, quitmsg);
98                                 return MOD_RES_DENY;
99                         }
100                 }
101                 else
102                 {
103                         conns = 1;
104                         first = next;
105                 }
106                 return MOD_RES_PASSTHRU;
107         }
108
109         virtual void OnRehash(User* user)
110         {
111                 InitConf();
112         }
113
114 };
115
116 MODULE_INIT(ModuleConnFlood)