Multithrading now working correctly

This commit is contained in:
nik012003
2022-06-26 01:54:02 +02:00
committed by DomySh
parent 282438b005
commit 67f04cc8dc
9 changed files with 112 additions and 60 deletions

View File

@@ -14,7 +14,8 @@
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/thread/mutex.hpp> #include <boost/thread/mutex.hpp>
#define DEBUG //#define DEBUG
//#define DEBUG_PACKET
using namespace std; using namespace std;
@@ -38,12 +39,15 @@ unhexlify(InputIterator first, InputIterator last, OutputIterator ascii) {
} }
return 0; return 0;
} }
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;
vector<pair<string,regex>> *regex_s_c_w, *regex_c_s_w, *regex_s_c_b, *regex_c_s_b;
const char* config_file; const char* config_file;
mutex mtx; mutex mtx;
bool filter_data(unsigned char* data, const size_t& bytes_transferred, vector<pair<string,regex>> *const blacklist, vector<pair<string,regex>> *const whitelist){ bool filter_data(unsigned char* data, const size_t& bytes_transferred, vector<pair<string,regex>> const &blacklist, vector<pair<string,regex>> const &whitelist){
#ifdef DEBUG_PACKET #ifdef DEBUG_PACKET
cout << "---------------- Packet ----------------" << endl; cout << "---------------- Packet ----------------" << endl;
for(int i=0;i<bytes_transferred;i++){ for(int i=0;i<bytes_transferred;i++){
@@ -51,13 +55,9 @@ bool filter_data(unsigned char* data, const size_t& bytes_transferred, vector<pa
} }
cout << "\n" << "---------------- End Packet ----------------" << endl; cout << "\n" << "---------------- End Packet ----------------" << endl;
#endif #endif
vector<pair<string,regex>> *bp = blacklist; for (pair<string,regex> ele:blacklist){
for (pair<string,regex> ele:*bp){
cout << "TRYING" << ele.first << endl;
cmatch what;
try{ try{
regex_search(reinterpret_cast<const char*>(data), reinterpret_cast<const char*>(data)+bytes_transferred, what, ele.second); if(regex_search(reinterpret_cast<const char*>(data), reinterpret_cast<const char*>(data)+bytes_transferred, ele.second)){
if(what.size() > 0){
cout << "BLOCKED " << ele.first << endl; cout << "BLOCKED " << ele.first << endl;
return false; return false;
} }
@@ -65,12 +65,9 @@ bool filter_data(unsigned char* data, const size_t& bytes_transferred, vector<pa
cerr << "Error while matching regex: " << ele.first << endl; cerr << "Error while matching regex: " << ele.first << endl;
} }
} }
vector<pair<string,regex>> *wp = whitelist; for (pair<string,regex> ele:whitelist){
for (pair<string,regex> ele:*wp){
cmatch what;
try{ try{
regex_search(reinterpret_cast<const char*>(data),reinterpret_cast<const char*>(data)+bytes_transferred, what, ele.second); if(!regex_search(reinterpret_cast<const char*>(data),reinterpret_cast<const char*>(data)+bytes_transferred, ele.second)){
if(what.size() < 0){
cout << "BLOCKED " << ele.first << endl; cout << "BLOCKED " << ele.first << endl;
return false; return false;
} }
@@ -161,7 +158,8 @@ namespace tcp_proxy
{ {
if (!error) if (!error)
{ {
if (filter_data(upstream_data_, bytes_transferred, regex_s_c_b, regex_s_c_w)){ 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)){
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::bind(&bridge::handle_downstream_write,
@@ -204,7 +202,8 @@ namespace tcp_proxy
{ {
if (!error) if (!error)
{ {
if (filter_data(downstream_data_, bytes_transferred, regex_c_s_b, regex_c_s_w)){ 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)){
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::bind(&bridge::handle_upstream_write,
@@ -325,7 +324,7 @@ namespace tcp_proxy
}; };
} }
void push_regex(char* arg, bool case_sensitive, vector<pair<string,regex>> *v){ void push_regex(char* arg, bool case_sensitive, vector<pair<string,regex>> &v){
std::unique_lock<std::mutex> lck(mtx); std::unique_lock<std::mutex> lck(mtx);
size_t expr_len = (strlen(arg)-2)/2; size_t expr_len = (strlen(arg)-2)/2;
char expr[expr_len]; char expr[expr_len];
@@ -337,13 +336,13 @@ void push_regex(char* arg, bool case_sensitive, vector<pair<string,regex>> *v){
#ifdef DEBUG #ifdef DEBUG
cout << "Added case sensitive regex " << expr_str << endl; cout << "Added case sensitive regex " << expr_str << endl;
#endif #endif
v->push_back(make_pair(string(arg), regex)); v.push_back(make_pair(string(arg), regex));
} else { } else {
regex regex(expr_str,regex_constants::icase); regex regex(expr_str,regex_constants::icase);
#ifdef DEBUG #ifdef DEBUG
cout << "Added case insensitive regex " << expr_str << endl; cout << "Added case insensitive regex " << expr_str << endl;
#endif #endif
v->push_back(make_pair(string(arg), regex)); v.push_back(make_pair(string(arg), regex));
} }
} catch(...){ } catch(...){
cerr << "Regex " << expr << " was not compiled successfully" << endl; cerr << "Regex " << expr << " was not compiled successfully" << endl;
@@ -358,16 +357,8 @@ void update_regex(){
cerr << "Error: config file couln't be opened" << endl; cerr << "Error: config file couln't be opened" << endl;
exit(1); exit(1);
} }
/*
regex_s_c_w.reset(new vector<pair<string,regex>>); regex_rules *regex_new_config = new regex_rules();
regex_c_s_w.reset(new vector<pair<string,regex>>);
regex_s_c_b.reset(new vector<pair<string,regex>>);
regex_c_s_b.reset(new vector<pair<string,regex>>);
*/
regex_s_c_w = new vector<pair<string,regex>>;
regex_c_s_w = new vector<pair<string,regex>>;
regex_s_c_b = new vector<pair<string,regex>>;
regex_c_s_b = new vector<pair<string,regex>>;
string line; string line;
while(getline(fd, line)){ while(getline(fd, line)){
@@ -380,24 +371,26 @@ void update_regex(){
} }
switch(tp[1]){ switch(tp[1]){
case 'C': { // Client to server Blacklist case 'C': { // Client to server Blacklist
push_regex(tp, case_sensitive, regex_c_s_b); push_regex(tp, case_sensitive, regex_new_config->regex_c_s_b);
break; break;
} }
case 'c': { // Client to server Whitelist case 'c': { // Client to server Whitelist
push_regex(tp, case_sensitive, regex_c_s_w); push_regex(tp, case_sensitive, regex_new_config->regex_c_s_w);
break; break;
} }
case 'S': { // Server to client Blacklist case 'S': { // Server to client Blacklist
push_regex(tp, case_sensitive, regex_s_c_b); push_regex(tp, case_sensitive, regex_new_config->regex_s_c_b);
break; break;
} }
case 's': { // Server to client Whitelist case 's': { // Server to client Whitelist
push_regex(tp, case_sensitive, regex_s_c_w); push_regex(tp, case_sensitive, regex_new_config->regex_s_c_w);
break; break;
} }
} }
} }
} }
regex_config.reset(regex_new_config);
} }
void signal_handler(int signal_num) void signal_handler(int signal_num)

View File

@@ -1,6 +1,4 @@
1C3733353138303461313162386234323563313931613664373937666264356630 S3065376433366539666631353833373263633764613966396235313538633766
1S3733353138303461313162386234323563313931613664373937666264356630
1S3065376433366539666631353833373263633764613966396235313538633766
1C3638316561366437356466353765656265383933396630326262393338336438 1C3638316561366437356466353765656265383933396630326262393338336438
1S3638316561366437356466353765656265383933396630326262393338336438 1S3638316561366437356466353765656265383933396630326262393338336438
1C3739663566343265663536633261656665623338346135616464633539663930 1C3739663566343265663536633261656665623338346135616464633539663930
@@ -21,9 +19,7 @@
1S3638316561366437356466353765656265383933396630326262393338336438 1S3638316561366437356466353765656265383933396630326262393338336438
1C3739663566343265663536633261656665623338346135616464633539663930 1C3739663566343265663536633261656665623338346135616464633539663930
1S3739663566343265663536633261656665623338346135616464633539663930 1S3739663566343265663536633261656665623338346135616464633539663930
1C3733353138303461313162386234323563313931613664373937666264356630 S3065376433366539666631353833373263633764613966396235313538633766
1S3733353138303461313162386234323563313931613664373937666264356630
1C3065376433366539666631353833373263633764613966396235313538633766
1C3638316561366437356466353765656265383933396630326262393338336438 1C3638316561366437356466353765656265383933396630326262393338336438
1S3638316561366437356466353765656265383933396630326262393338336438 1S3638316561366437356466353765656265383933396630326262393338336438
1C3739663566343265663536633261656665623338346135616464633539663930 1C3739663566343265663536633261656665623338346135616464633539663930
@@ -44,10 +40,70 @@
1S3638316561366437356466353765656265383933396630326262393338336438 1S3638316561366437356466353765656265383933396630326262393338336438
1C3739663566343265663536633261656665623338346135616464633539663930 1C3739663566343265663536633261656665623338346135616464633539663930
1S3739663566343265663536633261656665623338346135616464633539663930 1S3739663566343265663536633261656665623338346135616464633539663930
1C3733353138303461313162386234323563313931613664373937666264356630 S3065376433366539666631353833373263633764613966396235313538633766
1S3733353138303461313162386234323563313931613664373937666264356630 1C3638316561366437356466353765656265383933396630326262393338336438
1C3065376433366539666631353833373263633764613966396235313538633766 1S3638316561366437356466353765656265383933396630326262393338336438
1S3065376433366539666631353833373263633764613966396235313538633766 1C3739663566343265663536633261656665623338346135616464633539663930
1S3739663566343265663536633261656665623338346135616464633539663930
1C3733353138303461313162386234323563313931613664373937666264356630
1S3733353138303461313162386234323563313931613664373937666264356630
1C3065376433366539666631353833373263633764613966396235313538633766
1S3065376433366539666631353833373263633764613966396235313538633766
1C3638316561366437356466353765656265383933396630326262393338336438
1S3638316561366437356466353765656265383933396630326262393338336438
1C3739663566343265663536633261656665623338346135616464633539663930
1S3739663566343265663536633261656665623338346135616464633539663930
1C3733353138303461313162386234323563313931613664373937666264356630
1S3733353138303461313162386234323563313931613664373937666264356630
1C3065376433366539666631353833373263633764613966396235313538633766
1S3065376433366539666631353833373263633764613966396235313538633766
1C3638316561366437356466353765656265383933396630326262393338336438
1S3638316561366437356466353765656265383933396630326262393338336438
1C3739663566343265663536633261656665623338346135616464633539663930
1S3739663566343265663536633261656665623338346135616464633539663930
S3065376433366539666631353833373263633764613966396235313538633766
1C3638316561366437356466353765656265383933396630326262393338336438
1S3638316561366437356466353765656265383933396630326262393338336438
1C3739663566343265663536633261656665623338346135616464633539663930
1S3739663566343265663536633261656665623338346135616464633539663930
1C3733353138303461313162386234323563313931613664373937666264356630
1S3733353138303461313162386234323563313931613664373937666264356630
1C3065376433366539666631353833373263633764613966396235313538633766
1S3065376433366539666631353833373263633764613966396235313538633766
1C3638316561366437356466353765656265383933396630326262393338336438
1S3638316561366437356466353765656265383933396630326262393338336438
1C3739663566343265663536633261656665623338346135616464633539663930
1S3739663566343265663536633261656665623338346135616464633539663930
1C3733353138303461313162386234323563313931613664373937666264356630
1S3733353138303461313162386234323563313931613664373937666264356630
1C3065376433366539666631353833373263633764613966396235313538633766
1S3065376433366539666631353833373263633764613966396235313538633766
1C3638316561366437356466353765656265383933396630326262393338336438
1S3638316561366437356466353765656265383933396630326262393338336438
1C3739663566343265663536633261656665623338346135616464633539663930
1S3739663566343265663536633261656665623338346135616464633539663930
S3065376433366539666631353833373263633764613966396235313538633766
1C3638316561366437356466353765656265383933396630326262393338336438
1S3638316561366437356466353765656265383933396630326262393338336438
1C3739663566343265663536633261656665623338346135616464633539663930
1S3739663566343265663536633261656665623338346135616464633539663930
1C3733353138303461313162386234323563313931613664373937666264356630
1S3733353138303461313162386234323563313931613664373937666264356630
1C3065376433366539666631353833373263633764613966396235313538633766
1S3065376433366539666631353833373263633764613966396235313538633766
1C3638316561366437356466353765656265383933396630326262393338336438
1S3638316561366437356466353765656265383933396630326262393338336438
1C3739663566343265663536633261656665623338346135616464633539663930
1S3739663566343265663536633261656665623338346135616464633539663930
1C3733353138303461313162386234323563313931613664373937666264356630
1S3733353138303461313162386234323563313931613664373937666264356630
1C3065376433366539666631353833373263633764613966396235313538633766
1S3065376433366539666631353833373263633764613966396235313538633766
1C3638316561366437356466353765656265383933396630326262393338336438
1S3638316561366437356466353765656265383933396630326262393338336438
1C3739663566343265663536633261656665623338346135616464633539663930
1S3739663566343265663536633261656665623338346135616464633539663930
S3065376433366539666631353833373263633764613966396235313538633766
1C3638316561366437356466353765656265383933396630326262393338336438 1C3638316561366437356466353765656265383933396630326262393338336438
1S3638316561366437356466353765656265383933396630326262393338336438 1S3638316561366437356466353765656265383933396630326262393338336438
1C3739663566343265663536633261656665623338346135616464633539663930 1C3739663566343265663536633261656665623338346135616464633539663930

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.c372cb27.js", "main.js": "/static/js/main.d68a5509.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.c372cb27.js.map": "/static/js/main.c372cb27.js.map" "main.d68a5509.js.map": "/static/js/main.d68a5509.js.map"
}, },
"entrypoints": [ "entrypoints": [
"static/css/main.0efd334b.css", "static/css/main.0efd334b.css",
"static/js/main.c372cb27.js" "static/js/main.d68a5509.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.c372cb27.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.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>

File diff suppressed because one or more lines are too long

View File

@@ -21,7 +21,7 @@ function AddNewRegex({ opened, onClose, service }:{ opened:boolean, onClose:()=>
initialValues: { initialValues: {
regex:"", regex:"",
type:"blacklist", type:"blacklist",
mode:"C <-> S", mode:"C -> S",
is_case_insensitive:false, is_case_insensitive:false,
percentage_encoding:false percentage_encoding:false
}, },

View File

@@ -30,6 +30,7 @@ parser.add_argument("--num_of_regexes", "-r", type=int, required=True, help='Num
parser.add_argument("--duration", "-d", type=int, required=False, help='Duration of the Benchmark in seconds', default=5) parser.add_argument("--duration", "-d", type=int, required=False, help='Duration of the Benchmark in seconds', default=5)
parser.add_argument("--output_file", "-o", type=str, required=False, help='Output results csv file', default="benchmark.csv") parser.add_argument("--output_file", "-o", type=str, required=False, help='Output results csv file', default="benchmark.csv")
parser.add_argument("--num_of_streams", "-s", type=int, required=False, help='Output results csv file', default=1) parser.add_argument("--num_of_streams", "-s", type=int, required=False, help='Output results csv file', default=1)
parser.add_argument("--new_istance", "-i", action="store_true", help='Create a new service', default=False)
args = parser.parse_args() args = parser.parse_args()
sep() sep()
@@ -42,10 +43,11 @@ req = s.post(f"{args.address}api/login", json={"password":args.password})
assert req.json()["status"] == "ok", f"Benchmark Failed: Unknown response or wrong passowrd {req.text}" assert req.json()["status"] == "ok", f"Benchmark Failed: Unknown response or wrong passowrd {req.text}"
puts(f"Sucessfully logged in ✔", color=colors.green) puts(f"Sucessfully logged in ✔", color=colors.green)
#Create new Service if args.new_istance:
req = s.post(f"{args.address}api/services/add" , json={"name":args.service_name,"port":args.service_port}) #Create new Service
assert req.json()["status"] == "ok", f"Benchmark Failed: Couldn't create service {req.text}" req = s.post(f"{args.address}api/services/add" , json={"name":args.service_name,"port":args.service_port})
puts(f"Sucessfully created service {args.service_name} with public port {args.service_port}", color=colors.green) assert req.json()["status"] == "ok", f"Benchmark Failed: Couldn't create service {req.text}"
puts(f"Sucessfully created service {args.service_name} with public port {args.service_port}", color=colors.green)
#Find the Service #Find the Service
req = s.get(f"{args.address}api/services") req = s.get(f"{args.address}api/services")
@@ -119,9 +121,10 @@ with open(args.output_file,'w') as f:
puts(f"Sucessfully written results to {args.output_file}", color=colors.magenta) puts(f"Sucessfully written results to {args.output_file}", color=colors.magenta)
#Delete the Service if args.new_istance:
req = s.get(f"{args.address}api/service/{service_id}/delete") #Delete the Service
assert req.json()["status"] == "ok", f"Benchmark Failed: Couldn't delete service {req.text}" req = s.get(f"{args.address}api/service/{service_id}/delete")
puts(f"Sucessfully delete service with id {service_id}", color=colors.green) assert req.json()["status"] == "ok", f"Benchmark Failed: Couldn't delete service {req.text}"
puts(f"Sucessfully delete service with id {service_id}", color=colors.green)
server.terminate() server.terminate()