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

478 lines
14 KiB
C++
Raw Normal View History

2022-06-11 21:57:50 +02:00
#include <cstdlib>
#include <cstddef>
#include <iostream>
#include <string>
#include <csignal>
#include <fstream>
#include <regex>
2022-06-26 00:56:03 +02:00
#include <mutex>
2022-06-25 20:25:19 +02:00
#include <boost/thread.hpp>
2022-06-11 21:57:50 +02:00
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
2022-06-26 02:12:57 +02:00
#include <boost/bind/bind.hpp>
2022-06-11 21:57:50 +02:00
#include <boost/asio.hpp>
#include <boost/thread/mutex.hpp>
2022-06-26 12:15:17 +02:00
//#define MULTI_THREAD
2022-06-26 01:54:02 +02:00
//#define DEBUG
//#define DEBUG_PACKET
2022-06-26 13:29:54 +02:00
//#define THREAD_NUM
2022-06-18 20:32:27 +02:00
using namespace std;
2022-06-18 20:29:52 +02:00
2022-06-18 20:35:41 +02:00
boost::asio::io_service *ios_loop = nullptr;
2022-06-26 12:15:17 +02:00
bool unhexlify(string const &hex, string &newString) {
try{
int len = hex.length();
for(int i=0; i< len; i+=2)
{
std::string byte = hex.substr(i,2);
char chr = (char) (int)strtol(byte.c_str(), NULL, 16);
newString.push_back(chr);
}
return true;
}
catch (...){
return false;
}
2022-06-11 21:57:50 +02:00
}
2022-06-26 01:54:02 +02:00
struct regex_rules{
vector<pair<string,regex>> regex_s_c_w, regex_c_s_w, regex_s_c_b, regex_c_s_b;
};
shared_ptr<regex_rules> regex_config;
2022-06-11 21:57:50 +02:00
2022-06-18 20:32:27 +02:00
const char* config_file;
2022-06-26 02:12:57 +02:00
mutex update_mutex;
2022-06-26 12:15:17 +02:00
#ifdef MULTI_THREAD
mutex stdout_mutex;
#endif
2022-06-11 21:57:50 +02:00
2022-06-26 01:54:02 +02:00
bool filter_data(unsigned char* data, const size_t& bytes_transferred, vector<pair<string,regex>> const &blacklist, vector<pair<string,regex>> const &whitelist){
2022-06-26 00:56:03 +02:00
#ifdef DEBUG_PACKET
2022-06-26 02:12:57 +02:00
cerr << "---------------- Packet ----------------" << endl;
2022-06-11 21:57:50 +02:00
for(int i=0;i<bytes_transferred;i++){
2022-06-26 02:12:57 +02:00
cerr << data[i];
2022-06-11 21:57:50 +02:00
}
2022-06-26 02:12:57 +02:00
cerr << "\n" << "---------------- End Packet ----------------" << endl;
2022-06-11 21:57:50 +02:00
#endif
2022-06-26 01:54:02 +02:00
for (pair<string,regex> ele:blacklist){
try{
2022-06-26 01:54:02 +02:00
if(regex_search(reinterpret_cast<const char*>(data), reinterpret_cast<const char*>(data)+bytes_transferred, ele.second)){
2022-06-26 12:15:17 +02:00
#ifdef MULTI_THREAD
std::unique_lock<std::mutex> lck(stdout_mutex);
#endif
cout << "BLOCKED " << ele.first << endl;
return false;
}
} catch(...){
cerr << "Error while matching regex: " << ele.first << endl;
2022-06-11 21:57:50 +02:00
}
}
2022-06-26 01:54:02 +02:00
for (pair<string,regex> ele:whitelist){
try{
2022-06-26 01:54:02 +02:00
if(!regex_search(reinterpret_cast<const char*>(data),reinterpret_cast<const char*>(data)+bytes_transferred, ele.second)){
2022-06-26 12:15:17 +02:00
#ifdef MULTI_THREAD
std::unique_lock<std::mutex> lck(stdout_mutex);
#endif
cout << "BLOCKED " << ele.first << endl;
return false;
}
} catch(...){
cerr << "Error while matching regex: " << ele.first << endl;
}
2022-06-11 21:57:50 +02:00
}
#ifdef DEBUG
2022-06-26 02:12:57 +02:00
cerr << "Packet Accepted!" << endl;
2022-06-11 21:57:50 +02:00
#endif
return true;
}
namespace tcp_proxy
{
namespace ip = boost::asio::ip;
class bridge : public boost::enable_shared_from_this<bridge>
{
public:
2022-06-18 20:32:27 +02:00
typedef ip::tcp::socket socket_type;
typedef boost::shared_ptr<bridge> ptr_type;
2022-06-11 21:57:50 +02:00
bridge(boost::asio::io_service& ios)
: downstream_socket_(ios),
upstream_socket_ (ios)
{}
socket_type& downstream_socket()
{
2022-06-18 20:32:27 +02:00
// Client socket
2022-06-11 21:57:50 +02:00
return downstream_socket_;
}
socket_type& upstream_socket()
{
2022-06-18 20:32:27 +02:00
// Remote server socket
2022-06-11 21:57:50 +02:00
return upstream_socket_;
}
2022-06-22 01:31:46 +02:00
void start(const string& upstream_host, unsigned short upstream_port)
2022-06-11 21:57:50 +02:00
{
// Attempt connection to remote server (upstream side)
upstream_socket_.async_connect(
ip::tcp::endpoint(
boost::asio::ip::address::from_string(upstream_host),
upstream_port),
boost::bind(&bridge::handle_upstream_connect,
shared_from_this(),
boost::asio::placeholders::error));
}
void handle_upstream_connect(const boost::system::error_code& error)
{
if (!error)
{
// Setup async read from remote server (upstream)
upstream_socket_.async_read_some(
boost::asio::buffer(upstream_data_,max_data_length),
boost::bind(&bridge::handle_upstream_read,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
// Setup async read from client (downstream)
downstream_socket_.async_read_some(
boost::asio::buffer(downstream_data_,max_data_length),
boost::bind(&bridge::handle_downstream_read,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
close();
}
2022-06-18 20:32:27 +02:00
private:
2022-06-11 21:57:50 +02:00
/*
Section A: Remote Server --> Proxy --> Client
Process data recieved from remote sever then send to client.
*/
// Read from remote server complete, now send data to client
void handle_upstream_read(const boost::system::error_code& error,
const size_t& bytes_transferred) // Da Server a Client
{
if (!error)
{
2022-06-26 01:54:02 +02:00
shared_ptr<regex_rules> regex_old_config = regex_config;
if (filter_data(upstream_data_, bytes_transferred, regex_old_config->regex_s_c_b, regex_old_config->regex_s_c_w)){
2022-06-11 21:57:50 +02:00
async_write(downstream_socket_,
boost::asio::buffer(upstream_data_,bytes_transferred),
boost::bind(&bridge::handle_downstream_write,
shared_from_this(),
boost::asio::placeholders::error));
}else{
close();
}
}
else
close();
}
// Write to client complete, Async read from remote server
void handle_downstream_write(const boost::system::error_code& error)
{
if (!error)
{
2022-06-26 12:15:17 +02:00
2022-06-11 21:57:50 +02:00
upstream_socket_.async_read_some(
boost::asio::buffer(upstream_data_,max_data_length),
boost::bind(&bridge::handle_upstream_read,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
close();
}
// *** End Of Section A ***
/*
Section B: Client --> Proxy --> Remove Server
Process data recieved from client then write to remove server.
*/
// Read from client complete, now send data to remote server
void handle_downstream_read(const boost::system::error_code& error,
const size_t& bytes_transferred) // Da Client a Server
{
if (!error)
{
2022-06-26 01:54:02 +02:00
shared_ptr<regex_rules> regex_old_config = regex_config;
if (filter_data(downstream_data_, bytes_transferred, regex_old_config->regex_c_s_b, regex_old_config->regex_c_s_w)){
2022-06-11 21:57:50 +02:00
async_write(upstream_socket_,
boost::asio::buffer(downstream_data_,bytes_transferred),
boost::bind(&bridge::handle_upstream_write,
shared_from_this(),
boost::asio::placeholders::error));
}else{
close();
}
}
else
close();
}
// Write to remote server complete, Async read from client
void handle_upstream_write(const boost::system::error_code& error)
{
if (!error)
{
downstream_socket_.async_read_some(
boost::asio::buffer(downstream_data_,max_data_length),
boost::bind(&bridge::handle_downstream_read,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
close();
}
// *** End Of Section B ***
void close()
{
boost::mutex::scoped_lock lock(mutex_);
if (downstream_socket_.is_open())
{
downstream_socket_.close();
}
if (upstream_socket_.is_open())
{
upstream_socket_.close();
}
}
socket_type downstream_socket_;
socket_type upstream_socket_;
enum { max_data_length = 8192 }; //8KB
unsigned char downstream_data_[max_data_length];
unsigned char upstream_data_ [max_data_length];
2022-06-18 20:32:27 +02:00
boost::mutex mutex_;
2022-06-11 21:57:50 +02:00
public:
2022-06-18 20:32:27 +02:00
class acceptor
2022-06-18 20:29:52 +02:00
{
2022-06-18 20:32:27 +02:00
public:
acceptor(boost::asio::io_service& io_service,
2022-06-22 01:31:46 +02:00
const string& local_host, unsigned short local_port,
const string& upstream_host, unsigned short upstream_port)
2022-06-18 20:32:27 +02:00
: io_service_(io_service),
localhost_address(boost::asio::ip::address_v4::from_string(local_host)),
acceptor_(io_service_,ip::tcp::endpoint(localhost_address,local_port)),
upstream_port_(upstream_port),
upstream_host_(upstream_host)
{}
bool accept_connections()
2022-06-18 20:29:52 +02:00
{
2022-06-18 20:32:27 +02:00
try
{
session_ = boost::shared_ptr<bridge>(new bridge(io_service_));
2022-06-11 21:57:50 +02:00
2022-06-18 20:32:27 +02:00
acceptor_.async_accept(session_->downstream_socket(),
boost::bind(&acceptor::handle_accept,
this,
boost::asio::placeholders::error));
}
2022-06-22 01:31:46 +02:00
catch(exception& e)
2022-06-18 20:32:27 +02:00
{
2022-06-22 01:31:46 +02:00
cerr << "acceptor exception: " << e.what() << endl;
2022-06-18 20:32:27 +02:00
return false;
}
2022-06-18 20:29:52 +02:00
2022-06-18 20:32:27 +02:00
return true;
}
2022-06-18 20:29:52 +02:00
2022-06-18 20:32:27 +02:00
private:
2022-06-11 21:57:50 +02:00
2022-06-18 20:32:27 +02:00
void handle_accept(const boost::system::error_code& error)
2022-06-11 21:57:50 +02:00
{
2022-06-18 20:32:27 +02:00
if (!error)
{
session_->start(upstream_host_,upstream_port_);
2022-06-11 21:57:50 +02:00
2022-06-18 20:32:27 +02:00
if (!accept_connections())
{
2022-06-22 01:31:46 +02:00
cerr << "Failure during call to accept." << endl;
2022-06-18 20:32:27 +02:00
}
}
else
2022-06-11 21:57:50 +02:00
{
2022-06-22 01:31:46 +02:00
cerr << "Error: " << error.message() << endl;
2022-06-11 21:57:50 +02:00
}
}
2022-06-18 20:32:27 +02:00
boost::asio::io_service& io_service_;
ip::address_v4 localhost_address;
ip::tcp::acceptor acceptor_;
ptr_type session_;
unsigned short upstream_port_;
2022-06-22 01:31:46 +02:00
string upstream_host_;
2022-06-18 20:32:27 +02:00
};
2022-06-18 14:48:09 +02:00
2022-06-18 20:32:27 +02:00
};
}
2022-06-26 01:54:02 +02:00
void push_regex(char* arg, bool case_sensitive, vector<pair<string,regex>> &v){
2022-06-18 20:32:27 +02:00
size_t expr_len = (strlen(arg)-2)/2;
2022-06-26 12:15:17 +02:00
string hex(arg+2);
string expr;
if (!unhexlify(hex, expr)){
cerr << "Regex " << arg << " was not unhexlified successfully" << endl;
return;
}
try{
if (case_sensitive){
2022-06-26 12:15:17 +02:00
regex regex(expr);
#ifdef DEBUG
2022-06-26 02:12:57 +02:00
cerr << "Added case sensitive regex " << expr_str << endl;
#endif
2022-06-26 01:54:02 +02:00
v.push_back(make_pair(string(arg), regex));
} else {
2022-06-26 12:15:17 +02:00
regex regex(expr,regex_constants::icase);
#ifdef DEBUG
2022-06-26 02:12:57 +02:00
cerr << "Added case insensitive regex " << expr_str << endl;
#endif
2022-06-26 01:54:02 +02:00
v.push_back(make_pair(string(arg), regex));
}
} catch(...){
2022-06-22 01:31:46 +02:00
cerr << "Regex " << expr << " was not compiled successfully" << endl;
}
}
2022-06-18 20:32:27 +02:00
void update_regex(){
2022-06-26 02:12:57 +02:00
std::unique_lock<std::mutex> lck(update_mutex);
2022-06-18 20:32:27 +02:00
fstream fd;
fd.open(config_file,ios::in);
if (!fd.is_open()){
2022-06-22 01:31:46 +02:00
cerr << "Error: config file couln't be opened" << endl;
2022-06-18 20:32:27 +02:00
exit(1);
}
2022-06-26 01:54:02 +02:00
regex_rules *regex_new_config = new regex_rules();
2022-06-18 20:32:27 +02:00
string line;
while(getline(fd, line)){
char tp[line.length() +1];
strcpy(tp, line.c_str());
if (strlen(tp) >= 2){
bool case_sensitive = true;
if(tp[0] == '0'){
case_sensitive = false;
}
switch(tp[1]){
case 'C': { // Client to server Blacklist
2022-06-26 01:54:02 +02:00
push_regex(tp, case_sensitive, regex_new_config->regex_c_s_b);
2022-06-18 20:32:27 +02:00
break;
}
case 'c': { // Client to server Whitelist
2022-06-26 01:54:02 +02:00
push_regex(tp, case_sensitive, regex_new_config->regex_c_s_w);
2022-06-18 20:32:27 +02:00
break;
}
case 'S': { // Server to client Blacklist
2022-06-26 01:54:02 +02:00
push_regex(tp, case_sensitive, regex_new_config->regex_s_c_b);
2022-06-18 20:32:27 +02:00
break;
}
case 's': { // Server to client Whitelist
2022-06-26 01:54:02 +02:00
push_regex(tp, case_sensitive, regex_new_config->regex_s_c_w);
2022-06-18 20:32:27 +02:00
break;
}
}
}
}
2022-06-26 01:54:02 +02:00
regex_config.reset(regex_new_config);
2022-06-18 20:32:27 +02:00
}
void signal_handler(int signal_num)
{
if (signal_num == SIGUSR1){
2022-06-18 20:35:41 +02:00
#ifdef DEBUG
2022-06-26 02:12:57 +02:00
cerr << "Updating configurtation" << endl;
2022-06-18 20:35:41 +02:00
#endif
2022-06-18 20:32:27 +02:00
update_regex();
2022-06-18 20:35:41 +02:00
}else if(signal_num == SIGTERM){
if (ios_loop != nullptr) ios_loop->stop();
2022-06-26 02:12:57 +02:00
#ifdef DEBUG
cerr << "Close Requested" << endl;
#endif
2022-06-18 20:35:41 +02:00
exit(0);
2022-06-18 20:32:27 +02:00
}
}
int main(int argc, char* argv[])
{
if (argc < 6)
{
2022-06-22 01:31:46 +02:00
cerr << "usage: tcpproxy_server <local host ip> <local port> <forward host ip> <forward port> <config_file>" << endl;
2022-06-18 20:32:27 +02:00
return 1;
}
const unsigned short local_port = static_cast<unsigned short>(::atoi(argv[2]));
const unsigned short forward_port = static_cast<unsigned short>(::atoi(argv[4]));
2022-06-22 01:31:46 +02:00
const string local_host = argv[1];
const string forward_host = argv[3];
2022-06-18 20:32:27 +02:00
2022-06-18 23:48:10 +02:00
config_file = argv[5];
2022-06-18 20:35:41 +02:00
update_regex();
signal(SIGUSR1, signal_handler);
signal(SIGTERM, signal_handler);
2022-06-18 20:32:27 +02:00
2022-06-11 21:57:50 +02:00
boost::asio::io_service ios;
2022-06-18 20:35:41 +02:00
ios_loop = &ios;
2022-06-18 20:32:27 +02:00
2022-06-11 21:57:50 +02:00
try
{
2022-06-18 20:32:27 +02:00
tcp_proxy::bridge::acceptor acceptor(ios,
local_host, local_port,
forward_host, forward_port);
2022-06-11 21:57:50 +02:00
acceptor.accept_connections();
2022-06-26 12:15:17 +02:00
#ifdef MULTI_THREAD
2022-06-25 20:25:19 +02:00
boost::thread_group tg;
2022-06-26 13:29:54 +02:00
#ifdef THREAD_NUM
for (unsigned i = 0; i < THREAD_NUM; ++i)
#else
2022-06-25 20:25:19 +02:00
for (unsigned i = 0; i < thread::hardware_concurrency(); ++i)
2022-06-26 13:29:54 +02:00
#endif
2022-06-25 20:25:19 +02:00
tg.create_thread(boost::bind(&boost::asio::io_service::run, &ios));
tg.join_all();
2022-06-26 12:15:17 +02:00
#else
ios.run();
#endif
2022-06-11 21:57:50 +02:00
}
2022-06-22 01:31:46 +02:00
catch(exception& e)
2022-06-11 21:57:50 +02:00
{
2022-06-22 01:31:46 +02:00
cerr << "Error: " << e.what() << endl;
2022-06-18 20:32:27 +02:00
return 1;
2022-06-11 21:57:50 +02:00
}
2022-06-18 20:32:27 +02:00
return 0;
2022-06-11 21:57:50 +02:00
}
/*
* [Note] On posix systems the tcp proxy server build command is as follows:
* c++ -pedantic -ansi -Wall -Werror -O3 -o tcpproxy_server tcpproxy_server.cpp -L/usr/lib -lstdc++ -lpthread -lboost_thread -lboost_system
*/