From cf7a7ae77c15177e6410686e81b322a75c3392fd Mon Sep 17 00:00:00 2001 From: naiba Date: Fri, 1 Apr 2022 08:30:44 +0800 Subject: [PATCH] =?UTF-8?q?revert:=20=E9=9A=8F=E6=9C=BAAPI=E8=8E=B7?= =?UTF-8?q?=E5=8F=96IP(=E5=8F=8C=E6=A0=88=E6=94=AF=E6=8C=81=E9=97=AE?= =?UTF-8?q?=E9=A2=98)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/agent/monitor/myip.go | 50 ++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/cmd/agent/monitor/myip.go b/cmd/agent/monitor/myip.go index 78d699e..36114c8 100644 --- a/cmd/agent/monitor/myip.go +++ b/cmd/agent/monitor/myip.go @@ -3,7 +3,6 @@ package monitor import ( "fmt" "io/ioutil" - "math/rand" "net/http" "strings" "time" @@ -71,29 +70,32 @@ func fetchGeoIP(servers []string, isV6 bool) geoIP { var ip geoIP var resp *http.Response var err error - if isV6 { - resp, err = httpGetWithUA(httpClientV6, servers[rand.Intn(len(servers))]) - } else { - resp, err = httpGetWithUA(httpClientV4, servers[rand.Intn(len(servers))]) - } - if err != nil { - return ip - } - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return ip - } - resp.Body.Close() - if err := ip.Unmarshal(body); err != nil { - return ip - } - // 没取到 v6 IP - if isV6 && !strings.Contains(ip.IP, ":") { - return ip - } - // 没取到 v4 IP - if !isV6 && !strings.Contains(ip.IP, ".") { - return ip + // 双栈支持参差不齐,不能随机请求,有些 IPv6 取不到 IP + for i := 0; i < len(servers); i++ { + if isV6 { + resp, err = httpClientV6.Get(servers[i]) + } else { + resp, err = httpClientV4.Get(servers[i]) + } + if err == nil { + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + continue + } + resp.Body.Close() + if err := ip.Unmarshal(body); err != nil { + continue + } + // 没取到 v6 IP + if isV6 && !strings.Contains(ip.IP, ":") { + continue + } + // 没取到 v4 IP + if !isV6 && !strings.Contains(ip.IP, ".") { + continue + } + return ip + } } return ip }