|
|
@@ -0,0 +1,77 @@ |
|
|
|
////////////////////////////////////////////////////////////////////// |
|
|
|
// Import "hosts" file as a black list |
|
|
|
// Written by Jonathan A. Foster <ChipMaster@YeOlPiShack.net> |
|
|
|
// Started March 23rd, 2022 |
|
|
|
// |
|
|
|
// We want to read a file formatted as /etc/hosts and add all names |
|
|
|
// listed in it to the "dns" table as a "block" (status=2). |
|
|
|
////////////////////////////////////////////////////////////////////// |
|
|
|
#include <iostream> |
|
|
|
#include <fstream> |
|
|
|
#include <stdexcept> |
|
|
|
#include "../strutil.h" |
|
|
|
#include "appbase.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct BlakcImpApp: public TrafficMonBaseApp { |
|
|
|
int lnct; |
|
|
|
int impct; |
|
|
|
int cli_pass; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
virtual void do_switch_arg(const char *sw, const std::string &val) { |
|
|
|
// handle switches on pass 0 |
|
|
|
if(!cli_pass) TrafficMonBaseApp::do_switch_arg(sw, val); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
virtual void do_arg(const char *fname) { |
|
|
|
int x; |
|
|
|
std::string s; |
|
|
|
std::ifstream hosts(fname); |
|
|
|
cppdb::statement q; |
|
|
|
|
|
|
|
if(!cli_pass) return; // postpone until pass 1 (2) |
|
|
|
if(!db.is_open()) |
|
|
|
throw CLIerror("You must specify a configuration file first"); |
|
|
|
q = db << |
|
|
|
"INSERT INTO dns (name,status,note) VALUES (?,2,'import from black list')"; |
|
|
|
|
|
|
|
while(std::getline(hosts, s)) { |
|
|
|
s = trim(s); |
|
|
|
if(s=="" || s[0]=='#') continue; |
|
|
|
for(x=0; x<s.size() && s[x]>' '; x++); |
|
|
|
s=trim(s.substr(x)); |
|
|
|
if(s=="") continue; |
|
|
|
|
|
|
|
// TODO: more than one host on a line |
|
|
|
lnct++; |
|
|
|
q.reset(); |
|
|
|
try { |
|
|
|
q << s << cppdb::exec; |
|
|
|
impct+=q.affected(); |
|
|
|
} catch(const std::exception &e) { |
|
|
|
std::cerr << e.what() << std::endl; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main() { |
|
|
|
int x; |
|
|
|
|
|
|
|
cli_pass = lnct = impct = 0; |
|
|
|
if(x=TrafficMonBaseApp::main()) return x; |
|
|
|
++cli_pass; |
|
|
|
x=TrafficMonBaseApp::main(); |
|
|
|
std::cout << "Records: " << lnct << " imports: " << impct << '\n'; |
|
|
|
return x; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
MAIN(BlakcImpApp) |