DEV Community

Daniel Ziltener
Daniel Ziltener

Posted on

3 2

How to share your dev box with others in 5 minutes - Caddy edition

Sometimes you want to share what you are working on right now with your coworkers without either calling them over or pushing and waiting until it is deployed on the staging server. Then you take a look and find a bunch of complicated tools, proprietary helpers like ngrok, and a whole lot of similar stuff.

Yet all you need is a reverse proxy and SSH. And here's how it's done, by example of Caddy.

Main Server Configuration

Add something like that to your Caddy file:

mymachine.dev.example.com {
  reverse_proxy https://localhost:11010 {
    transport http {
      tls_insecure_skip_verify
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Dev Box Configuration

On the dev box, make sure the webserver either reacts to the domain set up on the main server, or to every request in general. Since this is Caddy, I had to also disable the automatic certificate generation - I generate self-signed certs -, and the HTTPS redirect.

{
  auto_https off
}
localhost, devbox.local, mymachine.dev.example.com {
  tls /etc/ssl/localcerts/bundle.crt /etc/ssl/localcerts/server.key
  # Rest of your config
}
Enter fullscreen mode Exit fullscreen mode

And finally, as the last step, open up the SSH tunnel and reverse-forward the port:

/usr/bin/ssh -N -T -R 11010:localhost:443 user@example.com
Enter fullscreen mode Exit fullscreen mode

N means "don't execute a remote command", T means "don't allocate a terminal", and R means to forward the port in reverse - from the target to the local machine.

That is all!

RC Service File

We're using FreeBSD machines set up using Bastille at work, so I created a simple RC file so the forwarding is done automatically at every devbox start:

#!/bin/sh

# PROVIDE: sompani_tunnel
# REQUIRE: LOGIN DAEMON NETWORKING
# KEYWORD: shutdown

. /etc/rc.subr

name=sompani_tunnel
rcvar=${name}_enable
pidfile="/var/run/${name}.pid"
pidfile_child="/var/run/${name}_jvm.pid"
logfile="/var/log/${name}.log"
sompani_tunnel_chdir="/usr/local/share/location-service"

command="/usr/sbin/daemon"
start_cmd="sompani_tunnel_start"
procname="daemon"

load_rc_config ${name}
: ${sompani_tunnel_enable:=no}

sompani_tunnel_start() {
    /usr/sbin/daemon -r -f -P ${pidfile} -p ${pidfile_child} -t ${name} -o ${logfile} /usr/bin/ssh -N -T -R 11005:localhost:443 sompani-live
}

run_rc_command "$1"
Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay