Initial filters for new peers and new connections. More filters to be added. #4

This commit is contained in:
Kleissner
2021-06-09 22:45:43 +02:00
parent 94c8e16724
commit f5ce15d33a
3 changed files with 32 additions and 0 deletions

View File

@@ -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
}

21
Filter.go Normal file
View File

@@ -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)
}

View File

@@ -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
}