forked from txthinking/brook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve.go
50 lines (46 loc) · 1006 Bytes
/
resolve.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
package brook
import (
"errors"
"net"
"github.com/miekg/dns"
)
func Resolve6(host string) (string, error) {
if net.ParseIP(host).To4() != nil {
return "", errors.New("This is ipv4")
}
if net.ParseIP(host).To16() != nil {
return host, nil
}
m := &dns.Msg{}
m.SetQuestion(host+".", dns.TypeAAAA)
r, err := dns.Exchange(m, "[2001:4860:4860::8888]:53")
if err != nil {
return "", err
}
for _, v := range r.Answer {
if t, ok := v.(*dns.AAAA); ok {
return t.AAAA.String(), nil
}
}
return "", errors.New("Can not find IP")
}
func Resolve4(host string) (string, error) {
if net.ParseIP(host).To4() != nil {
return host, nil
}
if net.ParseIP(host).To16() != nil {
return "", errors.New("This is ipv6")
}
m := &dns.Msg{}
m.SetQuestion(host+".", dns.TypeA)
r, err := dns.Exchange(m, "8.8.8.8:53")
if err != nil {
return "", err
}
for _, v := range r.Answer {
if t, ok := v.(*dns.A); ok {
return t.A.String(), nil
}
}
return "", errors.New("Can not find IP")
}