On this plate ProblemApproachResult
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:
- 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.
- 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.
- 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:
| Experiment | What it proves |
|---|---|
just ping | End to end: a ping (a basic “are you there?” message) crosses two networks with no route between them |
just capture | tcpdump, 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 telnet | Working at the IP level means any traffic: a full interactive TCP session (telnet, a remote terminal) through the tunnel |
just break | Kill 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
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.