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

@ -4,13 +4,13 @@
namespace Version{
//Software Status
static const char STATUS[] = "Release Candidate [chronos-vs]";
static const char STATUS[] = "RCx [nod.oe7drt.com]";
//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
static const char COPYRIGHT [] = "Copyright ©2015-2022 Dominic Reich\n";
static const char COPYRIGHT [] = "Copyright ©2015-2023 Dominic Reich\n";
}
#endif //VERSION_H

Loading…
Cancel
Save