A dynamic SSH forwarding system that automatically updates SSH port forwarding when the client's IP address changes.
This system consists of two components:
- Client (
client.js): Monitors public IP changes and notifies the server - Server (
server.js): Receives IP updates and maintains SSH port forwarding
npm installEdit the .env file with your settings:
# Server configuration (for client to connect to)
SERVER_HOST=your-server-ip-here
SERVER_PORT=3000
# SSH forwarding configuration
SSH_FORWARD_PORT=2222
SSH_TARGET_PORT=22
# Client configuration
CHECK_INTERVAL=30000Configuration Details:
SERVER_HOST: IP address of the server whereserver.jsrunsSERVER_PORT: Port for the HTTP API (default: 3000)SSH_FORWARD_PORT: Port on server that will forward SSH connections (default: 2222)SSH_TARGET_PORT: SSH port on the client machine (default: 22)CHECK_INTERVAL: How often to check for IP changes in milliseconds (default: 30 seconds)
- Copy the project files to your server
- Configure the
.envfile - Run:
npm run serverornode server.js
- Copy the project files to your client machine
- Configure the
.envfile with your server's IP - Run:
npm run clientornode client.js
npm run serverThe server will:
- Start an HTTP API on the configured port
- Wait for IP updates from clients
- Create SSH forwarding when a client IP is received
npm run clientThe client will:
- Check its public IP address immediately
- Monitor for IP changes at regular intervals
- Notify the server when IP changes are detected
Once the client has reported its IP to the server, you can SSH through the server:
ssh -p 2222 username@your-server-ipThis will forward your connection to the client machine's SSH port.
The server provides several endpoints:
GET /health- Health check and current statusPOST /update-ip- Endpoint for clients to report IP changesGET /status- Detailed forwarding status
- Automatic IP Detection: Uses public IP detection to handle NAT/firewall scenarios
- Dynamic Forwarding: Automatically updates SSH forwarding when client IP changes
- Graceful Handling: Properly closes old connections before establishing new ones
- Monitoring: Built-in health checks and status endpoints
- Error Recovery: Robust error handling and connection management
- Firewall: Ensure the server's SSH forward port is accessible
- Authentication: SSH authentication is handled by the target machine
- Network: Consider using HTTPS for the client-server communication in production
- Access Control: Implement additional authentication for the API endpoints if needed
- Check if
SERVER_HOSTandSERVER_PORTare correct - Verify network connectivity to the server
- Check for firewall restrictions
- Ensure the SSH forward port is not in use by another service
- Check firewall rules for both the API port and SSH forward port
- Verify the client machine's SSH service is running and accessible
- Test direct SSH connection to verify credentials
- Check if the client's IP is correctly reported to the server
- Verify port forwarding is active using the
/statusendpoint