2020-10-16 09:14:39 +11:00
|
|
|
use anyhow::Result;
|
|
|
|
use libp2p::{
|
|
|
|
core::{
|
|
|
|
identity,
|
|
|
|
muxing::StreamMuxerBox,
|
2020-10-22 13:39:18 +11:00
|
|
|
transport::Boxed,
|
2020-10-16 09:14:39 +11:00
|
|
|
upgrade::{SelectUpgrade, Version},
|
2020-11-03 14:54:36 +11:00
|
|
|
Transport,
|
2020-10-16 09:14:39 +11:00
|
|
|
},
|
2020-10-22 13:39:18 +11:00
|
|
|
dns::DnsConfig,
|
2020-10-16 09:14:39 +11:00
|
|
|
mplex::MplexConfig,
|
2020-10-22 13:39:18 +11:00
|
|
|
noise::{self, NoiseConfig, X25519Spec},
|
2020-10-16 09:14:39 +11:00
|
|
|
yamux, PeerId,
|
|
|
|
};
|
|
|
|
|
2020-10-26 16:55:53 +11:00
|
|
|
/// Builds a libp2p transport without Tor with the following features:
|
2020-10-16 09:14:39 +11:00
|
|
|
/// - TcpConnection
|
|
|
|
/// - DNS name resolution
|
|
|
|
/// - authentication via noise
|
|
|
|
/// - multiplexing via yamux or mplex
|
|
|
|
pub fn build(id_keys: identity::Keypair) -> Result<SwapTransport> {
|
2020-10-26 16:55:53 +11:00
|
|
|
use libp2p::tcp::TokioTcpConfig;
|
|
|
|
|
2020-10-16 09:14:39 +11:00
|
|
|
let dh_keys = noise::Keypair::<X25519Spec>::new().into_authentic(&id_keys)?;
|
|
|
|
let noise = NoiseConfig::xx(dh_keys).into_authenticated();
|
|
|
|
|
|
|
|
let tcp = TokioTcpConfig::new().nodelay(true);
|
|
|
|
let dns = DnsConfig::new(tcp)?;
|
|
|
|
|
|
|
|
let transport = dns
|
|
|
|
.upgrade(Version::V1)
|
|
|
|
.authenticate(noise)
|
|
|
|
.multiplex(SelectUpgrade::new(
|
2021-01-15 16:58:16 +11:00
|
|
|
yamux::YamuxConfig::default(),
|
2020-10-16 09:14:39 +11:00
|
|
|
MplexConfig::new(),
|
|
|
|
))
|
|
|
|
.map(|(peer, muxer), _| (peer, StreamMuxerBox::new(muxer)))
|
|
|
|
.boxed();
|
|
|
|
|
|
|
|
Ok(transport)
|
|
|
|
}
|
2020-10-26 16:55:53 +11:00
|
|
|
|
2020-10-22 13:39:18 +11:00
|
|
|
pub type SwapTransport = Boxed<(PeerId, StreamMuxerBox)>;
|