Skip to content

Commit

Permalink
Updated IP Whitelisting to be port-conscious as RemoteAddr has no spe…
Browse files Browse the repository at this point in the history
…cific format, but we need to look for IPv4 and IPv6 addresses
  • Loading branch information
Martin Buhr committed Nov 25, 2014
1 parent e60c1db commit 8c419bd
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion middleware_ip_whitelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net/http"
"net"
"strings"
)


Expand Down Expand Up @@ -34,7 +35,14 @@ func (i *IPWhiteListMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Re
// Enabled, check incoming IP address
for _, ip := range(ipConfig.AllowedIPs) {
allowedIP := net.ParseIP(ip)
remoteIP = net.ParseIP(r.RemoteAddr)
splitIP := strings.Split(r.RemoteAddr, ":")
remoteIPString := splitIP[0]
if len(splitIP) > 2 {
// Might be an IPv6 address, don't mess with it
remoteIPString = r.RemoteAddr
}
remoteIP = net.ParseIP(remoteIPString)

// We parse the IP to manage IPv4 and IPv6 easily
if allowedIP.String() == remoteIP.String() {
// matched, pass through
Expand Down

0 comments on commit 8c419bd

Please sign in to comment.