From d59acfa8248c99547e946e4ace2b66ebf173e73e Mon Sep 17 00:00:00 2001 From: naiba Date: Tue, 22 Jun 2021 14:04:07 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=B8=20=E4=BC=98=E5=8C=96=20Agent=20?= =?UTF-8?q?=E5=8F=96=20IP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/playground/main.go | 36 ++++++++++++++++- go.mod | 1 - go.sum | 2 - pkg/utils/http.go | 87 ++++++++++++++++++------------------------ 4 files changed, 72 insertions(+), 54 deletions(-) diff --git a/cmd/playground/main.go b/cmd/playground/main.go index 1bb569e..9834191 100644 --- a/cmd/playground/main.go +++ b/cmd/playground/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "crypto/tls" "fmt" "log" @@ -8,22 +9,53 @@ import ( "net/http" "os" "os/exec" + "strings" "time" "github.com/genkiroid/cert" "github.com/go-ping/ping" - "github.com/naiba/nezha/pkg/utils" "github.com/shirou/gopsutil/v3/cpu" "github.com/shirou/gopsutil/v3/disk" "github.com/shirou/gopsutil/v3/host" + + "github.com/naiba/nezha/pkg/utils" ) func main() { // icmp() // tcpping() - httpWithSSLInfo() + // httpWithSSLInfo() // sysinfo() // cmdExec() + resolveIP("ipapi.co", true) + resolveIP("ipapi.co", false) +} + +func resolveIP(addr string, ipv6 bool) { + url := strings.Split(addr, ":") + + 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"} + } + + log.Println(net.LookupIP(url[0])) + 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") + }, + } + log.Println(r.LookupIP(context.Background(), "ip", url[0])) + } } func tcpping() { diff --git a/go.mod b/go.mod index d98d4d2..07d9896 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,6 @@ require ( github.com/golang/protobuf v1.4.2 github.com/google/go-github v17.0.0+incompatible github.com/gorilla/websocket v1.4.2 - github.com/miekg/dns v1.0.14 github.com/onsi/ginkgo v1.7.0 // indirect github.com/onsi/gomega v1.4.3 // indirect github.com/p14yground/go-github-selfupdate v0.0.0-20210520015421-eddf14461293 diff --git a/go.sum b/go.sum index 6d449e9..a785187 100644 --- a/go.sum +++ b/go.sum @@ -228,7 +228,6 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky github.com/mattn/go-sqlite3 v1.14.5 h1:1IdxlwTNazvbKJQSxoJ5/9ECbEeaTTyeU7sEAZ5KKTQ= github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= -github.com/miekg/dns v1.0.14 h1:9jZdLNd/P4+SfEJ0TNyxYpsK8N4GtfylBLqtbYN1sbA= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= @@ -422,7 +421,6 @@ golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 h1:qwRHBd0NqMbJxfbotnDhm2ByMI1Shq4Y6oRJo21SGJA= golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= diff --git a/pkg/utils/http.go b/pkg/utils/http.go index 82408f1..c88df0a 100644 --- a/pkg/utils/http.go +++ b/pkg/utils/http.go @@ -6,10 +6,7 @@ import ( "net" "net/http" "strings" - "sync" "time" - - "github.com/miekg/dns" ) func NewSingleStackHTTPClient(httpTimeout, dialTimeout, keepAliveTimeout time.Duration, ipv6 bool) *http.Client { @@ -39,60 +36,52 @@ func NewSingleStackHTTPClient(httpTimeout, dialTimeout, keepAliveTimeout time.Du func resolveIP(addr string, ipv6 bool) (string, error) { url := strings.Split(addr, ":") - m := new(dns.Msg) - if ipv6 { - m.SetQuestion(dns.Fqdn(url[0]), dns.TypeAAAA) - } else { - m.SetQuestion(dns.Fqdn(url[0]), dns.TypeA) - } - m.RecursionDesired = true - 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"} } - var wg sync.WaitGroup - var resolveLock sync.RWMutex + 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") + }, + } + res, err = r.LookupIP(context.Background(), "ip", url[0]) + if err == nil { + break + } + } + } + + if err != nil { + return "", err + } + var ipv4Resolved, ipv6Resolved bool - wg.Add(len(dnsServers)) - go func() { - - }() - for i := 0; i < len(dnsServers); i++ { - go func(i int) { - defer wg.Done() - c := new(dns.Client) - c.Timeout = time.Second * 3 - r, _, err := c.Exchange(m, net.JoinHostPort(dnsServers[i], "53")) - if err != nil { - return - } - resolveLock.Lock() - defer resolveLock.Unlock() - if ipv6 && ipv6Resolved { - return - } - if !ipv6 && ipv4Resolved { - return - } - for _, ans := range r.Answer { - if ipv6 { - if aaaa, ok := ans.(*dns.AAAA); ok { - url[0] = "[" + aaaa.AAAA.String() + "]" - ipv6Resolved = true - } - } else { - if a, ok := ans.(*dns.A); ok { - url[0] = a.A.String() - ipv4Resolved = true - } - } - } - }(i) + for i := 0; i < len(res); i++ { + if ip4 := res[i].To4(); ip4 != nil && !ipv6 { + ipv4Resolved = true + url[0] = ip4.String() + break + } + if ip6 := res[i].To16(); ip6 != nil && ipv6 { + ipv6Resolved = true + url[0] = "[" + ip6.String() + "]" + break + } } - wg.Wait() if ipv6 && !ipv6Resolved { return "", errors.New("the AAAA record not resolved")