forked from libp2p/universal-connectivity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
389 lines (341 loc) · 14.8 KB
/
main.rs
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
mod protocol;
use anyhow::{Context, Result};
use clap::Parser;
use futures::future::{select, Either};
use futures::StreamExt;
use libp2p::{
core::muxing::StreamMuxerBox,
gossipsub, identify, identity,
kad::store::MemoryStore,
kad::{Behaviour as Kademlia, Config as KademliaConfig},
memory_connection_limits,
multiaddr::{Multiaddr, Protocol},
ping, relay,
request_response::{self, ProtocolSupport},
swarm::{NetworkBehaviour, Swarm, SwarmEvent},
PeerId, StreamProtocol, SwarmBuilder, Transport,
};
use libp2p_webrtc as webrtc;
use libp2p_webrtc::tokio::Certificate;
use log::{debug, error, info, warn};
use protocol::FileExchangeCodec;
use std::net::IpAddr;
use std::path::Path;
use std::{
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
time::Duration,
};
use tokio::fs;
use crate::protocol::FileRequest;
const TICK_INTERVAL: Duration = Duration::from_secs(15);
const KADEMLIA_PROTOCOL_NAME: StreamProtocol = StreamProtocol::new("/ipfs/kad/1.0.0");
const FILE_EXCHANGE_PROTOCOL: StreamProtocol =
StreamProtocol::new("/universal-connectivity-file/1");
const PORT_WEBRTC: u16 = 9090;
const PORT_QUIC: u16 = 9091;
const LOCAL_KEY_PATH: &str = "./local_key";
const LOCAL_CERT_PATH: &str = "./cert.pem";
const GOSSIPSUB_CHAT_TOPIC: &str = "universal-connectivity";
const GOSSIPSUB_CHAT_FILE_TOPIC: &str = "universal-connectivity-file";
const GOSSIPSUB_PEER_DISCOVERY: &str = "universal-connectivity-browser-peer-discovery";
const BOOTSTRAP_NODES: [&str; 4] = [
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
];
#[derive(Debug, Parser)]
#[clap(name = "universal connectivity rust peer")]
struct Opt {
/// Address to listen on.
#[clap(long, default_value = "0.0.0.0")]
listen_address: IpAddr,
/// If known, the external address of this node. Will be used to correctly advertise our external address across all transports.
#[clap(long, env)]
external_address: Option<IpAddr>,
/// Nodes to connect to on startup. Can be specified several times.
#[clap(
long,
default_value = "/dns/universal-connectivity-rust-peer.fly.dev/udp/9091/quic-v1"
)]
connect: Vec<Multiaddr>,
}
/// An example WebRTC peer that will accept connections
#[tokio::main]
async fn main() -> Result<()> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
let opt = Opt::parse();
let local_key = read_or_create_identity(Path::new(LOCAL_KEY_PATH))
.await
.context("Failed to read identity")?;
let webrtc_cert = read_or_create_certificate(Path::new(LOCAL_CERT_PATH))
.await
.context("Failed to read certificate")?;
let mut swarm = create_swarm(local_key, webrtc_cert)?;
let address_webrtc = Multiaddr::from(opt.listen_address)
.with(Protocol::Udp(PORT_WEBRTC))
.with(Protocol::WebRTCDirect);
let address_quic = Multiaddr::from(opt.listen_address)
.with(Protocol::Udp(PORT_QUIC))
.with(Protocol::QuicV1);
swarm
.listen_on(address_webrtc.clone())
.expect("listen on webrtc");
swarm
.listen_on(address_quic.clone())
.expect("listen on quic");
for addr in opt.connect {
if let Err(e) = swarm.dial(addr.clone()) {
debug!("Failed to dial {addr}: {e}");
}
}
for peer in &BOOTSTRAP_NODES {
let multiaddr: Multiaddr = peer.parse().expect("Failed to parse Multiaddr");
if let Err(e) = swarm.dial(multiaddr) {
debug!("Failed to dial {peer}: {e}");
}
}
let chat_topic_hash = gossipsub::IdentTopic::new(GOSSIPSUB_CHAT_TOPIC).hash();
let file_topic_hash = gossipsub::IdentTopic::new(GOSSIPSUB_CHAT_FILE_TOPIC).hash();
let peer_discovery_hash = gossipsub::IdentTopic::new(GOSSIPSUB_PEER_DISCOVERY).hash();
let mut tick = futures_timer::Delay::new(TICK_INTERVAL);
loop {
match select(swarm.next(), &mut tick).await {
Either::Left((event, _)) => match event.unwrap() {
SwarmEvent::NewListenAddr { address, .. } => {
if let Some(external_ip) = opt.external_address {
let external_address = address
.replace(0, |_| Some(external_ip.into()))
.expect("address.len > 1 and we always return `Some`");
swarm.add_external_address(external_address);
}
let p2p_address = address.with(Protocol::P2p(*swarm.local_peer_id()));
info!("Listening on {p2p_address}");
}
SwarmEvent::ConnectionEstablished { peer_id, .. } => {
info!("Connected to {peer_id}");
}
SwarmEvent::OutgoingConnectionError { peer_id, error, .. } => {
warn!("Failed to dial {peer_id:?}: {error}");
}
SwarmEvent::IncomingConnectionError { error, .. } => {
warn!("{:#}", anyhow::Error::from(error))
}
SwarmEvent::ConnectionClosed { peer_id, cause, .. } => {
warn!("Connection to {peer_id} closed: {cause:?}");
swarm.behaviour_mut().kademlia.remove_peer(&peer_id);
info!("Removed {peer_id} from the routing table (if it was in there).");
}
SwarmEvent::Behaviour(BehaviourEvent::Relay(e)) => {
debug!("{:?}", e);
}
SwarmEvent::Behaviour(BehaviourEvent::Gossipsub(
libp2p::gossipsub::Event::Message {
message_id: _,
propagation_source: _,
message,
},
)) => {
if message.topic == chat_topic_hash {
info!(
"Received message from {:?}: {}",
message.source,
String::from_utf8(message.data).unwrap()
);
continue;
}
if message.topic == file_topic_hash {
let file_id = String::from_utf8(message.data).unwrap();
info!("Received file {} from {:?}", file_id, message.source);
let request_id = swarm.behaviour_mut().request_response.send_request(
&message.source.unwrap(),
FileRequest {
file_id: file_id.clone(),
},
);
info!(
"Requested file {} to {:?}: req_id:{:?}",
file_id, message.source, request_id
);
continue;
}
if message.topic == peer_discovery_hash {
info!("Received peer discovery from {:?}", message.source);
continue;
}
error!("Unexpected gossipsub topic hash: {:?}", message.topic);
}
SwarmEvent::Behaviour(BehaviourEvent::Gossipsub(
libp2p::gossipsub::Event::Subscribed { peer_id, topic },
)) => {
debug!("{peer_id} subscribed to {topic}");
}
SwarmEvent::Behaviour(BehaviourEvent::Identify(e)) => {
info!("BehaviourEvent::Identify {:?}", e);
if let identify::Event::Received {
info: identify::Info { observed_addr, .. },
..
} = e
{
debug!("identify::Event::Received observed_addr: {}", observed_addr);
// this should switch us from client to server mode in kademlia
swarm.add_external_address(observed_addr);
}
}
SwarmEvent::Behaviour(BehaviourEvent::Kademlia(e)) => {
debug!("Kademlia event: {:?}", e);
}
SwarmEvent::Behaviour(BehaviourEvent::RequestResponse(
request_response::Event::Message { message, .. },
)) => match message {
request_response::Message::Request { request, .. } => {
//TODO: support ProtocolSupport::Full
debug!(
"umimplemented: request_response::Message::Request: {:?}",
request
);
}
request_response::Message::Response { response, .. } => {
info!(
"request_response::Message::Response: size:{}",
response.file_body.len()
);
// TODO: store this file (in memory or disk) and provider it via Kademlia
}
},
SwarmEvent::Behaviour(BehaviourEvent::RequestResponse(
request_response::Event::OutboundFailure {
request_id, error, ..
},
)) => {
error!(
"request_response::Event::OutboundFailure for request {:?}: {:?}",
request_id, error
);
}
event => {
debug!("Other type of event: {:?}", event);
}
},
Either::Right(_) => {
tick = futures_timer::Delay::new(TICK_INTERVAL);
debug!(
"external addrs: {:?}",
swarm.external_addresses().collect::<Vec<&Multiaddr>>()
);
if let Err(e) = swarm.behaviour_mut().kademlia.bootstrap() {
debug!("Failed to run Kademlia bootstrap: {e:?}");
}
}
}
}
}
#[derive(NetworkBehaviour)]
struct Behaviour {
gossipsub: gossipsub::Behaviour,
identify: identify::Behaviour,
kademlia: Kademlia<MemoryStore>,
relay: relay::Behaviour,
request_response: request_response::Behaviour<FileExchangeCodec>,
connection_limits: memory_connection_limits::Behaviour,
ping: ping::Behaviour,
}
fn create_swarm(
local_key: identity::Keypair,
certificate: Certificate,
) -> Result<Swarm<Behaviour>> {
let local_peer_id = PeerId::from(local_key.public());
debug!("Local peer id: {local_peer_id}");
// To content-address message, we can take the hash of message and use it as an ID.
let message_id_fn = |message: &gossipsub::Message| {
let mut s = DefaultHasher::new();
message.data.hash(&mut s);
gossipsub::MessageId::from(s.finish().to_string())
};
// Set a custom gossipsub configuration
let gossipsub_config = gossipsub::ConfigBuilder::default()
.validation_mode(gossipsub::ValidationMode::Permissive) // This sets the kind of message validation. The default is Strict (enforce message signing)
.message_id_fn(message_id_fn) // content-address messages. No two messages of the same content will be propagated.
.mesh_outbound_min(1)
.mesh_n_low(1)
.flood_publish(true)
.build()
.expect("Valid config");
// build a gossipsub network behaviour
let mut gossipsub = gossipsub::Behaviour::new(
gossipsub::MessageAuthenticity::Signed(local_key.clone()),
gossipsub_config,
)
.expect("Correct configuration");
// Create/subscribe Gossipsub topics
gossipsub.subscribe(&gossipsub::IdentTopic::new(GOSSIPSUB_CHAT_TOPIC))?;
gossipsub.subscribe(&gossipsub::IdentTopic::new(GOSSIPSUB_CHAT_FILE_TOPIC))?;
gossipsub.subscribe(&gossipsub::IdentTopic::new(GOSSIPSUB_PEER_DISCOVERY))?;
let identify_config = identify::Behaviour::new(identify::Config::new(
"/ipfs/0.1.0".into(),
local_key.public(),
));
// Create a Kademlia behaviour.
let cfg = KademliaConfig::new(KADEMLIA_PROTOCOL_NAME);
let store = MemoryStore::new(local_peer_id);
let kad_behaviour = Kademlia::with_config(local_peer_id, store, cfg);
let behaviour = Behaviour {
gossipsub,
identify: identify_config,
kademlia: kad_behaviour,
relay: relay::Behaviour::new(
local_peer_id,
relay::Config {
max_reservations: usize::MAX,
max_reservations_per_peer: 100,
reservation_rate_limiters: Vec::default(),
circuit_src_rate_limiters: Vec::default(),
max_circuits: usize::MAX,
max_circuits_per_peer: 100,
..Default::default()
},
),
request_response: request_response::Behaviour::new(
[(FILE_EXCHANGE_PROTOCOL, ProtocolSupport::Full)],
request_response::Config::default(),
),
connection_limits: memory_connection_limits::Behaviour::with_max_percentage(0.9),
ping: ping::Behaviour::default(),
};
Ok(SwarmBuilder::with_existing_identity(local_key.clone())
.with_tokio()
.with_quic()
.with_other_transport(|id_keys| {
Ok(webrtc::tokio::Transport::new(id_keys.clone(), certificate)
.map(|(peer_id, conn), _| (peer_id, StreamMuxerBox::new(conn))))
})?
.with_dns()?
.with_behaviour(|_key| behaviour)?
.build())
}
async fn read_or_create_certificate(path: &Path) -> Result<Certificate> {
if path.exists() {
let pem = fs::read_to_string(&path).await?;
info!("Using existing certificate from {}", path.display());
return Ok(Certificate::from_pem(&pem)?);
}
let cert = Certificate::generate(&mut rand::thread_rng())?;
fs::write(&path, &cert.serialize_pem().as_bytes()).await?;
info!(
"Generated new certificate and wrote it to {}",
path.display()
);
Ok(cert)
}
async fn read_or_create_identity(path: &Path) -> Result<identity::Keypair> {
if path.exists() {
let bytes = fs::read(&path).await?;
info!("Using existing identity from {}", path.display());
return Ok(identity::Keypair::from_protobuf_encoding(&bytes)?); // This only works for ed25519 but that is what we are using.
}
let identity = identity::Keypair::generate_ed25519();
fs::write(&path, &identity.to_protobuf_encoding()?).await?;
info!("Generated new identity and wrote it to {}", path.display());
Ok(identity)
}