This commit is contained in:
Jeff Becker 2019-01-19 08:48:35 -05:00
parent 0828fc3df1
commit 4031d38f34
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05
1 changed files with 17 additions and 0 deletions

17
main.go
View File

@ -22,6 +22,14 @@ func transfer(dst io.WriteCloser, src io.ReadCloser) {
io.Copy(dst, src)
}
func copyHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}
func (h *httpProxyHandler) dialOut(addr string) (net.Conn, error) {
host, _, err := net.SplitHostPort(addr)
if err != nil {
@ -56,6 +64,15 @@ func (h *httpProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
go transfer(conn, outConn)
go transfer(outConn, conn)
} else {
resp, err := http.DefaultTransport.RoundTrip(r)
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
defer resp.Body.Close()
copyHeader(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
w.WriteHeader(http.StatusMethodNotAllowed)
}
}