longkeyy/ssh-tunnel-server

By longkeyy

Updated about 1 year ago

A high-performance SSH tunnel server with dynamic port allocation.

Image
Networking
0

380

longkeyy/ssh-tunnel-server repository overview

SSH Tunnel Server


A high-performance SSH tunnel server with dynamic port allocation and structured logging.

Features
  • ✅ SSH remote port forwarding
  • ✅ Dynamic port allocation (sequential/random strategies)
  • ✅ Structured logging system (zap)
  • ✅ MAC address-based stable key generation
  • ✅ Cross-platform compilation support
  • ✅ Hot reload development support
  • ✅ Multi-architecture Docker images
  • ✅ Production-ready deployment
Installation & Compilation
Current Platform
make
# or
make build
Cross-Platform (All Platforms)
make all
Development Mode (Hot Reload)
make dev
Docker Deployment
# Pull and run from Docker Hub
docker run -d \
  --name ssh-tunnel-server \
  -p 2022:2022 \
  -p 10000-15000:10000-15000 \
  longkeyy/ssh-tunnel-server:latest

# Or use docker-compose
wget https://raw.githubusercontent.com/longkeyy/ssh-tunnel-server/main/docker-compose.yml
docker-compose up -d
Local Build
# Build image
make docker-build

# Start service
make docker-run

# View logs
make docker-logs

# Stop service
make docker-stop
Docker Compose
# Production - port 2022, range 10000-15000
docker-compose up -d

# Development - port 2023, range 20000-25000, debug enabled
docker-compose --profile dev up -d

# View logs
docker-compose logs -f

# Stop service
docker-compose down
Direct Docker Run
# Basic run
docker run -d \
  --name ssh-tunnel-server \
  -p 2022:2022 \
  -p 10000-15000:10000-15000 \
  longkeyy/ssh-tunnel-server:latest

# Custom parameters
docker run -d \
  --name ssh-tunnel-server \
  -p 2022:2022 \
  -p 20000-25000:20000-25000 \
  longkeyy/ssh-tunnel-server:latest \
  -l 0.0.0.0:2022 -s 20000 -e 25000 -m random -d
Multi-Architecture Build
# Default multi-arch build (linux/amd64, linux/arm64, linux/arm/v7)
make docker-build

# Single architecture build (current platform)
./scripts/docker-build.sh --single-arch

# Build and push to registry
./scripts/docker-build.sh --push -r your-registry.com

# Custom tag and push
./scripts/docker-build.sh -t v1.0.0 --push -r your-registry.com
Docker Hub Image Tags
# Use latest version
docker pull longkeyy/ssh-tunnel-server:latest

# Use specific version (if release tags available)
docker pull longkeyy/ssh-tunnel-server:v1.0.0

# Use development version
docker pull longkeyy/ssh-tunnel-server:main
Usage
Basic Usage
./ssh-tunnel-server
Command Line Parameters
ParameterDefaultDescription
-l0.0.0.0:2022SSH server listen address
-k""SSH host key file path
-dfalseEnable debug logging
-jfalseUse JSON format for logs
-o""Log file output path
-s10000Port allocation range start
-e50000Port allocation range end
-msequentialPort allocation mode (sequential|random)
Usage Examples
Random Port Allocation
./ssh-tunnel-server -m random -s 20000 -e 25000
Debug Mode + JSON Logs
./ssh-tunnel-server -d -j
Custom Listen Address and Port Range
./ssh-tunnel-server -l :2023 -s 15000 -e 20000
Output Logs to File
./ssh-tunnel-server -o /var/log/ssh-tunnel.log
Client Connection
Random Port Forwarding
ssh -p 2022 -R0:localhost:22 user@server-ip
Specific Port Forwarding
ssh -p 2022 -R8080:localhost:80 user@server-ip
Background Run
ssh -p 2022 -R0:localhost:22 -N -f user@server-ip
Port Allocation Strategies
Sequential (Default)
  • Allocate ports in order
  • Loop back to start when reaching the end
  • Suitable for predictable port allocation scenarios
Random
  • Randomly select ports within the specified range
  • Increases security by avoiding port scanning
  • Automatically falls back to sequential allocation on random failure
Log Format
Console Format (Default)
2025-06-28 02:47:01    INFO    SSH tunnel server started    {"address": "0.0.0.0:2022", "port_start": 10000}
JSON Format
{"level":"info","ts":"2025-06-28T02:47:01.147+0800","caller":"logger/logger.go:67","msg":"SSH tunnel server started","address":"0.0.0.0:2022","port_start":10000}
Build Options
Makefile Commands
make               # Build current platform (default)
make build         # Same as 'make'
make all           # Cross-compile for all platforms
make clean         # Clean build artifacts
make test          # Run tests
make run           # Build and run in debug mode
make dev           # Development mode (auto-reload)
make lint          # Code linting
make fmt           # Format code
Docker Targets
make docker-build  # Build Docker image
make docker-run    # Start server with docker-compose
make docker-stop   # Stop docker-compose services
make docker-logs   # Show container logs
make docker-dev    # Start development server (port 2023)

Build Features:

  • Uses -trimpath -a parameters for reproducible builds
  • All binaries output to build/ directory
  • Automatic version info and build time embedding
Supported Platforms
  • Linux (amd64, 386, arm64, armv5/6/7)
  • macOS (amd64, arm64)
  • Windows (amd64, 386, arm64)
  • FreeBSD (amd64, 386, arm64)
  • OpenBSD (amd64, 386, arm64)
Security Features
  • MAC address-based stable SSH host key generation
  • No dependency on external services for IP address retrieval
  • Custom port range support to avoid conflicts
  • Structured logging for security auditing
  • Non-root container execution
  • Read-only filesystem in containers
License

MIT License

Tag summary

Content type

Image

Digest

sha256:1fbddd766

Size

8 MB

Last updated

about 1 year ago

docker pull longkeyy/ssh-tunnel-server