mirror of
https://github.com/znc/znc.git
synced 2026-08-07 09:22:55 +02:00
Add clang-format configuration.
For now, it uses tabs like before, to make the diff easier to read/check. One of following commits will switch it to spaces.
This commit is contained in:
+50
-31
@@ -17,14 +17,26 @@
|
||||
#include <znc/znc.h>
|
||||
|
||||
class CFailToBanMod : public CModule {
|
||||
public:
|
||||
public:
|
||||
MODCONSTRUCTOR(CFailToBanMod) {
|
||||
AddHelpCommand();
|
||||
AddCommand("Timeout", static_cast<CModCommand::ModCmdFunc>(&CFailToBanMod::OnTimeoutCommand), "[minutes]", "The number of minutes IPs are blocked after a failed login.");
|
||||
AddCommand("Attempts", static_cast<CModCommand::ModCmdFunc>(&CFailToBanMod::OnAttemptsCommand), "[count]", "The number of allowed failed login attempts.");
|
||||
AddCommand("Ban", static_cast<CModCommand::ModCmdFunc>(&CFailToBanMod::OnBanCommand), "<hosts>", "Ban the specified hosts.");
|
||||
AddCommand("Unban", static_cast<CModCommand::ModCmdFunc>(&CFailToBanMod::OnUnbanCommand), "<hosts>", "Unban the specified hosts.");
|
||||
AddCommand("List", static_cast<CModCommand::ModCmdFunc>(&CFailToBanMod::OnListCommand), "", "List banned hosts.");
|
||||
AddCommand(
|
||||
"Timeout", static_cast<CModCommand::ModCmdFunc>(
|
||||
&CFailToBanMod::OnTimeoutCommand),
|
||||
"[minutes]",
|
||||
"The number of minutes IPs are blocked after a failed login.");
|
||||
AddCommand("Attempts", static_cast<CModCommand::ModCmdFunc>(
|
||||
&CFailToBanMod::OnAttemptsCommand),
|
||||
"[count]", "The number of allowed failed login attempts.");
|
||||
AddCommand("Ban", static_cast<CModCommand::ModCmdFunc>(
|
||||
&CFailToBanMod::OnBanCommand),
|
||||
"<hosts>", "Ban the specified hosts.");
|
||||
AddCommand("Unban", static_cast<CModCommand::ModCmdFunc>(
|
||||
&CFailToBanMod::OnUnbanCommand),
|
||||
"<hosts>", "Unban the specified hosts.");
|
||||
AddCommand("List", static_cast<CModCommand::ModCmdFunc>(
|
||||
&CFailToBanMod::OnListCommand),
|
||||
"", "List banned hosts.");
|
||||
}
|
||||
virtual ~CFailToBanMod() {}
|
||||
|
||||
@@ -40,10 +52,12 @@ public:
|
||||
|
||||
if (sArgs.empty()) {
|
||||
timeout = 1;
|
||||
} else if (timeout == 0 || m_uiAllowedFailed == 0 || !sArgs.Token(2, true).empty()) {
|
||||
sMessage = "Invalid argument, must be the number of minutes "
|
||||
"IPs are blocked after a failed login and can be "
|
||||
"followed by number of allowed failed login attempts";
|
||||
} else if (timeout == 0 || m_uiAllowedFailed == 0 ||
|
||||
!sArgs.Token(2, true).empty()) {
|
||||
sMessage =
|
||||
"Invalid argument, must be the number of minutes "
|
||||
"IPs are blocked after a failed login and can be "
|
||||
"followed by number of allowed failed login attempts";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -53,17 +67,13 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
void OnPostRehash() override {
|
||||
m_Cache.Clear();
|
||||
}
|
||||
void OnPostRehash() override { m_Cache.Clear(); }
|
||||
|
||||
void Add(const CString& sHost, unsigned int count) {
|
||||
m_Cache.AddItem(sHost, count, m_Cache.GetTTL());
|
||||
}
|
||||
|
||||
bool Remove(const CString& sHost) {
|
||||
return m_Cache.RemItem(sHost);
|
||||
}
|
||||
bool Remove(const CString& sHost) { return m_Cache.RemItem(sHost); }
|
||||
|
||||
void OnTimeoutCommand(const CString& sCommand) {
|
||||
CString sArg = sCommand.Token(1);
|
||||
@@ -74,11 +84,13 @@ public:
|
||||
PutModule("Usage: Timeout [minutes]");
|
||||
} else {
|
||||
m_Cache.SetTTL(uTimeout * 60 * 1000);
|
||||
SetArgs(CString(m_Cache.GetTTL() / 60 / 1000) + " " + CString(m_uiAllowedFailed));
|
||||
SetArgs(CString(m_Cache.GetTTL() / 60 / 1000) + " " +
|
||||
CString(m_uiAllowedFailed));
|
||||
PutModule("Timeout: " + CString(uTimeout) + " min");
|
||||
}
|
||||
} else {
|
||||
PutModule("Timeout: " + CString(m_Cache.GetTTL() / 60 / 1000) + " min");
|
||||
PutModule("Timeout: " + CString(m_Cache.GetTTL() / 60 / 1000) +
|
||||
" min");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +103,8 @@ public:
|
||||
PutModule("Usage: Attempts [count]");
|
||||
} else {
|
||||
m_uiAllowedFailed = uiAttempts;
|
||||
SetArgs(CString(m_Cache.GetTTL() / 60 / 1000) + " " + CString(m_uiAllowedFailed));
|
||||
SetArgs(CString(m_Cache.GetTTL() / 60 / 1000) + " " +
|
||||
CString(m_uiAllowedFailed));
|
||||
PutModule("Attempts: " + CString(uiAttempts));
|
||||
}
|
||||
} else {
|
||||
@@ -156,8 +169,9 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void OnClientConnect(CZNCSock* pClient, const CString& sHost, unsigned short uPort) override {
|
||||
unsigned int *pCount = m_Cache.GetItem(sHost);
|
||||
void OnClientConnect(CZNCSock* pClient, const CString& sHost,
|
||||
unsigned short uPort) override {
|
||||
unsigned int* pCount = m_Cache.GetItem(sHost);
|
||||
if (sHost.empty() || pCount == nullptr || *pCount < m_uiAllowedFailed) {
|
||||
return;
|
||||
}
|
||||
@@ -165,12 +179,15 @@ public:
|
||||
// refresh their ban
|
||||
Add(sHost, *pCount);
|
||||
|
||||
pClient->Write("ERROR :Closing link [Please try again later - reconnecting too fast]\r\n");
|
||||
pClient->Write(
|
||||
"ERROR :Closing link [Please try again later - reconnecting too "
|
||||
"fast]\r\n");
|
||||
pClient->Close(Csock::CLT_AFTERWRITE);
|
||||
}
|
||||
|
||||
void OnFailedLogin(const CString& sUsername, const CString& sRemoteIP) override {
|
||||
unsigned int *pCount = m_Cache.GetItem(sRemoteIP);
|
||||
void OnFailedLogin(const CString& sUsername,
|
||||
const CString& sRemoteIP) override {
|
||||
unsigned int* pCount = m_Cache.GetItem(sRemoteIP);
|
||||
if (pCount)
|
||||
Add(sRemoteIP, *pCount + 1);
|
||||
else
|
||||
@@ -181,10 +198,9 @@ public:
|
||||
// e.g. webadmin ends up here
|
||||
const CString& sRemoteIP = Auth->GetRemoteIP();
|
||||
|
||||
if (sRemoteIP.empty())
|
||||
return CONTINUE;
|
||||
if (sRemoteIP.empty()) return CONTINUE;
|
||||
|
||||
unsigned int *pCount = m_Cache.GetItem(sRemoteIP);
|
||||
unsigned int* pCount = m_Cache.GetItem(sRemoteIP);
|
||||
if (pCount && *pCount >= m_uiAllowedFailed) {
|
||||
// OnFailedLogin() will refresh their ban
|
||||
Auth->RefuseLogin("Please try again later - reconnecting too fast");
|
||||
@@ -194,15 +210,18 @@ public:
|
||||
return CONTINUE;
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
TCacheMap<CString, unsigned int> m_Cache;
|
||||
unsigned int m_uiAllowedFailed{};
|
||||
unsigned int m_uiAllowedFailed{};
|
||||
};
|
||||
|
||||
template<> void TModInfo<CFailToBanMod>(CModInfo& Info) {
|
||||
template <>
|
||||
void TModInfo<CFailToBanMod>(CModInfo& Info) {
|
||||
Info.SetWikiPage("fail2ban");
|
||||
Info.SetHasArgs(true);
|
||||
Info.SetArgsHelpText("You might enter the time in minutes for the IP banning and the number of failed logins before any action is taken.");
|
||||
Info.SetArgsHelpText(
|
||||
"You might enter the time in minutes for the IP banning and the number "
|
||||
"of failed logins before any action is taken.");
|
||||
}
|
||||
|
||||
GLOBALMODULEDEFS(CFailToBanMod, "Block IPs for some time after a failed login.")
|
||||
|
||||
Reference in New Issue
Block a user