]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_connflood.cpp
Fix some confusing logic in sanick.
[user/henk/code/inspircd.git] / src / modules / m_connflood.cpp
index 71e52fd01ad8b1e134968d32567b2b1031deebc8..809055a5ad3a854e4a99c02044ed764a14c37d0b 100644 (file)
@@ -1 +1,115 @@
-/*       +------------------------------------+\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 &parameter)\r     {\r              InitConf();\r    }\r\r};\r\rMODULE_INIT(ModuleConnFlood)\r
\ No newline at end of file
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
+ *
+ *   Copyright (C) 2013, 2018-2020 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2012-2013 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
+ *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
+ *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
+ *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2006-2007, 2010 Craig Edwards <brain@inspircd.org>
+ *
+ * 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"
+
+class ModuleConnFlood : public Module
+{
+       unsigned int seconds;
+       unsigned int timeout;
+       unsigned int boot_wait;
+       unsigned int conns;
+       unsigned int maxconns;
+       bool throttled;
+       time_t first;
+       std::string quitmsg;
+
+public:
+       ModuleConnFlood()
+               : conns(0), throttled(false)
+       {
+       }
+
+       Version GetVersion() CXX11_OVERRIDE
+       {
+               return Version("Throttles excessive connections to the server.", VF_VENDOR);
+       }
+
+       void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
+       {
+               /* read configuration variables */
+               ConfigTag* tag = ServerInstance->Config->ConfValue("connflood");
+               /* throttle configuration */
+               seconds = tag->getDuration("period", tag->getDuration("seconds", 30));
+               maxconns = tag->getUInt("maxconns", 3);
+               timeout = tag->getDuration("timeout", 30);
+               quitmsg = tag->getString("quitmsg");
+
+               /* seconds to wait when the server just booted */
+               boot_wait = tag->getDuration("bootwait", 60*2);
+
+               first = ServerInstance->Time();
+       }
+
+       ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
+       {
+               if (user->exempt)
+                       return MOD_RES_PASSTHRU;
+
+               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;
+       }
+};
+
+MODULE_INIT(ModuleConnFlood)