Skip to content

Plate II · Selected work

Draco

tun-vpn

The dragon guarding the passage — a guarded tunnel.

A VPN written from scratch in 560 lines of Go: it carries traffic between two separate networks through an encrypted tunnel, using only the standard library.

The result

It works, and four one-command experiments show it: traffic crosses two networks that cannot see each other, stays unreadable on the wire, and the tunnel rebuilds itself within two seconds of a server restart.

Role
Solo
design, implementation, experiments
Timeline
Spring 2026
Computer Security coursework, UB
Stack
  • Go
  • Linux TUN/TAP
  • TLS 1.3
  • Docker Compose
  • tcpdump
On this plate ProblemApproachResult
  1. 01 Problem
  2. 02 Approach
  3. 03 Result
internet_net · 10.0.0.0/24internal_net · 192.168.60.0/24TLS 1.3 · :55555 · length-framed IPvpn_clientvpn_serverhost_v10.0.0.20 · tun0 10.4.2.9910.0.0.10 · tun0 10.4.2.5ip_forward=1192.168.60.20tunnel is the only path
The topology, charted — the dragon guards the only passage

Problem

Everyone uses VPNs; very few people can say what one is beneath the product branding. The goal was to strip the idea to its core and build it: catch a computer’s IP packets (the basic units of internet traffic), carry them encrypted across a hostile network, and release them into a private network on the far side, with security you can demonstrate, not just claim.

Three constraints shaped the design:

  1. Self-contained: no extra hardware, no kernel modules (add-ons to the operating system’s core), no changes to the host’s networking. Anyone with Docker on Linux can run it all.
  2. Provable, not plausible: the demos had to show traffic genuinely flowing through the tunnel (and dying without it), not just two simulated machines pinging each other.
  3. Readable: the reference model is the C code in Wenliang Du’s Computer & Internet Security (ch. 19), built around select(2), a Unix call that watches several inputs at once. The challenge was turning it into natural Go without obscuring it.

Approach

The whole VPN comes down to two building blocks: about 560 lines of Go, with nothing outside the standard library.

CreateTun asks the kernel for a virtual network card that carries raw IP packets and nothing else, using TUNSETIFF with the flags IFF_TUN | IFF_NO_PI. The subtle part is the ifreq structure: the kernel reads it straight from memory, so the Go version must match C’s layout byte for byte, including 22 bytes of explicit padding after the flags field. The file handle that comes back is the packet stream: reads return packets the kernel sent to the card; writes inject packets as if they had arrived from the network.

Relay moves packets between the virtual card and an encrypted TLS connection, the kind that protects web traffic. Where Du’s code watches both sides with select(2), Go uses two goroutines (lightweight parallel tasks), and the first error from either wins. TLS runs over TCP, an unbroken stream of bytes, so each packet goes out with a 2-byte length in front (big-endian, biggest byte first) and is read back with io.ReadFull; a plain Read can stop partway through a packet. Before a packet reaches the kernel, a cheap check confirms its first four bits name a valid IP version, so a corrupted or malicious stream becomes a logged skip, not kernel noise. An earlier UDP version needed no prefix, since UDP keeps messages separate; TLS over TCP created the need.

Security model. The client trusts exactly one certificate authority (an issuer that vouches for identities): a private one generated at build time and pinned via RootCAs. It also pins the expected name with ServerName, so the connection fails unless the server’s certificate carries that name in its SAN field. Address and identity are thus independent: the server can change address without new certificates, and an attacker on the network path cannot pose as it. TLS 1.2 is the minimum; 1.3 is what actually gets used. The opening handshake is forced as soon as a client connects, so a certificate problem shows up as a clear handshake failed log line, not a cryptic error mid-stream. One honest limit, documented in the server itself: clients do not prove who they are with a certificate. Mutual TLS, where both sides do, is the known next step, not an oversight.

Topology as code. Docker Compose builds a world of three simulated machines: a client and server sharing a simulated public internet, and a target machine that lives only on the internal network. Network isolation makes the tunnel the only way in, exactly as for a real remote user. The server turns on ip_forward, letting it pass traffic between networks, and routes between the tunnel’s addresses and the private network. The piece that surprises everyone is on the target: a return route sending replies for tunnel addresses back through the VPN server. Without it, outgoing traffic works perfectly and every reply still dies at the default gateway, the machine’s usual exit.

Result

Four repeatable experiments, each one just command (a small task runner), prove these properties rather than assert them:

ExperimentWhat it proves
just pingEnd to end: a ping (a basic “are you there?” message) crosses two networks with no route between them
just capturetcpdump, a traffic recorder, watches the tunnel (tun0) and the real network card (eth0) at once: the ping is visible on the first and completely invisible on the second, which only sees encrypted TLS bytes
just telnetWorking at the IP level means any traffic: a full interactive TCP session (telnet, a remote terminal) through the tunnel
just breakKill the server and pings die: connectivity truly depends on the tunnel, not a Docker quirk. Restart it and the client rebuilds the tunnel by itself in about 2 seconds

The dual-capture experiment is the one that makes the idea land: the same ping, seen side by side as a readable IP packet inside the tunnel and as scrambled TLS data outside it.

Inside the tunnel tun0

  • ping → target
  • reply ← target

A readable IP packet: who, to whom, what

On the real network eth0

  • ░▒▓ ▒░▓▒ ▓░▒░
  • ▒▓░ ░▓▒░ ▒▓▒▓

Only TLS bytes: the ping itself is invisible

The same ping, recorded on both sides at once (just capture)

Deliberately left on the map for a future expedition: mutual TLS; DTLS, the UDP version of TLS, which would delete the length-prefix layer entirely; multiple clients with a routing table; and IPv6 setup scripts, though the virtual card already passes IPv6 (the newer internet addressing) untouched.