Refactor determining the remote IP

This commit is contained in:
Justin Li 2013-12-05 20:37:15 -05:00
parent 2b7d5b5bf0
commit 4406eab97a

View file

@ -260,21 +260,16 @@ func requestedPeerCount(fallback int, pq *parsedQuery) int {
}
func requestedIP(r *http.Request, pq *parsedQuery) (string, error) {
ip, ok := pq.Params["ip"]
ipv4, okv4 := pq.Params["ipv4"]
xRealIPs, xRealOk := pq.Params["X-Real-Ip"]
switch {
case ok:
if ip, ok := pq.Params["ip"]; ok {
return ip, nil
case okv4:
return ipv4, nil
case xRealOk && len(xRealIPs) > 0:
}
if ip, ok := pq.Params["ipv4"]; ok {
return ip, nil
}
if xRealIPs, ok := pq.Params["X-Real-Ip"]; ok {
return string(xRealIPs[0]), nil
}
default:
portIndex := len(r.RemoteAddr) - 1
for ; portIndex >= 0; portIndex-- {
if r.RemoteAddr[portIndex] == ':' {
@ -286,7 +281,6 @@ func requestedIP(r *http.Request, pq *parsedQuery) (string, error) {
}
return "", errors.New("Failed to parse IP address")
}
}
func minInt(a, b int) int {
if a < b {