|
| 1 | +import time |
| 2 | +from subprocess import run |
| 3 | + |
| 4 | +from app.services.ufw import UFWService |
| 5 | +from app.utils.subprocess_utils import run_commands |
| 6 | + |
| 7 | + |
| 8 | +class WrtUFWService(UFWService): |
| 9 | + def __init__(self): |
| 10 | + super().__init__() |
| 11 | + |
| 12 | + def install(self): |
| 13 | + print("OpenWrt uses built-in firewall") |
| 14 | + print("Configuring firewall for OpenWrt...") |
| 15 | + |
| 16 | + fw_version = self._detect_firewall_version() |
| 17 | + |
| 18 | + if fw_version == "fw4": |
| 19 | + self._configure_fw4() |
| 20 | + else: |
| 21 | + self._configure_fw3() |
| 22 | + |
| 23 | + run_commands([["/etc/init.d/firewall", "restart"]]) |
| 24 | + |
| 25 | + self._wait_for_status("running") |
| 26 | + |
| 27 | + run_commands( |
| 28 | + [ |
| 29 | + ["/etc/init.d/firewall", "status"], |
| 30 | + ["iptables", "-L", "-n", "-v"], |
| 31 | + ] |
| 32 | + ) |
| 33 | + |
| 34 | + print("Firewall successfully configured") |
| 35 | + |
| 36 | + def uninstall(self): |
| 37 | + print("Cannot remove built-in OpenWrt firewall") |
| 38 | + print("Resetting firewall to default settings...") |
| 39 | + |
| 40 | + run_commands( |
| 41 | + [ |
| 42 | + ["uci", "revert", "firewall"], |
| 43 | + ["uci", "commit", "firewall"], |
| 44 | + ["/etc/init.d/firewall", "restart"], |
| 45 | + ] |
| 46 | + ) |
| 47 | + |
| 48 | + print("Firewall reset to default settings") |
| 49 | + |
| 50 | + def _detect_firewall_version(self) -> str: |
| 51 | + result = run(["which", "fw4"], capture_output=True) |
| 52 | + return "fw4" if result.returncode == 0 else "fw3" |
| 53 | + |
| 54 | + def _configure_fw3(self): |
| 55 | + print("Configuring fw3...") |
| 56 | + |
| 57 | + commands = [ |
| 58 | + ["uci", "set", "firewall.ssh=rule"], |
| 59 | + ["uci", "set", "firewall.ssh.name=Allow-SSH"], |
| 60 | + ["uci", "set", "firewall.ssh.src=wan"], |
| 61 | + ["uci", "set", "firewall.ssh.proto=tcp"], |
| 62 | + ["uci", "set", "firewall.ssh.dest_port=22"], |
| 63 | + ["uci", "set", "firewall.ssh.target=ACCEPT"], |
| 64 | + ["uci", "set", "firewall.http=rule"], |
| 65 | + ["uci", "set", "firewall.http.name=Allow-HTTP"], |
| 66 | + ["uci", "set", "firewall.http.src=wan"], |
| 67 | + ["uci", "set", "firewall.http.proto=tcp"], |
| 68 | + ["uci", "set", "firewall.http.dest_port=80"], |
| 69 | + ["uci", "set", "firewall.http.target=ACCEPT"], |
| 70 | + ["uci", "set", "firewall.https=rule"], |
| 71 | + ["uci", "set", "firewall.https.name=Allow-HTTPS"], |
| 72 | + ["uci", "set", "firewall.https.src=wan"], |
| 73 | + ["uci", "set", "firewall.https.proto=tcp"], |
| 74 | + ["uci", "set", "firewall.https.dest_port=443"], |
| 75 | + ["uci", "set", "firewall.https.target=ACCEPT"], |
| 76 | + ["uci", "commit", "firewall"], |
| 77 | + ] |
| 78 | + |
| 79 | + run_commands(commands) |
| 80 | + |
| 81 | + def _configure_fw4(self): |
| 82 | + print("Configuring fw4...") |
| 83 | + self._configure_fw3() |
| 84 | + |
| 85 | + def _check_service_status(self, expected_status: str) -> bool: |
| 86 | + result = run(["/etc/init.d/firewall", "status"], capture_output=True, text=True) |
| 87 | + if expected_status == "running": |
| 88 | + return result.returncode == 0 |
| 89 | + else: |
| 90 | + return result.returncode != 0 |
| 91 | + |
| 92 | + def _wait_for_status(self, expected_status: str, timeout: int = 10): |
| 93 | + start_time = time.time() |
| 94 | + while time.time() - start_time < timeout: |
| 95 | + if self._check_service_status(expected_status): |
| 96 | + return |
| 97 | + time.sleep(0.5) |
| 98 | + print(f"Warning: failed to reach status '{expected_status}'") |
0 commit comments