Files
firegex-traffic-viewer/backend/proxy/__init__.py

126 lines
5.0 KiB
Python
Raw Normal View History

from signal import SIGUSR1
2022-06-18 13:05:59 +02:00
from secrets import token_urlsafe
2022-06-18 14:48:09 +02:00
import re, os
from ctypes import CDLL, c_char_p, c_int, c_ushort
from threading import Thread
2022-06-18 13:05:59 +02:00
2022-06-11 21:57:50 +02:00
#c++ -o proxy proxy.cpp
class Filter:
2022-06-17 20:48:27 +02:00
def __init__(self, regex, is_case_sensitive=True, is_blacklist=True, c_to_s=False, s_to_c=False, blocked_packets=0, code=None):
2022-06-11 21:57:50 +02:00
self.regex = regex
2022-06-17 20:48:27 +02:00
self.is_case_sensitive = is_case_sensitive
2022-06-11 21:57:50 +02:00
self.is_blacklist = is_blacklist
if c_to_s == s_to_c: c_to_s = s_to_c = True # (False, False) == (True, True)
self.c_to_s = c_to_s
self.s_to_c = s_to_c
2022-06-13 18:44:11 +02:00
self.blocked = blocked_packets
self.code = code
2022-06-11 21:57:50 +02:00
def compile(self):
if isinstance(self.regex, str): self.regex = self.regex.encode()
if not isinstance(self.regex, bytes): raise Exception("Invalid Regex Paramether")
re.compile(self.regex) # raise re.error if is invalid!
2022-06-17 20:48:27 +02:00
case_sensitive = "1" if self.is_case_sensitive else "0"
2022-06-11 21:57:50 +02:00
if self.c_to_s:
2022-06-17 20:48:27 +02:00
yield case_sensitive + "C" + self.regex.hex() if self.is_blacklist else case_sensitive + "c"+ self.regex.hex()
2022-06-11 21:57:50 +02:00
if self.s_to_c:
2022-06-17 20:48:27 +02:00
yield case_sensitive + "S" + self.regex.hex() if self.is_blacklist else case_sensitive + "s"+ self.regex.hex()
2022-06-11 21:57:50 +02:00
class Proxy:
2022-06-18 13:05:59 +02:00
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"):
2022-06-11 21:57:50 +02:00
self.public_host = public_host
self.public_port = public_port
self.internal_host = internal_host
self.internal_port = internal_port
self.filters = set(filters) if filters else set([])
self.process = None
2022-06-18 13:05:59 +02:00
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
2022-06-18 14:48:09 +02:00
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]
2022-06-11 21:57:50 +02:00
2022-06-18 13:05:59 +02:00
def start(self, in_pause=False):
2022-06-11 21:57:50 +02:00
if self.process is None:
filter_map = self.compile_filters()
2022-06-18 13:05:59 +02:00
filters_codes = list(filter_map.keys()) if not in_pause else []
self.__write_config(filters_codes)
2022-06-18 14:48:09 +02:00
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()
2022-06-11 21:57:50 +02:00
return self.process.wait()
def stop(self):
if self.process:
self.process.terminate()
try:
self.process.wait(timeout=3)
return True
except Exception:
self.process.kill()
return False
finally:
self.process = None
return True
2022-06-18 13:05:59 +02:00
def restart(self, in_pause=False):
2022-06-11 21:57:50 +02:00
status = self.stop()
2022-06-18 13:05:59 +02:00
self.start(in_pause=in_pause)
2022-06-11 21:57:50 +02:00
return status
2022-06-18 13:05:59 +02:00
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')
2022-06-11 21:57:50 +02:00
def reload(self):
2022-06-18 13:05:59 +02:00
if self.isactive():
filter_map = self.compile_filters()
filters_codes = list(filter_map.keys())
2022-06-18 13:05:59 +02:00
self.__write_config(filters_codes)
self.trigger_reload_config()
2022-06-11 21:57:50 +02:00
2022-06-13 18:44:11 +02:00
def isactive(self):
return True if self.process else False
2022-06-18 13:05:59 +02:00
def trigger_reload_config(self):
2022-06-18 14:48:09 +02:00
os.kill(self.process.native_id, SIGUSR1)
2022-06-18 13:05:59 +02:00
def pause(self):
if self.isactive():
self.__write_config([])
self.trigger_reload_config()
else:
self.start(in_pause=True)
2022-06-11 21:57:50 +02:00
def compile_filters(self):
res = {}
for filter_obj in self.filters:
2022-06-15 01:23:54 +02:00
try:
raw_filters = filter_obj.compile()
for filter in raw_filters:
res[filter] = filter_obj
except Exception: pass
2022-06-11 21:57:50 +02:00
return res