Multithread fix with strand implementation

This commit is contained in:
DomySh
2022-06-26 17:53:57 +02:00
parent 95cf16ce76
commit e4a96f3ad4
8 changed files with 54 additions and 45 deletions

View File

@@ -51,3 +51,6 @@ This means that firegex is projected to avoid any possibility to have the servic
6. If a regex makes trouble, you can delete it (this have an instant effect on the proxy), or put the service in pause (call also Transparent mode), this will deactivate all the filters from the proxy, but still continue to publish the service on the right port 6. If a regex makes trouble, you can delete it (this have an instant effect on the proxy), or put the service in pause (call also Transparent mode), this will deactivate all the filters from the proxy, but still continue to publish the service on the right port
7. Every status change (except if you decide to stop the proxy) that you made to the service, and so to the proxy is instantaneous and done with 0 down time. The proxy is **never** restarted, it's configuration changes during runtime 7. Every status change (except if you decide to stop the proxy) that you made to the service, and so to the proxy is instantaneous and done with 0 down time. The proxy is **never** restarted, it's configuration changes during runtime
# Credits
- Copyright (c) 2007 Arash Partow (http://www.partow.net) for the base of our proxy implementation
- Pwnzer0tt1

View File

@@ -1,3 +1,7 @@
/* Copyright (c) 2007 Arash Partow (http://www.partow.net)
URL: http://www.partow.net/programming/tcpproxy/index.html
Modified and adapted by Pwnzer0tt1
*/
#include <cstdlib> #include <cstdlib>
#include <cstddef> #include <cstddef>
#include <iostream> #include <iostream>
@@ -14,10 +18,6 @@
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/thread/mutex.hpp> #include <boost/thread/mutex.hpp>
//#define MULTI_THREAD
//#define DEBUG
//#define DEBUG_PACKET
//#define THREAD_NUM
using namespace std; using namespace std;
boost::asio::io_service *ios_loop = nullptr; boost::asio::io_service *ios_loop = nullptr;
@@ -149,7 +149,8 @@ namespace tcp_proxy
bridge(boost::asio::io_service& ios) bridge(boost::asio::io_service& ios)
: downstream_socket_(ios), : downstream_socket_(ios),
upstream_socket_ (ios) upstream_socket_ (ios),
thread_safety(ios)
{} {}
socket_type& downstream_socket() socket_type& downstream_socket()
@@ -171,9 +172,11 @@ namespace tcp_proxy
ip::tcp::endpoint( ip::tcp::endpoint(
boost::asio::ip::address::from_string(upstream_host), boost::asio::ip::address::from_string(upstream_host),
upstream_port), upstream_port),
boost::bind(&bridge::handle_upstream_connect, boost::asio::bind_executor(thread_safety,
boost::bind(
&bridge::handle_upstream_connect,
shared_from_this(), shared_from_this(),
boost::asio::placeholders::error)); boost::asio::placeholders::error)));
} }
void handle_upstream_connect(const boost::system::error_code& error) void handle_upstream_connect(const boost::system::error_code& error)
@@ -181,20 +184,23 @@ namespace tcp_proxy
if (!error) if (!error)
{ {
// Setup async read from remote server (upstream) // Setup async read from remote server (upstream)
upstream_socket_.async_read_some( upstream_socket_.async_read_some(
boost::asio::buffer(upstream_data_,max_data_length), boost::asio::buffer(upstream_data_,max_data_length),
boost::bind(&bridge::handle_upstream_read, boost::asio::bind_executor(thread_safety,
shared_from_this(), boost::bind(&bridge::handle_upstream_read,
boost::asio::placeholders::error, shared_from_this(),
boost::asio::placeholders::bytes_transferred)); boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
// Setup async read from client (downstream) // Setup async read from client (downstream)
downstream_socket_.async_read_some( downstream_socket_.async_read_some(
boost::asio::buffer(downstream_data_,max_data_length), boost::asio::buffer(downstream_data_,max_data_length),
boost::bind(&bridge::handle_downstream_read, boost::asio::bind_executor(thread_safety,
shared_from_this(), boost::bind(&bridge::handle_downstream_read,
boost::asio::placeholders::error, shared_from_this(),
boost::asio::placeholders::bytes_transferred)); boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
} }
else else
close(); close();
@@ -217,9 +223,10 @@ namespace tcp_proxy
if (filter_data(upstream_data_, bytes_transferred, regex_old_config->regex_s_c_b, regex_old_config->regex_s_c_w)){ if (filter_data(upstream_data_, bytes_transferred, regex_old_config->regex_s_c_b, regex_old_config->regex_s_c_w)){
async_write(downstream_socket_, async_write(downstream_socket_,
boost::asio::buffer(upstream_data_,bytes_transferred), boost::asio::buffer(upstream_data_,bytes_transferred),
boost::bind(&bridge::handle_downstream_write, boost::asio::bind_executor(thread_safety,
boost::bind(&bridge::handle_downstream_write,
shared_from_this(), shared_from_this(),
boost::asio::placeholders::error)); boost::asio::placeholders::error)));
}else{ }else{
close(); close();
} }
@@ -236,10 +243,11 @@ namespace tcp_proxy
upstream_socket_.async_read_some( upstream_socket_.async_read_some(
boost::asio::buffer(upstream_data_,max_data_length), boost::asio::buffer(upstream_data_,max_data_length),
boost::bind(&bridge::handle_upstream_read, boost::asio::bind_executor(thread_safety,
shared_from_this(), boost::bind(&bridge::handle_upstream_read,
boost::asio::placeholders::error, shared_from_this(),
boost::asio::placeholders::bytes_transferred)); boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
} }
else else
close(); close();
@@ -262,9 +270,10 @@ namespace tcp_proxy
if (filter_data(downstream_data_, bytes_transferred, regex_old_config->regex_c_s_b, regex_old_config->regex_c_s_w)){ if (filter_data(downstream_data_, bytes_transferred, regex_old_config->regex_c_s_b, regex_old_config->regex_c_s_w)){
async_write(upstream_socket_, async_write(upstream_socket_,
boost::asio::buffer(downstream_data_,bytes_transferred), boost::asio::buffer(downstream_data_,bytes_transferred),
boost::bind(&bridge::handle_upstream_write, boost::asio::bind_executor(thread_safety,
boost::bind(&bridge::handle_upstream_write,
shared_from_this(), shared_from_this(),
boost::asio::placeholders::error)); boost::asio::placeholders::error)));
}else{ }else{
close(); close();
} }
@@ -280,10 +289,11 @@ namespace tcp_proxy
{ {
downstream_socket_.async_read_some( downstream_socket_.async_read_some(
boost::asio::buffer(downstream_data_,max_data_length), boost::asio::buffer(downstream_data_,max_data_length),
boost::bind(&bridge::handle_downstream_read, boost::asio::bind_executor(thread_safety,
shared_from_this(), boost::bind(&bridge::handle_downstream_read,
boost::asio::placeholders::error, shared_from_this(),
boost::asio::placeholders::bytes_transferred)); boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
} }
else else
close(); close();
@@ -311,7 +321,7 @@ namespace tcp_proxy
enum { max_data_length = 8192 }; //8KB enum { max_data_length = 8192 }; //8KB
unsigned char downstream_data_[max_data_length]; unsigned char downstream_data_[max_data_length];
unsigned char upstream_data_ [max_data_length]; unsigned char upstream_data_ [max_data_length];
boost::asio::io_service::strand thread_safety;
boost::mutex mutex_; boost::mutex mutex_;
public: public:
@@ -336,9 +346,10 @@ namespace tcp_proxy
session_ = boost::shared_ptr<bridge>(new bridge(io_service_)); session_ = boost::shared_ptr<bridge>(new bridge(io_service_));
acceptor_.async_accept(session_->downstream_socket(), acceptor_.async_accept(session_->downstream_socket(),
boost::asio::bind_executor(session_->thread_safety,
boost::bind(&acceptor::handle_accept, boost::bind(&acceptor::handle_accept,
this, this,
boost::asio::placeholders::error)); boost::asio::placeholders::error)));
} }
catch(exception& e) catch(exception& e)
{ {
@@ -461,8 +472,3 @@ int main(int argc, char* argv[])
return 0; return 0;
} }
/*
* [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
*/

View File

@@ -1,13 +1,13 @@
{ {
"files": { "files": {
"main.css": "/static/css/main.0efd334b.css", "main.css": "/static/css/main.0efd334b.css",
"main.js": "/static/js/main.d68a5509.js", "main.js": "/static/js/main.f153478b.js",
"index.html": "/index.html", "index.html": "/index.html",
"main.0efd334b.css.map": "/static/css/main.0efd334b.css.map", "main.0efd334b.css.map": "/static/css/main.0efd334b.css.map",
"main.d68a5509.js.map": "/static/js/main.d68a5509.js.map" "main.f153478b.js.map": "/static/js/main.f153478b.js.map"
}, },
"entrypoints": [ "entrypoints": [
"static/css/main.0efd334b.css", "static/css/main.0efd334b.css",
"static/js/main.d68a5509.js" "static/js/main.f153478b.js"
] ]
} }

View File

@@ -1 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png"><link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="manifest" href="/site.webmanifest"><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#FFFFFFFF"/><meta name="description" content="Firegex by Pwnzer0tt1"/><title>Firegex</title><script defer="defer" src="/static/js/main.d68a5509.js"></script><link href="/static/css/main.0efd334b.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html> <!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png"><link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="manifest" href="/site.webmanifest"><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#FFFFFFFF"/><meta name="description" content="Firegex by Pwnzer0tt1"/><title>Firegex</title><script defer="defer" src="/static/js/main.f153478b.js"></script><link href="/static/css/main.0efd334b.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>

File diff suppressed because one or more lines are too long

View File

@@ -76,7 +76,7 @@ function RegexView({ regexInfo }:{ regexInfo:RegexFilter }) {
</Grid> </Grid>
<YesNoModal <YesNoModal
title='Are you sure to delete this regex?' title='Are you sure to delete this regex?'
description={`You are going to delete the regex '${regex_expr}', causing the restart of the firewall if it is active.`} description={`You are going to delete the regex '${regex_expr}'.`}
onClose={()=>setDeleteModal(false)} onClose={()=>setDeleteModal(false)}
action={deleteRegex} action={deleteRegex}
opened={deleteModal} opened={deleteModal}