]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_connflood.cpp
Remove Implements() method from every module. booya.
[user/henk/code/inspircd.git] / src / modules / m_connflood.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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(1,1,0,0,VF_VENDOR,API_VERSION);
46         }
47
48    
49         void InitConf()
50         {
51                 /* read configuration variables */
52                 conf = new ConfigReader(ServerInstance);
53                 /* throttle configuration */
54                 seconds = conf->ReadInteger("connflood", "seconds", 0, true);
55                 maxconns = conf->ReadInteger("connflood", "maxconns", 0, true);
56                 timeout = conf->ReadInteger("connflood", "timeout", 0, true);
57                 quitmsg = conf->ReadValue("connflood", "quitmsg", 0);
58
59                 /* seconds to wait when the server just booted */
60                 boot_wait = conf->ReadInteger("connflood", "bootwait", 0, true);
61
62                 first = ServerInstance->Time();
63         }
64  
65         virtual int OnUserRegister(User* user)
66         {
67                 time_t next = ServerInstance->Time();
68                 
69                 if ((ServerInstance->startup_time + boot_wait) > next)
70                         return 0;
71                 
72                 /* time difference between first and latest connection */
73                 time_t tdiff = next - first;
74
75                 /* increase connection count */
76                 conns++;
77
78                 if (throttled == 1)
79                 {
80                         if (tdiff > seconds + timeout)
81                         {
82                                 /* expire throttle */
83                                 throttled = 0;
84                                 ServerInstance->WriteOpers("*** Connection throttle deactivated");
85                                 return 0;
86                         }
87                         User::QuitUser(ServerInstance, user, quitmsg);
88                         return 1;
89                 }
90
91                 if (tdiff <= seconds)
92                 {
93                         if (conns >= maxconns)
94                         {
95                                 throttled = 1;
96                                 ServerInstance->WriteOpers("*** Connection throttle activated");
97                                 User::QuitUser(ServerInstance, user, quitmsg);
98                                 return 1;
99                         }
100                 }
101                 else
102                 {
103                         conns = 1;
104                         first = next;
105                 }
106                 return 0;
107         }
108
109         virtual void OnRehash(User* user, const std::string &parameter)
110         {
111                 InitConf();
112         }
113
114 };
115
116 MODULE_INIT(ModuleConnFlood)