diff options
author | Alex Auvolat <alex@adnab.me> | 2020-12-02 13:30:47 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2020-12-02 13:30:47 +0100 |
commit | d4de2ffc40fe9d003f12139053ca070eda0b7221 (patch) | |
tree | e95476f0b7a6d1c75cc462b3ea7eee74c4faf09f /examples | |
download | netapp-d4de2ffc40fe9d003f12139053ca070eda0b7221.tar.gz netapp-d4de2ffc40fe9d003f12139053ca070eda0b7221.zip |
First commit
Diffstat (limited to 'examples')
-rw-r--r-- | examples/basalt.rs | 76 | ||||
-rw-r--r-- | examples/fullmesh.rs | 68 |
2 files changed, 144 insertions, 0 deletions
diff --git a/examples/basalt.rs b/examples/basalt.rs new file mode 100644 index 0000000..e486e08 --- /dev/null +++ b/examples/basalt.rs @@ -0,0 +1,76 @@ +use std::net::SocketAddr; +use std::time::Duration; + +use log::info; + +use structopt::StructOpt; + +use sodiumoxide::crypto::auth; +use sodiumoxide::crypto::sign::ed25519; + +use netapp::netapp::*; +use netapp::peering::basalt::*; + +#[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() { + pretty_env_logger::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 basalt_params = BasaltParams{ + view_size: 100, + cache_size: 1000, + exchange_interval: Duration::from_secs(1), + reset_interval: Duration::from_secs(10), + reset_count: 20, + }; + let peering = Basalt::new(netapp.clone(), bootstrap_peers, basalt_params); + + tokio::join!(netapp.listen(), peering.run(),); +} diff --git a/examples/fullmesh.rs b/examples/fullmesh.rs new file mode 100644 index 0000000..8e2ae07 --- /dev/null +++ b/examples/fullmesh.rs @@ -0,0 +1,68 @@ +use std::net::SocketAddr; + +use log::info; + +use structopt::StructOpt; + +use sodiumoxide::crypto::auth; +use sodiumoxide::crypto::sign::ed25519; + +use netapp::netapp::*; +use netapp::peering::fullmesh::*; + +#[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() { + pretty_env_logger::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(),); +} |