push: code changes
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
from modules.nfproxy.nftables import FiregexTables
|
||||
from utils import run_func
|
||||
from modules.nfproxy.models import Service, PyFilter
|
||||
import os
|
||||
import asyncio
|
||||
from utils import DEBUG
|
||||
import traceback
|
||||
from fastapi import HTTPException
|
||||
import time
|
||||
|
||||
nft = FiregexTables()
|
||||
|
||||
@@ -13,29 +12,37 @@ class FiregexInterceptor:
|
||||
|
||||
def __init__(self):
|
||||
self.srv:Service
|
||||
self._stats_updater_cb:callable
|
||||
self.filter_map_lock:asyncio.Lock
|
||||
self.filter_map: dict[str, PyFilter]
|
||||
self.pyfilters: set[PyFilter]
|
||||
self.update_config_lock:asyncio.Lock
|
||||
self.process:asyncio.subprocess.Process
|
||||
self.update_task: asyncio.Task
|
||||
self.server_task: asyncio.Task
|
||||
self.sock_path: str
|
||||
self.unix_sock: asyncio.Server
|
||||
self.ack_arrived = False
|
||||
self.ack_status = None
|
||||
self.ack_fail_what = "Unknown"
|
||||
self.ack_fail_what = "Queue response timed-out"
|
||||
self.ack_lock = asyncio.Lock()
|
||||
|
||||
async def _call_stats_updater_callback(self, filter: PyFilter):
|
||||
if self._stats_updater_cb:
|
||||
await run_func(self._stats_updater_cb(filter))
|
||||
self.sock_reader:asyncio.StreamReader = None
|
||||
self.sock_writer:asyncio.StreamWriter = None
|
||||
self.sock_conn_lock:asyncio.Lock
|
||||
self.last_time_exception = 0
|
||||
|
||||
@classmethod
|
||||
async def start(cls, srv: Service, stats_updater_cb:callable):
|
||||
async def start(cls, srv: Service):
|
||||
self = cls()
|
||||
self._stats_updater_cb = stats_updater_cb
|
||||
self.srv = srv
|
||||
self.filter_map_lock = asyncio.Lock()
|
||||
self.update_config_lock = asyncio.Lock()
|
||||
self.sock_conn_lock = asyncio.Lock()
|
||||
if not self.sock_conn_lock.locked():
|
||||
await self.sock_conn_lock.acquire()
|
||||
self.sock_path = f"/tmp/firegex_nfproxy_{srv.id}.sock"
|
||||
if os.path.exists(self.sock_path):
|
||||
os.remove(self.sock_path)
|
||||
self.unix_sock = await asyncio.start_unix_server(self._server_listener,path=self.sock_path)
|
||||
self.server_task = asyncio.create_task(self.unix_sock.serve_forever())
|
||||
queue_range = await self._start_binary()
|
||||
self.update_task = asyncio.create_task(self.update_stats())
|
||||
nft.add(self.srv, queue_range)
|
||||
@@ -46,19 +53,20 @@ class FiregexInterceptor:
|
||||
async def _start_binary(self):
|
||||
proxy_binary_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),"../cpproxy")
|
||||
self.process = await asyncio.create_subprocess_exec(
|
||||
proxy_binary_path,
|
||||
stdout=asyncio.subprocess.PIPE, stdin=asyncio.subprocess.PIPE,
|
||||
proxy_binary_path, stdin=asyncio.subprocess.DEVNULL,
|
||||
env={
|
||||
"NTHREADS": os.getenv("NTHREADS","1"),
|
||||
"FIREGEX_NFQUEUE_FAIL_OPEN": "1" if self.srv.fail_open else "0",
|
||||
"FIREGEX_NFPROXY_SOCK": self.sock_path
|
||||
},
|
||||
)
|
||||
line_fut = self.process.stdout.readuntil()
|
||||
try:
|
||||
line_fut = await asyncio.wait_for(line_fut, timeout=3)
|
||||
async with asyncio.timeout(3):
|
||||
await self.sock_conn_lock.acquire()
|
||||
line_fut = await self.sock_reader.readuntil()
|
||||
except asyncio.TimeoutError:
|
||||
self.process.kill()
|
||||
raise Exception("Invalid binary output")
|
||||
raise Exception("Binary don't returned queue number until timeout")
|
||||
line = line_fut.decode()
|
||||
if line.startswith("QUEUE "):
|
||||
params = line.split()
|
||||
@@ -67,25 +75,45 @@ class FiregexInterceptor:
|
||||
self.process.kill()
|
||||
raise Exception("Invalid binary output")
|
||||
|
||||
async def _server_listener(self, reader:asyncio.StreamReader, writer:asyncio.StreamWriter):
|
||||
if self.sock_reader or self.sock_writer:
|
||||
writer.write_eof() # Technically never reached
|
||||
writer.close()
|
||||
reader.feed_eof()
|
||||
return
|
||||
self.sock_reader = reader
|
||||
self.sock_writer = writer
|
||||
self.sock_conn_lock.release()
|
||||
|
||||
async def update_stats(self):
|
||||
try:
|
||||
while True:
|
||||
line = (await self.process.stdout.readuntil()).decode()
|
||||
if DEBUG:
|
||||
print(line)
|
||||
try:
|
||||
line = (await self.sock_reader.readuntil()).decode()
|
||||
except Exception as e:
|
||||
self.ack_arrived = False
|
||||
self.ack_status = False
|
||||
self.ack_fail_what = "Can't read from nfq client"
|
||||
self.ack_lock.release()
|
||||
await self.stop()
|
||||
raise HTTPException(status_code=500, detail="Can't read from nfq client") from e
|
||||
if line.startswith("BLOCKED "):
|
||||
filter_id = line.split()[1]
|
||||
filter_name = line.split()[1]
|
||||
print("BLOCKED", filter_name)
|
||||
async with self.filter_map_lock:
|
||||
if filter_id in self.filter_map:
|
||||
self.filter_map[filter_id].blocked_packets+=1
|
||||
await self.filter_map[filter_id].update()
|
||||
print("LOCKED MAP LOCK")
|
||||
if filter_name in self.filter_map:
|
||||
print("ADDING BLOCKED PACKET")
|
||||
self.filter_map[filter_name].blocked_packets+=1
|
||||
await self.filter_map[filter_name].update()
|
||||
if line.startswith("MANGLED "):
|
||||
filter_id = line.split()[1]
|
||||
filter_name = line.split()[1]
|
||||
async with self.filter_map_lock:
|
||||
if filter_id in self.filter_map:
|
||||
self.filter_map[filter_id].edited_packets+=1
|
||||
await self.filter_map[filter_id].update()
|
||||
if filter_name in self.filter_map:
|
||||
self.filter_map[filter_name].edited_packets+=1
|
||||
await self.filter_map[filter_name].update()
|
||||
if line.startswith("EXCEPTION"):
|
||||
self.last_time_exception = time.time()
|
||||
print("TODO EXCEPTION HANDLING") # TODO
|
||||
if line.startswith("ACK "):
|
||||
self.ack_arrived = True
|
||||
@@ -101,22 +129,29 @@ class FiregexInterceptor:
|
||||
traceback.print_exc()
|
||||
|
||||
async def stop(self):
|
||||
self.server_task.cancel()
|
||||
self.update_task.cancel()
|
||||
self.unix_sock.close()
|
||||
if os.path.exists(self.sock_path):
|
||||
os.remove(self.sock_path)
|
||||
if self.process and self.process.returncode is None:
|
||||
self.process.kill()
|
||||
|
||||
async def _update_config(self, code):
|
||||
async with self.update_config_lock:
|
||||
self.process.stdin.write(len(code).to_bytes(4, byteorder='big')+code.encode())
|
||||
await self.process.stdin.drain()
|
||||
try:
|
||||
async with asyncio.timeout(3):
|
||||
await self.ack_lock.acquire()
|
||||
except TimeoutError:
|
||||
pass
|
||||
if not self.ack_arrived or not self.ack_status:
|
||||
await self.stop()
|
||||
raise HTTPException(status_code=500, detail=f"NFQ error: {self.ack_fail_what}")
|
||||
if self.sock_writer:
|
||||
self.sock_writer.write(len(code).to_bytes(4, byteorder='big')+code.encode())
|
||||
await self.sock_writer.drain()
|
||||
try:
|
||||
async with asyncio.timeout(3):
|
||||
await self.ack_lock.acquire()
|
||||
except TimeoutError:
|
||||
self.ack_fail_what = "Queue response timed-out"
|
||||
if not self.ack_arrived or not self.ack_status:
|
||||
await self.stop()
|
||||
raise HTTPException(status_code=500, detail=f"NFQ error: {self.ack_fail_what}")
|
||||
else:
|
||||
raise HTTPException(status_code=400, detail="Socket not ready")
|
||||
|
||||
async def reload(self, filters:list[PyFilter]):
|
||||
async with self.filter_map_lock:
|
||||
@@ -125,12 +160,13 @@ class FiregexInterceptor:
|
||||
filter_file = f.read()
|
||||
else:
|
||||
filter_file = ""
|
||||
self.filter_map = {ele.name: ele for ele in filters}
|
||||
await self._update_config(
|
||||
"global __firegex_pyfilter_enabled\n" +
|
||||
|
||||
filter_file + "\n\n" +
|
||||
"__firegex_pyfilter_enabled = [" + ", ".join([repr(f.name) for f in filters]) + "]\n" +
|
||||
"__firegex_proto = " + repr(self.srv.proto) + "\n" +
|
||||
"import firegex.nfproxy.internals\n\n" +
|
||||
filter_file + "\n\n" +
|
||||
"firegex.nfproxy.internals.compile()"
|
||||
"import firegex.nfproxy.internals\n" +
|
||||
"firegex.nfproxy.internals.compile(globals())\n"
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user