changed workflow from live computation to scheduled job to support previous structure

This commit is contained in:
carnivuth
2026-01-30 16:13:45 +01:00
parent 2118396dac
commit 09b986f1b0
2 changed files with 50 additions and 38 deletions

View File

@@ -14,7 +14,7 @@ from database import get_database
from config import Config, get_config from config import Config, get_config
from database import get_database from database import get_database
from config import Config,get_config from config import Config, get_config
from firewall.fwtype import FWType from firewall.fwtype import FWType
# imports for the __init_subclass__ method, do not remove pls # imports for the __init_subclass__ method, do not remove pls
@@ -512,7 +512,6 @@ class Handler(BaseHTTPRequestHandler):
if self.config.dashboard_secret_path and self.path.startswith( if self.config.dashboard_secret_path and self.path.startswith(
f"{self.config.dashboard_secret_path}/static/" f"{self.config.dashboard_secret_path}/static/"
): ):
import os
file_path = self.path.replace( file_path = self.path.replace(
f"{self.config.dashboard_secret_path}/static/", "" f"{self.config.dashboard_secret_path}/static/", ""
@@ -563,7 +562,6 @@ class Handler(BaseHTTPRequestHandler):
stats, self.config.dashboard_secret_path stats, self.config.dashboard_secret_path
).encode() ).encode()
) )
self.wfile.write(generate_dashboard(stats, self.config.dashboard_secret_path).encode())
except BrokenPipeError: except BrokenPipeError:
pass pass
except Exception as e: except Exception as e:
@@ -811,7 +809,7 @@ class Handler(BaseHTTPRequestHandler):
result = db.get_top_ips_paginated( result = db.get_top_ips_paginated(
page=page, page=page,
page_size=page_size, page_size=page_size,
pathsort_by=sort_by, sort_by=sort_by,
sort_order=sort_order, sort_order=sort_order,
) )
self.wfile.write(json.dumps(result).encode()) self.wfile.write(json.dumps(result).encode())
@@ -941,38 +939,42 @@ class Handler(BaseHTTPRequestHandler):
# API endpoint for downloading malicious IPs blocklist file # API endpoint for downloading malicious IPs blocklist file
if ( if (
self.config.dashboard_secret_path and self.config.dashboard_secret_path
request_path == f"{self.config.dashboard_secret_path}/api/get_banlist" and request_path == f"{self.config.dashboard_secret_path}/api/get_banlist"
): ):
# get fwtype from request params # get fwtype from request params
fwtype = query_params.get("fwtype", ["iptables"])[0] fwtype = query_params.get("fwtype", ["iptables"])[0]
# Query distinct suspicious IPs file_path = os.path.join(
results = ( os.path.dirname(__file__), "exports", f"{fwtype}.txt"
session.query(distinct(AccessLog.ip))
.filter(AccessLog.is_suspicious == True)
.all()
) )
try:
# Filter out local/private IPs and the server's own IP if os.path.exists(file_path):
config = get_config() with open(file_path, "rb") as f:
server_ip = config.get_server_ip() content = f.read()
public_ips = [ip for (ip,) in results if is_valid_public_ip(ip, server_ip)] self.send_response(200)
self.send_header("Content-type", "text/plain")
# get specific fwtype based on query parameter self.send_header(
fwtype_parser = FWType.create(fwtype) "Content-Disposition",
banlist = fwtype_parser.getBanlist(public_ips) f'attachment; filename="{fwtype}.txt"',
)
self.send_response(200) self.send_header("Content-Length", str(len(content)))
self.send_header("Content-type", "text/plain") self.end_headers()
self.send_header( self.wfile.write(content)
"Content-Disposition", else:
f'attachment; filename="{fwtype}.txt"', self.send_response(404)
) self.send_header("Content-type", "text/plain")
self.send_header("Content-Length", str(len(banlist))) self.end_headers()
self.end_headers() self.wfile.write(b"File not found")
self.wfile.write(banlist.encode()) except BrokenPipeError:
pass
except Exception as e:
self.app_logger.error(f"Error serving malicious IPs file: {e}")
self.send_response(500)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write(b"Internal server error")
return return
# API endpoint for downloading malicious IPs file # API endpoint for downloading malicious IPs file

View File

@@ -4,8 +4,12 @@ import os
from logger import get_app_logger from logger import get_app_logger
from database import get_database from database import get_database
from config import get_config from config import get_config
from models import IpStats from models import IpStats, AccessLog
from ip_utils import is_valid_public_ip from ip_utils import is_valid_public_ip
from sqlalchemy import distinct
from firewall.fwtype import FWType
from firewall.iptables import Iptables
from firewall.raw import Raw
app_logger = get_app_logger() app_logger = get_app_logger()
@@ -61,14 +65,20 @@ def main():
os.makedirs(EXPORTS_DIR, exist_ok=True) os.makedirs(EXPORTS_DIR, exist_ok=True)
# Write IPs to file (one per line) # Write IPs to file (one per line)
with open(OUTPUT_FILE, "w") as f: for fwname in FWType._registry:
for ip in public_ips:
f.write(f"{ip}\n")
app_logger.info( # get banlist for specific ip
f"[Background Task] {task_name} exported {len(public_ips)} attacker IPs " fw = FWType.create(fwname)
f"(filtered {len(attackers) - len(public_ips)} local/private IPs) to {OUTPUT_FILE}" banlist = fw.getBanlist(public_ips)
)
output_file = os.path.join(EXPORTS_DIR, f"{fwname}.txt")
with open(output_file, "w") as f:
f.write(f"{banlist}\n")
app_logger.info(
f"[Background Task] {task_name} exported {len(public_ips)} in {fwname} public IPs"
f"(filtered {len(attackers) - len(public_ips)} local/private IPs) to {output_file}"
)
except Exception as e: except Exception as e:
app_logger.error(f"[Background Task] {task_name} failed: {e}") app_logger.error(f"[Background Task] {task_name} failed: {e}")