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: Option<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
129impl Default for ConfigBuilder {
130 fn default() -> Self {
131 Self::new()
132 }
133}
134
135impl ConfigBuilder {
136 pub fn new() -> Self {
138 Self {
139 tcp: None,
140 #[cfg(feature = "quic")]
141 quic: None,
142 #[cfg(feature = "webrtc")]
143 webrtc: None,
144 #[cfg(feature = "websocket")]
145 websocket: None,
146 keypair: None,
147 ping: None,
148 identify: None,
149 kademlia: None,
150 bitswap: None,
151 mdns: None,
152 executor: None,
153 max_parallel_dials: MAX_PARALLEL_DIALS,
154 user_protocols: HashMap::new(),
155 notification_protocols: HashMap::new(),
156 request_response_protocols: HashMap::new(),
157 known_addresses: Vec::new(),
158 connection_limits: ConnectionLimitsConfig::default(),
159 keep_alive_timeout: KEEP_ALIVE_TIMEOUT,
160 }
161 }
162
163 pub fn with_tcp(mut self, config: TcpConfig) -> Self {
165 self.tcp = Some(config);
166 self
167 }
168
169 #[cfg(feature = "quic")]
171 pub fn with_quic(mut self, config: QuicConfig) -> Self {
172 self.quic = Some(config);
173 self
174 }
175
176 #[cfg(feature = "webrtc")]
178 pub fn with_webrtc(mut self, config: WebRtcConfig) -> Self {
179 self.webrtc = Some(config);
180 self
181 }
182
183 #[cfg(feature = "websocket")]
185 pub fn with_websocket(mut self, config: WebSocketConfig) -> Self {
186 self.websocket = Some(config);
187 self
188 }
189
190 pub fn with_keypair(mut self, keypair: Keypair) -> Self {
194 self.keypair = Some(keypair);
195 self
196 }
197
198 pub fn with_notification_protocol(mut self, config: notification::Config) -> Self {
200 self.notification_protocols.insert(config.protocol_name().clone(), config);
201 self
202 }
203
204 pub fn with_libp2p_ping(mut self, config: ping::Config) -> Self {
206 self.ping = Some(config);
207 self
208 }
209
210 pub fn with_libp2p_identify(mut self, config: identify::Config) -> Self {
212 self.identify = Some(config);
213 self
214 }
215
216 pub fn with_libp2p_kademlia(mut self, config: kademlia::Config) -> Self {
218 self.kademlia = Some(config);
219 self
220 }
221
222 pub fn with_libp2p_bitswap(mut self, config: bitswap::Config) -> Self {
224 self.bitswap = Some(config);
225 self
226 }
227
228 pub fn with_request_response_protocol(mut self, config: request_response::Config) -> Self {
230 self.request_response_protocols.insert(config.protocol_name().clone(), config);
231 self
232 }
233
234 pub fn with_user_protocol(mut self, protocol: Box<dyn UserProtocol>) -> Self {
236 self.user_protocols.insert(protocol.protocol(), protocol);
237 self
238 }
239
240 pub fn with_mdns(mut self, config: MdnsConfig) -> Self {
242 self.mdns = Some(config);
243 self
244 }
245
246 pub fn with_known_addresses(
248 mut self,
249 addresses: impl Iterator<Item = (PeerId, Vec<Multiaddr>)>,
250 ) -> Self {
251 self.known_addresses = addresses.collect();
252 self
253 }
254
255 pub fn with_executor(mut self, executor: Arc<dyn Executor>) -> Self {
259 self.executor = Some(executor);
260 self
261 }
262
263 pub fn with_max_parallel_dials(mut self, max_parallel_dials: usize) -> Self {
265 self.max_parallel_dials = max_parallel_dials;
266 self
267 }
268
269 pub fn with_connection_limits(mut self, config: ConnectionLimitsConfig) -> Self {
271 self.connection_limits = config;
272 self
273 }
274
275 pub fn with_keep_alive_timeout(mut self, timeout: Duration) -> Self {
277 self.keep_alive_timeout = timeout;
278 self
279 }
280
281 pub fn build(mut self) -> Litep2pConfig {
283 let keypair = match self.keypair {
284 Some(keypair) => keypair,
285 None => Keypair::generate(),
286 };
287
288 Litep2pConfig {
289 keypair,
290 tcp: self.tcp.take(),
291 mdns: self.mdns.take(),
292 #[cfg(feature = "quic")]
293 quic: self.quic.take(),
294 #[cfg(feature = "webrtc")]
295 webrtc: self.webrtc.take(),
296 #[cfg(feature = "websocket")]
297 websocket: self.websocket.take(),
298 ping: self.ping.take(),
299 identify: self.identify.take(),
300 kademlia: self.kademlia.take(),
301 bitswap: self.bitswap.take(),
302 max_parallel_dials: self.max_parallel_dials,
303 executor: self.executor.map_or(Arc::new(DefaultExecutor {}), |executor| executor),
304 user_protocols: self.user_protocols,
305 notification_protocols: self.notification_protocols,
306 request_response_protocols: self.request_response_protocols,
307 known_addresses: self.known_addresses,
308 connection_limits: self.connection_limits,
309 keep_alive_timeout: self.keep_alive_timeout,
310 }
311 }
312}
313
314pub struct Litep2pConfig {
316 pub(crate) tcp: Option<TcpConfig>,
318
319 #[cfg(feature = "quic")]
321 pub(crate) quic: Option<QuicConfig>,
322
323 #[cfg(feature = "webrtc")]
325 pub(crate) webrtc: Option<WebRtcConfig>,
326
327 #[cfg(feature = "websocket")]
329 pub(crate) websocket: Option<WebSocketConfig>,
330
331 pub(crate) keypair: Keypair,
333
334 pub(crate) ping: Option<ping::Config>,
336
337 pub(crate) identify: Option<identify::Config>,
339
340 pub(crate) kademlia: Option<kademlia::Config>,
342
343 pub(crate) bitswap: Option<bitswap::Config>,
345
346 pub(crate) notification_protocols: HashMap<ProtocolName, notification::Config>,
348
349 pub(crate) request_response_protocols: HashMap<ProtocolName, request_response::Config>,
351
352 pub(crate) user_protocols: HashMap<ProtocolName, Box<dyn UserProtocol>>,
354
355 pub(crate) mdns: Option<MdnsConfig>,
357
358 pub(crate) executor: Arc<dyn Executor>,
360
361 pub(crate) max_parallel_dials: usize,
363
364 pub(crate) known_addresses: Vec<(PeerId, Vec<Multiaddr>)>,
366
367 pub(crate) connection_limits: ConnectionLimitsConfig,
369
370 pub(crate) keep_alive_timeout: Duration,
372}