Files
firegex-traffic-viewer/backend/nfqueue/nfqueue.cpp

62 lines
1.6 KiB
C++
Raw Normal View History

#include "classes/regex_filter.hpp"
#include "classes/netfilter.hpp"
#include "utils.hpp"
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
shared_ptr<regex_rules> regex_config;
2022-07-16 15:24:05 +02:00
void config_updater (){
string line, data;
while (true){
getline(cin, line);
if (cin.eof()){
cerr << "[fatal] [upfdater] cin.eof()" << endl;
exit(EXIT_FAILURE);
}
2022-07-16 15:24:05 +02:00
if (cin.bad()){
cerr << "[fatal] [upfdater] 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);
regex_rules *regex_new_config = new regex_rules();
while(!config_stream.eof()){
config_stream >> data;
if (data != "" && data != "\n"){
regex_new_config->add(data.c_str());
}
2022-07-16 15:24:05 +02:00
}
regex_config.reset(regex_new_config);
cerr << "[info] [updater] Config update done" << endl;
}
}
2022-07-16 14:22:33 +02:00
template <bool is_input>
bool filter_callback(const uint8_t *data, uint32_t len){
shared_ptr<regex_rules> current_config = regex_config;
2022-07-16 14:44:44 +02:00
return current_config->check((unsigned char *)data, len, is_input);
2022-07-16 01:04:55 +02:00
}
2022-07-15 10:08:54 +02:00
int main(int argc, char *argv[])
{
2022-07-15 17:26:40 +02:00
if(!is_sudo()){
cerr << "[fatal] [main] You must be root to run this program" << endl;
2022-07-15 10:08:54 +02:00
exit(EXIT_FAILURE);
}
2022-07-15 17:26:40 +02:00
int n_of_queue = 1;
if (argc >= 2) n_of_queue = atoi(argv[1]);
2022-07-16 14:22:33 +02:00
regex_config.reset(new regex_rules());
NFQueueSequence<filter_callback<true>> input_queues(n_of_queue);
2022-07-16 01:04:55 +02:00
input_queues.start();
2022-07-16 14:22:33 +02:00
NFQueueSequence<filter_callback<false>> output_queues(n_of_queue);
2022-07-16 01:04:55 +02:00
output_queues.start();
2022-07-15 17:26:40 +02:00
2022-07-16 13:45:31 +02:00
cout << "QUEUES INPUT " << input_queues.init() << " " << input_queues.end() << " OUTPUT " << output_queues.init() << " " << output_queues.end() << endl;
2022-07-15 17:26:40 +02:00
config_updater();
2022-07-15 10:08:54 +02:00
}