-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve.go
95 lines (80 loc) · 2.71 KB
/
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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import "net"
// Send Queries to Google's DNS server
const serverAddr = "8.8.8.8:53"
// handleQuery handles incoming single queries
func handleQuery(conn *net.UDPConn) error {
requestBuf := NewBytePacketBuffer()
// Read incoming query from the connection and get the address of the client
_, addr, err := conn.ReadFromUDP(requestBuf.buf)
if err != nil {
return err
}
// read the query raw bytes information from the buffer and insert it into the packet
request := NewDnsPacket()
if err := request.fromBuffer(requestBuf); err != nil {
return err
}
// Create a new packet and set the header
packet := NewDnsPacket()
packet.header = DnsHeader{id: request.header.id, recursionDesired: true, recursionAvailable: true, response: true}
packet.questions = append(packet.questions, request.questions...)
question := request.questions[0]
// Lookup the domain name and query type
result, err := lookup(question.name, question.qtype)
if err != nil {
return err
}
packet.header.resCode = result.header.resCode
packet.answers = append(packet.answers, result.answers...)
packet.authorities = append(packet.authorities, result.authorities...)
packet.resources = append(packet.resources, result.resources...)
// write the response to the buffer
resBuffer := NewBytePacketBuffer()
if err := packet.write(resBuffer); err != nil {
return err
}
len := resBuffer.position()
data, err := resBuffer.getRange(0, len)
if err != nil {
return err
}
// Write the response to the client
_, err = conn.WriteTo(data, addr)
if err != nil {
return err
}
return nil
}
// lookup queries the domain name and returns the response
func lookup(domain string, qtype QueryType) (*DnsPacket, error) {
// create a new udp connection to listen on port 43210
udpAddr, _ := net.ResolveUDPAddr("udp", "0.0.0.0:43210")
conn, err := net.ListenUDP("udp", udpAddr)
if err != nil {
return nil, err
}
defer conn.Close()
// create a new dns packet and set the header
packet := NewDnsPacket()
requestBuf := NewBytePacketBuffer()
packet.header = DnsHeader{id: 6666, questions: 1, recursionDesired: true}
packet.questions = []DnsQuestion{{name: domain, qtype: qtype}}
packet.write(requestBuf)
// request dns query to dns Server
googleAddr, _ := net.ResolveUDPAddr("udp", serverAddr)
if _, err := conn.WriteTo(requestBuf.buf[0:requestBuf.pos], googleAddr); err != nil {
return nil, err
}
// read the response from the dns server
responseBuf := NewBytePacketBuffer()
if _, _, err := conn.ReadFromUDP(responseBuf.buf); err != nil {
return nil, err
}
// create a new packet and set from the buffer
resPacket := NewDnsPacket()
if err := resPacket.fromBuffer(responseBuf); err != nil {
return nil, err
}
return resPacket, nil
}