From 7f6d7366e3b7bb9bceb01bdd658b0210800f479d Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Tue, 9 Aug 2022 15:23:03 -0600 Subject: improve logging of ips, to account for xff and x-real-ip --- srv/src/http/middleware.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'srv') 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) -- cgit v1.2.3