diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2022-08-09 15:23:03 -0600 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2022-08-09 15:23:03 -0600 |
commit | 7f6d7366e3b7bb9bceb01bdd658b0210800f479d (patch) | |
tree | fe850377bb921dc561a5eae717454be25c034953 /srv/src | |
parent | fa47baffb6d665e0bd100b8f35f81c324d1b0d0d (diff) |
improve logging of ips, to account for xff and x-real-ip
Diffstat (limited to 'srv/src')
-rw-r--r-- | srv/src/http/middleware.go | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/srv/src/http/middleware.go b/srv/src/http/middleware.go index 943bc05..b82fc29 100644 --- a/srv/src/http/middleware.go +++ b/srv/src/http/middleware.go @@ -40,15 +40,24 @@ func setLoggerMiddleware(logger *mlog.Logger) middleware { type logCtxKey string - ip, _, _ := net.SplitHostPort(r.RemoteAddr) - ctx := r.Context() ctx = mctx.Annotate(ctx, - logCtxKey("remote_ip"), ip, logCtxKey("url"), r.URL, logCtxKey("method"), r.Method, ) + if xff := r.Header.Get("X-Forwarded-For"); xff != "" { + ctx = mctx.Annotate(ctx, logCtxKey("x_forwarded_for"), xff) + } + + if xrip := r.Header.Get("X-Real-IP"); xrip != "" { + ctx = mctx.Annotate(ctx, logCtxKey("x_real_ip"), xrip) + } + + if ip, _, _ := net.SplitHostPort(r.RemoteAddr); ip != "" { + ctx = mctx.Annotate(ctx, logCtxKey("remote_ip"), ip) + } + r = r.WithContext(ctx) r = apiutil.SetRequestLogger(r, logger) h.ServeHTTP(rw, r) |