Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SSH Forwarder

A dynamic SSH forwarding system that automatically updates SSH port forwarding when the client's IP address changes.

Overview

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

Setup

1. Install Dependencies

npm install

2. Configure Environment

Edit 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=30000

Configuration Details:

  • SERVER_HOST: IP address of the server where server.js runs
  • SERVER_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)

3. Deployment

On the Server Machine:

  1. Copy the project files to your server
  2. Configure the .env file
  3. Run: npm run server or node server.js

On the Client Machine:

  1. Copy the project files to your client machine
  2. Configure the .env file with your server's IP
  3. Run: npm run client or node client.js

Usage

Starting the Server

npm run server

The 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

Starting the Client

npm run client

The client will:

  • Check its public IP address immediately
  • Monitor for IP changes at regular intervals
  • Notify the server when IP changes are detected

Connecting via SSH

Once the client has reported its IP to the server, you can SSH through the server:

ssh -p 2222 username@your-server-ip

This will forward your connection to the client machine's SSH port.

API Endpoints

The server provides several endpoints:

  • GET /health - Health check and current status
  • POST /update-ip - Endpoint for clients to report IP changes
  • GET /status - Detailed forwarding status

Features

  • 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

Security Considerations

  1. Firewall: Ensure the server's SSH forward port is accessible
  2. Authentication: SSH authentication is handled by the target machine
  3. Network: Consider using HTTPS for the client-server communication in production
  4. Access Control: Implement additional authentication for the API endpoints if needed

Troubleshooting

Client Issues

  • Check if SERVER_HOST and SERVER_PORT are correct
  • Verify network connectivity to the server
  • Check for firewall restrictions

Server Issues

  • 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

Connection Issues

  • 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 /status endpoint

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages