forked from ARwMq9b6/dnsproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
78 lines (65 loc) · 1.55 KB
/
cache.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
package dnsproxy
import (
"time"
"github.com/miekg/dns"
"github.com/patrickmn/go-cache"
)
// ip cache, cache "ip" and transport
type ipcache struct {
inner *cache.Cache
}
// --- impl ipcache
func NewIpcache(defaultExpiration, cleanupInterval time.Duration) ipcache {
c := cache.New(defaultExpiration, cleanupInterval)
return ipcache{c}
}
func (c ipcache) Add(ip string, t transport) {
if ip == "" {
return
}
c.inner.Add(ip, t, cache.DefaultExpiration)
}
func (c ipcache) Get(ip string) (transport, bool) {
v, ok := c.inner.Get(ip)
if ok {
return v.(transport), true
} else {
return 0, false
}
}
// domain cache, cache "domain" and dns message info
type domaincache struct {
inner *cache.Cache
}
type domaincacheCell struct {
ans dns.RR // cached answer
trans transport // transport type for answered ips in dns message
}
// --- impl domaincache
func NewDomaincache(defaultExpiration, cleanupInterval time.Duration) domaincache {
c := cache.New(defaultExpiration, cleanupInterval)
return domaincache{c}
}
func (c domaincache) Add(domain string, answer dns.RR, t transport) {
if domain == "" {
return
}
if name := dns.Fqdn(domain); name != answer.Header().Name {
answer.Header().Name = name
}
cell := domaincacheCell{answer, t}
c.inner.Add(domain, &cell, cache.DefaultExpiration)
}
func (c domaincache) Get(domain string) (*domaincacheCell, bool) {
v, ok := c.inner.Get(domain)
if ok {
return v.(*domaincacheCell), true
} else {
return nil, false
}
}
type transport int8
const (
_TRANS_DIRECT transport = iota
_TRANS_PROXY
)