Skip to content

Commit

Permalink
Merge pull request #41 from delta10/feat/add-forwarded-for-headers
Browse files Browse the repository at this point in the history
Add X-Forwarded-* headers
  • Loading branch information
bartjkdp authored Mar 10, 2024
2 parents a361adb + 83b57ec commit 606fe5a
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions cmd/filter-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func main() {
}

utils.DelHopHeaders(r.Header)
addForwardedForHeaders(r, r)

client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
Expand Down Expand Up @@ -182,8 +183,6 @@ func main() {
return
}

log.Printf("%+v", backendRequest)

backendRequest.Header.Set("Content-Type", "application/json")
} else {
backendRequest, err = http.NewRequest(r.Method, fullBackendURL.String(), nil)
Expand Down Expand Up @@ -233,6 +232,8 @@ func main() {
backendRequest.Header.Set(headerKey, parsedHeaderValue)
}

addForwardedForHeaders(backendRequest, r)

client := &http.Client{
Timeout: 25 * time.Second,
Transport: transport,
Expand Down Expand Up @@ -312,12 +313,13 @@ func main() {

s := &http.Server{
Addr: config.ListenAddress,
Handler: httpHandler,
Handler: requestLoggingMiddleware(httpHandler),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}

log.Printf("listening on %v", config.ListenAddress)
if config.ListenTLS.Certificate != "" && config.ListenTLS.Key != "" {
log.Fatal(s.ListenAndServeTLS(config.ListenTLS.Certificate, config.ListenTLS.Key))
} else {
Expand Down Expand Up @@ -391,7 +393,7 @@ func authorizeRequestWithService(config *config.Config, backend config.Backend,

authorizationBody["params"] = params
} else if backend.Type != "" {
log.Printf("unsupported backend type configured: %s")
log.Printf("unsupported backend type configured: %s", backend.Type)
return http.StatusInternalServerError, nil
}

Expand All @@ -415,7 +417,7 @@ func authorizeRequestWithService(config *config.Config, backend config.Backend,
request.Header.Set("Authorization", r.Header.Get("Authorization"))
}

request.Header.Set("X-Forwarded-For", utils.ReadUserIP(r))
addForwardedForHeaders(request, r)

client := &http.Client{
Timeout: 25 * time.Second,
Expand Down Expand Up @@ -461,3 +463,29 @@ func writeError(w http.ResponseWriter, statusCode int, message string) {
w.Header().Set("Content-Type", "application/json")
w.Write(jsonResp)
}

func addForwardedForHeaders(backendRequest *http.Request, originalRequest *http.Request) {
backendRequest.Header.Set("X-Forwarded-Host", originalRequest.Host)
backendRequest.Header.Set("X-Forwarded-For", utils.ReadUserIP(originalRequest))

if originalRequest.TLS == nil {
backendRequest.Header.Set("X-Forwarded-Proto", "http")
} else {
backendRequest.Header.Set("X-Forwarded-Proto", "https")
}
}

func requestLoggingMiddleware(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
log.Printf(
"%s %s %s",
r.Method,
r.URL.Path,
r.Header.Get("User-Agent"),
)

next.ServeHTTP(w, r)
}

return http.HandlerFunc(fn)
}

0 comments on commit 606fe5a

Please sign in to comment.