From 3f4f4cf03157e1903e0c4e9248760182c59c23fd Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Sat, 20 Aug 2022 08:01:54 +0300 Subject: [PATCH] [#282] reversed the X-Forwarded-For ips iteration --- apis/middlewares.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/apis/middlewares.go b/apis/middlewares.go index fcd097d6..06c6cb7b 100644 --- a/apis/middlewares.go +++ b/apis/middlewares.go @@ -317,9 +317,10 @@ func realUserIp(r *http.Request, fallbackIp string) string { if ipsList := r.Header.Get("X-Forwarded-For"); ipsList != "" { ips := strings.Split(ipsList, ",") // extract the rightmost ip - for _, ip := range ips { - if trimmedIp := strings.TrimSpace(ip); trimmedIp != "" { - return trimmedIp + for i := len(ips) - 1; i >= 0; i-- { + ip := strings.TrimSpace(ips[i]) + if ip != "" { + return ip } } }