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