Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support X-Forwarded-For in logging when behind a proxy #233

Open
systemmonkey42 opened this issue May 11, 2023 · 1 comment
Open

Support X-Forwarded-For in logging when behind a proxy #233

systemmonkey42 opened this issue May 11, 2023 · 1 comment

Comments

@systemmonkey42
Copy link

Hi,

By default, the logging in rest-server will always log the IP address of the connection, which in many cases will be the nearest proxy.

Adding support for the X-Forwarded-For headers will allow the logging to display the correct external IP.

Currently 'gorilla/handlers' is used for logging. 'gorilla/handlers' fully supports decoding the X-Forwarded-For headers if
you add the proxyHeaders middleware before the logging middleware.

I'm currently using the following patch (against master) to implemented the additional middleware:

diff --git mux.go mux.go
index 77fcdb4..294708e 100644
--- mux.go
+++ mux.go
@@ -21,6 +21,10 @@ func (s *Server) debugHandler(next http.Handler) http.Handler {
        })
 }

+func (s *Server) proxyHandler(next http.Handler) http.Handler {
+   return handlers.ProxyHeaders(next)
+}
+
 func (s *Server) logHandler(next http.Handler) http.Handler {
    var accessLog io.Writer

@@ -104,6 +108,9 @@ func NewHandler(server *Server) (http.Handler, error) {
    if server.Debug {
        handler = server.debugHandler(handler)
    }
+
+   handler = server.proxyHandler(handler)
+
    if server.Log != "" {
        handler = server.logHandler(handler)
    }

As a result, my logs now show the correct external IP, instead of the IP address of my proxy.

Any thoughts?

@MichaelEischer
Copy link
Member

Sounds useful. Interpreting the X-Forwarded-For should be opt-in (via a CLI option) as not everyone uses a proxy in front of rest-server.

Are you willing to open a PR for that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants