-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdns.go
42 lines (39 loc) · 870 Bytes
/
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
package main
import (
"context"
"fmt"
"net"
"time"
)
//Lookup Resolve hosts
func (h Domain) Lookup() (string, error) {
var nameserver string
var err error
if IsIPv4(h.Ns) {
nameserver = h.Ns + ":53"
} else if IsIPv6(h.Ns) {
nameserver = "[" + h.Ns + "]:53"
} else {
return "", fmt.Errorf("nameserver is not valid")
}
r := &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{
Timeout: time.Millisecond * time.Duration(5000),
}
return d.DialContext(ctx, network, nameserver)
},
}
if h.Qtype == "ip" {
h.Qtype = "ip4"
}
ips, err := r.LookupIP(context.Background(), h.Qtype, h.Name)
if err != nil {
return "", err
}
if len(ips) != 1 {
return "", fmt.Errorf("resolver expected one ip but get %v", len(ips))
}
return fmt.Sprintf("%v", ips[0]), nil
}