Skip to content

Instantly share code, notes, and snippets.

@mpfund
Last active November 9, 2020 10:26
Show Gist options
  • Save mpfund/b0d06b476a23cc732e61 to your computer and use it in GitHub Desktop.
Save mpfund/b0d06b476a23cc732e61 to your computer and use it in GitHub Desktop.
simple golang http proxy
package main
import (
"fmt"
"io"
"net/http"
)
// A very simple http proxy
func main() {
simpleProxyHandler := http.HandlerFunc(simpleProxyHandlerFunc)
http.ListenAndServe(":8080", simpleProxyHandler)
}
func simpleProxyHandlerFunc(w http.ResponseWriter, r *http.Request) {
httpc := &http.Client{}
// request uri can't be set in client requests
r.RequestURI = ""
if r.Method == "CONNECT" {
fmt.Println(r.URL, " CONNECT / SSL not implemented")
return
}
fmt.Println(r.URL, r.Method)
resp, err := httpc.Do(r)
if err != nil {
fmt.Println(r.URL, " ", err.Error())
return
}
copyHeaders(w.Header(), resp.Header)
// copy content
io.Copy(w, resp.Body)
}
func copyHeaders(dest http.Header, source http.Header) {
for header := range source {
dest.Add(header, source.Get(header))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment