From a783d2cac9ff67b86ebe29595ccb70bc7f7b242a Mon Sep 17 00:00:00 2001 From: Dominic Reich Date: Sat, 18 Feb 2023 12:42:04 +0100 Subject: [PATCH] adds counting from iptables (-CL) --- blacklist.cpp | 34 +++++++++++++++++++++++++++++++++- version.h | 2 +- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/blacklist.cpp b/blacklist.cpp index a60c8c7..003f58c 100644 --- a/blacklist.cpp +++ b/blacklist.cpp @@ -75,6 +75,7 @@ int loadIptables(const char *filename); int flushIptables(); int listIptables(); int addIpToIptables(string ip); +int countIptablesAdresses(); int checkIp(string ip); inline bool addIpToFile(const char *filename, string ip); inline bool removeIpFromFile(const char *filename, string ip); @@ -154,7 +155,10 @@ int main(int argc, char **argv) } else if(Choice == "-f") { if(argc != 3) + { + printHelp(false); return 1; + } cmd = checkIp(argv[2]); if(cmd != 0) @@ -169,6 +173,15 @@ int main(int argc, char **argv) if(!countIpAdresses(Filename)) return 1; + /// show count of ips in iptables + } else if(Choice == "-CL") + { + if(checkRoot() == false) + return 1; + + if(!countIptablesAdresses()) + return 1; + /// sort and check file for duplicates (check for root) } else if(Choice == "-c") { @@ -288,7 +301,8 @@ void printHelp(bool printAll) cout << "Usage: blacklist [options] "; if(printAll == true) { - cout << "The blacklist file is located at: " << DEFAULT_FILENAME << endl; + cout << endl << endl; + cout << "The blacklist file is located at: " << DEFAULT_FILENAME << endl << endl; cout << "Options: -a add ip-address(es) (to file)" << endl; cout << " -d delete ip-address (from file) (still only 1!)" << endl; cout << " -l list ip-addresses (from file)" << endl; @@ -296,6 +310,7 @@ void printHelp(bool printAll) cout << " -c check file (sort and remove dulicates)" << endl; cout << " -r reload (check file, flush iptables, load ips from file)" << endl; cout << " -C count ip-addresses (from file)" << endl; + cout << " -CL count ip-addresses (from iptables)" << endl; cout << " -L list ip-addresses (from iptables)" << endl; cout << " -F flush (iptables)"; } @@ -464,6 +479,23 @@ int addIpToIptables(string ip) return 0; } +/** \brief counts the content of BLACKLIST chain + * + * \param /nothing + * \return int number of ips + * + */ + +// iptables -nL BLACKLIST|tail -n +3|wc -l +int countIptablesAdresses() +{ + string cmd_iptables = IPTABLES; + int cmd = 0; + cmd = system((cmd_iptables+" -nL BLACKLIST | tail -n +3 | wc -l").c_str()); + + return cmd; +} + /** \brief checks for a valid ip-address * * \param ip string diff --git a/version.h b/version.h index 0c9880e..d3ddd8a 100644 --- a/version.h +++ b/version.h @@ -7,7 +7,7 @@ namespace Version{ static const char STATUS[] = "RCx [nod.oe7drt.com]"; //Miscellaneous Version Types - static const char VERSION_STRING [] = "0.6.0"; + static const char VERSION_STRING [] = "0.6.1"; //Copyright line usable in the program static const char COPYRIGHT [] = "Copyright ©2015-2023 Dominic Reich\n";