diff --git a/sn_networking/src/driver.rs b/sn_networking/src/driver.rs index e79766db2b..a1a2d48850 100644 --- a/sn_networking/src/driver.rs +++ b/sn_networking/src/driver.rs @@ -34,7 +34,7 @@ use futures::future::Either; use futures::StreamExt; #[cfg(feature = "local-discovery")] use libp2p::mdns; -use libp2p::Transport as _; +use libp2p::{swarm::behaviour::toggle::Toggle, Transport as _}; use libp2p::{core::muxing::StreamMuxerBox, relay}; use libp2p::{ identity::Keypair, @@ -187,7 +187,7 @@ pub enum VerificationKind { #[behaviour(to_swarm = "NodeEvent")] pub(super) struct NodeBehaviour { #[cfg(feature = "upnp")] - pub(super) upnp: libp2p::swarm::behaviour::toggle::Toggle, + pub(super) upnp: Toggle, pub(super) request_response: request_response::cbor::Behaviour, pub(super) kademlia: kad::Behaviour, #[cfg(feature = "local-discovery")] @@ -195,7 +195,7 @@ pub(super) struct NodeBehaviour { pub(super) identify: libp2p::identify::Behaviour, pub(super) dcutr: libp2p::dcutr::Behaviour, pub(super) relay_client: libp2p::relay::client::Behaviour, - pub(super) relay_server: libp2p::relay::Behaviour, + pub(super) relay_server: Toggle, } #[derive(Debug)] @@ -215,6 +215,7 @@ pub struct NetworkBuilder { metrics_server_port: Option, #[cfg(feature = "upnp")] upnp: bool, + relay_server: bool, } impl NetworkBuilder { @@ -234,6 +235,7 @@ impl NetworkBuilder { metrics_server_port: None, #[cfg(feature = "upnp")] upnp: false, + relay_server: true, } } @@ -272,6 +274,10 @@ impl NetworkBuilder { self.upnp = upnp; } + pub fn relay_server(&mut self, enabled: bool) { + self.relay_server = enabled; + } + /// Creates a new `SwarmDriver` instance, along with a `Network` handle /// for sending commands and an `mpsc::Receiver` for receiving /// network events. It initializes the swarm, sets up the transport, and @@ -334,6 +340,7 @@ impl NetworkBuilder { let listen_addr = self.listen_addr; #[cfg(feature = "upnp")] let upnp = self.upnp; + let relay_server = self.relay_server; let (network, events_receiver, mut swarm_driver) = self.build( kad_cfg, @@ -343,6 +350,7 @@ impl NetworkBuilder { IDENTIFY_NODE_VERSION_STR.to_string(), #[cfg(feature = "upnp")] upnp, + relay_server, )?; // Listen on the provided address @@ -396,12 +404,14 @@ impl NetworkBuilder { IDENTIFY_CLIENT_VERSION_STR.to_string(), #[cfg(feature = "upnp")] false, + false, )?; Ok((network, net_event_recv, driver)) } /// Private helper to create the network components with the provided config and req/res behaviour + #[allow(clippy::too_many_arguments)] fn build( self, kad_cfg: kad::Config, @@ -410,6 +420,7 @@ impl NetworkBuilder { req_res_protocol: ProtocolSupport, identify_version: String, #[cfg(feature = "upnp")] upnp: bool, + relay_server: bool, ) -> Result<(Network, mpsc::Receiver, SwarmDriver)> { let peer_id = PeerId::from(self.keypair.public()); // vdash metric (if modified please notify at https://github.com/happybeing/vdash/issues): @@ -543,15 +554,20 @@ impl NetworkBuilder { }) .boxed(); - let relay_server = { + let relay_server = if relay_server { + debug!("Enabling relay server behavior"); let relay_server_cfg = relay::Config { max_reservations: 1024, // the number of home nodes that we can support max_circuits: 32_000, // total max number of relayed connections we can support max_circuits_per_peer: 1024, // max number of relayed connections per peer ..Default::default() }; - libp2p::relay::Behaviour::new(peer_id, relay_server_cfg) - }; + Some(libp2p::relay::Behaviour::new(peer_id, relay_server_cfg)) + } else { + debug!("Disabling relay server behavior"); + None + } + .into(); let behaviour = NodeBehaviour { relay_client: relay_behaviour, diff --git a/sn_node/src/bin/safenode/main.rs b/sn_node/src/bin/safenode/main.rs index e3bd77dd20..00a768fdcf 100644 --- a/sn_node/src/bin/safenode/main.rs +++ b/sn_node/src/bin/safenode/main.rs @@ -79,6 +79,10 @@ struct Opt { #[clap(long, default_value_t = false)] upnp: bool, + /// Disable relay server functionality. Peers wil not be able to use this server as a relay. + #[clap(long, default_value_t = false)] + no_relay_server: bool, + /// Specify the logging output destination. /// /// Valid values are "stdout", "data-dir", or a custom path. @@ -220,6 +224,7 @@ fn main() -> Result<()> { opt.owner.clone(), #[cfg(feature = "upnp")] opt.upnp, + !opt.no_relay_server, ); node_builder.is_behind_home_network = opt.home_network; #[cfg(feature = "open-metrics")] diff --git a/sn_node/src/node.rs b/sn_node/src/node.rs index 2f91875b1a..982e7cc0ad 100644 --- a/sn_node/src/node.rs +++ b/sn_node/src/node.rs @@ -83,10 +83,12 @@ pub struct NodeBuilder { owner: Option, #[cfg(feature = "upnp")] upnp: bool, + relay_server: bool, } impl NodeBuilder { /// Instantiate the builder + #[allow(clippy::too_many_arguments)] pub fn new( keypair: Keypair, addr: SocketAddr, @@ -95,6 +97,7 @@ impl NodeBuilder { root_dir: PathBuf, owner: Option, #[cfg(feature = "upnp")] upnp: bool, + relay_server: bool, ) -> Self { Self { keypair, @@ -108,6 +111,7 @@ impl NodeBuilder { owner, #[cfg(feature = "upnp")] upnp, + relay_server, } } @@ -162,9 +166,9 @@ impl NodeBuilder { network_builder.metrics_server_port(self.metrics_server_port); network_builder.initial_peers(self.initial_peers.clone()); network_builder.is_behind_home_network(self.is_behind_home_network); - #[cfg(feature = "upnp")] network_builder.upnp(self.upnp); + network_builder.relay_server(self.relay_server); let (network, network_event_receiver, swarm_driver) = network_builder.build_node()?; let node_events_channel = NodeEventsChannel::default();