Created
May 23, 2018 21:41
-
-
Save notjrbauer/a9de5630c84cc0953badbcdaf81491c4 to your computer and use it in GitHub Desktop.
Overwrites the host forwarded by NewReverseProxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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