forked from ethersphere/bee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocklist.go
142 lines (117 loc) · 3.06 KB
/
blocklist.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
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package blocklist
import (
"errors"
"strings"
"time"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm"
)
var keyPrefix = "blocklist-"
type currentTimeFn = func() time.Time
type Blocklist struct {
store storage.StateStorer
currentTimeFn currentTimeFn
}
func NewBlocklist(store storage.StateStorer) *Blocklist {
return &Blocklist{
store: store,
currentTimeFn: time.Now,
}
}
type entry struct {
Timestamp time.Time `json:"timestamp"`
Duration string `json:"duration"` // Duration is string because the time.Duration does not implement MarshalJSON/UnmarshalJSON methods.
Reason string `json:"reason"`
Full bool `json:"full"`
}
func (b *Blocklist) Exists(overlay swarm.Address) (bool, error) {
key := generateKey(overlay)
e, duration, err := b.get(key)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
return false, nil
}
return false, err
}
if b.currentTimeFn().Sub(e.Timestamp) > duration && duration != 0 {
_ = b.store.Delete(key)
return false, nil
}
return true, nil
}
func (b *Blocklist) Add(overlay swarm.Address, duration time.Duration, reason string, full bool) (err error) {
key := generateKey(overlay)
_, d, err := b.get(key)
if err != nil {
if !errors.Is(err, storage.ErrNotFound) {
return err
}
}
// if peer is already blacklisted, blacklist it for the maximum amount of time
if duration < d && duration != 0 || d == 0 {
duration = d
}
return b.store.Put(key, &entry{
Timestamp: b.currentTimeFn(),
Duration: duration.String(),
Reason: reason,
Full: full,
})
}
// Peers returns all currently blocklisted peers.
func (b *Blocklist) Peers() ([]p2p.BlockListedPeer, error) {
var peers []p2p.BlockListedPeer
if err := b.store.Iterate(keyPrefix, func(k, v []byte) (bool, error) {
if !strings.HasPrefix(string(k), keyPrefix) {
return true, nil
}
addr, err := unmarshalKey(string(k))
if err != nil {
return true, err
}
entry, d, err := b.get(string(k))
if err != nil {
return true, err
}
if b.currentTimeFn().Sub(entry.Timestamp) > d && d != 0 {
// skip to the next item
return false, nil
}
p := p2p.BlockListedPeer{
Peer: p2p.Peer{
Address: addr,
FullNode: entry.Full,
},
Duration: d,
Reason: entry.Reason,
}
peers = append(peers, p)
return false, nil
}); err != nil {
return nil, err
}
return peers, nil
}
func (b *Blocklist) get(key string) (entry, time.Duration, error) {
var e entry
err := b.store.Get(key, &e)
if err != nil {
return e, -1, err
}
dur, err := time.ParseDuration(e.Duration)
if err != nil {
return e, -1, err
}
return e, dur, nil
}
func generateKey(overlay swarm.Address) string {
return keyPrefix + overlay.String()
}
func unmarshalKey(s string) (swarm.Address, error) {
addr := s[len(keyPrefix):] // trim prefix
return swarm.ParseHexAddress(addr)
}