diff --git a/cmd/agent/monitor/myip.go b/cmd/agent/monitor/myip.go index b5e77870b6..17b09a4696 100644 --- a/cmd/agent/monitor/myip.go +++ b/cmd/agent/monitor/myip.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "net/http" + "strings" "time" "github.com/naiba/nezha/pkg/utils" @@ -64,6 +65,14 @@ func fetchGeoIP(servers []string, isV6 bool) geoIP { if err != nil { continue } + // 没取到 v6 IP + if isV6 && !strings.Contains(ip.IP, ":") { + continue + } + // 没取到 v4 IP + if !isV6 && !strings.Contains(ip.IP, ".") { + continue + } if ip.IP == "" && ip.Query != "" { ip.IP = ip.Query } diff --git a/pkg/utils/http.go b/pkg/utils/http.go index 45ffda81b9..06d946a586 100644 --- a/pkg/utils/http.go +++ b/pkg/utils/http.go @@ -36,7 +36,7 @@ func NewSingleStackHTTPClient(httpTimeout, dialTimeout, keepAliveTimeout time.Du func resolveIP(addr string, ipv6 bool) (string, error) { url := strings.Split(addr, ":") - dnsServers := []string{"2606:4700:4700::1001", "2001:4860:4860::8844", "2400:3200::1", "2400:3200:baba::1"} + dnsServers := []string{"[2606:4700:4700::1001]", "[2001:4860:4860::8844]", "[2400:3200::1]", "[2400:3200:baba::1]"} if !ipv6 { dnsServers = []string{"1.0.0.1", "8.8.4.4", "223.5.5.5", "223.6.6.6"} } @@ -44,17 +44,13 @@ func resolveIP(addr string, ipv6 bool) (string, error) { res, err := net.LookupIP(url[0]) if err != nil { for i := 0; i < len(dnsServers); i++ { - dnsServer := dnsServers[i] - if ipv6 { - dnsServer = "[" + dnsServer + "]" - } r := &net.Resolver{ PreferGo: true, Dial: func(ctx context.Context, network, address string) (net.Conn, error) { d := net.Dialer{ Timeout: time.Second * 10, } - return d.DialContext(ctx, "udp", dnsServer+":53") + return d.DialContext(ctx, "udp", dnsServers[i]+":53") }, } res, err = r.LookupIP(context.Background(), "ip", url[0])