Repository navigation
Expand file tree
/
Copy pathcache.go
More file actions
134 lines (121 loc) · 3.17 KB
/
Copy pathcache.go
File metadata and controls
134 lines (121 loc) · 3.17 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package tinyresolver
import (
"reflect"
"strings"
"sync"
"time"
"github.com/miekg/dns"
)
type rrDetails struct {
rr dns.RR
expires time.Time
}
type cache struct {
rrs []rrDetails
w sync.RWMutex
}
// newCache creates a new cache pool
func newCache() *cache {
c := &cache{}
for t := range dns.ParseZone(strings.NewReader(root), "", "") {
if t.Error != nil {
continue
}
c.addRR(t.RR)
}
return c
}
// addMsg adds all entries in a message to the cache
func (c *cache) addMsg(rmsg *dns.Msg) {
if rmsg == nil {
return
}
for _, rr := range rmsg.Ns {
c.addRR(dns.Copy(rr))
}
for _, rr := range rmsg.Answer {
c.addRR(dns.Copy(rr))
}
for _, rr := range rmsg.Extra {
c.addRR(dns.Copy(rr))
}
}
// addRR adds a single record to the cache
func (c *cache) addRR(rr dns.RR) {
c.w.Lock()
defer c.w.Unlock()
//log.Printf("CACHED ADD REQUEST object: %v", rr)
rr.Header().Name = toLowerFQDN(rr.Header().Name)
switch rr.(type) {
case *dns.NS:
rr.(*dns.NS).Ns = toLowerFQDN(rr.(*dns.NS).Ns)
}
for id, cachedrr := range c.rrs {
// get record without TTL
newRR := removeSliceString(strings.Split(rr.String(), "\t"), 1)
cachedRR := removeSliceString(strings.Split(cachedrr.rr.String(), "\t"), 1)
if reflect.DeepEqual(newRR, cachedRR) {
// record already exists
newExpire := time.Now().Add(time.Duration(rr.Header().Ttl) * time.Second)
if newExpire.After(cachedrr.expires) {
c.rrs[id].expires = newExpire
}
//log.Printf("CACHED UPDATE EXISTING objects: %v", rr)
return
}
}
rrDetail := rrDetails{
rr: rr,
expires: time.Now().Add(time.Duration(rr.Header().Ttl) * time.Second),
}
c.rrs = append(c.rrs, rrDetail)
//log.Printf("CACHED NEW objects: %v %v", rrDetail.expires, rrDetail.rr)
}
// get retreives a query from the cache
func (c *cache) get(qname, qtype string) *dns.Msg {
msg := &dns.Msg{}
now := time.Now()
qname = toLowerFQDN(qname)
dtype := dns.StringToType[qtype]
c.w.Lock()
for _, rr := range c.rrs {
if rr.rr.Header().Rrtype == dtype && rr.rr.Header().Name == qname && now.Before(rr.expires) {
////log.Printf("expires: %v + in seconds = %v", rr.expires, rr.expires.Sub(now)/time.Second)
res := dns.Copy(rr.rr)
res.Header().Ttl = uint32(rr.expires.Sub(now) / time.Second)
//rr.rr.Header().Ttl = uint32(rr.expires.Sub(now) / time.Second)
msg.Answer = append(msg.Answer, res)
}
}
c.w.Unlock()
//log.Printf("CACHED search: %v %v result1:%d", qname, qtype, len(msg.Answer))
if len(msg.Answer) == 0 {
return msg
}
switch qtype {
case "MX":
mxs := findMX(msg.Answer)
for _, mx := range mxs {
t := c.get(mx, "A")
msg.Extra = append(msg.Extra, t.Answer...)
}
case "NS":
nss := findNS(msg.Answer)
for _, ns := range nss {
t := c.get(ns, "A")
msg.Extra = append(msg.Extra, t.Answer...)
}
case "CNAME":
cnames := findCNAME(msg.Answer)
for _, cname := range cnames {
t := c.get(cname, "A")
msg.Extra = append(msg.Extra, t.Answer...)
}
}
//log.Printf("CACHED search: %v %v result2:%d", qname, qtype, len(msg.Answer))
return msg
}
// removeSliceString removes a string from a slice of strings
func removeSliceString(slice []string, s int) []string {
return append(slice[:s], slice[s+1:]...)
}