From becd5526f5dcce33a513c4c0ef2c70c761ca66e0 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Mon, 28 Sep 2015 17:03:07 -0400 Subject: [PATCH] X-Forward-For can return a list --- httplog.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/httplog.go b/httplog.go index 7f77913..795fe10 100644 --- a/httplog.go +++ b/httplog.go @@ -8,7 +8,7 @@ import ( ) var ( - /* This default icon is empty with a long lived cache */ + /* This default icon is empty with a long lived cache */ DefaultFavIcon FavIcon = defaultFavIcon{} ) @@ -63,19 +63,27 @@ func LogRequest(r *http.Request, statusCode int) { r.ContentLength) } -func RealIP(r *http.Request) (ip string) { - ip = r.RemoteAddr +func RealIP(r *http.Request) string { + rip := RealIPs(r) + return rip[len(rip)] +} + +func RealIPs(r *http.Request) (ips []string) { + ip := r.RemoteAddr port_pos := strings.LastIndex(ip, ":") if port_pos != -1 { ip = ip[0:port_pos] } - - for k, v := range r.Header { - if k == "X-Forwarded-For" { - ip = strings.Join(v, " ") - } + if ip != "" { + ips = append(ips, ip) } - return ip + val := r.Header.Get("X-Forwarded-For") + if val != "" { + for _, ip := range strings.Split(val, ", ") { + ips = append(ips, ip) + } + } + return ips }