use libp2p::{
bandwidth,
core::{
self,
either::EitherTransport,
muxing::StreamMuxerBox,
transport::{Boxed, OptionalTransport},
upgrade,
},
dns, identity, mplex, noise, tcp, websocket, PeerId, Transport,
};
use std::{sync::Arc, time::Duration};
pub use self::bandwidth::BandwidthSinks;
pub fn build_transport(
keypair: identity::Keypair,
memory_only: bool,
yamux_window_size: Option<u32>,
yamux_maximum_buffer_size: usize,
) -> (Boxed<(PeerId, StreamMuxerBox)>, Arc<BandwidthSinks>) {
let transport = if !memory_only {
let tcp_config = tcp::Config::new().nodelay(true);
let desktop_trans = tcp::tokio::Transport::new(tcp_config.clone());
let desktop_trans = websocket::WsConfig::new(desktop_trans)
.or_transport(tcp::tokio::Transport::new(tcp_config.clone()));
let dns_init = dns::TokioDnsConfig::system(desktop_trans);
EitherTransport::Left(if let Ok(dns) = dns_init {
EitherTransport::Left(dns)
} else {
let desktop_trans = tcp::tokio::Transport::new(tcp_config.clone());
let desktop_trans = websocket::WsConfig::new(desktop_trans)
.or_transport(tcp::tokio::Transport::new(tcp_config));
EitherTransport::Right(desktop_trans.map_err(dns::DnsErr::Transport))
})
} else {
EitherTransport::Right(OptionalTransport::some(
libp2p::core::transport::MemoryTransport::default(),
))
};
let (transport, bandwidth) = bandwidth::BandwidthLogging::new(transport);
let authentication_config =
{
let noise_keypair = noise::Keypair::<noise::X25519Spec>::new().into_authentic(&keypair)
.expect("can only fail in case of a hardware bug; since this signing is performed only \
once and at initialization, we're taking the bet that the inconvenience of a very \
rare panic here is basically zero");
let noise_legacy =
noise::LegacyConfig { recv_legacy_handshake: true, ..Default::default() };
let mut xx_config = noise::NoiseConfig::xx(noise_keypair);
xx_config.set_legacy_config(noise_legacy);
xx_config.into_authenticated()
};
let multiplexing_config = {
let mut mplex_config = mplex::MplexConfig::new();
mplex_config.set_max_buffer_behaviour(mplex::MaxBufferBehaviour::Block);
mplex_config.set_max_buffer_size(usize::MAX);
let mut yamux_config = libp2p::yamux::YamuxConfig::default();
yamux_config.set_window_update_mode(libp2p::yamux::WindowUpdateMode::on_read());
yamux_config.set_max_buffer_size(yamux_maximum_buffer_size);
if let Some(yamux_window_size) = yamux_window_size {
yamux_config.set_receive_window_size(yamux_window_size);
}
core::upgrade::SelectUpgrade::new(yamux_config, mplex_config)
};
let transport = transport
.upgrade(upgrade::Version::V1Lazy)
.authenticate(authentication_config)
.multiplex(multiplexing_config)
.timeout(Duration::from_secs(20))
.boxed();
(transport, bandwidth)
}