From 3e85d0148bd89c62e24b349169a99af8fdecc810 Mon Sep 17 00:00:00 2001 From: Marvin Frick Date: Mon, 20 Nov 2017 20:31:06 +0100 Subject: [PATCH] Fixes setting of X-Forwarded-For-Proto The downstream request 'X-Forwarded-For' headers should be set to whatever the proxy (devd in our case) is running on. Also do not overwrite or limit the ability to proxy to any schema, independent of what the proxy in serving. --- cmd/devd/devd.go | 8 ++++++++ reverseproxy/reverseproxy.go | 2 +- server.go | 4 +++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/cmd/devd/devd.go b/cmd/devd/devd.go index b38fd05..72c82a9 100644 --- a/cmd/devd/devd.go +++ b/cmd/devd/devd.go @@ -187,11 +187,19 @@ func main() { hdrs.Set("Access-Control-Allow-Origin", "*") } + var servingScheme string + if *tls { + servingScheme = "https" + } else { + servingScheme = "http" + } + dd := devd.Devd{ // Shaping Latency: *latency, DownKbps: *downKbps, UpKbps: *upKbps, + ServingScheme: servingScheme, AddHeaders: &hdrs, diff --git a/reverseproxy/reverseproxy.go b/reverseproxy/reverseproxy.go index ce616ed..f38235d 100644 --- a/reverseproxy/reverseproxy.go +++ b/reverseproxy/reverseproxy.go @@ -71,7 +71,6 @@ func singleJoiningSlash(a, b string) string { func NewSingleHostReverseProxy(target *url.URL, ci inject.CopyInject) *ReverseProxy { targetQuery := target.RawQuery director := func(req *http.Request) { - req.URL.Scheme = target.Scheme req.URL.Host = target.Host req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path) if req.Header.Get("X-Forwarded-Host") == "" { @@ -80,6 +79,7 @@ func NewSingleHostReverseProxy(target *url.URL, ci inject.CopyInject) *ReversePr if req.Header.Get("X-Forwarded-Proto") == "" { req.Header.Set("X-Forwarded-Proto", req.URL.Scheme) } + req.URL.Scheme = target.Scheme // Set "identity"-only content encoding, in order for injector to // work on text response diff --git a/server.go b/server.go index 7b5a049..92b47e5 100644 --- a/server.go +++ b/server.go @@ -139,6 +139,7 @@ type Devd struct { Latency int DownKbps uint UpKbps uint + ServingScheme string // Add headers AddHeaders *http.Header @@ -163,6 +164,7 @@ type Devd struct { // logging, latency, and so forth. func (dd *Devd) WrapHandler(log termlog.TermLog, next httpctx.Handler) http.Handler { h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + r.URL.Scheme = dd.ServingScheme revertOriginalHost(r) timr := timer.Timer{} sublog := log.Group() @@ -177,7 +179,7 @@ func (dd *Devd) WrapHandler(log termlog.TermLog, next httpctx.Handler) http.Hand timr.RequestHeaders() time.Sleep(time.Millisecond * time.Duration(dd.Latency)) - dpath := r.URL.String() + dpath := r.RequestURI if !strings.HasPrefix(dpath, "/") { dpath = "/" + dpath }