diff --git a/backend/proxy/__init__.py b/backend/proxy/__init__.py index eac517a..fa58ecb 100755 --- a/backend/proxy/__init__.py +++ b/backend/proxy/__init__.py @@ -1,7 +1,8 @@ from signal import SIGUSR1 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 @@ -40,24 +41,34 @@ class Proxy: config_file_path = os.path.join("/tmp/" + token_urlsafe(16)) if not os.path.exists(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): if self.process is None: filter_map = self.compile_filters() 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.process = subprocess.Popen( - [proxy_binary_path, str(self.public_host), str(self.public_port), str(self.internal_host), str(self.internal_port), self.config_file_path], - stdout=subprocess.PIPE, universal_newlines=True - ) - for stdout_line in iter(self.process.stdout.readline, ""): - if stdout_line.startswith("BLOCKED"): - regex_id = stdout_line.split()[1] - filter_map[regex_id].blocked+=1 - if self.callback_blocked_update: self.callback_blocked_update(filter_map[regex_id]) - self.process.stdout.close() + self.process = Thread( + target=self.lib.proxy_start, + 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"): + # regex_id = stdout_line.split()[1] + # filter_map[regex_id].blocked+=1 + # if self.callback_blocked_update: self.callback_blocked_update(filter_map[regex_id]) + #self.process.stdout.close() + self.process.start() return self.process.wait() def stop(self): @@ -94,8 +105,7 @@ class Proxy: return True if self.process else False def trigger_reload_config(self): - self.process.send_signal(SIGUSR1) - + os.kill(self.process.native_id, SIGUSR1) def pause(self): if self.isactive(): diff --git a/backend/proxy/proxy.cpp b/backend/proxy/proxy.cpp index 605f3a2..2fb7512 100644 --- a/backend/proxy/proxy.cpp +++ b/backend/proxy/proxy.cpp @@ -38,7 +38,7 @@ unhexlify(InputIterator first, InputIterator last, OutputIterator ascii) { } vector> 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> const &blacklist, vector> const &whitelist){ #ifdef DEBUG @@ -350,6 +350,7 @@ void update_regex(){ 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'){ @@ -380,29 +381,22 @@ void update_regex(){ void signal_handler(int signal_num) { if (signal_num == SIGUSR1){ + #ifdef DEBUG cout << "Updating configurtation" << endl; + #endif 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) - { - std::cerr << "usage: tcpproxy_server " << std::endl; - return 1; - } - - const unsigned short local_port = static_cast(::atoi(argv[2])); - const unsigned short forward_port = static_cast(::atoi(argv[4])); - const std::string local_host = argv[1]; - const std::string forward_host = argv[3]; + const std::string local_host = local_host_p; + const std::string forward_host = forward_host_p; + config_file = config_file_p; - signal(SIGUSR1, signal_handler);\ - - config_file = argv[5]; update_regex(); + signal(SIGUSR1, signal_handler); boost::asio::io_service ios; diff --git a/backend/proxy/proxy.so b/backend/proxy/proxy.so new file mode 100755 index 0000000..7c33ca3 Binary files /dev/null and b/backend/proxy/proxy.so differ