forked from openatx/atx-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.go
81 lines (74 loc) · 1.86 KB
/
dns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"context"
"errors"
"log"
"net"
"net/http"
"strings"
"time"
"github.com/codeskyblue/goreq"
"github.com/miekg/dns"
)
func dnsLookupHost(hostname string) (ip net.IP, err error) {
for _, dnsServer := range []string{"114.114.114.114", "8.8.4.4"} {
ip, err = dnsLookupHostWithDNS(hostname, dnsServer)
if err == nil {
return
}
}
defaultDNSResolver := getProperty("net.dns1")
if defaultDNSResolver == "" {
return
}
return dnsLookupHostWithDNS(hostname, defaultDNSResolver)
}
func dnsLookupHostWithDNS(hostname string, dnsServer string) (ip net.IP, err error) {
if !strings.HasSuffix(hostname, ".") {
hostname += "."
}
m1 := new(dns.Msg)
m1.Id = dns.Id()
m1.RecursionDesired = true
m1.Question = []dns.Question{
{hostname, dns.TypeA, dns.ClassINET},
}
c := new(dns.Client)
c.SingleInflight = true
in, _, err := c.Exchange(m1, dnsServer+":53")
if err != nil {
return nil, err
}
if len(in.Answer) == 0 {
return nil, errors.New("dns return empty answer")
}
log.Println("dns:"+dnsServer, in.Answer[0])
if t, ok := in.Answer[0].(*dns.A); ok {
return t.A, nil
}
if t, ok := in.Answer[0].(*dns.CNAME); ok {
return dnsLookupHostWithDNS(t.Target, dnsServer)
}
return nil, errors.New("dns resolve failed: " + hostname)
}
func init() {
dialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}
// manualy dns resolve
newDialContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
host, port, _ := net.SplitHostPort(addr)
if net.ParseIP(host) == nil {
ip, err := dnsLookupHost(host)
if err != nil {
return nil, err
}
addr = ip.String() + ":" + port
}
return dialer.DialContext(ctx, network, addr)
}
http.DefaultTransport.(*http.Transport).DialContext = newDialContext
goreq.DefaultTransport.(*http.Transport).DialContext = newDialContext
}