forked from kaspanet/dnsseeder
-
Notifications
You must be signed in to change notification settings - Fork 4
/
manager.go
322 lines (273 loc) · 7.49 KB
/
manager.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright (c) 2018 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"net"
"os"
"path/filepath"
"sync"
"time"
"github.com/karlsen-network/karlsend/infrastructure/network/addressmanager"
"github.com/karlsen-network/karlsend/app/appmessage"
"github.com/karlsen-network/karlsend/domain/consensus/model/externalapi"
"github.com/miekg/dns"
"github.com/pkg/errors"
)
// Node repesents a node in the Karlsen network
type Node struct {
Addr *appmessage.NetAddress
LastAttempt time.Time
LastSuccess time.Time
LastSeen time.Time
SubnetworkID *externalapi.DomainSubnetworkID
}
// Manager is dnsseeder's main worker-type, storing all information required
// for operation
type Manager struct {
mtx sync.RWMutex
nodes map[string]*Node
wg sync.WaitGroup
quit chan struct{}
peersFile string
}
const (
// defaultMaxAddresses is the maximum number of addresses to return.
defaultMaxAddresses = 16
// defaultStaleTimeout is the time in which a host is considered
// stale.
defaultStaleTimeout = time.Hour
// dumpAddressInterval is the interval used to dump the address
// cache to disk for future use.
dumpAddressInterval = time.Second * 30
// peersFilename is the name of the file.
peersFilename = "nodes.json"
// pruneAddressInterval is the interval used to run the address
// pruner.
pruneAddressInterval = time.Minute * 1
// pruneExpireTimeout is the expire time in which a node is
// considered dead.
pruneExpireTimeout = time.Hour * 8
)
// NewManager constructs and returns a new dnsseeder manager, with the provided dataDir
func NewManager(dataDir string) (*Manager, error) {
amgr := Manager{
nodes: make(map[string]*Node),
peersFile: filepath.Join(dataDir, peersFilename),
quit: make(chan struct{}),
}
err := amgr.deserializePeers()
if err != nil {
log.Warnf("Failed to parse file %s: %v", amgr.peersFile, err)
// if it is invalid we nuke the old one unconditionally.
err = os.Remove(amgr.peersFile)
if err != nil {
log.Warnf("Failed to remove corrupt peers file %s: %v",
amgr.peersFile, err)
}
}
amgr.wg.Add(1)
spawn("NewManager-Manager.addressHandler", amgr.addressHandler)
return &amgr, nil
}
// AddAddresses adds an address to this dnsseeder manager, and returns the number of
// address currently held
func (m *Manager) AddAddresses(addrs []*appmessage.NetAddress) int {
var count int
m.mtx.Lock()
for _, addr := range addrs {
if !addressmanager.IsRoutable(addr, ActiveConfig().NetParams().AcceptUnroutable) {
continue
}
addrStr := addr.IP.String()
_, exists := m.nodes[addrStr]
if exists {
m.nodes[addrStr].LastSeen = time.Now()
continue
}
node := Node{
Addr: addr,
LastSeen: time.Now(),
}
m.nodes[addrStr] = &node
count++
}
m.mtx.Unlock()
return count
}
// Addresses returns IPs that need to be tested again.
func (m *Manager) Addresses() []*appmessage.NetAddress {
addrs := make([]*appmessage.NetAddress, 0, defaultMaxAddresses*8)
now := time.Now()
i := defaultMaxAddresses
m.mtx.RLock()
for _, node := range m.nodes {
if i == 0 {
break
}
if now.Sub(node.LastSuccess) < defaultStaleTimeout ||
now.Sub(node.LastAttempt) < defaultStaleTimeout {
continue
}
addrs = append(addrs, node.Addr)
i--
}
m.mtx.RUnlock()
return addrs
}
// AddressCount returns number of known nodes.
func (m *Manager) AddressCount() int {
return len(m.nodes)
}
// GoodAddresses returns good working IPs that match both the
// passed DNS query type and have the requested services.
func (m *Manager) GoodAddresses(qtype uint16, includeAllSubnetworks bool, subnetworkID *externalapi.DomainSubnetworkID,
) []*appmessage.NetAddress {
addrs := make([]*appmessage.NetAddress, 0, defaultMaxAddresses)
i := defaultMaxAddresses
if qtype != dns.TypeA && qtype != dns.TypeAAAA {
return addrs
}
now := time.Now()
m.mtx.RLock()
for _, node := range m.nodes {
if i == 0 {
break
}
if node.Addr.Port != uint16(peersDefaultPort) {
continue
}
if !includeAllSubnetworks && !node.SubnetworkID.Equal(subnetworkID) {
continue
}
if qtype == dns.TypeA && node.Addr.IP.To4() == nil {
continue
} else if qtype == dns.TypeAAAA && node.Addr.IP.To4() != nil {
continue
}
if node.LastSuccess.IsZero() ||
now.Sub(node.LastSuccess) > defaultStaleTimeout {
continue
}
addrs = append(addrs, node.Addr)
i--
}
m.mtx.RUnlock()
return addrs
}
// Attempt updates the last connection attempt for the specified ip address to now
func (m *Manager) Attempt(ip net.IP) {
m.mtx.Lock()
node, exists := m.nodes[ip.String()]
if exists {
node.LastAttempt = time.Now()
}
m.mtx.Unlock()
}
// Good updates the last successful connection attempt for the specified ip address to now
func (m *Manager) Good(ip net.IP, subnetworkid *externalapi.DomainSubnetworkID) {
m.mtx.Lock()
node, exists := m.nodes[ip.String()]
if exists {
node.LastSuccess = time.Now()
node.SubnetworkID = subnetworkid
}
m.mtx.Unlock()
}
// addressHandler is the main handler for the address manager. It must be run
// as a goroutine.
func (m *Manager) addressHandler() {
defer m.wg.Done()
pruneAddressTicker := time.NewTicker(pruneAddressInterval)
defer pruneAddressTicker.Stop()
dumpAddressTicker := time.NewTicker(dumpAddressInterval)
defer dumpAddressTicker.Stop()
out:
for {
select {
case <-dumpAddressTicker.C:
m.savePeers()
case <-pruneAddressTicker.C:
m.prunePeers()
case <-m.quit:
break out
}
}
log.Infof("Address manager: saving peers")
m.savePeers()
log.Infof("Address manager shoutdown")
}
func (m *Manager) prunePeers() {
var count int
now := time.Now()
m.mtx.Lock()
lastSeenAbovePruneExpire := func(node *Node) bool {
return now.Sub(node.LastSeen) > pruneExpireTimeout
}
hadAttemptsButNoSuccess := func(node *Node) bool {
return !node.LastAttempt.IsZero() && node.LastSuccess.IsZero()
}
hadSuccessButLongTimeAgo := func(node *Node) bool {
return !node.LastSuccess.IsZero() && now.Sub(node.LastSuccess) > pruneExpireTimeout
}
for k, node := range m.nodes {
if lastSeenAbovePruneExpire(node) ||
hadAttemptsButNoSuccess(node) ||
hadSuccessButLongTimeAgo(node) {
delete(m.nodes, k)
count++
}
}
l := len(m.nodes)
m.mtx.Unlock()
log.Infof("Pruned %d addresses: %d remaining", count, l)
}
func (m *Manager) deserializePeers() error {
filePath := m.peersFile
_, err := os.Stat(filePath)
if os.IsNotExist(err) {
return nil
}
r, err := os.Open(filePath)
if err != nil {
return errors.Errorf("%s error opening file: %v", filePath, err)
}
defer r.Close()
var nodes map[string]*Node
dec := json.NewDecoder(r)
err = dec.Decode(&nodes)
if err != nil {
return errors.Errorf("error reading %s: %v", filePath, err)
}
l := len(nodes)
m.mtx.Lock()
m.nodes = nodes
m.mtx.Unlock()
log.Infof("%d nodes loaded", l)
return nil
}
func (m *Manager) savePeers() {
m.mtx.RLock()
defer m.mtx.RUnlock()
// Write temporary peers file and then move it into place.
tmpfile := m.peersFile + ".new"
w, err := os.Create(tmpfile)
if err != nil {
log.Errorf("Error opening file %s: %v", tmpfile, err)
return
}
enc := json.NewEncoder(w)
if err := enc.Encode(&m.nodes); err != nil {
log.Errorf("Failed to encode file %s: %v", tmpfile, err)
return
}
if err := w.Close(); err != nil {
log.Errorf("Error closing file %s: %v", tmpfile, err)
return
}
if err := os.Rename(tmpfile, m.peersFile); err != nil {
log.Errorf("Error writing file %s: %v", m.peersFile, err)
return
}
}