-
Notifications
You must be signed in to change notification settings - Fork 0
/
godns.go
67 lines (56 loc) · 1.75 KB
/
godns.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
package main
import (
"crypto/rand"
"encoding/binary"
"fmt"
"godns/types/dnsmessage"
"net"
"os"
"time"
)
func main() {
fmt.Println("Started godns with arguments=", os.Args[1:])
message, err := sendMessage("8.8.8.8:53", prepareTestMessage())
//message, err := sendMessage("1.1.1.1:53", prepareTestMessage())
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
fmt.Println(message)
}
func sendMessage(ipAddress string, message dnsmessage.Message) (dnsmessage.Message, error) {
var responseMessage dnsmessage.Message
conn, err := net.Dial("udp", ipAddress)
if err != nil {
return responseMessage, err
}
defer conn.Close()
_, err = conn.Write(message.Serialize())
if err != nil {
return responseMessage, err
}
deadline := time.Now().Add(time.Duration(5) * time.Second)
err = conn.SetReadDeadline(deadline)
if err != nil {
return responseMessage, err
}
buffer := make([]byte, 1024)
readCount, err := conn.Read(buffer)
if err != nil {
return responseMessage, err
}
result := buffer[0:readCount]
responseMessage, err = dnsmessage.DeserializeMessage(result)
if err != nil {
return responseMessage, err
}
return responseMessage, nil
}
func prepareTestMessage() dnsmessage.Message {
random := make([]byte, 2)
rand.Read(random)
var header = dnsmessage.Header{ID: binary.BigEndian.Uint16(random), OpCode: dnsmessage.OpCodeQuery, QuestionCount: 0, RecursionDesired: true}
var question1 = dnsmessage.Question{QName: "google.com", QType: dnsmessage.ResourceRecordA, QClass: dnsmessage.ClassInternet}
//var question2 = dnsmessage.Question{QName: "google.com", QType: dnsmessage.ResourceRecordA, QClass: dnsmessage.ClassInternet}
return dnsmessage.Message{Header: header, Questions: []dnsmessage.Question{question1}}
}