revert: 随机API获取IP(双栈支持问题)

This commit is contained in:
naiba 2022-04-01 08:30:44 +08:00
parent 80320488d1
commit cf7a7ae77c

View File

@ -3,7 +3,6 @@ package monitor
import (
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"strings"
"time"
@ -71,30 +70,33 @@ func fetchGeoIP(servers []string, isV6 bool) geoIP {
var ip geoIP
var resp *http.Response
var err error
// 双栈支持参差不齐,不能随机请求,有些 IPv6 取不到 IP
for i := 0; i < len(servers); i++ {
if isV6 {
resp, err = httpGetWithUA(httpClientV6, servers[rand.Intn(len(servers))])
resp, err = httpClientV6.Get(servers[i])
} else {
resp, err = httpGetWithUA(httpClientV4, servers[rand.Intn(len(servers))])
}
if err != nil {
return ip
resp, err = httpClientV4.Get(servers[i])
}
if err == nil {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return ip
continue
}
resp.Body.Close()
if err := ip.Unmarshal(body); err != nil {
return ip
continue
}
// 没取到 v6 IP
if isV6 && !strings.Contains(ip.IP, ":") {
return ip
continue
}
// 没取到 v4 IP
if !isV6 && !strings.Contains(ip.IP, ".") {
continue
}
return ip
}
}
return ip
}