Skip to content

Instantly share code, notes, and snippets.

@Jeremy-Gstein
Created February 25, 2024 02:48
Show Gist options
  • Save Jeremy-Gstein/557176b51d19b628f2768ab8c250d9e7 to your computer and use it in GitHub Desktop.
Save Jeremy-Gstein/557176b51d19b628f2768ab8c250d9e7 to your computer and use it in GitHub Desktop.
using ssh, caddy, and the cloud provider of your choice - spin up 1 virtual machine to proxy all your local services without exposing your external gateway.

selfhost

using ssh, caddy, and the cloud provider of your choice - spin up 1 virtual machine to proxy all your local services without exposing your external gateway.

The Goal:

  • host locally without forwarding ports on external gateway.
  • no ddns
  • no port forwarding
  • mask your public IP address
  • available without manual intervention

Motivation:

I started this project trying to find an alternative to proxy hosting services like ngrok. Say you have a webserver running locally on port 8080. For external machines to access this we need a simple way to view the web service. Using a cloud VPC (Virtual Machine running in the Cloud [AWS, Azure, Digital Ocean, etc...]) we can use this public IP address instead of our public IP address.

Diagram:

diagram of 4 locations. Homelab, DigitalOcean, Cloudflare, and Client/User.


Steps:

  1. Start Service on local server
  2. Enable systemd service to handle tunnel
  3. logon to remote server
  4. edit /etc/caddy/Caddyfile
  5. add a reverse proxy to your desired url and the local port forwarded.

Template systemd:

[Unit]
Description=ssh tunnel to proxy server 

[Service]
User=root
Environment=SSH_AUTH_SOCK=/run/user/1000/ssh-agent.socket
ExecStart=/usr/bin/ssh -o ServerAliveInterval=60 -i /home/$USER/.ssh/id_rsa -NT -R $service_port:localhost:$service_port root@$remote_host
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Template Caddyfile:

test.example.com {
	reverse_proxy 127.0.0.1:$service_port
}

Mirror of selfhost repository


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment