-
Notifications
You must be signed in to change notification settings - Fork 5
/
handler.go
112 lines (90 loc) · 2.01 KB
/
handler.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/golang/groupcache/lru"
"github.com/miekg/dns"
"time"
)
const (
notIPQuery = 0
_IP4Query = 4
_IP6Query = 6
)
type Question struct {
qname string
qtype string
qclass string
}
func (q *Question) String() string {
return q.qname + " " + q.qclass + " " + q.qtype
}
type GODNSHandler struct {
resolver *Resolver
Cache *MemoryCache
}
func NewHandler() *GODNSHandler {
var (
resolver *Resolver
Cache *MemoryCache
)
resolver = &Resolver{}
Cache = &MemoryCache{lru.New(MAX_CACHES), time.Duration(EXPIRE_SECONDS) * time.Second, MAX_CACHES}
return &GODNSHandler{resolver, Cache}
}
func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) {
q := req.Question[0]
Q := Question{UnFqdn(q.Name), dns.TypeToString[q.Qtype], dns.ClassToString[q.Qclass]}
fmt.Println("DNS Lookup ", Q.String())
IPQuery := h.isIPQuery(q)
// Only query cache when qtype == 'A'|'AAAA' , qclass == 'IN'
hasher := md5.New()
hasher.Write([]byte(Q.String()))
key := hex.EncodeToString(hasher.Sum(nil))
if IPQuery > 0 {
mesg, err := h.Cache.Get(key)
if err == nil {
fmt.Println("Hit cache", Q.String())
mesg.Id = req.Id
w.WriteMsg(mesg)
return
}
}
mesg, err := h.resolver.Lookup(Net, req)
if err != nil {
fmt.Println("Resolve query error ", err)
dns.HandleFailed(w, req)
return
}
w.WriteMsg(mesg)
if IPQuery > 0 && len(mesg.Answer) > 0 {
h.Cache.Set(key, mesg)
fmt.Println("Insert into cache", Q.String())
}
}
func (h *GODNSHandler) DoTCP(w dns.ResponseWriter, req *dns.Msg) {
h.do("tcp", w, req)
}
func (h *GODNSHandler) DoUDP(w dns.ResponseWriter, req *dns.Msg) {
h.do("udp", w, req)
}
func (h *GODNSHandler) isIPQuery(q dns.Question) int {
if q.Qclass != dns.ClassINET {
return notIPQuery
}
switch q.Qtype {
case dns.TypeA:
return _IP4Query
case dns.TypeAAAA:
return _IP6Query
default:
return notIPQuery
}
}
func UnFqdn(s string) string {
if dns.IsFqdn(s) {
return s[:len(s)-1]
}
return s
}