diff --git a/backend/proxy/__init__.py b/backend/proxy/__init__.py index eac517a..94f4c8c 100755 --- a/backend/proxy/__init__.py +++ b/backend/proxy/__init__.py @@ -46,26 +46,28 @@ class Proxy: 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() - return self.process.wait() + try: + 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() + return self.process.wait() + finally: + self.__delete_config() def stop(self): if self.process: self.process.terminate() try: self.process.wait(timeout=3) - return True except Exception: self.process.kill() return False @@ -82,6 +84,10 @@ class Proxy: with open(self.config_file_path,'w') as config_file: for line in filters_codes: config_file.write(line + '\n') + + def __delete_config(self): + if os.path.exists(self.config_file_path): + os.remove(self.config_file_path) def reload(self): if self.isactive(): diff --git a/backend/proxy/proxy.cpp b/backend/proxy/proxy.cpp index 605f3a2..cee8b6f 100644 --- a/backend/proxy/proxy.cpp +++ b/backend/proxy/proxy.cpp @@ -18,6 +18,8 @@ using namespace std; +boost::asio::io_service *ios_loop = nullptr; + int to_int(int c) { if (not isxdigit(c)) return -1; // error: non-hexadecimal digit found if (isdigit(c)) return c - '0'; @@ -380,8 +382,15 @@ void update_regex(){ void signal_handler(int signal_num) { if (signal_num == SIGUSR1){ + #ifdef DEBUG cout << "Updating configurtation" << endl; + #endif update_regex(); + }else if(signal_num == SIGTERM){ + if (ios_loop != nullptr) ios_loop->stop(); + exit(0); + }else if (signal_num == SIGSEGV){ + exit(0); } } @@ -398,13 +407,16 @@ int main(int argc, char* argv[]) const std::string local_host = argv[1]; const std::string forward_host = argv[3]; - - signal(SIGUSR1, signal_handler);\ + update_regex(); + signal(SIGUSR1, signal_handler); + signal(SIGTERM, signal_handler); + signal(SIGSEGV, signal_handler); config_file = argv[5]; - update_regex(); + boost::asio::io_service ios; + ios_loop = &ios; try { diff --git a/backend/utils.py b/backend/utils.py index f998c75..def1ecd 100755 --- a/backend/utils.py +++ b/backend/utils.py @@ -155,7 +155,7 @@ class ProxyManager: self.__update_status_db(id, next_status) if saved_status[0] == "wait": saved_status[0] = next_status proxy_status = proxy.start(in_pause=(next_status==STATUS.PAUSE)) - if proxy_status == 1: + if proxy_status != 0: self.__update_status_db(id, STATUS.STOP) return else: @@ -210,7 +210,6 @@ class ProxyManager: def stats_updater(filter:Filter): - print("Callback received",filter.blocked, filter.code) self.db.query("UPDATE regexes SET blocked_packets = ? WHERE regex_id = ?;", (filter.blocked, filter.code)) if not proxy: