func getIPAdress(r *http.Request) string {
for _, h := range []string{"X-Forwarded-For", "X-Real-Ip"} {
addresses := strings.Split(r.Header.Get(h), ",")
// march from right to left until we get a public address
// that will be the address right before our proxy.
for i := len(addresses) -1 ; i >= 0; i-- {
ip := addresses[i]
// header can contain spaces too, strip those out.
realIP := net.ParseIP(strings.Replace(ip, " ", "", -1))
if !realIP.IsGlobalUnicast() && !isPrivateSubnet(realIP) {
// bad address, go to next
continue
}
return ip
}
}
return ""
}
https://husobee.github.io/golang/ip-address/2015/12/17/remote-ip-go.html mentions:
So the code that he/she suggests is the following:
Thoughts?