feat: introduce libp2p-allow-block-list connection management module

Currently, banning peers is a first-class feature of `Swarm`. With the new connection management capabilities of `NetworkBehaviour`, we can now implement allow and block lists as a separate module.

We introduce a new crate `libp2p-allow-block-list` and deprecate `Swarm::ban_peer_id` in favor of that.

Related #2824.

Pull-Request: #3590.
This commit is contained in:
Thomas Eizinger
2023-03-21 21:58:09 +01:00
committed by GitHub
parent 3fa10be0d5
commit f64187049d
12 changed files with 532 additions and 2 deletions

View File

@ -251,6 +251,7 @@ pub enum SwarmEvent<TBehaviourOutEvent, THandlerErr> {
error: DialError,
},
/// We connected to a peer, but we immediately closed the connection because that peer is banned.
#[deprecated(note = "Use `libp2p::allow_block_list` instead.", since = "0.42.1")]
BannedPeer {
/// Identity of the banned peer.
peer_id: PeerId,
@ -556,6 +557,7 @@ where
if let Some(peer_id) = peer_id {
// Check if peer is banned.
if self.banned_peers.contains(&peer_id) {
#[allow(deprecated)]
let error = DialError::Banned;
self.behaviour
.on_swarm_event(FromSwarm::DialFailure(DialFailure {
@ -738,6 +740,7 @@ where
///
/// Any incoming connection and any dialing attempt will immediately be rejected.
/// This function has no effect if the peer is already banned.
#[deprecated(note = "Use `libp2p::allow_block_list` instead.", since = "0.42.1")]
pub fn ban_peer_id(&mut self, peer_id: PeerId) {
if self.banned_peers.insert(peer_id) {
// Note that established connections to the now banned peer are closed but not
@ -749,6 +752,7 @@ where
}
/// Unbans a peer.
#[deprecated(note = "Use `libp2p::allow_block_list` instead.", since = "0.42.1")]
pub fn unban_peer_id(&mut self, peer_id: PeerId) {
self.banned_peers.remove(&peer_id);
}
@ -809,6 +813,7 @@ where
established_in,
} => {
if self.banned_peers.contains(&peer_id) {
#[allow(deprecated)]
return Some(SwarmEvent::BannedPeer { peer_id, endpoint });
}
@ -1716,6 +1721,7 @@ where
#[derive(Debug)]
pub enum DialError {
/// The peer is currently banned.
#[deprecated(note = "Use `libp2p::allow_block_list` instead.", since = "0.42.1")]
Banned,
/// The configured limit for simultaneous outgoing connections
/// has been reached.
@ -1776,6 +1782,7 @@ impl fmt::Display for DialError {
f,
"Dial error: tried to dial local peer id at {endpoint:?}."
),
#[allow(deprecated)]
DialError::Banned => write!(f, "Dial error: peer is banned."),
DialError::DialPeerConditionFalse(c) => {
write!(f, "Dial error: condition {c:?} for dialing peer was false.")
@ -1827,6 +1834,7 @@ impl error::Error for DialError {
DialError::ConnectionLimit(err) => Some(err),
DialError::LocalPeerId { .. } => None,
DialError::NoAddresses => None,
#[allow(deprecated)]
DialError::Banned => None,
DialError::DialPeerConditionFalse(_) => None,
DialError::Aborted => None,
@ -1926,6 +1934,9 @@ impl error::Error for ListenError {
}
}
/// A connection was denied.
///
/// To figure out which [`NetworkBehaviour`] denied the connection, use [`ConnectionDenied::downcast`].
#[derive(Debug)]
pub struct ConnectionDenied {
inner: Box<dyn error::Error + Send + Sync + 'static>,
@ -2117,6 +2128,7 @@ mod tests {
/// [`FromSwarm::ConnectionEstablished`], [`FromSwarm::ConnectionClosed`]
/// calls should be registered.
#[test]
#[allow(deprecated)]
fn test_connect_disconnect_ban() {
let _ = env_logger::try_init();