1use crate::{
24 crypto::ed25519::Keypair,
25 executor::{DefaultExecutor, Executor},
26 protocol::{
27 libp2p::{bitswap, identify, kademlia, ping},
28 mdns::Config as MdnsConfig,
29 notification, request_response, UserProtocol,
30 },
31 transport::{
32 manager::limits::ConnectionLimitsConfig, tcp::config::Config as TcpConfig,
33 KEEP_ALIVE_TIMEOUT, MAX_PARALLEL_DIALS,
34 },
35 types::protocol::ProtocolName,
36 PeerId,
37};
38
39#[cfg(feature = "quic")]
40use crate::transport::quic::config::Config as QuicConfig;
41#[cfg(feature = "webrtc")]
42use crate::transport::webrtc::config::Config as WebRtcConfig;
43#[cfg(feature = "websocket")]
44use crate::transport::websocket::config::Config as WebSocketConfig;
45
46use multiaddr::Multiaddr;
47
48use std::{collections::HashMap, sync::Arc, time::Duration};
49
50#[derive(Debug, Copy, Clone)]
52pub enum Role {
53 Dialer,
55
56 Listener,
58}
59
60impl From<Role> for crate::yamux::Mode {
61 fn from(value: Role) -> Self {
62 match value {
63 Role::Dialer => crate::yamux::Mode::Client,
64 Role::Listener => crate::yamux::Mode::Server,
65 }
66 }
67}
68
69pub struct ConfigBuilder {
71 tcp: Option<TcpConfig>,
73
74 #[cfg(feature = "quic")]
76 quic: Option<QuicConfig>,
77
78 #[cfg(feature = "webrtc")]
80 webrtc: Option<WebRtcConfig>,
81
82 #[cfg(feature = "websocket")]
84 websocket: Option<WebSocketConfig>,
85
86 keypair: Option<Keypair>,
88
89 ping: Option<ping::Config>,
91
92 identify: Option<identify::Config>,
94
95 kademlia: Vec<kademlia::Config>,
97
98 bitswap: Option<bitswap::Config>,
100
101 notification_protocols: HashMap<ProtocolName, notification::Config>,
103
104 request_response_protocols: HashMap<ProtocolName, request_response::Config>,
106
107 user_protocols: HashMap<ProtocolName, Box<dyn UserProtocol>>,
109
110 mdns: Option<MdnsConfig>,
112
113 known_addresses: Vec<(PeerId, Vec<Multiaddr>)>,
115
116 executor: Option<Arc<dyn Executor>>,
118
119 max_parallel_dials: usize,
121
122 connection_limits: ConnectionLimitsConfig,
124
125 keep_alive_timeout: Duration,
127
128 use_system_dns_config: bool,
130}
131
132impl Default for ConfigBuilder {
133 fn default() -> Self {
134 Self::new()
135 }
136}
137
138impl ConfigBuilder {
139 pub fn new() -> Self {
141 Self {
142 tcp: None,
143 #[cfg(feature = "quic")]
144 quic: None,
145 #[cfg(feature = "webrtc")]
146 webrtc: None,
147 #[cfg(feature = "websocket")]
148 websocket: None,
149 keypair: None,
150 ping: None,
151 identify: None,
152 kademlia: Vec::new(),
153 bitswap: None,
154 mdns: None,
155 executor: None,
156 max_parallel_dials: MAX_PARALLEL_DIALS,
157 user_protocols: HashMap::new(),
158 notification_protocols: HashMap::new(),
159 request_response_protocols: HashMap::new(),
160 known_addresses: Vec::new(),
161 connection_limits: ConnectionLimitsConfig::default(),
162 keep_alive_timeout: KEEP_ALIVE_TIMEOUT,
163 use_system_dns_config: false,
164 }
165 }
166
167 pub fn with_tcp(mut self, config: TcpConfig) -> Self {
169 self.tcp = Some(config);
170 self
171 }
172
173 #[cfg(feature = "quic")]
175 pub fn with_quic(mut self, config: QuicConfig) -> Self {
176 self.quic = Some(config);
177 self
178 }
179
180 #[cfg(feature = "webrtc")]
182 pub fn with_webrtc(mut self, config: WebRtcConfig) -> Self {
183 self.webrtc = Some(config);
184 self
185 }
186
187 #[cfg(feature = "websocket")]
189 pub fn with_websocket(mut self, config: WebSocketConfig) -> Self {
190 self.websocket = Some(config);
191 self
192 }
193
194 pub fn with_keypair(mut self, keypair: Keypair) -> Self {
198 self.keypair = Some(keypair);
199 self
200 }
201
202 pub fn with_notification_protocol(mut self, config: notification::Config) -> Self {
204 self.notification_protocols.insert(config.protocol_name().clone(), config);
205 self
206 }
207
208 pub fn with_libp2p_ping(mut self, config: ping::Config) -> Self {
210 self.ping = Some(config);
211 self
212 }
213
214 pub fn with_libp2p_identify(mut self, config: identify::Config) -> Self {
216 self.identify = Some(config);
217 self
218 }
219
220 pub fn with_libp2p_kademlia(mut self, config: kademlia::Config) -> Self {
222 self.kademlia.push(config);
223 self
224 }
225
226 pub fn with_libp2p_bitswap(mut self, config: bitswap::Config) -> Self {
228 self.bitswap = Some(config);
229 self
230 }
231
232 pub fn with_request_response_protocol(mut self, config: request_response::Config) -> Self {
234 self.request_response_protocols.insert(config.protocol_name().clone(), config);
235 self
236 }
237
238 pub fn with_user_protocol(mut self, protocol: Box<dyn UserProtocol>) -> Self {
240 self.user_protocols.insert(protocol.protocol(), protocol);
241 self
242 }
243
244 pub fn with_mdns(mut self, config: MdnsConfig) -> Self {
246 self.mdns = Some(config);
247 self
248 }
249
250 pub fn with_known_addresses(
252 mut self,
253 addresses: impl Iterator<Item = (PeerId, Vec<Multiaddr>)>,
254 ) -> Self {
255 self.known_addresses = addresses.collect();
256 self
257 }
258
259 pub fn with_executor(mut self, executor: Arc<dyn Executor>) -> Self {
263 self.executor = Some(executor);
264 self
265 }
266
267 pub fn with_max_parallel_dials(mut self, max_parallel_dials: usize) -> Self {
271 self.max_parallel_dials = max_parallel_dials.max(1);
272 self
273 }
274
275 pub fn with_connection_limits(mut self, config: ConnectionLimitsConfig) -> Self {
277 self.connection_limits = config;
278 self
279 }
280
281 pub fn with_keep_alive_timeout(mut self, timeout: Duration) -> Self {
283 self.keep_alive_timeout = timeout;
284 self
285 }
286
287 pub fn with_system_resolver(mut self) -> Self {
289 self.use_system_dns_config = true;
290 self
291 }
292
293 pub fn build(mut self) -> Litep2pConfig {
295 let keypair = match self.keypair {
296 Some(keypair) => keypair,
297 None => Keypair::generate(),
298 };
299
300 Litep2pConfig {
301 keypair,
302 tcp: self.tcp.take(),
303 mdns: self.mdns.take(),
304 #[cfg(feature = "quic")]
305 quic: self.quic.take(),
306 #[cfg(feature = "webrtc")]
307 webrtc: self.webrtc.take(),
308 #[cfg(feature = "websocket")]
309 websocket: self.websocket.take(),
310 ping: self.ping.take(),
311 identify: self.identify.take(),
312 kademlia: self.kademlia,
313 bitswap: self.bitswap.take(),
314 max_parallel_dials: self.max_parallel_dials,
315 executor: self.executor.map_or(Arc::new(DefaultExecutor {}), |executor| executor),
316 user_protocols: self.user_protocols,
317 notification_protocols: self.notification_protocols,
318 request_response_protocols: self.request_response_protocols,
319 known_addresses: self.known_addresses,
320 connection_limits: self.connection_limits,
321 keep_alive_timeout: self.keep_alive_timeout,
322 use_system_dns_config: self.use_system_dns_config,
323 }
324 }
325}
326
327pub struct Litep2pConfig {
329 pub(crate) tcp: Option<TcpConfig>,
331
332 #[cfg(feature = "quic")]
334 pub(crate) quic: Option<QuicConfig>,
335
336 #[cfg(feature = "webrtc")]
338 pub(crate) webrtc: Option<WebRtcConfig>,
339
340 #[cfg(feature = "websocket")]
342 pub(crate) websocket: Option<WebSocketConfig>,
343
344 pub(crate) keypair: Keypair,
346
347 pub(crate) ping: Option<ping::Config>,
349
350 pub(crate) identify: Option<identify::Config>,
352
353 pub(crate) kademlia: Vec<kademlia::Config>,
355
356 pub(crate) bitswap: Option<bitswap::Config>,
358
359 pub(crate) notification_protocols: HashMap<ProtocolName, notification::Config>,
361
362 pub(crate) request_response_protocols: HashMap<ProtocolName, request_response::Config>,
364
365 pub(crate) user_protocols: HashMap<ProtocolName, Box<dyn UserProtocol>>,
367
368 pub(crate) mdns: Option<MdnsConfig>,
370
371 pub(crate) executor: Arc<dyn Executor>,
373
374 pub(crate) max_parallel_dials: usize,
376
377 pub(crate) known_addresses: Vec<(PeerId, Vec<Multiaddr>)>,
379
380 pub(crate) connection_limits: ConnectionLimitsConfig,
382
383 pub(crate) keep_alive_timeout: Duration,
385
386 pub(crate) use_system_dns_config: bool,
388}