parent
ae56c1744e
commit
4c4791ee0f
10
conf.yaml
10
conf.yaml
|
|
@ -1,11 +1,11 @@
|
||||||
p: 54243
|
p: 54243
|
||||||
r:
|
r:
|
||||||
- echo:http://localhost:49982
|
- echo:http://localhost:49982
|
||||||
- echo2:http://localhost:49983
|
- echo2:http://192.168.0.86:38090
|
||||||
crt: ./testdata/rproxy3server.crt
|
#crt: ./testdata/rproxy3server.crt
|
||||||
key: ./testdata/rproxy3server.key
|
#key: ./testdata/rproxy3server.key
|
||||||
user: bel
|
#user: bel
|
||||||
pass: bel
|
#pass: bel
|
||||||
rate: 1
|
rate: 1
|
||||||
burst: 2
|
burst: 2
|
||||||
timeout: 10
|
timeout: 10
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,17 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type redirPurge struct {
|
||||||
|
proxyHost string
|
||||||
|
targetHost string
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) Proxy(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) Proxy(w http.ResponseWriter, r *http.Request) {
|
||||||
newURL, err := s.lookup(r.Host)
|
newURL, err := s.lookup(r.Host)
|
||||||
|
transport := &redirPurge{
|
||||||
|
proxyHost: r.Host,
|
||||||
|
targetHost: newURL.Host,
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
log.Printf("unknown host lookup %q", r.Host)
|
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
|
r.Host = newURL.Host
|
||||||
proxy := httputil.NewSingleHostReverseProxy(newURL)
|
proxy := httputil.NewSingleHostReverseProxy(newURL)
|
||||||
|
proxy.Transport = transport
|
||||||
proxy.ServeHTTP(w, r)
|
proxy.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -28,3 +38,14 @@ func (s *Server) lookup(host string) (*url.URL, error) {
|
||||||
err := s.db.Get(nsRouting, host, v)
|
err := s.db.Get(nsRouting, host, v)
|
||||||
return v.URL(), err
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue