libp2p/builder/phase/
relay.rs

1use std::marker::PhantomData;
2
3#[cfg(feature = "relay")]
4use libp2p_core::muxing::StreamMuxerBox;
5use libp2p_core::upgrade::{InboundConnectionUpgrade, OutboundConnectionUpgrade};
6#[cfg(feature = "relay")]
7use libp2p_core::Transport;
8#[cfg(any(feature = "relay", feature = "websocket"))]
9use libp2p_core::{InboundUpgrade, Negotiated, OutboundUpgrade, StreamMuxer, UpgradeInfo};
10#[cfg(feature = "relay")]
11use libp2p_identity::PeerId;
12
13use super::*;
14use crate::SwarmBuilder;
15
16pub struct RelayPhase<T> {
17    pub(crate) transport: T,
18}
19
20#[cfg(feature = "relay")]
21impl<Provider, T: AuthenticatedMultiplexedTransport> SwarmBuilder<Provider, RelayPhase<T>> {
22    /// Adds a relay client transport.
23    ///
24    /// Note that both `security_upgrade` and `multiplexer_upgrade` take function pointers,
25    /// i.e. they take the function themselves (without the invocation via `()`), not the
26    /// result of the function invocation. See example below.
27    ///
28    /// ``` rust
29    /// # use libp2p::SwarmBuilder;
30    /// # use std::error::Error;
31    /// # async fn build_swarm() -> Result<(), Box<dyn Error>> {
32    /// let swarm = SwarmBuilder::with_new_identity()
33    ///     .with_tokio()
34    ///     .with_tcp(
35    ///         Default::default(),
36    ///         (libp2p_tls::Config::new, libp2p_noise::Config::new),
37    ///         libp2p_yamux::Config::default,
38    ///     )?
39    ///      .with_relay_client(
40    ///          (libp2p_tls::Config::new, libp2p_noise::Config::new),
41    ///          libp2p_yamux::Config::default,
42    ///      )?
43    /// # ;
44    /// # Ok(())
45    /// # }
46    /// ```
47    pub fn with_relay_client<SecUpgrade, SecStream, SecError, MuxUpgrade, MuxStream, MuxError>(
48        self,
49        security_upgrade: SecUpgrade,
50        multiplexer_upgrade: MuxUpgrade,
51    ) -> Result<
52        SwarmBuilder<
53            Provider,
54            BandwidthLoggingPhase<impl AuthenticatedMultiplexedTransport, libp2p_relay::client::Behaviour>,
55        >,
56        SecUpgrade::Error,
57        > where
58
59        SecStream: futures::AsyncRead + futures::AsyncWrite + Unpin + Send + 'static,
60        SecError: std::error::Error + Send + Sync + 'static,
61        SecUpgrade: IntoSecurityUpgrade<libp2p_relay::client::Connection>,
62        SecUpgrade::Upgrade: InboundConnectionUpgrade<Negotiated<libp2p_relay::client::Connection>, Output = (PeerId, SecStream), Error = SecError> + OutboundConnectionUpgrade<Negotiated<libp2p_relay::client::Connection>, Output = (PeerId, SecStream), Error = SecError> + Clone + Send + 'static,
63    <SecUpgrade::Upgrade as InboundConnectionUpgrade<Negotiated<libp2p_relay::client::Connection>>>::Future: Send,
64    <SecUpgrade::Upgrade as OutboundConnectionUpgrade<Negotiated<libp2p_relay::client::Connection>>>::Future: Send,
65    <<<SecUpgrade as IntoSecurityUpgrade<libp2p_relay::client::Connection>>::Upgrade as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send,
66    <<SecUpgrade as IntoSecurityUpgrade<libp2p_relay::client::Connection>>::Upgrade as UpgradeInfo>::Info: Send,
67
68        MuxStream: StreamMuxer + Send + 'static,
69        MuxStream::Substream: Send + 'static,
70        MuxStream::Error: Send + Sync + 'static,
71        MuxUpgrade: IntoMultiplexerUpgrade<SecStream>,
72        MuxUpgrade::Upgrade: InboundConnectionUpgrade<Negotiated<SecStream>, Output = MuxStream, Error = MuxError> + OutboundConnectionUpgrade<Negotiated<SecStream>, Output = MuxStream, Error = MuxError> + Clone + Send + 'static,
73    <MuxUpgrade::Upgrade as InboundConnectionUpgrade<Negotiated<SecStream>>>::Future: Send,
74    <MuxUpgrade::Upgrade as OutboundConnectionUpgrade<Negotiated<SecStream>>>::Future: Send,
75        MuxError: std::error::Error + Send + Sync + 'static,
76    <<<MuxUpgrade as IntoMultiplexerUpgrade<SecStream>>::Upgrade as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send,
77    <<MuxUpgrade as IntoMultiplexerUpgrade<SecStream>>::Upgrade as UpgradeInfo>::Info: Send,
78    {
79        let (relay_transport, relay_behaviour) =
80            libp2p_relay::client::new(self.keypair.public().to_peer_id());
81        let relay_transport = relay_transport
82            .upgrade(libp2p_core::upgrade::Version::V1Lazy)
83            .authenticate(security_upgrade.into_security_upgrade(&self.keypair)?)
84            .multiplex(multiplexer_upgrade.into_multiplexer_upgrade())
85            .map(|(p, c), _| (p, StreamMuxerBox::new(c)));
86
87        Ok(SwarmBuilder {
88            phase: BandwidthLoggingPhase {
89                relay_behaviour,
90                transport: relay_transport
91                    .or_transport(self.phase.transport)
92                    .map(|either, _| either.into_inner()),
93            },
94            keypair: self.keypair,
95            phantom: PhantomData,
96        })
97    }
98}
99
100pub struct NoRelayBehaviour;
101
102impl<Provider, T> SwarmBuilder<Provider, RelayPhase<T>> {
103    pub(crate) fn without_relay(
104        self,
105    ) -> SwarmBuilder<Provider, BandwidthLoggingPhase<T, NoRelayBehaviour>> {
106        SwarmBuilder {
107            keypair: self.keypair,
108            phantom: PhantomData,
109            phase: BandwidthLoggingPhase {
110                transport: self.phase.transport,
111                relay_behaviour: NoRelayBehaviour,
112            },
113        }
114    }
115}
116
117// Shortcuts
118#[cfg(feature = "metrics")]
119impl<Provider, T: AuthenticatedMultiplexedTransport> SwarmBuilder<Provider, RelayPhase<T>> {
120    pub fn with_bandwidth_metrics(
121        self,
122        registry: &mut libp2p_metrics::Registry,
123    ) -> SwarmBuilder<
124        Provider,
125        BehaviourPhase<impl AuthenticatedMultiplexedTransport, NoRelayBehaviour>,
126    > {
127        self.without_relay()
128            .without_bandwidth_logging()
129            .with_bandwidth_metrics(registry)
130    }
131}
132impl<Provider, T: AuthenticatedMultiplexedTransport> SwarmBuilder<Provider, RelayPhase<T>> {
133    pub fn with_behaviour<B, R: TryIntoBehaviour<B>>(
134        self,
135        constructor: impl FnOnce(&libp2p_identity::Keypair) -> R,
136    ) -> Result<SwarmBuilder<Provider, SwarmPhase<T, B>>, R::Error> {
137        self.without_relay()
138            .without_bandwidth_logging()
139            .without_bandwidth_metrics()
140            .with_behaviour(constructor)
141    }
142}