]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_connflood.cpp
FindNick, FindChan, ChanModes, UserList, CountInvisible, PurgeEmptyChannels, GetClass...
[user/henk/code/inspircd.git] / src / modules / m_connflood.cpp
1 /*   +------------------------------------+
2  *   | Inspire Internet Relay Chat Daemon |
3  *   +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                    E-mail:
7  *             <brain@chatspike.net>
8  *             <Craig@chatspike.net>
9  *
10  *     --- This module contributed by pippijn ---
11  * 
12  * Written by Craig Edwards, Craig McLure, and others.
13  * This program is free but copyrighted software; see
14  * the file COPYING for details.
15  *
16  * ---------------------------------------------------
17  */
18
19 using namespace std;
20
21 #include "users.h"
22 #include "modules.h"
23 #include "helperfuncs.h"
24 #include "inspircd.h"
25
26 /* $ModDesc: Connection throttle */
27
28 int conns = 0, throttled = 0;
29 extern time_t TIME;
30
31 extern InspIRCd* ServerInstance;
32
33 class ModuleConnFlood : public Module
34 {
35 private:
36         int seconds, maxconns, timeout, boot_wait;
37         time_t first;
38         std::string quitmsg;
39
40         ConfigReader* conf;
41         Server *Srv;
42
43 public:
44         ModuleConnFlood(Server* Me) : Module::Module(Me)
45         {
46                 Srv = Me;
47                 InitConf();
48         }
49
50         virtual ~ModuleConnFlood()
51         {
52         }
53
54         virtual Version GetVersion()
55         {
56                 return Version(1,0,0,0,0);
57         }
58
59         void Implements(char* List)
60         {
61                 List[I_OnRehash] = List[I_OnUserRegister] = 1;
62         }
63    
64         void InitConf()
65         {
66                 /* read configuration variables */
67                 conf = new ConfigReader;
68                 /* throttle configuration */
69                 seconds = conf->ReadInteger("connflood", "seconds", 0, true);
70                 maxconns = conf->ReadInteger("connflood", "maxconns", 0, true);
71                 timeout = conf->ReadInteger("connflood", "timeout", 0, true);
72                 quitmsg = conf->ReadValue("connflood", "quitmsg", 0);
73
74                 /* seconds to wait when the server just booted */
75                 boot_wait = conf->ReadInteger("connflood", "bootwait", 0, true);
76
77                 first = TIME;
78         }
79  
80         virtual void OnUserRegister(userrec* user)
81         {
82                 time_t next = TIME;
83                 if (!first)
84                         first = next - boot_wait;
85
86                 /* time difference between first and latest connection */
87                 time_t tdiff = next - first;
88
89                 /* increase connection count */
90                 conns++;
91
92                 if (throttled == 1)
93                 {
94                         if (tdiff > seconds + timeout)
95                         {
96                                 /* expire throttle */
97                                 throttled = 0;
98                                 ServerInstance->WriteOpers("*** Connection throttle deactivated");
99                                 return;
100                         }
101                         userrec::QuitUser(ServerInstance, user, quitmsg);
102                         return;
103                 }
104
105                 if (tdiff <= seconds)
106                 {
107                         if (conns >= maxconns)
108                         {
109                                 throttled = 1;
110                                 ServerInstance->WriteOpers("*** Connection throttle activated");
111                                 userrec::QuitUser(ServerInstance, user, quitmsg);
112                                 return;
113                         }
114                 }
115                 else
116                 {
117                         conns = 1;
118                         first = next;
119                 }
120         }
121
122         virtual void OnRehash(const std::string &parameter)
123         {
124                 InitConf();
125         }
126
127 };
128
129
130 class ModuleConnFloodFactory : public ModuleFactory
131 {
132 public:
133         ModuleConnFloodFactory()
134         {
135         }
136
137         ~ModuleConnFloodFactory()
138         {
139         }
140     
141         virtual Module * CreateModule(Server* Me)
142         {
143                 return new ModuleConnFlood(Me);
144         }
145 };
146
147
148 extern "C" void * init_module( void )
149 {
150         return new ModuleConnFloodFactory;
151 }