adds counting from iptables (-CL)

master
Dominic Reich 2 years ago
parent 7c16fc0eac
commit a783d2cac9
Signed by: dominic
GPG Key ID: BC9D6AE1A3BE169A

@ -75,6 +75,7 @@ int loadIptables(const char *filename);
int flushIptables(); int flushIptables();
int listIptables(); int listIptables();
int addIpToIptables(string ip); int addIpToIptables(string ip);
int countIptablesAdresses();
int checkIp(string ip); int checkIp(string ip);
inline bool addIpToFile(const char *filename, string ip); inline bool addIpToFile(const char *filename, string ip);
inline bool removeIpFromFile(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") } else if(Choice == "-f")
{ {
if(argc != 3) if(argc != 3)
{
printHelp(false);
return 1; return 1;
}
cmd = checkIp(argv[2]); cmd = checkIp(argv[2]);
if(cmd != 0) if(cmd != 0)
@ -169,6 +173,15 @@ int main(int argc, char **argv)
if(!countIpAdresses(Filename)) if(!countIpAdresses(Filename))
return 1; 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) /// sort and check file for duplicates (check for root)
} else if(Choice == "-c") } else if(Choice == "-c")
{ {
@ -288,7 +301,8 @@ void printHelp(bool printAll)
cout << "Usage: blacklist [options] <ip-address>"; cout << "Usage: blacklist [options] <ip-address>";
if(printAll == true) 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 << "Options: -a add ip-address(es) (to file)" << endl;
cout << " -d delete ip-address (from file) (still only 1!)" << endl; cout << " -d delete ip-address (from file) (still only 1!)" << endl;
cout << " -l list ip-addresses (from file)" << 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 << " -c check file (sort and remove dulicates)" << endl;
cout << " -r reload (check file, flush iptables, load ips from file)" << endl; cout << " -r reload (check file, flush iptables, load ips from file)" << endl;
cout << " -C count ip-addresses (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 << " -L list ip-addresses (from iptables)" << endl;
cout << " -F flush (iptables)"; cout << " -F flush (iptables)";
} }
@ -464,6 +479,23 @@ int addIpToIptables(string ip)
return 0; 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 /** \brief checks for a valid ip-address
* *
* \param ip string * \param ip string

@ -7,7 +7,7 @@ namespace Version{
static const char STATUS[] = "RCx [nod.oe7drt.com]"; static const char STATUS[] = "RCx [nod.oe7drt.com]";
//Miscellaneous Version Types //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 //Copyright line usable in the program
static const char COPYRIGHT [] = "Copyright ©2015-2023 Dominic Reich\n"; static const char COPYRIGHT [] = "Copyright ©2015-2023 Dominic Reich\n";

Loading…
Cancel
Save