A black bunker on the left with a closed cream door emits a dotted red curve that arcs up through an orange cloud holding a verified-shield icon, then descends into a cream post-office building with columns on the right. Public visitors enter at the post office; the bunker never accepts inbound traffic.
[ Networking & Security ]7 min read

Exposing Services to the World (Safely)

Phase 2 · The Network Matrix

Why port forwarding is a security risk, how outbound tunnels flip the model, and the day I accidentally exposed an internal endpoint to the public internet.

Cloudflare TunnelcloudflaredTraefikDMZ

The homelab had become a quiet, useful system. Files were safe, services were containerised, VLANs were tight. Inside the house it was fast. Outside the house it did not exist. Every time I left the flat with a laptop, I lost access to everything I had built. A portfolio of self-hosted tools that I could only use on my own sofa is a hobby, not infrastructure. I needed a way in from the outside.

The first answer the internet will give you is port forwarding: tell your home router that incoming traffic on port 443 should be sent to the server at 203.0.113.5 on your internal network. Five minutes of work, one button in the router’s web interface. It also happens to be the single most common way that homelabs end up on Shodan, which at the time of writing indexes over six hundred million publicly exposed services around the world, most of them accidentally. Within minutes of opening a port, automated bots begin probing it. Some of those bots are researchers. Most are not.

Port forwarding is a security model from a different era. It assumes the internet is mostly polite and the perimeter is mostly sufficient. Neither assumption survives contact with 2024. The pattern I eventually landed on (and that every professional remote-access setup now uses) inverts the direction of the connection: the server reaches out to a verified intermediary, and the intermediary brokers inbound traffic. No open ports. No inbound firewall holes. No footprint on Shodan.

The Analogy

Imagine a private workshop in a converted warehouse. You want friends to be able to come in and see your work, but you do not want to hang a sign on the street saying “workshop, door unlocked, come in”. The sign, in networking terms, is an open port.

The alternative is a trusted concierge at a separate, public building. You install a discreet intercom between your workshop and the concierge’s desk, initiated from your side. When a friend wants to visit, they go to the concierge. The concierge verifies their identity, rings your intercom, and asks if the friend is welcome. You answer. The conversation flows through the intercom. Your workshop is never listed on a map, never gets cold-callers, and the concierge takes the abuse on your behalf.

That is exactly the shape of a modern tunnel-plus-reverse-proxy setup. Your server initiates an outbound connection to a broker. The broker handles inbound requests from the public internet. Neither a bot scanning your residential IP nor a targeted attacker probing your router can find the service, because from the outside, the service does not live at your address.

The Tech Translated

I use Cloudflare Tunnel. It is not the only option (Tailscale Funnel, ngrok, frp, wireguard with a small VPS all work), but Cloudflare’s free tier is generous, the documentation is solid, and the broker runs in a network much better at absorbing abuse than my residential connection. A small daemon (cloudflared) runs on a machine inside the homelab. On startup it establishes an outbound TLS connection to Cloudflare’s network and holds the connection open. When a request arrives at my public domain, Cloudflare routes it through the open tunnel to the internal service. Nothing on my router ever has to accept an inbound connection from the public internet.

The architecture has three pieces:

  1. The DMZ host. A small Linux VM that runs cloudflared and nothing else of consequence. “DMZ” is a borrowed military term for demilitarised zone: a network segment that is trusted by neither the internet nor the internal network. If the DMZ host is compromised, the attacker does not get a foothold inside the homelab, because the DMZ’s firewall rules explicitly forbid it from initiating connections inward.
  2. The reverse proxy. Inside the homelab, a proxy (I use Traefik) handles the job of routing requests to the right container based on the hostname. The tunnel drops traffic at the proxy’s door.
  3. The services themselves. Unchanged from their containerised form in entry four. None of them know or care that they are being reached from outside.

Configuring the tunnel

The entire broker configuration is one YAML file. Read it slowly:

# /etc/cloudflared/config.yml
tunnel: 8a7c-tunnel-id-redacted-0123
credentials-file: /etc/cloudflared/creds.json

# No ingress mapping is implicit. Every hostname has to be named.
ingress:
  # The personal wiki.
  - hostname: wiki.example.com
    service: http://203.0.113.5:8080

  # The photo gallery.
  - hostname: photos.example.com
    service: http://203.0.113.5:2342
    originRequest:
      connectTimeout: 5s
      tlsTimeout: 5s

  # Default deny — any hostname not named above returns a 404.
  - service: http_status:404

Read it as intent. Two named hostnames, each with an explicit internal target, and a catch-all that refuses to serve anything else. Every new service is a two-line addition. If a hostname is not in the file, the tunnel does not carry it.

The daemon is started with a single command and runs as a systemd service:

# Install from Cloudflare's APT repository, then authenticate.
sudo cloudflared service install --config /etc/cloudflared/config.yml

# Enable and start as a daemon.
sudo systemctl enable --now cloudflared

What broke

The day I accidentally exposed an internal health-check endpoint to the public internet.

The tunnel had been running quietly for two months. I added a new service (a self-hosted RSS reader), configured a hostname for it, tested it worked, and went to bed. The next morning the RSS reader’s dashboard showed a surge of traffic on an endpoint called /healthz that was supposed to be internal-only. I checked the tunnel config. I had written the ingress rule as service: http://203.0.113.5:8080 without further path restrictions, which was fine for the public UI, but the service also exposed /healthz on the same port without authentication. Cloudflare was dutifully routing every public request to every endpoint on that port, including the unauthenticated health check that leaked cache statistics and a service version number.

The version number was the real problem. It revealed that I was running a build of the RSS reader that had a known CVE from three weeks earlier. A targeted attacker with that information would not have needed to guess anything. I was lucky. The traffic was bots, not humans.

Three fixes, all of which I now apply by default:

  1. Path-scoped ingress rules. The tunnel should only expose the paths you name.
  2. Default-deny at the reverse proxy. Traefik (or nginx, or Caddy) sits between the tunnel and the service and strips any path that is not explicitly allowed.
  3. Authentication in front of anything internal. Cloudflare Access (free tier includes five users) puts a short email-verification step in front of any route you mark as internal. The RSS reader’s dashboard is public; everything else requires the email link.

Here is the corrected tunnel config:

ingress:
  - hostname: rss.example.com
    path: "^/($|login|feed|article/|static/)"   # Only these paths.
    service: http://203.0.113.5:8080

  - hostname: rss.example.com
    path: ".*"
    service: http_status:404                    # Everything else: 404.

And the Traefik middleware that enforces the same shape internally, so even if the tunnel ever misroutes, the proxy refuses the request:

http:
  middlewares:
    strip-internal:
      replacePathRegex:
        regex: "^/healthz|^/admin|^/metrics"
        replacement: "/blocked"
  routers:
    rss:
      rule: Host(`rss.example.com`)
      middlewares: [ strip-internal ]
      service: rss-service

Layered defence. If the outer layer fails, the inner layer catches it. If both fail, the service itself is behind Cloudflare Access and asks for an email.

The quiet part

The goal was never to build a fortress. The goal was to keep the doors I intended open, open, and keep the doors I did not intend open, closed. Every tool in this entry (Cloudflare Tunnel, DMZ segmentation, reverse proxy, Access) is a small piece of that second category. Each one has a single job: “refuse requests I did not explicitly invite.”

Two months after the health-check incident, the RSS reader, the wiki, and the photo gallery all reach me from a café, a plane, a hotel room. The residential IP does not appear in their DNS records. A port scan of the home network returns nothing. The services simply do not live at the house, even though the files do. That inversion is the whole point of the chapter.

Phase two of this journal ends here. The homelab can talk to itself, keep its own data safe, survive disasters, and be reached from the outside. Phase three is about turning it into something that can run AI models, which is the reason I started in the first place. The hardware gets heavier from here.

Footnotes

  1. Shodan is a search engine for internet-connected devices. Founded by John Matherly in 2009, it indexes banners returned by scanned ports. The statistic cited reflects public index size as of mid-2024. shodan.io. ↩

  2. Cloudflare Tunnel (formerly Argo Tunnel) is documented at developers.cloudflare.com/cloudflare-one/connections/connect-networks. The free tier covers tunnels and up to fifty Access users. ↩

Table of Contents