Writing a reverse proxy in Go

Bartłomiej Klimczak
developer2.0
Published in
2 min readJan 6, 2020

Some time ago, I found a video called Building a DIY proxy with the net package. I recommend watching it. Filippo Valsorda builds a simple proxy using low-level packages. It’s fun to watch it but I think it’s a bit complicated. In Go, it has to be an easier way so I decided to continue writing series https://developer20.com/categories/GoInPractice/ by writing a simple but yet powerful reverse proxy as fast as it’s possible.

The first step will be to create a proxy for a single host. The core of our code will be https://golang.org/pkg/net/http/httputil/#ReverseProxy which does all the work for us. This is the magic of the rich standard library. The RevewseProxy is a struct for writing reverse proxies :) The only thing we have to do is to configure the director. The director modifies original requests which will be sent to proxied service.

And that’s all! We have a fully functional proxy! Let’s check if it works. For the test, I wrote a simple server that returns the port it’s listening on.

Now, we can run the service which listens on port 8080 and the proxy which listens on port 80.

Our proxy works on HTTP only. To tweak it to support HTTPS we have to make a small change. The thing we need to do is to detect (in a very naive way) if the proxy is running using SSL or not. We’ll detect it based on the port it’s running on.

To make it work, we need one more thing: a valid certificate. You can generate it using https://github.com/FiloSottile/mkcert.

mkcert localhost

And… that’s all! We have a proxy that works on both HTTP/HTTPS.

As you can see, writing the new tool was extremely simple and all we need is the standard library. We didn’t have to write complicated code so we’ll can focus on our real goals. As usual, the source code is available on Github: https://github.com/bkielbasa/go-proxy. This project will be a starting point for other tools so keep in touch and don’t miss a post! If you have any questions, let me know in the comments section below.

Buy me a coffee

Originally published at https://developer20.com.

--

--

Bartłomiej Klimczak
developer2.0

I'M a backend engineer, blogger, speaker and open-source developer :)