A lightweight, efficient bash script that automatically detects active ports on a local machine and dynamically configures HAProxy to forward traffic to these ports.
This tool continuously scans a specified port range on the local machine, detects active ports, and automatically updates HAProxy configuration to create frontend-backend pairs for each active port. It's particularly useful for:
- Dynamic service discovery in development environments
- Auto-configuring reverse proxies for ephemeral services
- Dynamic port forwarding with support for PROXY protocol
- Any scenario where services come online with unpredictable port assignments
This project was originally developed to solve a specific challenge at FreeServers.pl (FSRV.pl), a platform that provides freemium Minecraft server hosting. The platform needed to efficiently manage thousands of user-created game servers across multiple nodes, each with dynamic port assignments.
The traditional approach of manually updating HAProxy configurations whenever servers came online or went offline wasn't feasible at this scale, and we needed it to assign SRV records at domain level. This tool provided an automated solution that:
- Continuously monitored for active game server ports
- Dynamically updated the HAProxy configuration in real-time
- Ensured traffic was properly routed to the correct node and port
- Maintained high availability even as servers were created and destroyed
While originally built for Minecraft servers, the tool has been generalized to work with any service that requires dynamic port management.
- Fast, efficient port scanning using nmap
- Real-time detection of new active ports
- Automatic HAProxy configuration generation and reloading
- Minimal resource footprint
- Configurable scan intervals for new and existing ports
- Support for transparent proxying and PROXY protocol
- Linux environment with bash
- HAProxy installed and configured
- nmap for port scanning
- Root or sudo privileges (to modify HAProxy config)
-
Clone this repository:
git clone https://github.com/ajarmoszuk/haproxy-dynamic-port-manager.git cd haproxy-dynamic-port-manager -
Make the script executable:
chmod +x haproxy_port_manager.sh
-
Adjust configuration variables in the script to match your environment:
START_PORTandEND_PORT: The port range to monitorPRIVATE_IP: The IP address to forward traffic toCONFIG_FILE: Path to your HAProxy configuration fileCHECK_INTERVAL_NEWandCHECK_INTERVAL_ACTIVE: Scan intervals
Run the script with root privileges:
sudo ./haproxy_port_manager.shFor production use, you might want to set up a systemd service:
sudo nano /etc/systemd/system/haproxy-port-manager.serviceAdd the following content:
[Unit]
Description=HAProxy Dynamic Port Manager
After=network.target haproxy.service
[Service]
Type=simple
ExecStart=/path/to/haproxy_port_manager.sh
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
Then enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable haproxy-port-manager
sudo systemctl start haproxy-port-manager- The script uses nmap to scan the configured port range on the local machine
- When an active port is detected, it generates a new HAProxy configuration with frontend-backend pairs
- The script validates the new configuration and reloads HAProxy
- This process repeats based on the configured check intervals
Edit the variables at the top of the script to customize behavior:
START_PORT=25566 # Beginning of port range to scan
END_PORT=45566 # End of port range to scan
PRIVATE_IP=127.0.0.1 # IP address to forward traffic to
CONFIG_FILE="/etc/haproxy/haproxy.cfg" # HAProxy config location
CHECK_INTERVAL_NEW=10 # Check for new ports every 10 seconds
CHECK_INTERVAL_ACTIVE=60 # Re-check active ports every 60 secondsMIT License - see LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request