Minor fixes

This commit is contained in:
DomySh
2022-06-18 20:35:41 +02:00
parent 8bff1a888d
commit 0601b7b281
3 changed files with 36 additions and 19 deletions

View File

@@ -46,26 +46,28 @@ class Proxy:
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") proxy_binary_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),"./proxy")
self.__write_config(filters_codes) 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], self.process = subprocess.Popen(
stdout=subprocess.PIPE, universal_newlines=True [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"): 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])
return self.process.wait() self.process.stdout.close()
return self.process.wait()
finally:
self.__delete_config()
def stop(self): def stop(self):
if self.process: if self.process:
self.process.terminate() self.process.terminate()
try: try:
self.process.wait(timeout=3) self.process.wait(timeout=3)
return True
except Exception: except Exception:
self.process.kill() self.process.kill()
return False return False
@@ -82,6 +84,10 @@ class Proxy:
with open(self.config_file_path,'w') as config_file: with open(self.config_file_path,'w') as config_file:
for line in filters_codes: for line in filters_codes:
config_file.write(line + '\n') 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): def reload(self):
if self.isactive(): if self.isactive():

View File

@@ -18,6 +18,8 @@
using namespace std; using namespace std;
boost::asio::io_service *ios_loop = nullptr;
int to_int(int c) { int to_int(int c) {
if (not isxdigit(c)) return -1; // error: non-hexadecimal digit found if (not isxdigit(c)) return -1; // error: non-hexadecimal digit found
if (isdigit(c)) return c - '0'; if (isdigit(c)) return c - '0';
@@ -380,8 +382,15 @@ 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();
}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 local_host = argv[1];
const std::string forward_host = argv[3]; const std::string forward_host = argv[3];
update_regex();
signal(SIGUSR1, signal_handler);\ signal(SIGUSR1, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGSEGV, signal_handler);
config_file = argv[5]; config_file = argv[5];
update_regex();
boost::asio::io_service ios; boost::asio::io_service ios;
ios_loop = &ios;
try try
{ {

View File

@@ -155,7 +155,7 @@ class ProxyManager:
self.__update_status_db(id, next_status) self.__update_status_db(id, next_status)
if saved_status[0] == "wait": saved_status[0] = next_status if saved_status[0] == "wait": saved_status[0] = next_status
proxy_status = proxy.start(in_pause=(next_status==STATUS.PAUSE)) 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) self.__update_status_db(id, STATUS.STOP)
return return
else: else:
@@ -210,7 +210,6 @@ class ProxyManager:
def stats_updater(filter:Filter): 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)) self.db.query("UPDATE regexes SET blocked_packets = ? WHERE regex_id = ?;", (filter.blocked, filter.code))
if not proxy: if not proxy: