forked from shiyanhui/dht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blacklist.go
92 lines (77 loc) · 1.89 KB
/
blacklist.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
package dht
import (
"time"
)
// blockedItem represents a blocked node.
type blockedItem struct {
ip string
port int
createTime time.Time
}
// blackList manages the blocked nodes including which sends bad information
// and can't ping out.
type blackList struct {
list *syncedMap
maxSize int
expiredAfter time.Duration
}
// newBlackList returns a blackList pointer.
func newBlackList(size int) *blackList {
return &blackList{
list: newSyncedMap(),
maxSize: size,
expiredAfter: time.Hour * 1,
}
}
// genKey returns a key. If port is less than 0, the key wil be ip. Ohterwise
// it will be `ip:port` format.
func (bl *blackList) genKey(ip string, port int) string {
key := ip
if port >= 0 {
key = genAddress(ip, port)
}
return key
}
// insert adds a blocked item to the blacklist.
func (bl *blackList) insert(ip string, port int) {
if bl.list.Len() >= bl.maxSize {
return
}
bl.list.Set(bl.genKey(ip, port), &blockedItem{
ip: ip,
port: port,
createTime: time.Now(),
})
}
// delete removes blocked item form the blackList.
func (bl *blackList) delete(ip string, port int) {
bl.list.Delete(bl.genKey(ip, port))
}
// validate checks whether ip-port pair is in the block nodes list.
func (bl *blackList) in(ip string, port int) bool {
if _, ok := bl.list.Get(ip); ok {
return true
}
key := bl.genKey(ip, port)
v, ok := bl.list.Get(key)
if ok {
if time.Now().Sub(v.(*blockedItem).createTime) < bl.expiredAfter {
return true
}
bl.list.Delete(key)
}
return false
}
// clear cleans the expired items every 10 minutes.
func (bl *blackList) clear() {
for _ = range time.Tick(time.Minute * 10) {
keys := make([]interface{}, 0, 100)
for item := range bl.list.Iter() {
if time.Now().Sub(
item.val.(*blockedItem).createTime) > bl.expiredAfter {
keys = append(keys, item.key)
}
}
bl.list.DeleteMulti(keys)
}
}