Skip to content

Commit

Permalink
🚸 优化NAT64机器取IP场景
Browse files Browse the repository at this point in the history
  • Loading branch information
naiba committed Jul 13, 2021
1 parent 32cb05e commit fea7858
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 9 additions & 0 deletions cmd/agent/monitor/myip.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"

"github.com/naiba/nezha/pkg/utils"
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 2 additions & 6 deletions pkg/utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,21 @@ 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"}
}

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])
Expand Down

0 comments on commit fea7858

Please sign in to comment.