From 67ad94ea26ca5517675adff59bdbb59a131d1a1c Mon Sep 17 00:00:00 2001 From: Reditus Date: Thu, 14 May 2026 00:37:45 +0100 Subject: [PATCH] fix: firewall block/unblock in INPUT, FORWARD, and mangle PREROUTING chains; combine SSH calls to avoid timeouts; check actual iptables rules instead of settings config --- app/routes/api.py | 40 +++++++++++++++++++--------------------- setup.ps1 | 4 +++- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/app/routes/api.py b/app/routes/api.py index a65c9df..ee04ca1 100644 --- a/app/routes/api.py +++ b/app/routes/api.py @@ -96,13 +96,11 @@ def register_api_routes(app, services, settings): 'postgres': {'port': 15432, 'name': 'PostgreSQL'}, } - out_input, _, _ = ssh.run('sudo iptables -L INPUT -n 2>/dev/null | grep -E "dpt:(18888|31820|15432)"', timeout=10) - out_forward, _, _ = ssh.run('sudo iptables -L FORWARD -n 2>/dev/null | grep -E "dpt:(18888|31820|15432)"', timeout=10) - all_output = out_input + out_forward + out, _, _ = ssh.run('sudo iptables -L INPUT -n 2>/dev/null | grep -E "dpt:(18888|31820|15432)"; sudo iptables -L FORWARD -n 2>/dev/null | grep -E "dpt:(18888|31820|15432)"; sudo iptables -t mangle -L PREROUTING -n 2>/dev/null | grep -E "dpt:(18888|31820|15432)"', timeout=15) blocked_ports = set() rules = {} - for line in all_output.split('\n'): + for line in out.split('\n'): for port in ['18888', '31820', '15432']: if f'dpt:{port}' in line and 'DROP' in line: blocked_ports.add(port) @@ -135,9 +133,11 @@ def register_api_routes(app, services, settings): f'sudo iptables -I INPUT 1 -p tcp --dport {port} -s 127.0.0.1 -j ACCEPT && ' f'sudo iptables -I INPUT 2 -p tcp --dport {port} -j DROP && ' f'sudo iptables -I FORWARD 1 -p tcp --dport {port} -s 127.0.0.1 -j ACCEPT && ' - f'sudo iptables -I FORWARD 2 -p tcp --dport {port} -j DROP' + f'sudo iptables -I FORWARD 2 -p tcp --dport {port} -j DROP && ' + f'sudo iptables -t mangle -I PREROUTING 1 -p tcp --dport {port} -s 127.0.0.1 -j ACCEPT && ' + f'sudo iptables -t mangle -I PREROUTING 2 -p tcp --dport {port} -j DROP' ) - out, err, rc = ssh.run(cmd, timeout=15) + out, err, rc = ssh.run(cmd, timeout=20) if rc != 0 and 'already exists' not in (out + err): return jsonify({'success': False, 'output': err}) @@ -164,20 +164,18 @@ def register_api_routes(app, services, settings): if port not in (18888, 31820, 15432): return jsonify({'success': False, 'output': 'Invalid port'}) - cmds = [ - f'sudo iptables -D INPUT -p tcp --dport {port} -j DROP', - f'sudo iptables -D INPUT -p tcp --dport {port} -s 127.0.0.1 -j ACCEPT', - f'sudo iptables -D FORWARD -p tcp --dport {port} -j DROP', - f'sudo iptables -D FORWARD -p tcp --dport {port} -s 127.0.0.1 -j ACCEPT', - ] - all_ok = True - results = [] - for cmd in cmds: - out, err, rc = ssh.run(cmd, timeout=10) - combined = (out + err).strip() - results.append(combined if combined else 'ok') - if rc != 0 and 'Bad rule' not in combined and 'No chain' not in combined and 'not found' not in combined: - all_ok = False + cmd = ( + f'sudo iptables -D INPUT -p tcp --dport {port} -j DROP 2>/dev/null; ' + f'sudo iptables -D INPUT -p tcp --dport {port} -s 127.0.0.1 -j ACCEPT 2>/dev/null; ' + f'sudo iptables -D FORWARD -p tcp --dport {port} -j DROP 2>/dev/null; ' + f'sudo iptables -D FORWARD -p tcp --dport {port} -s 127.0.0.1 -j ACCEPT 2>/dev/null; ' + f'sudo iptables -t mangle -D PREROUTING -p tcp --dport {port} -j DROP 2>/dev/null; ' + f'sudo iptables -t mangle -D PREROUTING -p tcp --dport {port} -s 127.0.0.1 -j ACCEPT 2>/dev/null; ' + f'echo DONE' + ) + out, err, rc = ssh.run(cmd, timeout=20) + combined = (out + err).strip() + all_ok = 'DONE' in out or rc == 0 port_key = {18888: 'block_filebrowser', 31820: 'block_director', 15432: 'block_postgres'}.get(port) if port_key: @@ -193,7 +191,7 @@ def register_api_routes(app, services, settings): with open(settings_path, 'w') as f: yaml.dump(dict(settings), f) - return jsonify({'success': all_ok, 'output': ' / '.join(r for r in results if r)}) + return jsonify({'success': all_ok, 'output': 'Port unblocked' if all_ok else combined}) # Chat API @app.route('/api/chat_logs') diff --git a/setup.ps1 b/setup.ps1 index 9ee9460..b99f149 100644 --- a/setup.ps1 +++ b/setup.ps1 @@ -698,13 +698,15 @@ if (-not $SshValid) { $tmpFwScript = Join-Path $env:TEMP ([System.IO.Path]::GetRandomFileName() + ".sh") $fwContent = "for PORT in $($portsToBlock -join ' '); do`n" - $fwContent += " if iptables -C FORWARD -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT 2>/dev/null; then`n" + $fwContent += " if iptables -t mangle -C PREROUTING -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT 2>/dev/null; then`n" $fwContent += " echo 'Port `$PORT already blocked'`n" $fwContent += " else`n" $fwContent += " iptables -I INPUT 1 -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT`n" $fwContent += " iptables -I INPUT 2 -p tcp --dport `$PORT -j DROP`n" $fwContent += " iptables -I FORWARD 1 -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT`n" $fwContent += " iptables -I FORWARD 2 -p tcp --dport `$PORT -j DROP`n" + $fwContent += " iptables -t mangle -I PREROUTING 1 -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT`n" + $fwContent += " iptables -t mangle -I PREROUTING 2 -p tcp --dport `$PORT -j DROP`n" $fwContent += " echo 'Port `$PORT blocked'`n" $fwContent += " fi`n" $fwContent += "done`n"