-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnscache.go
108 lines (92 loc) · 2.1 KB
/
dnscache.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
package tgbotapi
// https://github.com/rs/dnscache
import (
"context"
"net"
"sync"
"time"
)
type CacheResolver struct {
lock sync.RWMutex
cache map[string][]net.IP
resolver *net.Resolver
}
func NewCachedResolver(refreshRate time.Duration) *CacheResolver {
return NewCustomCachedResolver(net.DefaultResolver, refreshRate)
}
func NewCustomCachedResolver(resolver *net.Resolver, refreshRate time.Duration) *CacheResolver {
cacheResolver := &CacheResolver{
resolver: resolver,
cache: make(map[string][]net.IP, 64),
}
if refreshRate > 0 {
go cacheResolver.autoRefresh(refreshRate)
}
return cacheResolver
}
func (r *CacheResolver) Fetch(address string) ([]net.IP, error) {
r.lock.RLock()
ips, exists := r.cache[address]
r.lock.RUnlock()
if exists {
log.Printf("hit DNS cache ", address, ips)
return ips, nil
}
return r.Lookup(address)
}
func (r *CacheResolver) FetchOne(address string) (net.IP, error) {
ips, err := r.Fetch(address)
if err != nil || len(ips) == 0 {
return nil, err
}
return ips[0], nil
}
func (r *CacheResolver) FetchOneString(address string) (string, error) {
ip, err := r.FetchOne(address)
if err != nil || ip == nil {
return "", err
}
return ip.String(), nil
}
func (r *CacheResolver) Refresh() {
i := 0
r.lock.RLock()
addresses := make([]string, len(r.cache))
for key, _ := range r.cache {
addresses[i] = key
i++
}
r.lock.RUnlock()
for _, address := range addresses {
r.Lookup(address)
time.Sleep(time.Second * 2)
}
}
func (r *CacheResolver) Lookup(address string) ([]net.IP, error) {
ips, err := r.LookupIP(address)
if err != nil {
return nil, err
}
r.lock.Lock()
r.cache[address] = ips
r.lock.Unlock()
return ips, nil
}
func (r *CacheResolver) autoRefresh(rate time.Duration) {
for {
time.Sleep(rate)
r.Refresh()
}
}
func (r *CacheResolver) LookupIP(host string) ([]net.IP, error) {
address, err := r.resolver.LookupIPAddr(context.Background(), host)
if err != nil {
return nil, err
}
ips := make([]net.IP, len(address))
for i, ia := range address {
ips[i] = ia.IP
}
log.Println("LookupIPAddr ", host, ips)
return ips, nil
}