From 4c4791ee0fafaf4742b690d0198b6aa74560836d Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Sat, 6 Apr 2019 17:02:42 -0600 Subject: [PATCH] fix redir stuff i hope --- conf.yaml | 10 +++++----- server/proxy.go | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/conf.yaml b/conf.yaml index d989266..6f5044c 100644 --- a/conf.yaml +++ b/conf.yaml @@ -1,11 +1,11 @@ p: 54243 r: - echo:http://localhost:49982 -- echo2:http://localhost:49983 -crt: ./testdata/rproxy3server.crt -key: ./testdata/rproxy3server.key -user: bel -pass: bel +- echo2:http://192.168.0.86:38090 +#crt: ./testdata/rproxy3server.crt +#key: ./testdata/rproxy3server.key +#user: bel +#pass: bel rate: 1 burst: 2 timeout: 10 diff --git a/server/proxy.go b/server/proxy.go index e9740d3..3348ec3 100644 --- a/server/proxy.go +++ b/server/proxy.go @@ -9,8 +9,17 @@ import ( "strings" ) +type redirPurge struct { + proxyHost string + targetHost string +} + func (s *Server) Proxy(w http.ResponseWriter, r *http.Request) { newURL, err := s.lookup(r.Host) + transport := &redirPurge{ + proxyHost: r.Host, + targetHost: newURL.Host, + } if err != nil { http.NotFound(w, r) log.Printf("unknown host lookup %q", r.Host) @@ -18,6 +27,7 @@ func (s *Server) Proxy(w http.ResponseWriter, r *http.Request) { } r.Host = newURL.Host proxy := httputil.NewSingleHostReverseProxy(newURL) + proxy.Transport = transport proxy.ServeHTTP(w, r) } @@ -28,3 +38,14 @@ func (s *Server) lookup(host string) (*url.URL, error) { err := s.db.Get(nsRouting, host, v) return v.URL(), err } + +func (rp *redirPurge) RoundTrip(r *http.Request) (*http.Response, error) { + resp, err := http.DefaultTransport.RoundTrip(r) + if err != nil { + return resp, err + } + if loc := resp.Header.Get("Location"); loc != "" { + resp.Header.Set("Location", strings.Replace(loc, rp.targetHost, rp.proxyHost, 1)) + } + return resp, err +}