Skip to content

Commit aaf87f8

Browse files
committed
Merge branch 'main' into ui-updates
2 parents d2809d6 + 3bd8921 commit aaf87f8

File tree

2 files changed

+57
-68
lines changed

2 files changed

+57
-68
lines changed

rust-peer/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
target/
1+
target/
2+
*.pem
3+
local_key

rust-peer/src/main.rs

Lines changed: 54 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::Result;
1+
use anyhow::{Context, Result};
22
use clap::Parser;
33
use futures::StreamExt;
44
use libp2p::{
@@ -15,58 +15,19 @@ use libp2p::{
1515
};
1616
use libp2p_quic as quic;
1717
use libp2p_webrtc as webrtc;
18+
use libp2p_webrtc::tokio::Certificate;
1819
use log::{debug, error, info, warn};
20+
use std::path::Path;
1921
use std::{
2022
borrow::Cow,
2123
collections::hash_map::DefaultHasher,
22-
fs::File,
2324
hash::{Hash, Hasher},
24-
io::{BufReader, Read},
2525
time::Duration,
2626
};
27+
use tokio::fs;
2728

2829
const TICK_INTERVAL: Duration = Duration::from_secs(15);
2930
const KADEMLIA_PROTOCOL_NAME: &'static [u8] = b"/universal-connectivity/lan/kad/1.0.0";
30-
const STATIC_CERTIFICATE: &'static [u8] = &[
31-
45, 45, 45, 45, 45, 66, 69, 71, 73, 78, 32, 69, 88, 80, 73, 82, 69, 83, 45, 45, 45, 45, 45, 10,
32-
65, 80, 102, 104, 110, 103, 56, 65, 65, 65, 65, 61, 10, 45, 45, 45, 45, 45, 69, 78, 68, 32, 69,
33-
88, 80, 73, 82, 69, 83, 45, 45, 45, 45, 45, 10, 10, 45, 45, 45, 45, 45, 66, 69, 71, 73, 78, 32,
34-
80, 82, 73, 86, 65, 84, 69, 95, 75, 69, 89, 45, 45, 45, 45, 45, 10, 77, 73, 71, 72, 65, 103,
35-
69, 65, 77, 66, 77, 71, 66, 121, 113, 71, 83, 77, 52, 57, 65, 103, 69, 71, 67, 67, 113, 71, 83,
36-
77, 52, 57, 65, 119, 69, 72, 66, 71, 48, 119, 97, 119, 73, 66, 65, 81, 81, 103, 105, 86, 56,
37-
82, 72, 69, 100, 118, 85, 101, 48, 57, 108, 80, 57, 83, 10, 104, 52, 78, 70, 73, 74, 69, 70,
38-
113, 88, 104, 88, 43, 68, 85, 97, 51, 76, 109, 88, 112, 79, 119, 115, 117, 76, 71, 104, 82, 65,
39-
78, 67, 65, 65, 84, 115, 69, 73, 65, 53, 121, 113, 67, 119, 56, 78, 122, 73, 87, 73, 114, 117,
40-
111, 86, 87, 43, 109, 116, 83, 113, 105, 70, 122, 106, 10, 50, 51, 77, 84, 43, 107, 88, 73, 52,
41-
77, 77, 49, 115, 52, 109, 65, 121, 122, 100, 82, 57, 90, 113, 115, 81, 70, 72, 43, 104, 55, 86,
42-
75, 106, 70, 67, 86, 122, 73, 86, 70, 52, 76, 47, 49, 97, 54, 81, 75, 47, 86, 81, 47, 53, 120,
43-
102, 66, 10, 45, 45, 45, 45, 45, 69, 78, 68, 32, 80, 82, 73, 86, 65, 84, 69, 95, 75, 69, 89,
44-
45, 45, 45, 45, 45, 10, 10, 45, 45, 45, 45, 45, 66, 69, 71, 73, 78, 32, 67, 69, 82, 84, 73, 70,
45-
73, 67, 65, 84, 69, 45, 45, 45, 45, 45, 10, 77, 73, 73, 66, 87, 84, 67, 66, 47, 54, 65, 68, 65,
46-
103, 69, 67, 65, 103, 104, 111, 117, 49, 43, 70, 77, 118, 103, 76, 88, 106, 65, 75, 66, 103,
47-
103, 113, 104, 107, 106, 79, 80, 81, 81, 68, 65, 106, 65, 104, 77, 82, 56, 119, 72, 81, 89, 68,
48-
86, 81, 81, 68, 68, 66, 90, 121, 10, 89, 50, 100, 108, 98, 105, 66, 122, 90, 87, 120, 109, 73,
49-
72, 78, 112, 90, 50, 53, 108, 90, 67, 66, 106, 90, 88, 74, 48, 77, 67, 65, 88, 68, 84, 99, 49,
50-
77, 68, 69, 119, 77, 84, 65, 119, 77, 68, 65, 119, 77, 70, 111, 89, 68, 122, 81, 119, 79, 84,
51-
89, 119, 77, 84, 65, 120, 10, 77, 68, 65, 119, 77, 68, 65, 119, 87, 106, 65, 104, 77, 82, 56,
52-
119, 72, 81, 89, 68, 86, 81, 81, 68, 68, 66, 90, 121, 89, 50, 100, 108, 98, 105, 66, 122, 90,
53-
87, 120, 109, 73, 72, 78, 112, 90, 50, 53, 108, 90, 67, 66, 106, 90, 88, 74, 48, 77, 70, 107,
54-
119, 69, 119, 89, 72, 10, 75, 111, 90, 73, 122, 106, 48, 67, 65, 81, 89, 73, 75, 111, 90, 73,
55-
122, 106, 48, 68, 65, 81, 99, 68, 81, 103, 65, 69, 55, 66, 67, 65, 79, 99, 113, 103, 115, 80,
56-
68, 99, 121, 70, 105, 75, 55, 113, 70, 86, 118, 112, 114, 85, 113, 111, 104, 99, 52, 57, 116,
57-
122, 69, 47, 112, 70, 10, 121, 79, 68, 68, 78, 98, 79, 74, 103, 77, 115, 51, 85, 102, 87, 97,
58-
114, 69, 66, 82, 47, 111, 101, 49, 83, 111, 120, 81, 108, 99, 121, 70, 82, 101, 67, 47, 57, 87,
59-
117, 107, 67, 118, 49, 85, 80, 43, 99, 88, 119, 97, 77, 102, 77, 66, 48, 119, 71, 119, 89, 68,
60-
86, 82, 48, 82, 10, 66, 66, 81, 119, 69, 111, 73, 81, 101, 85, 57, 76, 97, 122, 90, 84, 77,
61-
107, 70, 87, 97, 70, 108, 120, 86, 106, 86, 70, 89, 84, 65, 75, 66, 103, 103, 113, 104, 107,
62-
106, 79, 80, 81, 81, 68, 65, 103, 78, 74, 65, 68, 66, 71, 65, 105, 69, 65, 119, 65, 109, 109,
63-
47, 114, 103, 112, 10, 84, 80, 48, 57, 88, 77, 102, 83, 49, 118, 70, 79, 84, 65, 48, 122, 79,
64-
66, 75, 103, 53, 97, 56, 111, 86, 106, 83, 111, 89, 67, 48, 80, 83, 103, 52, 67, 73, 81, 68,
65-
54, 73, 121, 73, 115, 56, 76, 111, 117, 106, 109, 82, 102, 78, 102, 53, 115, 57, 106, 97, 121,
66-
72, 102, 107, 112, 10, 80, 67, 106, 106, 111, 104, 68, 112, 114, 122, 49, 67, 78, 75, 79, 66,
67-
53, 81, 61, 61, 10, 45, 45, 45, 45, 45, 69, 78, 68, 32, 67, 69, 82, 84, 73, 70, 73, 67, 65, 84,
68-
69, 45, 45, 45, 45, 45, 10,
69-
];
7031

7132
#[derive(Debug, Parser)]
7233
#[clap(name = "universal connectivity rust peer")]
@@ -90,8 +51,14 @@ async fn main() -> Result<()> {
9051
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
9152

9253
let opt = Opt::parse();
54+
let local_key = read_or_create_identity(&Path::new("./local_key"))
55+
.await
56+
.context("Failed to read identity")?;
57+
let webrtc_cert = read_or_create_certificate(&Path::new("./cert.pem"))
58+
.await
59+
.context("Failed to read certificate")?;
9360

94-
let mut swarm = create_swarm()?;
61+
let mut swarm = create_swarm(local_key, webrtc_cert)?;
9562

9663
swarm.listen_on(format!("/ip4/0.0.0.0/udp/9090/webrtc-direct").parse()?)?;
9764
swarm.listen_on(format!("/ip4/0.0.0.0/udp/9091/quic-v1").parse()?)?;
@@ -232,23 +199,10 @@ struct Behaviour {
232199
relay: relay::Behaviour,
233200
}
234201

235-
fn create_swarm() -> Result<Swarm<Behaviour>> {
236-
let local_key;
237-
238-
let opt = Opt::parse();
239-
240-
if let Some(use_cert) = opt.use_cert {
241-
let f = File::open(use_cert)?;
242-
let mut reader = BufReader::new(f);
243-
let mut buffer = Vec::new();
244-
245-
reader.read_to_end(&mut buffer)?;
246-
247-
local_key = identity::Keypair::ed25519_from_bytes(&mut buffer)?;
248-
} else {
249-
local_key = identity::Keypair::generate_ed25519();
250-
}
251-
202+
fn create_swarm(
203+
local_key: identity::Keypair,
204+
certificate: Certificate,
205+
) -> Result<Swarm<Behaviour>> {
252206
let local_peer_id = PeerId::from(local_key.public());
253207
debug!("Local peer id: {local_peer_id}");
254208

@@ -282,13 +236,8 @@ fn create_swarm() -> Result<Swarm<Behaviour>> {
282236
// subscribes to our topic
283237
gossipsub.subscribe(&topic)?;
284238

285-
let pem_str = std::str::from_utf8(STATIC_CERTIFICATE).unwrap();
286-
287239
let transport = {
288-
let webrtc = webrtc::tokio::Transport::new(
289-
local_key.clone(),
290-
webrtc::tokio::Certificate::from_pem(pem_str)?,
291-
);
240+
let webrtc = webrtc::tokio::Transport::new(local_key.clone(), certificate);
292241

293242
let quic = quic::tokio::Transport::new(quic::Config::new(&local_key));
294243

@@ -326,3 +275,41 @@ fn create_swarm() -> Result<Swarm<Behaviour>> {
326275
};
327276
Ok(SwarmBuilder::with_tokio_executor(transport, behaviour, local_peer_id).build())
328277
}
278+
279+
async fn read_or_create_certificate(path: &Path) -> Result<Certificate> {
280+
if path.exists() {
281+
let pem = fs::read_to_string(&path).await?;
282+
283+
info!("Using existing certificate from {}", path.display());
284+
285+
return Ok(Certificate::from_pem(&pem)?);
286+
}
287+
288+
let cert = Certificate::generate(&mut rand::thread_rng())?;
289+
fs::write(&path, &cert.serialize_pem().as_bytes()).await?;
290+
291+
info!(
292+
"Generated new certificate and wrote it to {}",
293+
path.display()
294+
);
295+
296+
Ok(cert)
297+
}
298+
299+
async fn read_or_create_identity(path: &Path) -> Result<identity::Keypair> {
300+
if path.exists() {
301+
let bytes = fs::read(&path).await?;
302+
303+
info!("Using existing identity from {}", path.display());
304+
305+
return Ok(identity::Keypair::from_protobuf_encoding(&bytes)?); // This only works for ed25519 but that is what we are using.
306+
}
307+
308+
let identity = identity::Keypair::generate_ed25519();
309+
310+
fs::write(&path, &identity.to_protobuf_encoding()?).await?;
311+
312+
info!("Generated new identity and wrote it to {}", path.display());
313+
314+
Ok(identity)
315+
}

0 commit comments

Comments
 (0)