-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdns.go
64 lines (53 loc) · 1.21 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
package main
import (
"fmt"
"log"
"strings"
"github.com/miekg/dns"
)
func parseQuery(m *dns.Msg) {
for _, q := range m.Question {
switch q.Qtype {
case dns.TypeA:
log.Printf("Query for %s\n", q.Name)
if q.Name == "broker-ari.everyware-cloud.com." {
rr, err := dns.NewRR(fmt.Sprintf("%s A %s", q.Name, Config.Dns_resolve_to))
if err == nil {
m.Answer = append(m.Answer, rr)
}
}
if strings.Contains(q.Name, "pool.ntp.org.") {
rr, err := dns.NewRR(fmt.Sprintf("%s A %s", q.Name, Config.Ntp_resolve_to))
if err == nil {
m.Answer = append(m.Answer, rr)
}
}
}
}
}
func handleDnsRequest(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
m.Compress = false
switch r.Opcode {
case dns.OpcodeQuery:
parseQuery(m)
}
w.WriteMsg(m)
}
func dnsLogic() {
if Config.Dns_resolve_to == "" {
return
}
// attach request handler func
dns.HandleFunc(".", handleDnsRequest)
// start server
server := &dns.Server{Addr: Config.Dns_listener, Net: "udp"}
log.Printf("Starting DNS listener at %v\n", Config.Dns_listener)
go func() {
err := server.ListenAndServe()
if err != nil {
log.Fatalf("Failed to start server: %s\n ", err.Error())
}
}()
}