From de330d561a9090b8b9529a31877e6230d3837e87 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Tue, 18 Jun 2013 15:09:12 -0400 Subject: [PATCH] adding a RealIP func --- log-request.go => httplog.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) rename log-request.go => httplog.go (65%) diff --git a/log-request.go b/httplog.go similarity index 65% rename from log-request.go rename to httplog.go index d1d380b..bd1b281 100644 --- a/log-request.go +++ b/httplog.go @@ -13,7 +13,7 @@ func LogRequest(r *http.Request, statusCode int) { var user_agent string user_agent = "" - addr = r.RemoteAddr + addr = RealIP(r) for k, v := range r.Header { if k == "User-Agent" { @@ -33,3 +33,20 @@ func LogRequest(r *http.Request, statusCode int) { statusCode, r.ContentLength) } + +func RealIP(r *http.Request) (ip 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, " ") + } + } + + return ip +}