forked from multiversx/mx-chain-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectionMonitorWrapper.go
122 lines (100 loc) · 3.74 KB
/
connectionMonitorWrapper.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
package libp2p
import (
"sync"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/p2p"
"github.com/libp2p/go-libp2p-core/network"
"github.com/multiformats/go-multiaddr"
)
var _ ConnectionMonitor = (*connectionMonitorWrapper)(nil)
// connectionMonitorWrapper is a wrapper over p2p.ConnectionMonitor that satisfies the Notifiee interface
// and is able to be notified by the current running host (connection status changes)
// it handles black list peers
type connectionMonitorWrapper struct {
ConnectionMonitor
network network.Network
mutPeerBlackList sync.RWMutex
peerDenialEvaluator p2p.PeerDenialEvaluator
}
func newConnectionMonitorWrapper(
network network.Network,
connMonitor ConnectionMonitor,
peerDenialEvaluator p2p.PeerDenialEvaluator,
) *connectionMonitorWrapper {
return &connectionMonitorWrapper{
ConnectionMonitor: connMonitor,
network: network,
peerDenialEvaluator: peerDenialEvaluator,
}
}
// Listen is called when network starts listening on an addr
func (cmw *connectionMonitorWrapper) Listen(netw network.Network, ma multiaddr.Multiaddr) {
cmw.ConnectionMonitor.Listen(netw, ma)
}
// ListenClose is called when network stops listening on an addr
func (cmw *connectionMonitorWrapper) ListenClose(netw network.Network, ma multiaddr.Multiaddr) {
cmw.ConnectionMonitor.ListenClose(netw, ma)
}
// Connected is called when a connection opened
func (cmw *connectionMonitorWrapper) Connected(netw network.Network, conn network.Conn) {
cmw.mutPeerBlackList.RLock()
peerBlackList := cmw.peerDenialEvaluator
cmw.mutPeerBlackList.RUnlock()
pid := conn.RemotePeer()
if peerBlackList.IsDenied(core.PeerID(pid)) {
log.Trace("dropping connection to blacklisted peer",
"pid", pid.Pretty(),
)
_ = conn.Close()
return
}
cmw.ConnectionMonitor.Connected(netw, conn)
}
// Disconnected is called when a connection closed
func (cmw *connectionMonitorWrapper) Disconnected(netw network.Network, conn network.Conn) {
cmw.ConnectionMonitor.Disconnected(netw, conn)
}
// OpenedStream is called when a stream opened
func (cmw *connectionMonitorWrapper) OpenedStream(netw network.Network, stream network.Stream) {
cmw.ConnectionMonitor.OpenedStream(netw, stream)
}
// ClosedStream is called when a stream closed
func (cmw *connectionMonitorWrapper) ClosedStream(netw network.Network, stream network.Stream) {
cmw.ConnectionMonitor.ClosedStream(netw, stream)
}
// CheckConnectionsBlocking does a peer sweep, calling Close on those peers that are black listed
func (cmw *connectionMonitorWrapper) CheckConnectionsBlocking() {
peers := cmw.network.Peers()
cmw.mutPeerBlackList.RLock()
peerDenialEvaluator := cmw.peerDenialEvaluator
cmw.mutPeerBlackList.RUnlock()
for _, pid := range peers {
if peerDenialEvaluator.IsDenied(core.PeerID(pid)) {
log.Trace("dropping connection to blacklisted peer",
"pid", pid.Pretty(),
)
_ = cmw.network.ClosePeer(pid)
}
}
}
// SetPeerDenialEvaluator sets the handler that is able to tell if a peer can connect to self or not (is or not blacklisted)
func (cmw *connectionMonitorWrapper) SetPeerDenialEvaluator(handler p2p.PeerDenialEvaluator) error {
if check.IfNil(handler) {
return p2p.ErrNilPeerDenialEvaluator
}
cmw.mutPeerBlackList.Lock()
cmw.peerDenialEvaluator = handler
cmw.mutPeerBlackList.Unlock()
return nil
}
// PeerDenialEvaluator gets the peer denial evauator
func (cmw *connectionMonitorWrapper) PeerDenialEvaluator() p2p.PeerDenialEvaluator {
cmw.mutPeerBlackList.RLock()
defer cmw.mutPeerBlackList.RUnlock()
return cmw.peerDenialEvaluator
}
// IsInterfaceNil returns true if there is no value under the interface
func (cmw *connectionMonitorWrapper) IsInterfaceNil() bool {
return cmw == nil
}