aboutsummaryrefslogtreecommitdiff
path: root/examples/fullmesh.rs
blob: a61ab3da20d5bf26b90325bdb6dad6383dc3c3f3 (plain) (blame)
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
use std::net::SocketAddr;

use log::info;

use structopt::StructOpt;

use sodiumoxide::crypto::auth;
use sodiumoxide::crypto::sign::ed25519;

use netapp::peering::fullmesh::*;
use netapp::NetApp;

#[derive(StructOpt, Debug)]
#[structopt(name = "netapp")]
pub struct Opt {
	#[structopt(long = "network-key", short = "n")]
	network_key: Option<String>,

	#[structopt(long = "private-key", short = "p")]
	private_key: Option<String>,

	#[structopt(long = "bootstrap-peer", short = "b")]
	bootstrap_peers: Vec<String>,

	#[structopt(long = "listen-addr", short = "l", default_value = "127.0.0.1:1980")]
	listen_addr: String,
}

#[tokio::main]
async fn main() {
	env_logger::Builder::new()
		.parse_env("RUST_LOG")
		.format(|buf, record| {
			writeln!(buf,
					 "{} {} {}",
					 chrono::Local::now().format("%s%.6f"),
					 record.level(),
					 record.args()
					)
		})
		.init();

	let opt = Opt::from_args();

	let netid = match &opt.network_key {
		Some(k) => auth::Key::from_slice(&hex::decode(k).unwrap()).unwrap(),
		None => auth::gen_key(),
	};
	info!("Network key: {}", hex::encode(&netid));

	let privkey = match &opt.private_key {
		Some(k) => ed25519::SecretKey::from_slice(&hex::decode(k).unwrap()).unwrap(),
		None => {
			let (_pk, sk) = ed25519::gen_keypair();
			sk
		}
	};

	info!("Node private key: {}", hex::encode(&privkey));
	info!("Node public key: {}", hex::encode(&privkey.public_key()));

	let listen_addr = opt.listen_addr.parse().unwrap();
	let netapp = NetApp::new(listen_addr, netid, privkey);

	let mut bootstrap_peers = vec![];
	for peer in opt.bootstrap_peers.iter() {
		if let Some(delim) = peer.find('@') {
			let (key, ip) = peer.split_at(delim);
			let pubkey = ed25519::PublicKey::from_slice(&hex::decode(&key).unwrap()).unwrap();
			let ip = ip[1..].parse::<SocketAddr>().unwrap();
			bootstrap_peers.push((pubkey, ip));
		}
	}

	let peering = FullMeshPeeringStrategy::new(netapp.clone(), bootstrap_peers);

	tokio::join!(netapp.listen(), peering.run(),);
}