-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (79 loc) · 2.22 KB
/
main.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 (
"flag"
"github.com/hkiiita/GopherDNS/config"
"github.com/hkiiita/GopherDNS/dnsProcessing"
"github.com/hkiiita/GopherDNS/utils"
"golang.org/x/net/dns/dnsmessage"
"log"
"net"
"os"
"time"
)
func main() {
var dnsPort = flag.Int("dnsPort", 0, "DNS port.")
var serverRefreshTime = flag.Int("serverRefreshTime", 0, "Time under which server would periodically refresh domain IPs")
var ttlForResponse = flag.Int("ttlForResponse", 0, "time to live to be sent in DNS response")
flag.Usage = utils.CustomFlagUsageMessage
flag.Parse()
if *dnsPort == 0 || *serverRefreshTime == 0 || *ttlForResponse == 0 {
flag.Usage()
}
var err error
err = config.SetDomainConfig()
if err != nil {
log.Fatalf("Error getting IP values for domains %+v", err)
}
ticker := time.NewTicker(time.Duration(*serverRefreshTime) * time.Second)
defer func() {
ticker.Stop()
}()
go func() {
for {
select {
case <-ticker.C:
err = config.SetDomainConfig()
if err != nil {
log.Fatalf("Error getting IP values for domains %+v", err)
}
}
}
}()
// UDP end point.
addr := net.UDPAddr{
IP: net.ParseIP("0.0.0.0"),
Port: *dnsPort,
}
// open up a process listening on above address.
conn, err := net.ListenUDP("udp", &addr)
if err != nil {
log.Printf("Error creating a UDP connection %+v", err)
os.Exit(1)
}
defer conn.Close()
log.Printf("DNS server listening on port %+v", addr.Port)
// Read requests
for {
buf := make([]byte, 512)
log.Printf("waiting for requests...")
n, address, err := conn.ReadFromUDP(buf)
if err != nil {
log.Printf("Error reading incoming reuqest into buffer : %+v", err)
continue
}
go processRequest(buf[:n], conn, address, *ttlForResponse)
}
}
func processRequest(dnsRequest []byte, conn *net.UDPConn, address *net.UDPAddr, ttlForResponse int) {
log.Printf("Received request... processing...")
parser := dnsmessage.Parser{}
parsedHeader, err := parser.Start(dnsRequest)
parsedQuestion, err := parser.Question()
resp, err := dnsProcessing.CreateResponseMessage(parsedHeader, parsedQuestion, ttlForResponse)
if err != nil {
log.Printf("Unable to create response. %+v", err)
return
}
utils.PrintHex(resp)
dnsProcessing.SendResponse(resp, address, conn)
}