A lightweight, dynamic firewall manager that “opens ports when needed, closes them when not.” It consists of:
-
Core Service (
dynamic-port-guard)
A bash daemon that monitors listening ports and updates aniptables‑based allow chain automatically. -
Web UI (
dynamic-port-webui)
An optional Flask interface (running in its own Python 3 venv) for status, configuration, logs, and—new—port‑sharing via SSH reverse tunnels.
- How It Works
- Key Features
- Prerequisites
- Installation
- Usage
- Configuration
- Security Considerations
- Troubleshooting
- Contributing
- License
-
Monitor
Every 10 s (default), the core script runsssto list TCP/UDP ports inLISTENon IPv4/IPv6. -
Whitelist
Merges live ports with user‑specifiedWHITELISTin/etc/dynamic-port-guard.conf. -
iptables Chain
- Creates/jumps to a dedicated chain (default:
PORTGUARD_ALLOW). - Flushes its rules each cycle.
- Adds
ACCEPTfor each allowed port/protocol.
- Creates/jumps to a dedicated chain (default:
-
Default-Deny
Assumes your mainINPUTpolicy isDROPor has rules after the jump to block everything else.
- Dynamic Port Allowing
- Whitelist Support for critical services
- IPv4 & IPv6
- Dedicated
iptableschain for easy management - Systemd integration for both core & Web UI
- Isolated Python venv for Web UI
- Minimal resource usage
- Port Sharing: Expose any local port securely via an SSH reverse tunnel (default:
localhost.run). The Web UI automatically captures the generated.lhr.lifeURL, updates it live through a background monitor thread, and displays a QR code for easy mobile access.⚠️ WARNING: Exposing services publicly can pose security risks—only share ports you trust.
- OS: Linux with
systemd(tested on Debian/Ubuntu). - Firewall tools:
iptables,ip6tables. - Core tools:
bash,ss(fromiproute2),awk,grep,sort. - SSH: Client must be installed; port‑sharing uses
localhost.run. - Python 3:
python3,python3-pip,python3-venv(orpython3-virtualenv). - Root access: Required to manage firewall and systemd.
git clone https://github.com/1999AZZAR/dynamic_port_guard.git
cd dynamic-port-guard
sudo ./install.sh # Core + Web UI (default)
# or:
sudo ./install.sh --core-only
sudo ./install.sh --webui-onlyThis will:
- Install
dynamic_port_guard.shto/usr/local/bin/ - Create
/etc/dynamic-port-guard.conf(from example) - Enable & start
dynamic-port-guard.service - (Web UI) Copy
webui/→/opt/dynamic-port-webui, create venv, install Flask - Enable & start
dynamic-port-webui.service - Add
portguardalias to your~/.bashrc
sudo systemctl status dynamic-port-guard.service
sudo systemctl start dynamic-port-guard.service
sudo systemctl stop dynamic-port-guard.service
sudo systemctl restart dynamic-port-guard.service
sudo journalctl -u dynamic-port-guard.service -fVisit http://localhost:6060 (or http://<your-server-ip>:6060). You can:
- View listening ports & statuses
- Edit & save
/etc/dynamic-port-guard.conf - Tail the log (
/var/log/dynamic_ports.log) - Control core service via buttons
- Share local ports (see below)
In the Web UI’s “Ports” list, click Share on any port:
- Creates an SSH reverse tunnel to
localhost.run. - Captures & displays the
.lhr.lifeURL. - Generates a QR code for easy mobile access.
- Runs a background monitor thread to detect URL changes and update live.
- Click Stop to terminate the tunnel.
After installing Web UI, run:
source ~/.bashrc
portguardThis shows core + UI status and attempts to open the Web UI in your browser.
All settings live in /etc/dynamic-port-guard.conf. Example options:
# Poll interval in seconds
INTERVAL=10
# Always allow these ports
WHITELIST="22 80 443"
# Firewall chain name
IPTABLES_CHAIN="PORTGUARD_ALLOW"
# Log file
LOG_FILE="/var/log/dynamic_ports.log"Edit & save via Web UI or your editor, then restart:
sudo systemctl restart dynamic-port-guard.service
⚠️ CRITICAL WARNING: This tool directly manipulatesiptablesrules. A misconfiguration may lock you out of your server. Always ensure SSH (or your remote access port) is whitelisted and you have alternative access methods.
- Default‑deny policy is required; this tool only adds allow rules.
- Services run as
root; consider a hardened sudo setup for least privilege. - Web UI listens on port 6060—limit exposure via firewall or host binding.
- Only whitelist truly necessary ports.
- No egress filtering—this is inbound only.
-
Core fails:
sudo journalctl -u dynamic-port-guard.service -n 50 --no-pager tail -n 50 /var/log/dynamic_ports.log
-
Web UI fails:
sudo journalctl -u dynamic-port-webui.service -n 50 --no-pager /opt/dynamic-port-webui/venv/bin/pip list | grep Flask -
Cannot access UI:
sudo ss -tlpn 'sport == :6060' iptables -nvL INPUT --line-numbers iptables -nvL PORTGUARD_ALLOW | grep 6060
Contributions welcome! Please fork, create a branch, and open a PR.
Ensure you test on a systemd‑based distro and update docs accordingly.
MIT 2025 1999AZZAR


