Browse Source

Added proxy-pass-host header

master v2.2.0
Darshil Chanpura 2 months ago
parent
commit
5ce237cb89
  1. 3
      cmd/dumb-http/main.go
  2. 3
      cmd/dumb-proxy/main.go
  3. 16
      proxy/proxy.go

3
cmd/dumb-http/main.go

@ -20,6 +20,7 @@ var (
upstream = flag.String("proxy-upstream", "", "Reverse Proxy upstream (backend service). Proxy is disabled if empty")
proxyPath = flag.String("proxy-path", "/proxy/", "Reverse Proxy path")
stripPrefix = flag.Bool("proxy-strip-prefix", false, "Strip Prefix while proxying")
proxyHost = flag.String("proxy-pass-host", "", "Set the value as Host Header")
)
func main() {
@ -31,7 +32,7 @@ func main() {
log.Fatal(err)
}
// initilize proxy routes and static server routes
p.Routes(*proxyPath, *stripPrefix)
p.Routes(*proxyPath, *stripPrefix, *proxyHost)
addr := net.JoinHostPort(*host, fmt.Sprint(*port))

3
cmd/dumb-proxy/main.go

@ -18,6 +18,7 @@ var (
upstream = flag.String("upstream", "http://127.0.0.1:8081", "Reverse Proxy upstream (backend service)")
proxyPath = flag.String("proxy-path", "/proxy/", "Reverse Proxy path")
stripPrefix = flag.Bool("strip-prefix", false, "Strip Prefix while proxying")
proxyHost = flag.String("proxy-host", "", "Override Host while proxying")
)
func main() {
@ -26,7 +27,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
p.Routes(*proxyPath, *stripPrefix)
p.Routes(*proxyPath, *stripPrefix, *proxyHost)
fmt.Printf("Serving with dumb-proxy/%s at http://%s \n", version, *addr)
fmt.Printf(" Root Configuration: http://%s/ -> %q\n", *addr, *root)
if *stripPrefix {

16
proxy/proxy.go

@ -39,15 +39,15 @@ func New(upstream, root string) (*Proxy, error) {
// Routes will set routes based on the values of Proxy.rp and
// by default set / to file server.
func (p *Proxy) Routes(proxyPath string, stripPrefix bool) {
func (p *Proxy) Routes(proxyPath string, stripPrefix bool, host string) {
if p.rp == nil {
p.mux.Handle("/", http.FileServer(http.Dir(p.root)))
return
}
if stripPrefix {
p.mux.Handle(proxyPath, http.StripPrefix(proxyPath, p.rp))
p.mux.Handle(proxyPath, http.StripPrefix(proxyPath, p.handler(host)))
} else {
p.mux.Handle(proxyPath, p.rp)
p.mux.Handle(proxyPath, p.handler(host))
}
p.mux.Handle("/", http.FileServer(http.Dir(p.root)))
}
@ -55,3 +55,13 @@ func (p *Proxy) Routes(proxyPath string, stripPrefix bool) {
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p.mux.ServeHTTP(w, r)
}
func (p *Proxy) handler(host string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if host != "" {
r.Host = host
r.Header.Set("Host", host)
}
p.rp.ServeHTTP(w, r)
}
}

Loading…
Cancel
Save