Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swarm: ban connections based on peerid #1065

Merged
merged 14 commits into from
Apr 18, 2019
34 changes: 32 additions & 2 deletions core/src/swarm/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use crate::{
use futures::prelude::*;
use smallvec::SmallVec;
use std::{error, fmt, io, ops::{Deref, DerefMut}};
use std::collections::HashSet;

/// Contains the state of the network, plus the way it should behave.
pub type Swarm<TTransport, TBehaviour> = ExpandedSwarm<
Expand Down Expand Up @@ -73,6 +74,9 @@ where
/// List of multiaddresses we're listening on, after account for external IP addresses and
/// similar mechanisms.
external_addrs: SmallVec<[Multiaddr; 8]>,

/// List of nodes for which we deny any incoming connection.
banned_peers: HashSet<PeerId>,
montekki marked this conversation as resolved.
Show resolved Hide resolved
}

impl<TTransport, TBehaviour, TInEvent, TOutEvent, THandler, THandlerErr> Deref for
Expand Down Expand Up @@ -210,6 +214,20 @@ where TBehaviour: NetworkBehaviour<ProtocolsHandler = THandler>,
me.external_addrs.push(addr);
}
}

/// Bans a peer by its peer ID.
///
/// Any incoming connection and any dialing attempt will immediately be rejected.
/// This function has no effect is the peer is already banned.
pub fn ban_peer_id(&mut self, peer_id: PeerId) {
montekki marked this conversation as resolved.
Show resolved Hide resolved
self.banned_peers.insert(peer_id.clone());
self.raw_swarm.peer(peer_id).into_connected().map(|c| c.close());
}

/// Unbans a peer.
pub fn unban_peer_id(&mut self, peer_id: PeerId) {
self.banned_peers.remove(&peer_id);
}
}

impl<TTransport, TBehaviour, TMuxer, TInEvent, TOutEvent, THandler, THandlerErr> Stream for
Expand Down Expand Up @@ -258,7 +276,14 @@ where TBehaviour: NetworkBehaviour<ProtocolsHandler = THandler>,
self.behaviour.inject_node_event(conn_info.peer_id().clone(), event);
},
Async::Ready(RawSwarmEvent::Connected { conn_info, endpoint }) => {
self.behaviour.inject_connected(conn_info.peer_id().clone(), endpoint);
if self.banned_peers.contains(conn_info.peer_id()) {
self.raw_swarm.peer(conn_info.peer_id().clone())
.into_connected()
.expect("the RawSwarm just notified us that we were connected; QED")
.close();
} else {
self.behaviour.inject_connected(conn_info.peer_id().clone(), endpoint);
}
},
Async::Ready(RawSwarmEvent::NodeClosed { conn_info, endpoint, .. }) => {
self.behaviour.inject_disconnected(conn_info.peer_id(), endpoint);
Expand Down Expand Up @@ -313,7 +338,11 @@ where TBehaviour: NetworkBehaviour<ProtocolsHandler = THandler>,
let _ = Swarm::dial_addr(self, address);
},
Async::Ready(NetworkBehaviourAction::DialPeer { peer_id }) => {
Swarm::dial(self, peer_id)
if self.banned_peers.contains(&peer_id) {
self.behaviour.inject_dial_failure(&peer_id);
} else {
Swarm::dial(self, peer_id);
}
},
Async::Ready(NetworkBehaviourAction::SendEvent { peer_id, event }) => {
if let Some(mut peer) = self.raw_swarm.peer(peer_id).into_connected() {
Expand Down Expand Up @@ -444,6 +473,7 @@ where TBehaviour: NetworkBehaviour,
supported_protocols,
listened_addrs: SmallVec::new(),
external_addrs: SmallVec::new(),
banned_peers: HashSet::new(),
}
}
}
Expand Down