Skip to content

ajarmoszuk/haproxy-dynamic-port-manager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HAProxy Dynamic Port Manager

A lightweight, efficient bash script that automatically detects active ports on a local machine and dynamically configures HAProxy to forward traffic to these ports.

Overview

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

Why This Was Built

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:

  1. Continuously monitored for active game server ports
  2. Dynamically updated the HAProxy configuration in real-time
  3. Ensured traffic was properly routed to the correct node and port
  4. 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.

Features

  • 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

Requirements

  • Linux environment with bash
  • HAProxy installed and configured
  • nmap for port scanning
  • Root or sudo privileges (to modify HAProxy config)

Installation

  1. Clone this repository:

    git clone https://github.com/ajarmoszuk/haproxy-dynamic-port-manager.git
    cd haproxy-dynamic-port-manager
  2. Make the script executable:

    chmod +x haproxy_port_manager.sh
  3. Adjust configuration variables in the script to match your environment:

    • START_PORT and END_PORT: The port range to monitor
    • PRIVATE_IP: The IP address to forward traffic to
    • CONFIG_FILE: Path to your HAProxy configuration file
    • CHECK_INTERVAL_NEW and CHECK_INTERVAL_ACTIVE: Scan intervals

Usage

Run the script with root privileges:

sudo ./haproxy_port_manager.sh

For production use, you might want to set up a systemd service:

sudo nano /etc/systemd/system/haproxy-port-manager.service

Add 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

How It Works

  1. The script uses nmap to scan the configured port range on the local machine
  2. When an active port is detected, it generates a new HAProxy configuration with frontend-backend pairs
  3. The script validates the new configuration and reloads HAProxy
  4. This process repeats based on the configured check intervals

Configuration Options

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 seconds

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

About

A lightweight, efficient bash script that automatically detects active ports on a local machine and dynamically configures HAProxy to forward traffic to these ports.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages