-/* +------------------------------------+\r * | Inspire Internet Relay Chat Daemon |\r * +------------------------------------+\r *\r * InspIRCd: (C) 2002-2007 InspIRCd Development Team\r * See: http://www.inspircd.org/wiki/index.php/Credits\r *\r * This program is free but copyrighted software; see\r * the file COPYING for details.\r *\r * ---------------------------------------------------\r */\r\r#include "inspircd.h"\r#include "users.h"\r#include "modules.h"\r\r/* $ModDesc: Connection throttle */\r\rint conns = 0, throttled = 0;\r\rclass ModuleConnFlood : public Module\r{\rprivate:\r int seconds, maxconns, timeout, boot_wait;\r time_t first;\r std::string quitmsg;\r\r ConfigReader* conf;\r \r\rpublic:\r ModuleConnFlood(InspIRCd* Me) : Module(Me)\r {\r \r InitConf();\r }\r\r virtual ~ModuleConnFlood()\r {\r }\r\r virtual Version GetVersion()\r {\r return Version(1,1,0,0,VF_VENDOR,API_VERSION);\r }\r\r void Implements(char* List)\r {\r List[I_OnRehash] = List[I_OnUserRegister] = 1;\r }\r \r void InitConf()\r {\r /* read configuration variables */\r conf = new ConfigReader(ServerInstance);\r /* throttle configuration */\r seconds = conf->ReadInteger("connflood", "seconds", 0, true);\r maxconns = conf->ReadInteger("connflood", "maxconns", 0, true);\r timeout = conf->ReadInteger("connflood", "timeout", 0, true);\r quitmsg = conf->ReadValue("connflood", "quitmsg", 0);\r\r /* seconds to wait when the server just booted */\r boot_wait = conf->ReadInteger("connflood", "bootwait", 0, true);\r\r first = ServerInstance->Time();\r }\r \r virtual int OnUserRegister(userrec* user)\r {\r time_t next = ServerInstance->Time();\r \r if ((ServerInstance->startup_time + boot_wait) > next)\r return 0;\r \r /* time difference between first and latest connection */\r time_t tdiff = next - first;\r\r /* increase connection count */\r conns++;\r\r if (throttled == 1)\r {\r if (tdiff > seconds + timeout)\r {\r /* expire throttle */\r throttled = 0;\r ServerInstance->WriteOpers("*** Connection throttle deactivated");\r return 0;\r }\r userrec::QuitUser(ServerInstance, user, quitmsg);\r return 1;\r }\r\r if (tdiff <= seconds)\r {\r if (conns >= maxconns)\r {\r throttled = 1;\r ServerInstance->WriteOpers("*** Connection throttle activated");\r userrec::QuitUser(ServerInstance, user, quitmsg);\r return 1;\r }\r }\r else\r {\r conns = 1;\r first = next;\r }\r return 0;\r }\r\r virtual void OnRehash(userrec* user, const std::string ¶meter)\r {\r InitConf();\r }\r\r};\r\rMODULE_INIT(ModuleConnFlood)\r
\ No newline at end of file
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
+ *
+ * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
+ * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
+ * Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
+ *
+ * This file is part of InspIRCd. InspIRCd is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation, version 2.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "inspircd.h"
+
+/* $ModDesc: Connection throttle */
+
+class ModuleConnFlood : public Module
+{
+private:
+ int seconds, timeout, boot_wait;
+ unsigned int conns;
+ unsigned int maxconns;
+ bool throttled;
+ time_t first;
+ std::string quitmsg;
+
+public:
+ ModuleConnFlood()
+ : conns(0), throttled(false)
+ {
+ InitConf();
+ Implementation eventlist[] = { I_OnRehash, I_OnUserRegister };
+ ServerInstance->Modules->Attach(eventlist, this, 2);
+ }
+
+ virtual Version GetVersion()
+ {
+ return Version("Connection throttle", VF_VENDOR);
+ }
+
+ void InitConf()
+ {
+ /* read configuration variables */
+ ConfigReader conf;
+ /* throttle configuration */
+ seconds = conf.ReadInteger("connflood", "seconds", 0, true);
+ maxconns = conf.ReadInteger("connflood", "maxconns", 0, true);
+ timeout = conf.ReadInteger("connflood", "timeout", 0, true);
+ quitmsg = conf.ReadValue("connflood", "quitmsg", 0);
+
+ /* seconds to wait when the server just booted */
+ boot_wait = conf.ReadInteger("connflood", "bootwait", 0, true);
+
+ first = ServerInstance->Time();
+ }
+
+ virtual ModResult OnUserRegister(LocalUser* user)
+ {
+ time_t next = ServerInstance->Time();
+
+ if ((ServerInstance->startup_time + boot_wait) > next)
+ return MOD_RES_PASSTHRU;
+
+ /* time difference between first and latest connection */
+ time_t tdiff = next - first;
+
+ /* increase connection count */
+ conns++;
+
+ if (throttled)
+ {
+ if (tdiff > seconds + timeout)
+ {
+ /* expire throttle */
+ throttled = false;
+ ServerInstance->SNO->WriteGlobalSno('a', "Connection throttle deactivated");
+ return MOD_RES_PASSTHRU;
+ }
+
+ ServerInstance->Users->QuitUser(user, quitmsg);
+ return MOD_RES_DENY;
+ }
+
+ if (tdiff <= seconds)
+ {
+ if (conns >= maxconns)
+ {
+ throttled = true;
+ ServerInstance->SNO->WriteGlobalSno('a', "Connection throttle activated");
+ ServerInstance->Users->QuitUser(user, quitmsg);
+ return MOD_RES_DENY;
+ }
+ }
+ else
+ {
+ conns = 1;
+ first = next;
+ }
+ return MOD_RES_PASSTHRU;
+ }
+
+ virtual void OnRehash(User* user)
+ {
+ InitConf();
+ }
+
+};
+
+MODULE_INIT(ModuleConnFlood)