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 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():