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