added firewall strategy pattern

This commit is contained in:
carnivuth
2026-01-27 17:41:41 +01:00
parent e4cf4702eb
commit ace04c1f5e
3 changed files with 103 additions and 0 deletions

43
src/firewall/iptables.py Normal file
View File

@@ -0,0 +1,43 @@
from typing_extensions import override
from firewall.fwtype import FWType
class Iptables(FWType):
@override
def getBanlist(self,ips) -> str:
"""
Generate iptables ban rules from an array of IP addresses.
Args:
ips: List of IP addresses to ban
Returns:
String containing iptables commands, one per line
"""
if not ips:
return ""
rules = []
chain = "INPUT"
target = "DROP"
rules.append("#!/bin/bash")
rules.append("# iptables ban rules")
rules.append("")
for ip in ips:
ip = ip.strip()
# Build the iptables command
rule_parts = [
"iptables",
"-A", chain,
"-s", ip
]
# Add target
rule_parts.extend(["-j", target])
rules.append(" ".join(rule_parts))
return "\n".join(rules)