2020-12-02 12:30:47 +00:00
|
|
|
use std::net::SocketAddr;
|
2020-12-02 17:10:07 +00:00
|
|
|
use std::sync::Arc;
|
2020-12-02 12:30:47 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2020-12-02 20:21:42 +00:00
|
|
|
use log::{info, warn};
|
2020-12-02 12:30:47 +00:00
|
|
|
|
|
|
|
use structopt::StructOpt;
|
2020-12-02 20:21:42 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-12-02 12:30:47 +00:00
|
|
|
|
|
|
|
use sodiumoxide::crypto::auth;
|
|
|
|
use sodiumoxide::crypto::sign::ed25519;
|
|
|
|
|
2020-12-02 19:12:24 +00:00
|
|
|
use netapp::NetApp;
|
2020-12-02 12:30:47 +00:00
|
|
|
use netapp::peering::basalt::*;
|
2020-12-02 20:21:42 +00:00
|
|
|
use netapp::message::*;
|
|
|
|
use netapp::proto::*;
|
2020-12-02 12:30:47 +00:00
|
|
|
|
|
|
|
#[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,
|
2020-12-02 17:10:07 +00:00
|
|
|
|
|
|
|
#[structopt(long = "view-size", short = "v", default_value = "100")]
|
|
|
|
view_size: usize,
|
|
|
|
|
|
|
|
#[structopt(long = "cache-size", short = "c", default_value = "1000")]
|
|
|
|
cache_size: usize,
|
|
|
|
|
|
|
|
#[structopt(long = "exchange-interval-secs", short = "x", default_value = "1")]
|
|
|
|
exchange_interval: u64,
|
|
|
|
|
|
|
|
#[structopt(long = "reset-interval-secs", short = "r", default_value = "10")]
|
|
|
|
reset_interval: u64,
|
|
|
|
|
|
|
|
#[structopt(long = "reset-count", short = "k", default_value = "20")]
|
|
|
|
reset_count: usize,
|
2020-12-02 12:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 17:10:07 +00:00
|
|
|
let basalt_params = BasaltParams {
|
|
|
|
view_size: opt.view_size,
|
|
|
|
cache_size: opt.cache_size,
|
|
|
|
exchange_interval: Duration::from_secs(opt.exchange_interval),
|
|
|
|
reset_interval: Duration::from_secs(opt.reset_interval),
|
|
|
|
reset_count: opt.reset_count,
|
2020-12-02 12:30:47 +00:00
|
|
|
};
|
|
|
|
let peering = Basalt::new(netapp.clone(), bootstrap_peers, basalt_params);
|
|
|
|
|
2020-12-02 20:21:42 +00:00
|
|
|
netapp.add_msg_handler::<ExampleMessage, _, _>(
|
|
|
|
|_from: ed25519::PublicKey, msg: ExampleMessage| {
|
|
|
|
info!("Got example message: {:?}, sending example response", msg);
|
|
|
|
async {
|
|
|
|
ExampleResponse{example_field: false}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-12-02 17:10:07 +00:00
|
|
|
tokio::join!(
|
2020-12-02 20:21:42 +00:00
|
|
|
sampling_loop(netapp.clone(), peering.clone()),
|
2020-12-02 17:10:07 +00:00
|
|
|
netapp.listen(),
|
|
|
|
peering.run(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-02 20:21:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
async fn sampling_loop(netapp: Arc<NetApp>, basalt: Arc<Basalt>) {
|
2020-12-02 17:10:07 +00:00
|
|
|
loop {
|
|
|
|
tokio::time::delay_for(Duration::from_secs(10)).await;
|
|
|
|
|
|
|
|
let peers = basalt.sample(10);
|
|
|
|
for p in peers {
|
|
|
|
info!("Sampled: {}", hex::encode(p));
|
2020-12-02 20:21:42 +00:00
|
|
|
|
|
|
|
let netapp2 = netapp.clone();
|
|
|
|
tokio::spawn(async move {
|
|
|
|
match netapp2.request(&p, ExampleMessage{
|
|
|
|
example_field: 42,
|
|
|
|
}, PRIO_NORMAL).await {
|
|
|
|
Ok(resp) => info!("Got example response: {:?}", resp),
|
|
|
|
Err(e) => warn!("Error with example request: {}", e),
|
|
|
|
}
|
|
|
|
});
|
2020-12-02 17:10:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-02 12:30:47 +00:00
|
|
|
}
|
2020-12-02 20:21:42 +00:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
struct ExampleMessage {
|
|
|
|
example_field: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
struct ExampleResponse {
|
|
|
|
example_field: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Message for ExampleMessage {
|
|
|
|
const KIND: MessageKind = 0x99000001;
|
|
|
|
type Response = ExampleResponse;
|
|
|
|
}
|