can use multiple ips now for `blacklist -a`

- also a few more verbose functions (comments)
master
Dominic Reich 2 years ago
parent c38dfbbcdb
commit 8d66e0d159
Signed by untrusted user who does not match committer: dominic
GPG Key ID: BC9D6AE1A3BE169A

@ -29,8 +29,7 @@
* SOME SPECIAL NOTES FOR THIS PROGRAM * SOME SPECIAL NOTES FOR THIS PROGRAM
* 1. When compiling, use -lboost_system in 'link-libraries' * 1. When compiling, use -lboost_system in 'link-libraries'
* 2. When compiling for other hosts, use -static in 'linker-options' * 2. When compiling for other hosts, use -static in 'linker-options'
* * (doesn't work always)
*
*/ */
#include <iostream> #include <iostream>
@ -94,7 +93,8 @@ int main(int argc, char **argv)
if(checkIptables() == false) if(checkIptables() == false)
return 1; return 1;
if(argc < 2 || argc > 3) //if(argc < 2 || argc > 3)
if(argc < 2)
{ {
printHelp(false); printHelp(false);
return 0; return 0;
@ -111,6 +111,19 @@ int main(int argc, char **argv)
if(checkRoot() == false) if(checkRoot() == false)
return 1; return 1;
if(argc > 3)
{
// many args, iterate over them
for(int i = 2; i < argc; i++)
{
// cout << i << ": " << argv[i] << endl;
cmd = addIpToFile(Filename, argv[i]);
if(cmd == false)
continue;
}
return 0;
}
if(argc != 3) if(argc != 3)
return 1; return 1;
@ -272,19 +285,19 @@ void printHelp(bool printAll)
cout << "blacklist " << Version::VERSION_STRING << ", "; cout << "blacklist " << Version::VERSION_STRING << ", ";
cout << Version::STATUS << endl; cout << Version::STATUS << endl;
cout << Version::COPYRIGHT << endl; cout << Version::COPYRIGHT << endl;
cout << "Usage: blacklist [options] <ip-address>" << endl; cout << "Usage: blacklist [options] <ip-address>";
if(printAll == true) if(printAll == true)
{ {
cout << "The blacklist file is located at: " << DEFAULT_FILENAME << endl; cout << "The blacklist file is located at: " << DEFAULT_FILENAME << endl;
cout << "Options: -a add ip-address (to file)" << endl; cout << "Options: -a add ip-address(es) (to file)" << endl;
cout << " -d delete ip-address (from file)" << 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;
cout << " -f find ip-address in file" << endl; cout << " -f find ip-address in file" << endl;
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 << " -L list ip-addresses (from iptables)" << endl; cout << " -L list ip-addresses (from iptables)" << endl;
cout << " -F flush (iptables)" << endl; cout << " -F flush (iptables)";
} }
} }
@ -463,7 +476,7 @@ int checkIp(string ip)
boost::asio::ip::address::from_string(ip, ec); boost::asio::ip::address::from_string(ip, ec);
if(ec) if(ec)
{ {
cout << ec.message() << endl; cerr << ec.message() << ": " << ip;
return ec.value(); return ec.value();
} }
@ -495,7 +508,7 @@ inline bool addIpToFile(const char *filename, string ip)
File.close(); File.close();
// disable auto-check for now // disable auto-check for now
//checkFile(Filename); checkFile(filename);
return true; return true;
} }
@ -513,11 +526,13 @@ inline bool removeIpFromFile(const char *filename, string ip)
cmd = checkIp(ip); cmd = checkIp(ip);
if(cmd != 0) if(cmd != 0)
{ {
// input is not an IP
return false; return false;
} }
if(!findIpInFile(filename, ip)) if(!findIpInFile(filename, ip))
{ {
// IP not found in blacklist file
return false; return false;
} }
@ -526,12 +541,14 @@ inline bool removeIpFromFile(const char *filename, string ip)
ifstream inFile(filename); ifstream inFile(filename);
if(!inFile.is_open()) if(!inFile.is_open())
{ {
// can't open file for reading
return false; return false;
} }
ofstream outFile(tmpFile, ios::out); ofstream outFile(tmpFile, ios::out);
if(!outFile.is_open()) if(!outFile.is_open())
{ {
// can't open tmp file for writing
return false; return false;
} }
@ -549,12 +566,14 @@ inline bool removeIpFromFile(const char *filename, string ip)
ifstream a(tmpFile); ifstream a(tmpFile);
if(!a.is_open()) if(!a.is_open())
{ {
// now we can't open previosly created tmp file oO`
return false; return false;
} }
ofstream b(filename, ios::out); ofstream b(filename, ios::out);
if(!b.is_open()) if(!b.is_open())
{ {
// can't open blacklist file for ordered/checked write
return false; return false;
} }
@ -624,14 +643,14 @@ inline bool findIpInFile(const char *filename, string ip)
if(boost::iequals(ip, line) == true) if(boost::iequals(ip, line) == true)
{ {
//cout << ip << " found in " << filename << endl; //cout << ip << " found in " << filename << endl;
cout << "Found." << endl; cerr << "Found: " << ip << endl;
iFound = 1; iFound = 1;
return true; return true;
} }
} }
if(iFound == 0) if(iFound == 0)
{ {
cout << "Not found." << endl; cerr << "Not found: " << ip << endl;
//cout << ip << " not found in " << filename << endl; //cout << ip << " not found in " << filename << endl;
} }
File.close(); File.close();

@ -4,13 +4,13 @@
namespace Version{ namespace Version{
//Software Status //Software Status
static const char STATUS[] = "Release Candidate [chronos-vs]"; static const char STATUS[] = "RCx [nod.oe7drt.com]";
//Miscellaneous Version Types //Miscellaneous Version Types
static const char VERSION_STRING [] = "0.5.2"; static const char VERSION_STRING [] = "0.6.0";
//Copyright line usable in the program //Copyright line usable in the program
static const char COPYRIGHT [] = "Copyright ©2015-2022 Dominic Reich\n"; static const char COPYRIGHT [] = "Copyright ©2015-2023 Dominic Reich\n";
} }
#endif //VERSION_H #endif //VERSION_H

Loading…
Cancel
Save