2020-12-04 16:12:10 +00:00
|
|
|
use std::io::Write;
|
2021-10-15 13:34:03 +00:00
|
|
|
use std::net::SocketAddr;
|
2020-12-02 12:30:47 +00:00
|
|
|
|
|
|
|
use log::info;
|
|
|
|
|
|
|
|
use structopt::StructOpt;
|
|
|
|
|
|
|
|
use sodiumoxide::crypto::auth;
|
|
|
|
use sodiumoxide::crypto::sign::ed25519;
|
|
|
|
|
|
|
|
use netapp::peering::fullmesh::*;
|
2021-10-14 09:58:09 +00:00
|
|
|
use netapp::util::*;
|
2020-12-02 19:12:24 +00:00
|
|
|
use netapp::NetApp;
|
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-11 14:53:59 +00:00
|
|
|
|
|
|
|
#[structopt(long = "public-addr", short = "a")]
|
|
|
|
public_addr: Option<String>,
|
2020-12-02 12:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2020-12-04 12:58:34 +00:00
|
|
|
env_logger::Builder::new()
|
|
|
|
.parse_env("RUST_LOG")
|
|
|
|
.format(|buf, record| {
|
2020-12-07 12:35:24 +00:00
|
|
|
writeln!(
|
|
|
|
buf,
|
|
|
|
"{} {} {} {}",
|
|
|
|
chrono::Local::now().format("%s%.6f"),
|
|
|
|
record.module_path().unwrap_or("_"),
|
|
|
|
record.level(),
|
|
|
|
record.args()
|
|
|
|
)
|
2020-12-04 12:58:34 +00:00
|
|
|
})
|
|
|
|
.init();
|
2020-12-02 12:30:47 +00:00
|
|
|
|
|
|
|
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()));
|
|
|
|
|
2021-10-15 13:34:03 +00:00
|
|
|
let public_addr = opt.public_addr.map(|x| x.parse().unwrap());
|
|
|
|
let listen_addr: SocketAddr = opt.listen_addr.parse().unwrap();
|
|
|
|
info!("Node public address: {:?}", public_addr);
|
|
|
|
info!("Node listen address: {}", listen_addr);
|
|
|
|
|
2022-02-21 15:43:17 +00:00
|
|
|
let netapp = NetApp::new(0u64, netid.clone(), privkey.clone());
|
2020-12-02 12:30:47 +00:00
|
|
|
|
|
|
|
let mut bootstrap_peers = vec![];
|
|
|
|
for peer in opt.bootstrap_peers.iter() {
|
2021-10-14 09:58:09 +00:00
|
|
|
bootstrap_peers.push(parse_peer_addr(peer).expect("Invalid peer address"));
|
2020-12-02 12:30:47 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 13:34:03 +00:00
|
|
|
let peering = FullMeshPeeringStrategy::new(
|
|
|
|
netapp.clone(),
|
|
|
|
bootstrap_peers,
|
|
|
|
public_addr.map(|a| SocketAddr::new(a, listen_addr.port())),
|
|
|
|
);
|
2021-10-13 10:33:14 +00:00
|
|
|
|
|
|
|
info!("Add more peers to this mesh by running: fullmesh -n {} -l 127.0.0.1:$((1000 + $RANDOM)) -b {}@{}",
|
|
|
|
hex::encode(&netid),
|
|
|
|
hex::encode(&privkey.public_key()),
|
|
|
|
listen_addr);
|
|
|
|
|
2021-10-13 15:12:13 +00:00
|
|
|
let watch_cancel = netapp::util::watch_ctrl_c();
|
|
|
|
|
|
|
|
tokio::join!(
|
|
|
|
netapp.listen(listen_addr, public_addr, watch_cancel.clone()),
|
|
|
|
peering.run(watch_cancel),
|
|
|
|
);
|
2020-12-02 12:30:47 +00:00
|
|
|
}
|