-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.go
71 lines (59 loc) · 1.39 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
package http
import (
"io/ioutil"
"strings"
)
var (
dnsServer = [4]byte{8, 8, 8, 8}
dnsPort = 53
qHeader = []byte{0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
qType = []byte{0x00, 0x01}
qClass = []byte{0x00, 0x01}
)
func packet(domain string) []byte {
domains := strings.Split(domain, ".")
var r []byte
r = append(r, qHeader...)
for _, d := range domains {
l := len(d)
r = append(r, byte(l))
r = append(r, d...)
}
r = append(r, 0x00)
r = append(r, append(qType, qClass...)...)
r = append(r, 0x00)
return r
}
// DNSClient is an interface for the DNS client
type DNSClient interface {
// Resolve returns an IP address from the given domain
Resolve(string) ([4]byte, error)
}
// NewDNSClient creates a client
func NewDNSClient() DNSClient {
return &dnsClient{udpClient: NewUDPClient()}
}
type dnsClient struct {
udpClient UDPClient
}
func (c *dnsClient) Resolve(domain string) ([4]byte, error) {
if domain == "127.0.0.1" || domain == "localhost" {
return [4]byte{127, 0, 0, 1}, nil
}
p := packet(domain)
if err := c.udpClient.SendTo(p, dnsServer, dnsPort); err != nil {
return [4]byte{}, err
}
defer c.udpClient.Close()
r, err := c.udpClient.Recvfrom()
if err != nil {
return [4]byte{}, err
}
resp, err := ioutil.ReadAll(r)
if err != nil {
return [4]byte{}, err
}
var ip [4]byte
copy(ip[:], resp[len(resp)-4:])
return ip, nil
}