A high-performance network port multiplexer daemon written in Rust that intelligently routes incoming connections to different backend services based on protocol detection and host-based routing.
- Protocol Detection: Automatically detects HTTP, HTTPS, OpenVPN, SSH,SFTP and custom protocols
- Host-based Routing: Route HTTP/HTTPS requests based on the Host header
- Python Integration: Custom protocol analysis through embedded Python scripts
- Performance Optimization: Configurable priority settings (latency, throughput, both)
- Systemd Integration: Full systemd service support with security hardening
PortMux acts as a reverse proxy that listens on a single port and forwards connections to appropriate backend services based on:
- Protocol Analysis: Examines initial packet data to determine the protocol
- Host Header Inspection: For HTTP/HTTPS, routes based on the Host header
- Custom Rules: Uses Python scripts for advanced protocol detection
- Fallback Routing: Default backends for unmatched connections
yay -S portmux
# or
paru -S portmuxClone the repository:
git clone https://github.com/yourusername/portmux.git
cd portmuxBuild and install:
make installBuild the binary:
cargo build --releaseThe binary will be available at target/release/portmux
The main configuration file is located at /etc/portmux/config.yaml. Here's the structure:
PORTMUX:
ip: 0.0.0.0
port: 8080
HTTP:
"example.com":
port: 3000
priority: latency
"api.example.com":
port: 3001
priority: throughput
"default":
port: 80
priority: latency
HTTPS:
"secure.example.com":
port: 3443
priority: latency
"default":
port: 443
priority: both
OPENVPN:
"tcp":
port: 443
priority: both
"udp":
port: 1194
priority: both
"default":
port: 1194
priority: both
SSH:
"default": 22latency: Optimized for low-latency connectionsthroughput: Optimized for high-throughput transfersboth: Balanced optimization
PortMux can use Python scripts for custom protocol analysis. Edit /etc/portmux/script.py:
def analyse(buffer):
"""
Analyze incoming buffer and return appropriate port number
Args:
buffer (bytes): Initial packet data
Returns:
int: Port number for routing, or None for default handling
"""
if buffer.startswith(b"CUSTOM_PROTOCOL"):
return 9000
elif buffer.startswith(b"GAME_DATA"):
return 7777
else:
return Nonesystemctl enable --now portmux.serviceCheck status:
systemctl status portmux.serviceView logs:
journalctl -u portmux.service -fRun in foreground (for testing):
portmuxTest HTTP routing:
curl -H "Host: example.com" http://your-server:8080Test HTTPS routing:
curl -k -H "Host: secure.example.com" https://your-server:8080Test SSH forwarding:
ssh user@your-server -p 8080PortMux runs with several security hardening measures:
- Unprivileged User: Runs as dedicated
portmuxuser - Capability-based: Only has
CAP_NET_BIND_SERVICEcapability - Read-only Configuration: Configuration files are read-only at runtime
- System Protection: Protected home directory and system files
- No New Privileges: Cannot escalate privileges
Allow PortMux port:
sudo ufw allow 8080/tcpEnsure backend services are not directly accessible:
sudo ufw deny 3000:3010/tcpView real-time logs:
journalctl -u portmux.service -fView logs from last hour:
journalctl -u portmux.service --since "1 hour ago"Check if service is running:
systemctl is-active portmux.serviceCheck listening ports:
sudo netstat -tulpn | grep portmux- Permission Denied: Ensure the service has
CAP_NET_BIND_SERVICEcapability - Config Parse Error: Validate YAML syntax with
yamllint config.yaml - Python Script Errors: Check logs for Python integration issues
- Connection Refused: Verify backend services are running and accessible
Install Rust toolchain:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shClone and build:
git clone https://github.com/yourusername/portmux.git
cd portmux
cargo build --releasecargo testThis project is licensed under the MIT License - see the LICENSE file for details.
- Initial release
- Basic protocol detection and routing
- HTTP/HTTPS host-based routing
- OpenVPN, SSH, SFTP support
- Python integration for custom protocols
- Systemd service integration