From 82b274cc0efafd69306fd4475fb96cbabaf71789 Mon Sep 17 00:00:00 2001 From: Rei-Tw <22585066+Rei-Tw@users.noreply.github.com> Date: Wed, 6 Nov 2024 00:47:57 +0100 Subject: [PATCH] Implement bans_find command. This rcon command simplifies searching for all ban records for a specific IP if the ban list has too many records. --- src/engine/shared/netban.cpp | 50 ++++++++++++++++++++++++++++++++++++ src/engine/shared/netban.h | 1 + 2 files changed, 51 insertions(+) diff --git a/src/engine/shared/netban.cpp b/src/engine/shared/netban.cpp index 0c42f4c32..1137b5ea9 100644 --- a/src/engine/shared/netban.cpp +++ b/src/engine/shared/netban.cpp @@ -286,6 +286,7 @@ void CNetBan::Init(IConsole *pConsole, IStorage *pStorage) Console()->Register("unban_range", "s[first ip] s[last ip]", CFGFLAG_SERVER | CFGFLAG_MASTER | CFGFLAG_STORE, ConUnbanRange, this, "Unban ip range"); Console()->Register("unban_all", "", CFGFLAG_SERVER | CFGFLAG_MASTER | CFGFLAG_STORE, ConUnbanAll, this, "Unban all entries"); Console()->Register("bans", "?i[page]", CFGFLAG_SERVER | CFGFLAG_MASTER, ConBans, this, "Show banlist (page 1 by default, 20 entries per page)"); + Console()->Register("bans_find", "s[ip]", CFGFLAG_SERVER | CFGFLAG_MASTER, ConBansFind, this, "Find all ban records for the specified IP address"); Console()->Register("bans_save", "s[file]", CFGFLAG_SERVER | CFGFLAG_MASTER | CFGFLAG_STORE, ConBansSave, this, "Save banlist in a file"); } @@ -528,6 +529,55 @@ void CNetBan::ConBans(IConsole::IResult *pResult, void *pUser) pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "net_ban", aMsg); } +void CNetBan::ConBansFind(IConsole::IResult *pResult, void *pUser) +{ + CNetBan *pThis = static_cast(pUser); + + const char *pStr = pResult->GetString(0); + char aBuf[256], aMsg[256]; + + NETADDR Addr; + if(net_addr_from_str(&Addr, pStr) != 0) + { + pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "net_ban", "bans_find error (invalid network address)"); + return; + } + + int Count = 0; + int Found = 0; + // Check first for bans + for(CBanAddr *pBan = pThis->m_BanAddrPool.First(); pBan; pBan = pBan->m_pNext, Count++) + { + if(NetComp(&pBan->m_Data, &Addr) == 0) + { + pThis->MakeBanInfo(pBan, aBuf, sizeof(aBuf), MSGTYPE_LIST); + str_format(aMsg, sizeof(aMsg), "#%i %s", Count, aBuf); + pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "net_ban", aMsg); + + Found++; + } + } + // check ban ranges + for(CBanRange *pBan = pThis->m_BanRangePool.First(); pBan; pBan = pBan->m_pNext, Count++) + { + if(pThis->NetMatch(&pBan->m_Data, &Addr)) + { + pThis->MakeBanInfo(pBan, aBuf, sizeof(aBuf), MSGTYPE_LIST); + str_format(aMsg, sizeof(aMsg), "#%i %s", Count, aBuf); + pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "net_ban", aMsg); + + Found++; + } + } + + if(Found) + str_format(aMsg, sizeof(aMsg), "%i ban records found.", Found); + else + str_copy(aMsg, "No ban records found.", sizeof(aMsg)); + + pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "net_ban", aMsg); +} + void CNetBan::ConBansSave(IConsole::IResult *pResult, void *pUser) { CNetBan *pThis = static_cast(pUser); diff --git a/src/engine/shared/netban.h b/src/engine/shared/netban.h index 72c88f443..633773ef1 100644 --- a/src/engine/shared/netban.h +++ b/src/engine/shared/netban.h @@ -191,6 +191,7 @@ public: static void ConUnbanRange(class IConsole::IResult *pResult, void *pUser); static void ConUnbanAll(class IConsole::IResult *pResult, void *pUser); static void ConBans(class IConsole::IResult *pResult, void *pUser); + static void ConBansFind(class IConsole::IResult *pResult, void *pUser); static void ConBansSave(class IConsole::IResult *pResult, void *pUser); };