Files
firegex-traffic-viewer/backend/binsrc/nfregex.cpp

82 lines
2.1 KiB
C++
Raw Normal View History

#include "regex/regex_rules.cpp"
#include "regex/regexfilter.cpp"
#include "classes/netfilter.cpp"
#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;
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:
NFQUEUE_FAIL_OPEN - enable fail-open option of nfqueueß
---
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);
if (cin.eof()){
2022-07-19 15:17:34 +02:00
cerr << "[fatal] [updater] cin.eof()" << endl;
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);
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;
if (data != "" && data != "\n"){
raw_rules.push_back(data);
}
2022-07-16 15:24:05 +02: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;
osyncstream(cout) << "ACK OK" << endl;
}catch(const std::exception& e){
cerr << "[error] [updater] Failed to build new configuration!" << endl;
osyncstream(cout) << "ACK FAIL " << e.what() << endl;
}
}
2025-02-04 21:09:03 +01:00
}
int main(int argc, char *argv[]){
2022-07-22 00:34:57 +02:00
int n_of_threads = 1;
char * n_threads_str = getenv("NTHREADS");
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;
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
regex_config.reset(new RegexRules(stream_mode));
MultiThreadQueue<RegexNfQueue> queue_manager(n_of_threads);
2022-07-15 17:26:40 +02:00
osyncstream(cout) << "QUEUE " << queue_manager.queue_num() << endl;
cerr << "[info] [main] Queue: " << queue_manager.queue_num() << " threads assigned: " << n_of_threads << " stream mode: " << stream_mode << endl;
2022-07-15 17:26:40 +02:00
thread qthr([&](){
queue_manager.start();
});
2022-07-15 17:26:40 +02:00
config_updater();
qthr.join();
2022-07-15 10:08:54 +02:00
}