mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-17 02:47:51 +01:00
Initial filters for new peers and new connections. More filters to be added. #4
This commit is contained in:
@@ -175,6 +175,10 @@ func (peer *PeerInfo) registerConnection(incoming *Connection) (result *Connecti
|
|||||||
peer.connectionActive = append(peer.connectionActive, incoming)
|
peer.connectionActive = append(peer.connectionActive, incoming)
|
||||||
peer.setConnectionLatest(incoming)
|
peer.setConnectionLatest(incoming)
|
||||||
|
|
||||||
|
if Filters.NewPeerConnection != nil {
|
||||||
|
Filters.NewPeerConnection(peer, incoming)
|
||||||
|
}
|
||||||
|
|
||||||
return incoming
|
return incoming
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
21
Filter.go
Normal file
21
Filter.go
Normal 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)
|
||||||
|
}
|
||||||
@@ -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
|
return peer, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user