diff --git a/Connection.go b/Connection.go index e97b27d..a4db005 100644 --- a/Connection.go +++ b/Connection.go @@ -175,6 +175,10 @@ func (peer *PeerInfo) registerConnection(incoming *Connection) (result *Connecti peer.connectionActive = append(peer.connectionActive, incoming) peer.setConnectionLatest(incoming) + if Filters.NewPeerConnection != nil { + Filters.NewPeerConnection(peer, incoming) + } + return incoming } diff --git a/Filter.go b/Filter.go new file mode 100644 index 0000000..e5ed4d0 --- /dev/null +++ b/Filter.go @@ -0,0 +1,21 @@ +/* +File Name: Filter.go +Copyright: 2021 Peernet s.r.o. +Author: Peter Kleissner + +Filters allow the caller to intercept events to log, modify, or prevent. +*/ + +package core + +// Filters contains all functions to install the hook. Use nil for unused. +// The functions are called sequentially and block execution; if the filter takes a long time it should start a Go routine. +var Filters struct { + // NewPeer is called every time a new peer, that is one that is not currently in the peer list. + // Note that peers might be removed from peer lists and reappear quickly, i.e. this function may be called multiple times for the same peers. + // The filter must maintain its own map of unique peer IDs if actual uniqueness of new peers is desired. + NewPeer func(peer *PeerInfo, connection *Connection) + + // NewPeerConnection is called for each new established connection to a peer. Note that connections might be dropped and reconnected at anytime. + NewPeerConnection func(peer *PeerInfo, connection *Connection) +} diff --git a/Peer ID.go b/Peer ID.go index 4ea3cf4..ac831af 100644 --- a/Peer ID.go +++ b/Peer ID.go @@ -145,6 +145,13 @@ func PeerlistAdd(PublicKey *btcec.PublicKey, connections ...*Connection) (peer * } } + if Filters.NewPeer != nil { + Filters.NewPeer(peer, connections[0]) + } + if Filters.NewPeerConnection != nil { + Filters.NewPeerConnection(peer, connections[0]) + } + return peer, true }