restic / restic/rest-server

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

Open
#233 1 comment 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
1.5k
Forks
178
Avg merge
15d 9h
Merged PRs (30d)
2

Description

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?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in mux.go, reading NewHandler and the existing debugHandler and logHandler middleware, then inspect how gorilla/handlers is used for logging. Verify the middleware ordering and confirm that logs show the external client IP from X-Forwarded-For when the server is behind a proxy.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend, networking
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.