Field Notes · Side Projects
Building a Tunnel I Didn't Need
I have used ngrok a lot, mostly for testing: a webhook that needs a public URL, a local build someone else needs to see. It works, I like it, and I have never had a real complaint about it. At some point I noticed I was relying on a tool I could not explain. A request hits a public URL and arrives at a port on my laptop, and I had no clear idea what happened in between.
So I built one. It is called GoTunnel, and it is a learning project, not a product. I did not build it to compete with anything. I built it for myself, and anyone else who wants to use it can.
This is not a feature list. It is what happened, roughly in order, and what each part taught me.
Why Go
I wanted something with real networking and real concurrency in it, not another CRUD app. That narrowed the language choice quickly.
A tunnel is mostly many streams running at once over a few connections, and goroutines and channels are a natural way to write that. The client also had to be a single binary that runs on macOS, Linux and Windows with nothing to install, and Go cross-compiles to exactly that. I considered Rust, but I suspected its learning curve would have become the project, and I wanted the project to be the networking. A JVM felt like a lot to ship for a small CLI. Go sat in the middle.
The first version, and the first humbling
The first versions came together in October 2026. The shape is simple to describe. You run gotunnel http 3000, the client opens a connection to the server, and you get a public https URL that forwards to your port. A local inspector on 127.0.0.1:4040 shows the requests going through, lets you replay one or copy it as cURL, and keeps its history only in memory.
Underneath is a simple binary protocol: a 10-byte frame header, many streams multiplexed over one connection, and a state machine that moves from handshake to auth to forwarding. Heartbeats notice dead connections, the client reconnects with backoff, and it can run over TLS or WebSocket on port 443 so it gets through networks that only allow web traffic.
It worked, and I thought it was in decent shape. Then I ran a fuzzer against the handshake for the first time.
It found a crash. A crafted handshake triggered an integer overflow, and the server went down. That was humbling. The code handled every input I had thought of, and the fuzzer did not care what I had thought of. It just tried the inputs I hadn't.
That started a round of hardening I should have done from the beginning: limits on frame sizes, deadlines on reads and writes so a slow or silent peer can't hold resources forever, lockout after repeated failed auth, and mTLS. None of it is exciting. All of it exists because a parser that trusts its input is a parser anyone can crash.
Raw TCP has no hostname
HTTP tunnels have a convenient property: every request says where it is going. The Host header tells the server which tunnel a request belongs to, so thousands of tunnels can share port 443 and be routed by subdomain.
When I added gotunnel tcp, for sharing a database, SSH or any other TCP service, that convenience disappeared. A raw TCP connection carries no hostname. It arrives at an IP and a port, and that is all the server knows.
I looked at how other systems get around this. Managed databases like AWS RDS give each instance its own hostname, but that works because every instance gets its own IP, which a single shared server does not have. Postgres can be routed by TLS SNI, because the client sends the hostname when it starts TLS. MySQL can't, because in MySQL the server speaks first, so there is nothing to route on before you have already picked a backend. Any scheme that depends on the protocol stops working the moment someone tunnels something other than Postgres.
So I landed where ngrok did: one public port per TCP tunnel. It is the least clever option and the only one that works for every protocol.
I also rejected a client-side connector, a small program the other person would run to reach the tunnel. That would have solved routing, but it would break the main point of the feature. The person connecting should use the tools they already have:
gotunnel tcp 5432
On the other end it is plain psql, mysql or ssh, pointed at the host and port the tunnel prints. Nothing to install.
Locked by default
A public port forwarding to someone's database worried me more than anything else in the project. Open ports get found by scanners, and I did not want to be the reason someone's dev database ended up on the internet.
So TCP tunnels are locked by default. Nobody can connect unless their IP is on an allowlist the owner passes with --allowlist, or they request access through a link and verify their email with a one-time code. The owner sees those approvals as they happen. On the server, nftables only opens a port to approved (IP, port) pairs, so to anyone else the port does not appear to exist. A scanner gets nothing back.
Around that I added the things you only think about once you imagine it being misused: an audit log that stores metadata only and keeps it for 90 days, a gotunnel admin command for listing sessions, killing them and banning devices over a local unix socket, and abuse alert emails that include ready-to-run kill and ban commands. When an alert arrives, the response is a copy and paste, not an investigation.
The free hosted server also has fair-use limits: 30-minute sessions, a daily session limit and one tunnel per device. Devices register with a one-time code sent by email and get HMAC-signed tokens bound to that device. You can also self-host the same binary and skip all of that.
Security on a side project isn't about being a target. It's about not becoming the reason someone else is.
The bugs that tests found
I write tests before the feature they cover. For TCP, those tests turned up two bugs before the feature was finished, and the more serious one was not in new code.
The first could take down every user at once. When a visitor disconnected in the middle of a transfer, the server could try to write to a channel that another goroutine was closing. In Go, writing to a closed channel panics, and that panic took the whole server with it. Tests caught it before it happened in production. The fix was to stop treating two different events as one: "the peer has closed its side" is a normal ending, where you finish what is in flight and wind down, and "abort" means stop now. Once those had separate paths, the race went away.
The second was quieter. If a local service answered and closed its connection immediately, the reply could be lost. The code used a select waiting on both "data arrived" and "connection closed", and when both are ready at once, Go picks one at random. Sometimes it picked "closed" first and dropped the data. That code was older than the TCP feature. It had been wrong the whole time, and nothing had made it obvious until the TCP tests started closing connections aggressively. The lesson I took is that when the order of events matters, it has to be written down in the code, not left to the scheduler.
A smaller one: admin kill could hang if the client on the other end was stuck. It now delivers the kill message with a timeout and moves on.
The server is part of the program
Running it on a real server taught me things the code never could.
- The sandbox blocked my firewall. I had hardened the systemd unit, including
RestrictAddressFamilies. That also blocked netlink, whichnftneeds to talk to the kernel, so the server could not open ports for approved visitors. A hardening setting broke a security feature. - The firewall almost broke everything else. Enabling
nftables.servicewould have flushed the existing UFW and Docker rules on a server other things run on. I caught it before it happened, and it changed how I approach shared machines: check what owns the firewall before touching it. - certbot broke every tunnel. Running
certbot --nginxfor a new site installed the new certificate into the wildcard server block that serves the HTTP tunnels. For a short time TLS was broken for every tunnel. As far as I know only I noticed, but it was still an outage. Now I usecertbot certonlyand give each site its own server block, so a certificate for one thing can't land on another.
None of these were Go bugs, and none would have shown up in a test suite. They came from the code meeting a real machine with other things already on it.
What I'd do differently
Test how connections end, from the first day. The lost-reply bug lived in older code because no test closed connections the way the TCP tests later did. Testing the happy path first is not enough when most of the bugs live in the endings.
Fuzz and set limits before the first release. Frame limits, deadlines and fuzzing came after the first versions. A network-facing parser should start with them.
Design for abuse at the start. Locked-by-default, the audit log and the admin tooling all came later than they should have. Anything that opens a port to the internet needs to answer "what happens when someone misuses this?" before it answers anything else.
Where it is now
GoTunnel runs on a free hosted server at gotunnel.bakaredev.site, and the docs are at docs.gotunnel.bakaredev.site. For my own use, I'm switching to GoTunnel. I built it to share local services, and that is what I need a tunnel for. I plan to keep working on it: security improvements, features I find I need, and possibly things a friend using it with me asks for.
I still like ngrok. It does a lot more than share a local port, with things like its AI gateway and API gateway, and GoTunnel isn't trying to be any of that. It is the part I wanted to understand, built so that I do.
Also in the notebook
Do You Use Kafka, or Just Pay for It?
Most teams reach for a distributed log and then use it as a work queue. Here are the questions that tell you which one you actually have, before the invoice does.
SOLID Is One Idea Wearing Five Hats
Five principles, memorised as five rules, applied as five ceremonies. They were never five things. They are one question asked from five angles: what happens here when the requirements change?