Skip to content

Instantly share code, notes, and snippets.

@notjrbauer
Created May 23, 2018 21:41
Show Gist options
  • Save notjrbauer/a9de5630c84cc0953badbcdaf81491c4 to your computer and use it in GitHub Desktop.
Save notjrbauer/a9de5630c84cc0953badbcdaf81491c4 to your computer and use it in GitHub Desktop.
Overwrites the host forwarded by NewReverseProxy
package main
import (
"net/http"
"net/http/httputil"
"net/url"
)
func main() {
remote, err := url.Parse("https://www.google.com")
if err != nil {
panic(err)
}
proxyHandler := NewProxyHandler(remote)
http.Handle("/", proxyHandler)
err = http.ListenAndServe(":3022", nil)
if err != nil {
panic(err)
}
}
func NewProxyHandler(target *url.URL) *httputil.ReverseProxy {
director := func(req *http.Request) {
req.Host = target.Host
req.URL.Host = target.Host
req.URL.Scheme = target.Scheme
}
return &httputil.ReverseProxy{Director: director}
}
type ProxyHandler struct {
p *httputil.ReverseProxy
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment