1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use libp2p::{
bandwidth,
core::{
self,
either::EitherTransport,
muxing::StreamMuxerBox,
transport::{Boxed, OptionalTransport},
upgrade,
},
identity, mplex, noise, wasm_ext, PeerId, Transport,
};
#[cfg(not(target_os = "unknown"))]
use libp2p::{dns, tcp, websocket};
use std::{sync::Arc, time::Duration};
pub use self::bandwidth::BandwidthSinks;
pub fn build_transport(
keypair: identity::Keypair,
memory_only: bool,
wasm_external_transport: Option<wasm_ext::ExtTransport>,
yamux_window_size: Option<u32>,
yamux_maximum_buffer_size: usize,
) -> (Boxed<(PeerId, StreamMuxerBox)>, Arc<BandwidthSinks>) {
let transport = if let Some(t) = wasm_external_transport {
OptionalTransport::some(t)
} else {
OptionalTransport::none()
};
#[cfg(not(target_os = "unknown"))]
let transport = transport.or_transport(if !memory_only {
let desktop_trans = tcp::TcpConfig::new().nodelay(true);
let desktop_trans =
websocket::WsConfig::new(desktop_trans.clone()).or_transport(desktop_trans);
let dns_init = futures::executor::block_on(dns::DnsConfig::system(desktop_trans.clone()));
OptionalTransport::some(if let Ok(dns) = dns_init {
EitherTransport::Left(dns)
} else {
EitherTransport::Right(desktop_trans.map_err(dns::DnsErr::Transport))
})
} else {
OptionalTransport::none()
});
let transport = transport.or_transport(if memory_only {
OptionalTransport::some(libp2p::core::transport::MemoryTransport::default())
} else {
OptionalTransport::none()
});
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 mut noise_legacy = noise::LegacyConfig::default();
noise_legacy.recv_legacy_handshake = true;
let mut xx_config = noise::NoiseConfig::xx(noise_keypair);
xx_config.set_legacy_config(noise_legacy.clone());
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)
}