Home

Exposing your local dev-server to the Internet

I'm a big fan of having utility scripts, where each script solves a small problem I have in my day-to-day work. One of those problems is to share something I have running locally with colleagues. For example:

In all those cases, I would ideally just run this in the terminal:


tunnel 3000
      

... and be done with it. Any request from example.com (or whatever domain you own) should be forwarded to port 3000. There are already solutions to this (ngrok and opentunnel are both great). However, I want some tweaks that isn't provided out-of-the-box:

Subdomains as passwords

the first part, about access control is a bit tricky. How can we ensure colleagues with access to the link is able to view the page, whereas strangers that might also know the public domain it ends up on cannot see the contents? We allow ourselves to do some cowboy-style access control. We're not building a cryptocurrency wallet. We're just sharing birthday cards with our colleagues, or share something that shouldn't be accessed publicly. Anything really confidential should probably be shared in-person, anyways. With that in mind, we have several alternatives:

So generating a random subdomain is the way to go for this script. I'm sure there are weaknesses to this approach. But it seems to be good enough for my needs.

Setting up a cloudflare tunnel

We're using Cloudflare Tunnel for the heavy lifting. We have the tunnel client in place. However, we need to create a new tunnel, specify a configuration file and run. In addition, once the tunnel close, we also need to tear down resources, like the good citizen we are. I let Claude Code do the implementation. It's a python script, and it was written by an initial spec by me. I had to iterate with Claude a few times before the end result was good enough. All in all, I estimate I spent around 1 - 2 hours for this script. Very roughly, the code is like this:


function tunnel(port) {
    tunnel    = "mytunnel"              # randomly generated
    subdomain = "80b162bc.example.com"  # randomly generated

    # destroy tunnel when process exits
    setup_interrupt_handlers()

    tunnel_id = create_tunnel(tunnel, subdomain)
    write_config(tunnel_id, tunnel, subdomain, port)
    start_tunnel()
    copy_url_to_clipboard()
    display_qr_code()
}
      

Copying to clipboard and generating QR-codes

The URL is copied over to clipboard with the xclip tool:


echo "$link" | xclip -selection clipboard
 

And for QR-code generation, I used qrencode that prints the code to stdout:


qrencode -t ansi256 "$link"
 

Bonus: serving file contents

With a small tweak, we can serve static files in a directory! We'll just run python -m http.server /tmp/foo/bar on a random port, and then we create a tunnel there:


tunnel /tmp/foo/bar

Summary

Creating this helper script has been really fun and I use it weekly-ish. The script lets me do exactly what I want, eliminating the small friction points I have. Claude did most of the gruntwork, while I defined its behaviours. All in all, I probably spent 1 - 2 hours implementing this.