Log invalid remote IPs

This commit is contained in:
Richard 2015-03-25 13:44:16 -07:00
parent e21a425f88
commit 78562258b2

View file

@ -9,6 +9,7 @@ import (
"time" "time"
"code.google.com/p/go-uuid/uuid" "code.google.com/p/go-uuid/uuid"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
@ -18,6 +19,14 @@ var (
ErrNoRequestContext = errors.New("no http request in context") ErrNoRequestContext = errors.New("no http request in context")
) )
func parseIP(ipStr string) net.IP {
ip := net.ParseIP(ipStr)
if ip == nil {
log.Warnf("invalid remote IP address: %q", ipStr)
}
return ip
}
// RemoteAddr extracts the remote address of the request, taking into // RemoteAddr extracts the remote address of the request, taking into
// account proxy headers. // account proxy headers.
func RemoteAddr(r *http.Request) string { func RemoteAddr(r *http.Request) string {
@ -25,7 +34,7 @@ func RemoteAddr(r *http.Request) string {
proxies := strings.Split(prior, ",") proxies := strings.Split(prior, ",")
if len(proxies) > 0 { if len(proxies) > 0 {
remoteAddr := strings.Trim(proxies[0], " ") remoteAddr := strings.Trim(proxies[0], " ")
if net.ParseIP(remoteAddr) != nil { if parseIP(remoteAddr) != nil {
return remoteAddr return remoteAddr
} }
} }
@ -33,7 +42,7 @@ func RemoteAddr(r *http.Request) string {
// X-Real-Ip is less supported, but worth checking in the // X-Real-Ip is less supported, but worth checking in the
// absence of X-Forwarded-For // absence of X-Forwarded-For
if realIP := r.Header.Get("X-Real-Ip"); realIP != "" { if realIP := r.Header.Get("X-Real-Ip"); realIP != "" {
if net.ParseIP(realIP) != nil { if parseIP(realIP) != nil {
return realIP return realIP
} }
} }