Read config from stdin

This commit is contained in:
DomySh
2022-06-28 00:36:26 +02:00
parent 753e54a22c
commit 4770fe7314
2 changed files with 52 additions and 61 deletions

View File

@@ -30,6 +30,7 @@ class Proxy:
def __init__(self, internal_port, public_port, callback_blocked_update=None, filters=None, public_host="0.0.0.0", internal_host="127.0.0.1"):
self.filter_map = {}
self.filter_map_lock = Lock()
self.update_config_lock = Lock()
self.public_host = public_host
self.public_port = public_port
self.internal_host = internal_host
@@ -37,35 +38,27 @@ class Proxy:
self.filters = set(filters) if filters else set([])
self.process = None
self.callback_blocked_update = callback_blocked_update
self.config_file_path = None
while self.config_file_path is None:
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
def start(self, in_pause=False):
if not self.isactive():
self.filter_map = self.compile_filters()
filters_codes = list(self.filter_map.keys()) if not in_pause else []
proxy_binary_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),"./proxy")
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]
with self.filter_map_lock:
self.filter_map[regex_id].blocked+=1
if self.callback_blocked_update: self.callback_blocked_update(self.filter_map[regex_id])
self.process.stdout.close()
return self.process.wait()
finally:
self.__delete_config()
self.process = subprocess.Popen(
[ proxy_binary_path, str(self.public_host), str(self.public_port), str(self.internal_host), str(self.internal_port)],
stdout=subprocess.PIPE, stdin=subprocess.PIPE, universal_newlines=True
)
self.update_config(filters_codes, sendsignal=False)
for stdout_line in iter(self.process.stdout.readline, ""):
if stdout_line.startswith("BLOCKED"):
regex_id = stdout_line.split()[1]
with self.filter_map_lock:
self.filter_map[regex_id].blocked+=1
if self.callback_blocked_update: self.callback_blocked_update(self.filter_map[regex_id])
self.process.stdout.close()
return self.process.wait()
def stop(self):
if self.isactive():
@@ -84,36 +77,31 @@ class Proxy:
self.start(in_pause=in_pause)
return status
def __write_config(self, filters_codes):
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 update_config(self, filters_codes, sendsignal=True):
with self.update_config_lock:
if (self.isactive()):
self.process.stdin.write(" ".join(filters_codes))
self.process.stdin.write(" END ")
self.process.stdin.flush()
if sendsignal:
self.process.send_signal(SIGUSR1)
def reload(self):
if self.isactive():
with self.filter_map_lock:
self.filter_map = self.compile_filters()
filters_codes = list(self.filter_map.keys())
self.__write_config(filters_codes)
self.trigger_reload_config()
self.update_config(filters_codes)
def isactive(self):
if self.process and not self.process.poll() is None:
self.process = None
return True if self.process else False
def trigger_reload_config(self):
self.process.send_signal(SIGUSR1)
def pause(self):
if self.isactive():
self.__write_config([])
self.trigger_reload_config()
self.update_config([])
else:
self.start(in_pause=True)