begin multithreading work

This commit is contained in:
nik012003
2022-06-18 14:48:09 +02:00
committed by DomySh
parent fa88940112
commit 621f8e4129
3 changed files with 34 additions and 30 deletions

View File

@@ -1,7 +1,8 @@
from signal import SIGUSR1 from signal import SIGUSR1
from secrets import token_urlsafe from secrets import token_urlsafe
import subprocess, re, os import re, os
from ctypes import CDLL, c_char_p, c_int, c_ushort
from threading import Thread
#c++ -o proxy proxy.cpp #c++ -o proxy proxy.cpp
@@ -40,24 +41,34 @@ class Proxy:
config_file_path = os.path.join("/tmp/" + token_urlsafe(16)) config_file_path = os.path.join("/tmp/" + token_urlsafe(16))
if not os.path.exists(config_file_path): if not os.path.exists(config_file_path):
self.config_file_path = config_file_path self.config_file_path = config_file_path
self.lib = CDLL(os.path.join(os.path.dirname(os.path.abspath(__file__)),"./proxy.so"))
self.lib.proxy_start.restype = c_int
#char* local_host_p, unsigned short local_port, char* forward_host_p, unsigned short forward_port, char* config_file_p
self.lib.proxy_start.argtypes = [c_char_p, c_ushort, c_char_p, c_ushort, c_char_p]
def start(self, in_pause=False): def start(self, in_pause=False):
if self.process is None: if self.process is None:
filter_map = self.compile_filters() filter_map = self.compile_filters()
filters_codes = list(filter_map.keys()) if not in_pause else [] filters_codes = list(filter_map.keys()) if not in_pause else []
proxy_binary_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),"./proxy")
self.__write_config(filters_codes) self.__write_config(filters_codes)
self.process = subprocess.Popen( self.process = Thread(
[proxy_binary_path, str(self.public_host), str(self.public_port), str(self.internal_host), str(self.internal_port), self.config_file_path], target=self.lib.proxy_start,
stdout=subprocess.PIPE, universal_newlines=True args=(self.public_host.encode(), self.public_port,
self.internal_host.encode(), self.internal_port,
self.config_file_path.encode()
),
) )
for stdout_line in iter(self.process.stdout.readline, ""):
if stdout_line.startswith("BLOCKED"): #for stdout_line in iter(self.process.stdout.readline, ""):
regex_id = stdout_line.split()[1] # if stdout_line.startswith("BLOCKED"):
filter_map[regex_id].blocked+=1 # regex_id = stdout_line.split()[1]
if self.callback_blocked_update: self.callback_blocked_update(filter_map[regex_id]) # filter_map[regex_id].blocked+=1
self.process.stdout.close() # if self.callback_blocked_update: self.callback_blocked_update(filter_map[regex_id])
#self.process.stdout.close()
self.process.start()
return self.process.wait() return self.process.wait()
def stop(self): def stop(self):
@@ -94,8 +105,7 @@ class Proxy:
return True if self.process else False return True if self.process else False
def trigger_reload_config(self): def trigger_reload_config(self):
self.process.send_signal(SIGUSR1) os.kill(self.process.native_id, SIGUSR1)
def pause(self): def pause(self):
if self.isactive(): if self.isactive():

View File

@@ -38,7 +38,7 @@ unhexlify(InputIterator first, InputIterator last, OutputIterator ascii) {
} }
vector<pair<string,boost::regex>> regex_s_c_w, regex_c_s_w, regex_s_c_b, regex_c_s_b; vector<pair<string,boost::regex>> regex_s_c_w, regex_c_s_w, regex_s_c_b, regex_c_s_b;
const char* config_file; string config_file;
bool filter_data(unsigned char* data, const size_t& bytes_transferred, vector<pair<string,boost::regex>> const &blacklist, vector<pair<string,boost::regex>> const &whitelist){ bool filter_data(unsigned char* data, const size_t& bytes_transferred, vector<pair<string,boost::regex>> const &blacklist, vector<pair<string,boost::regex>> const &whitelist){
#ifdef DEBUG #ifdef DEBUG
@@ -350,6 +350,7 @@ void update_regex(){
while(getline(fd, line)){ while(getline(fd, line)){
char tp[line.length() +1]; char tp[line.length() +1];
strcpy(tp, line.c_str()); strcpy(tp, line.c_str());
if (strlen(tp) >= 2){ if (strlen(tp) >= 2){
bool case_sensitive = true; bool case_sensitive = true;
if(tp[0] == '0'){ if(tp[0] == '0'){
@@ -380,29 +381,22 @@ void update_regex(){
void signal_handler(int signal_num) void signal_handler(int signal_num)
{ {
if (signal_num == SIGUSR1){ if (signal_num == SIGUSR1){
#ifdef DEBUG
cout << "Updating configurtation" << endl; cout << "Updating configurtation" << endl;
#endif
update_regex(); update_regex();
} }
} }
int main(int argc, char* argv[]) extern "C" int start_proxy(char* local_host_p, unsigned short local_port, char* forward_host_p, unsigned short forward_port, char* config_file_p, int pid)
{ {
if (argc < 6) const std::string local_host = local_host_p;
{ const std::string forward_host = forward_host_p;
std::cerr << "usage: tcpproxy_server <local host ip> <local port> <forward host ip> <forward port> <config_file>" << std::endl;
return 1;
}
const unsigned short local_port = static_cast<unsigned short>(::atoi(argv[2])); config_file = config_file_p;
const unsigned short forward_port = static_cast<unsigned short>(::atoi(argv[4]));
const std::string local_host = argv[1];
const std::string forward_host = argv[3];
signal(SIGUSR1, signal_handler);\
config_file = argv[5];
update_regex(); update_regex();
signal(SIGUSR1, signal_handler);
boost::asio::io_service ios; boost::asio::io_service ios;

BIN
backend/proxy/proxy.so Executable file

Binary file not shown.