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;
|
2025-02-16 16:33:34 +01:00
|
|
|
using namespace Firegex::Regex;
|
|
|
|
|
using Firegex::NfQueue::MultiThreadQueue;
|
2022-07-15 17:26:40 +02:00
|
|
|
|
2025-02-17 13:07:06 +01:00
|
|
|
/*
|
|
|
|
|
Compile options:
|
|
|
|
|
USE_PIPES_FOR_BLOKING_QUEUE - use pipes instead of conditional variable, queue and mutex for blocking queue
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
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[]){
|
2025-02-18 21:20:19 +01:00
|
|
|
|
|
|
|
|
char * test_regex = getenv("FIREGEX_TEST_REGEX");
|
|
|
|
|
if (test_regex != nullptr){
|
|
|
|
|
cerr << "[info] [main] Testing regex: " << test_regex << endl;
|
|
|
|
|
try{
|
|
|
|
|
RegexRules::compile_regex(test_regex);
|
|
|
|
|
cerr << "[info] [main] Test passed" << endl;
|
|
|
|
|
return 0;
|
|
|
|
|
}catch(const std::exception& e){
|
|
|
|
|
cerr << "[error] [updater] Test failed" << endl;
|
|
|
|
|
cout << e.what() << flush;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-18 17:36:15 +01:00
|
|
|
|
|
|
|
|
bool fail_open = strcmp(getenv("FIREGEX_NFQUEUE_FAIL_OPEN"), "1") == 0;
|
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-16 16:33:34 +01:00
|
|
|
|
|
|
|
|
MultiThreadQueue<RegexNfQueue> queue_manager(n_of_threads);
|
|
|
|
|
osyncstream(cout) << "QUEUE " << queue_manager.queue_num() << endl;
|
2025-02-18 17:36:15 +01:00
|
|
|
cerr << "[info] [main] Queue: " << queue_manager.queue_num() << " threads assigned: " << n_of_threads << " stream mode: " << stream_mode << " fail open: " << fail_open << endl;
|
2022-07-15 17:26:40 +02:00
|
|
|
|
2025-02-16 16:33:34 +01:00
|
|
|
thread qthr([&](){
|
|
|
|
|
queue_manager.start();
|
|
|
|
|
});
|
2022-07-15 17:26:40 +02:00
|
|
|
config_updater();
|
2025-02-16 16:33:34 +01:00
|
|
|
qthr.join();
|
|
|
|
|
|
2022-07-15 10:08:54 +02:00
|
|
|
}
|