Skip to content

Commit

Permalink
Fixes setting of X-Forwarded-For-Proto
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Marvin Frick committed Nov 20, 2017
1 parent 91d564b commit 3e85d01
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
8 changes: 8 additions & 0 deletions cmd/devd/devd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
2 changes: 1 addition & 1 deletion reverseproxy/reverseproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") == "" {
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type Devd struct {
Latency int
DownKbps uint
UpKbps uint
ServingScheme string

// Add headers
AddHeaders *http.Header
Expand All @@ -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()
Expand All @@ -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
}
Expand Down

0 comments on commit 3e85d01

Please sign in to comment.