forked from anacrolix/dht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
56 lines (44 loc) · 1.09 KB
/
node.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
package dht
import (
"time"
"github.com/anacrolix/dht/v2/int160"
"github.com/anacrolix/dht/v2/krpc"
)
type nodeKey struct {
Addr Addr
Id int160.T
}
type node struct {
nodeKey
lastGotQuery time.Time // From the remote node
lastGotResponse time.Time // From the remote node
numReceivesFrom int
failedLastQuestionablePing bool
}
func (s *Server) IsQuestionable(n *node) bool {
return !s.IsGood(n) && !s.nodeIsBad(n)
}
func (n *node) hasAddrAndID(addr Addr, id int160.T) bool {
return id == n.Id && n.Addr.String() == addr.String()
}
func (n *node) IsSecure() bool {
return NodeIdSecure(n.Id.AsByteArray(), n.Addr.IP())
}
func (n *node) idString() string {
return n.Id.ByteString()
}
func (n *node) NodeInfo() (ret krpc.NodeInfo) {
ret.Addr = n.Addr.KRPC()
if n := copy(ret.ID[:], n.idString()); n != 20 {
panic(n)
}
return
}
// Per the spec in BEP 5.
func (s *Server) IsGood(n *node) bool {
if s.nodeIsBad(n) {
return false
}
return time.Since(n.lastGotResponse) < 15*time.Minute ||
!n.lastGotResponse.IsZero() && time.Since(n.lastGotQuery) < 15*time.Minute
}