2025-02-09 22:32:48 +01:00
|
|
|
#include "regex/regex_rules.cpp"
|
|
|
|
|
#include "regex/regexfilter.cpp"
|
2025-02-02 19:54:42 +01:00
|
|
|
#include "classes/netfilter.cpp"
|
2025-02-09 22:32:48 +01:00
|
|
|
#include <syncstream>
|
2022-07-15 10:08:54 +02:00
|
|
|
#include <iostream>
|
2022-07-16 14:22:33 +02:00
|
|
|
|
2022-07-15 10:08:54 +02:00
|
|
|
using namespace std;
|
2022-07-15 17:26:40 +02:00
|
|
|
|
2022-07-16 15:24:05 +02:00
|
|
|
void config_updater (){
|
2022-07-18 23:01:24 +02:00
|
|
|
string line;
|
2022-07-16 15:24:05 +02:00
|
|
|
while (true){
|
|
|
|
|
getline(cin, line);
|
2022-07-18 18:52:14 +02:00
|
|
|
if (cin.eof()){
|
2022-07-19 15:17:34 +02:00
|
|
|
cerr << "[fatal] [updater] cin.eof()" << endl;
|
2022-07-18 18:52:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
2022-07-16 15:24:05 +02:00
|
|
|
if (cin.bad()){
|
2022-07-19 15:17:34 +02:00
|
|
|
cerr << "[fatal] [updater] cin.bad()" << endl;
|
2022-07-16 15:24:05 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
cerr << "[info] [updater] Updating configuration with line " << line << endl;
|
|
|
|
|
istringstream config_stream(line);
|
2025-02-02 19:54:42 +01:00
|
|
|
vector<string> raw_rules;
|
|
|
|
|
|
2022-07-16 15:24:05 +02:00
|
|
|
while(!config_stream.eof()){
|
2022-07-18 23:01:24 +02:00
|
|
|
string data;
|
2022-07-16 15:24:05 +02:00
|
|
|
config_stream >> data;
|
2022-07-18 18:52:14 +02:00
|
|
|
if (data != "" && data != "\n"){
|
2025-02-02 19:54:42 +01:00
|
|
|
raw_rules.push_back(data);
|
2022-07-18 18:52:14 +02:00
|
|
|
}
|
2022-07-16 15:24:05 +02:00
|
|
|
}
|
2025-02-02 19:54:42 +01:00
|
|
|
try{
|
|
|
|
|
regex_config.reset(new RegexRules(raw_rules, regex_config->stream_mode()));
|
2025-02-03 02:04:10 +01:00
|
|
|
cerr << "[info] [updater] Config update done to ver "<< regex_config->ver() << endl;
|
2025-02-09 22:32:48 +01:00
|
|
|
osyncstream(cout) << "ACK OK" << endl;
|
2025-02-05 01:48:36 +01:00
|
|
|
}catch(const std::exception& e){
|
2025-02-02 19:54:42 +01:00
|
|
|
cerr << "[error] [updater] Failed to build new configuration!" << endl;
|
2025-02-09 22:32:48 +01:00
|
|
|
osyncstream(cout) << "ACK FAIL " << e.what() << endl;
|
2025-02-02 19:54:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
2025-02-04 21:09:03 +01:00
|
|
|
|
2025-02-02 19:54:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]){
|
2022-07-22 00:34:57 +02:00
|
|
|
int n_of_threads = 1;
|
2022-08-02 19:45:28 +00:00
|
|
|
char * n_threads_str = getenv("NTHREADS");
|
2025-02-02 19:54:42 +01:00
|
|
|
if (n_threads_str != nullptr) n_of_threads = ::atoi(n_threads_str);
|
2022-07-22 00:34:57 +02:00
|
|
|
if(n_of_threads <= 0) n_of_threads = 1;
|
2025-02-02 19:54:42 +01:00
|
|
|
|
|
|
|
|
char * matchmode = getenv("MATCH_MODE");
|
|
|
|
|
bool stream_mode = true;
|
|
|
|
|
if (matchmode != nullptr && strcmp(matchmode, "block") == 0){
|
|
|
|
|
stream_mode = false;
|
|
|
|
|
}
|
2025-02-04 22:51:30 +01:00
|
|
|
|
2025-02-02 19:54:42 +01:00
|
|
|
regex_config.reset(new RegexRules(stream_mode));
|
|
|
|
|
|
2025-02-09 22:32:48 +01:00
|
|
|
NFQueueSequence<RegexQueue> queues(n_of_threads);
|
2025-02-02 19:54:42 +01:00
|
|
|
queues.start();
|
2022-07-15 17:26:40 +02:00
|
|
|
|
2025-02-09 22:32:48 +01:00
|
|
|
osyncstream(cout) << "QUEUES " << queues.init() << " " << queues.end() << endl;
|
2025-02-04 22:51:30 +01:00
|
|
|
cerr << "[info] [main] Queues: " << queues.init() << ":" << queues.end() << " threads assigned: " << n_of_threads << " stream mode: " << stream_mode << endl;
|
2022-07-15 17:26:40 +02:00
|
|
|
|
|
|
|
|
config_updater();
|
2022-07-15 10:08:54 +02:00
|
|
|
}
|