From 4406eab97a2c9984cdad6c40799aa0d8311a5cc4 Mon Sep 17 00:00:00 2001 From: Justin Li Date: Thu, 5 Dec 2013 20:37:15 -0500 Subject: [PATCH] Refactor determining the remote IP --- server/announce.go | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/server/announce.go b/server/announce.go index 809e997..3fa24fe 100644 --- a/server/announce.go +++ b/server/announce.go @@ -260,32 +260,26 @@ 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: - return string(xRealIPs[0]), nil - - default: - portIndex := len(r.RemoteAddr) - 1 - for ; portIndex >= 0; portIndex-- { - if r.RemoteAddr[portIndex] == ':' { - break - } - } - if portIndex != -1 { - return r.RemoteAddr[0:portIndex], nil - } - return "", errors.New("Failed to parse IP address") } + if ip, ok := pq.Params["ipv4"]; ok { + return ip, nil + } + if xRealIPs, ok := pq.Params["X-Real-Ip"]; ok { + return string(xRealIPs[0]), nil + } + + portIndex := len(r.RemoteAddr) - 1 + for ; portIndex >= 0; portIndex-- { + if r.RemoteAddr[portIndex] == ':' { + break + } + } + if portIndex != -1 { + return r.RemoteAddr[0:portIndex], nil + } + return "", errors.New("Failed to parse IP address") } func minInt(a, b int) int {