Senin, 28 Juli 2025

Understanding Proxies: What, Why, and How to Build One in Go

| Senin, 28 Juli 2025

Hi there! I'm Maneshwar. Right now, I’m building LiveAPI, a first-of-its-kind tool that helps you automatically index API endpoints across all your repositories. LiveAPI makes it easier to discover, understand, and interact with APIs in large infrastructures.

When you hear “proxy,” you might think of anonymity tools or corporate firewalls. But proxies are used everywhere: in load balancers, reverse gateways, API middleware, and more.

This post will explain:

  • What a proxy is
  • How it works
  • Why we use it
  • And how to write a basic HTTP proxy in Go

What is a Proxy?

A proxy is an intermediary between a client and a server.

Instead of the client talking directly to the server, it talks to the proxy, and the proxy forwards the request to the actual server. The server responds to the proxy, and the proxy sends the response back to the client.

Client <--> Proxy <--> Server

There are two common types:

  • Forward Proxy: Used by clients to access outside resources (e.g., web proxy to bypass firewall)
  • Reverse Proxy: Sits in front of servers, commonly used in load balancers or microservices (e.g., NGINX reverse proxying APIs)

Why Use a Proxy?

1. Load Balancing
Distribute incoming traffic across multiple servers.

2. Caching
Cache responses to reduce load on backend servers.

3. Authentication
Handle auth at proxy layer before hitting internal services.

4. Logging & Monitoring
Track traffic without modifying backend services.

5. Security & Anonymity
Hide internal architecture, block malicious requests.

How Does It Work?

Example:

  1. Browser sends a GET request to proxy.local
  2. Proxy receives it, modifies if needed, and forwards it to target.com
  3. Proxy gets the response from target.com
  4. Proxy sends that response back to the browser

The proxy behaves as a middleman – potentially modifying, logging, or filtering requests/responses.

Writing a Basic HTTP Proxy in Go

Here’s a minimal forward proxy in Go using net/http and httputil.

package main

import (
    "log"
    "net/http"
    "net/http/httputil"
    "net/url"
)

func main() {
    target, _ := url.Parse("https://example.com")

    proxy := httputil.NewSingleHostReverseProxy(target)

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        log.Println("Proxying request:", r.URL.Path)
        proxy.ServeHTTP(w, r)
    })

    log.Println("Starting proxy on :8080")
    log.Fatal(http.ListenAndServe(":8080", nil))
}

Try It Out

  1. Run the Go app
  2. Visit http://localhost:8080/ in browser
  3. It proxies to https://example.com

You can change the target URL to anything you want.

Full Proxy (Client-Defined)

For a more dynamic proxy (like a browser setting a proxy and targeting any site), you can use a custom http.Transport and forward requests manually — useful for tools like mitmproxy.

Use Cases in Real World

  • NGINX: Reverse proxy for apps
  • Squid: Forward proxy for networks
  • Envoy, Traefik: Smart service proxies
  • Go custom proxies: Debuggers, scrapers, auth filters

Summary

Feature Forward Proxy Reverse Proxy
Who uses it? Client Server
Used for Access control, anonymity Load balancing, caching
Common tools Squid, Burp NGINX, Envoy

Building one in Go is just a few lines of code using httputil.

TL;DR

  • A proxy forwards HTTP requests between client and server
  • Why use it? Caching, security, load balancing, logging
  • In Go, use httputil.NewSingleHostReverseProxy to set one up quickly

LiveAPI helps you get all your backend APIs documented in a few minutes.

With LiveAPI, you can generate interactive API docs that allow users to search and execute endpoints directly from the browser.

LiveAPI Demo

If you're tired of updating Swagger manually or syncing Postman collections, give it a shot.


Related Posts

Tidak ada komentar:

Posting Komentar