From 1297773012279f76319fd1fb89352d33c01e21c1 Mon Sep 17 00:00:00 2001 From: Quentin Machu Date: Thu, 10 Mar 2016 19:18:21 -0500 Subject: [PATCH] Add reverse proxy capability --- proxy.go | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/proxy.go b/proxy.go index e4ed0600..4901f672 100644 --- a/proxy.go +++ b/proxy.go @@ -19,6 +19,7 @@ type ProxyHttpServer struct { // setting Verbose to true will log information on each request sent to the proxy Verbose bool Logger *log.Logger + isReverseProxy bool NonproxyHandler http.Handler reqHandlers []ReqHandler respHandlers []RespHandler @@ -32,7 +33,7 @@ type ProxyHttpServer struct { var hasPort = regexp.MustCompile(`:\d+$`) func copyHeaders(dst, src http.Header) { - for k, _ := range dst { + for k := range dst { dst.Del(k) } for k, vs := range src { @@ -101,20 +102,26 @@ func (proxy *ProxyHttpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) var err error ctx.Logf("Got request %v %v %v %v", r.URL.Path, r.Host, r.Method, r.URL.String()) - if !r.URL.IsAbs() { + + if (proxy.isReverseProxy && r.URL.IsAbs()) || (!proxy.isReverseProxy && !r.URL.IsAbs()) { proxy.NonproxyHandler.ServeHTTP(w, r) return } + r, resp := proxy.filterRequest(r, ctx) if resp == nil { + if proxy.isReverseProxy && (r.URL.Scheme == "" || r.URL.Host == "") { + panic("ReverseProxy did not rewrite request's Scheme or Host") + } + removeProxyHeaders(ctx, r) resp, err = ctx.RoundTrip(r) if err != nil { ctx.Error = err resp = proxy.filterResponse(nil, ctx) if resp == nil { - ctx.Logf("error read response %v %v:", r.URL.Host, err.Error()) + ctx.Logf("Error read response %v %v:", r.URL.Host, err.Error()) http.Error(w, err.Error(), 500) return } @@ -152,11 +159,26 @@ func NewProxyHttpServer() *ProxyHttpServer { respHandlers: []RespHandler{}, httpsHandlers: []HttpsHandler{}, NonproxyHandler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - http.Error(w, "This is a proxy server. Does not respond to non-proxy requests.", 500) + http.Error(w, "This is a forward proxy server. Does not respond to non-proxy requests.", 500) }), - Tr: &http.Transport{TLSClientConfig: tlsClientSkipVerify, - Proxy: http.ProxyFromEnvironment}, + Tr: &http.Transport{ + TLSClientConfig: tlsClientSkipVerify, + Proxy: http.ProxyFromEnvironment, + }, } + proxy.ConnectDial = dialerFromEnv(&proxy) + return &proxy } + +func NewReverseProxyHttpServer() *ProxyHttpServer { + reverseProxy := NewProxyHttpServer() + + reverseProxy.isReverseProxy = true + reverseProxy.NonproxyHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + http.Error(w, "This is a reverse proxy server. Does not respond to proxy requests.", 500) + }) + + return reverseProxy +}