Tunnel Server Development Setup
Set up the Portr tunnel server for local development, so you can run and contribute to the SSH-based tunneling backend on your machine.
The tunnel server is built using Go and handles SSH remote port forwarding to tunnel HTTP/TCP connections.
Requirements
- Go (1.25+)
- PostgreSQL (16+) or SQLite (3.0+)
Setup Process
Configure environment variables
Create a new .env file using the .env.template file as a reference.
Start the server
go run ./cmd/portrd start tunnelYou should see output similar to:
time=2024-03-29T19:16:35.023+05:30 level=INFO msg="starting SSH server" port=:2222
time=2024-03-29T19:16:35.023+05:30 level=INFO msg="starting proxy server" port=:8001
time=2024-03-29T19:16:35.023+05:30 level=INFO msg="Starting 1 cron jobs"This starts:
- SSH server on port
:2222for tunnel connections - Proxy server on port
:8001for routing traffic
Server Architecture
The tunnel server consists of several components:
SSH Server
- Handles incoming tunnel connections from clients
- Manages SSH remote port forwarding
- Authenticates clients using tokens
Proxy Server
- Routes incoming HTTP requests to the appropriate tunnels
- Handles subdomain routing
- Manages SSL termination in production
Cron Jobs
- Cleans up expired tunnel connections
- Performs maintenance tasks
Development Workflow
Available Commands
# Start the server
go run ./cmd/portrd start tunnel
# Build the server
go build ./...
# Run tests
go test ./...
# Format code
go fmt ./...
# Run linting
go vet ./...Configuration
For all configuration variables, check out the tunnel server config file.
Key configuration options include:
- Database connection: PostgreSQL connection string
- SSH server settings: Port, host keys, and authentication
- Proxy settings: HTTP server configuration
- Security settings: Token validation and access control
Testing Tunnel Connections
Make sure the admin server is running before testing tunnel connections, as it handles client authentication.
Local Testing
- Start the admin server
- Start the tunnel server
- Build and run the client to create a test tunnel
Connection Flow
- Client connects to SSH server on port 2222
- SSH server validates the client token with the admin server
- Remote port forwarding is established
- Proxy server routes incoming requests to the client
Troubleshooting
Common Issues
Port conflicts: Ensure ports 2222 and 8001 are available.
Database connection errors: Verify your PostgreSQL instance is running and accessible.
SSH connection failures: Check that the SSH server is properly configured and the client has valid credentials.
Debugging
Enable tunnel debug logging with:
PORTR_TUNNEL_DEBUG=trueView detailed logs to troubleshoot connection issues:
# Follow server logs
go run ./cmd/portrd start tunnelAdmin Development Setup
Set up the Portr admin dashboard for local development, so you can run and contribute to the team management interface on your machine.
Client Development Setup
Set up the Portr client CLI for local development, so you can build, run, and contribute changes to the tunnel client on your machine.