Skip to content

Instantly share code, notes, and snippets.

@hu553in
Last active November 27, 2020 06:21
Show Gist options
  • Save hu553in/6e74ff63dbf2e22d3d81e4dd564e538f to your computer and use it in GitHub Desktop.
Save hu553in/6e74ff63dbf2e22d3d81e4dd564e538f to your computer and use it in GitHub Desktop.
Dead simple HTTP reverse proxy
// Thanks to: https://siberianlaika.ru/node/29/
package main
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path/filepath"
"time"
)
func main() {
if len(os.Args) != 3 {
fmt.Printf("Usage: %s <src> <dest>\n", filepath.Base(os.Args[0]))
os.Exit(1)
}
src := os.Args[1]
dest := os.Args[2]
destURL, error := url.Parse(dest)
if error != nil {
fmt.Println("Invalid dest (see https://golang.org/pkg/net/url/#URL)")
os.Exit(1)
}
proxy := httputil.NewSingleHostReverseProxy(destURL)
server := &http.Server{
Addr: src,
Handler: proxy,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
fmt.Printf("%s\n", server.ListenAndServe().Error())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment