From 287d2b31a56a30f492fd20045720b048583ef86e Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Mon, 16 Dec 2019 12:54:00 +0000 Subject: [PATCH 1/9] main: deleting unused key_file argument --- src/main.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 86882ebce0..a7f299ab5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,12 +32,6 @@ fn main() { .takes_value(true) .required(true), ) - .arg( - Arg::with_name("keyfile") - .long("keyfile") - .help("Optional path to the persistent keyfile of the node") - .takes_value(true), - ) .arg( Arg::with_name("local") .long("local") From 2b5b61ea26d929fb425870d54315042aa77a81e1 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Mon, 16 Dec 2019 12:54:16 +0000 Subject: [PATCH 2/9] node: adding a config struct --- src/node/mod.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/node/mod.rs b/src/node/mod.rs index 2e0ff0ed38..9bb7e6677c 100644 --- a/src/node/mod.rs +++ b/src/node/mod.rs @@ -13,6 +13,12 @@ use crate::mix_peer::MixPeer; mod presence; pub mod runner; +pub struct Config { + socket_address: SocketAddr, + secret_key: Scalar, + layer: usize, +} + // TODO: this will probably need to be moved elsewhere I imagine #[derive(Debug)] pub enum MixProcessingError { From 9d969904f2f9a58bcd18b92f5d815d96f6393f80 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Mon, 16 Dec 2019 13:29:48 +0000 Subject: [PATCH 3/9] cargo: adding base64 crate --- Cargo.lock | 1 + Cargo.toml | 1 + 2 files changed, 2 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index fe3e8fc935..b0bc07bb6e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -907,6 +907,7 @@ dependencies = [ name = "nym-mixnode" version = "0.1.0" dependencies = [ + "base64 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "curve25519-dalek 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "nym-client 0.1.0", diff --git a/Cargo.toml b/Cargo.toml index 737f97603a..47f4f140e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +base64 = "0.11.0" clap = "2.33.0" curve25519-dalek = "1.2.3" nym-client = { path = "../nym-client" } From efb900a538bda36f55fad0cca862ee42c5a4e995 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Mon, 16 Dec 2019 13:30:18 +0000 Subject: [PATCH 4/9] node: adding a config struct and using it to pass args around --- src/node/mod.rs | 23 ++++++++++++++------ src/node/presence.rs | 14 ++++++------ src/node/runner.rs | 51 +++++++++++++++++++++++++------------------- 3 files changed, 52 insertions(+), 36 deletions(-) diff --git a/src/node/mod.rs b/src/node/mod.rs index 9bb7e6677c..d79c617291 100644 --- a/src/node/mod.rs +++ b/src/node/mod.rs @@ -2,6 +2,7 @@ use std::net::SocketAddr; use std::sync::{Arc, RwLock}; use std::time::Duration; +use curve25519_dalek::montgomery::MontgomeryPoint; use curve25519_dalek::scalar::Scalar; use sphinx::header::delays::Delay as SphinxDelay; use sphinx::{ProcessedPacket, SphinxPacket}; @@ -14,9 +15,19 @@ mod presence; pub mod runner; pub struct Config { - socket_address: SocketAddr, - secret_key: Scalar, + directory_server: String, layer: usize, + public_key: MontgomeryPoint, + secret_key: Scalar, + socket_address: SocketAddr, +} + +impl Config { + pub fn public_key_string(&self) -> String { + let key_bytes = self.public_key.to_bytes().to_vec(); + let b64 = base64::encode(&key_bytes); + b64.to_string() + } } // TODO: this will probably need to be moved elsewhere I imagine @@ -119,11 +130,11 @@ pub struct MixNode { } impl MixNode { - pub fn new(network_address: SocketAddr, secret_key: Scalar, layer: usize) -> Self { + pub fn new(config: &Config) -> Self { MixNode { - network_address, - secret_key, - layer, + network_address: config.socket_address, + secret_key: config.secret_key, + layer: config.layer, } } diff --git a/src/node/presence.rs b/src/node/presence.rs index dac82d66cc..3e837c33b9 100644 --- a/src/node/presence.rs +++ b/src/node/presence.rs @@ -1,3 +1,4 @@ +use crate::node; use nym_client::clients::directory; use nym_client::clients::directory::presence::MixNodePresence; use nym_client::clients::directory::requests::presence_mixnodes_post::PresenceMixNodesPoster; @@ -11,17 +12,14 @@ pub struct Notifier { } impl Notifier { - pub fn new(is_local: bool) -> Notifier { - let url = if is_local { - "http://localhost:8080".to_string() - } else { - "https://directory.nymtech.net".to_string() + pub fn new(node_config: &node::Config) -> Notifier { + let config = directory::Config { + base_url: node_config.directory_server.clone(), }; - let config = directory::Config { base_url: url }; let net_client = directory::Client::new(config); let presence = MixNodePresence { - host: "halpin.org:6666".to_string(), - pub_key: "superkey".to_string(), + host: "localhost:6666".to_string(), // send dummy address as the directory server formats the real incoming IP. + pub_key: node_config.public_key_string(), layer: 666, last_seen: 666, }; diff --git a/src/node/runner.rs b/src/node/runner.rs index e91f0c53cd..c841069dd9 100644 --- a/src/node/runner.rs +++ b/src/node/runner.rs @@ -1,9 +1,9 @@ use crate::banner; +use crate::node; use crate::node::presence; use crate::node::MixNode; use clap::ArgMatches; -use curve25519_dalek::scalar::Scalar; use std::net::ToSocketAddrs; use std::thread; @@ -11,6 +11,20 @@ pub fn start(matches: &ArgMatches) { println!("{}", banner()); println!("Starting mixnode..."); + let config = new_config(matches); + + // println!("Startup on: {}", config.socket_address); + println!("Listening for incoming packets..."); + + let mix = MixNode::new(&config); + thread::spawn(move || { + let notifier = presence::Notifier::new(&config); + notifier.run(); + }); + mix.start_listening().unwrap(); +} + +fn new_config(matches: &ArgMatches) -> node::Config { let host = matches.value_of("host").unwrap_or("0.0.0.0"); let port = match matches.value_of("port").unwrap().parse::() { @@ -23,17 +37,6 @@ pub fn start(matches: &ArgMatches) { Err(err) => panic!("Invalid layer value provided - {:?}", err), }; - let secret_key: Scalar = match matches.value_of("keyfile") { - Some(keyfile) => { - // println!("TODO: load keyfile from <{:?}>", keyfile); - Default::default() - } - None => { - // println!("TODO: generate fresh sphinx keypair"); - Default::default() - } - }; - let is_local = matches.is_present("local"); let socket_address = (host, port) @@ -42,15 +45,19 @@ pub fn start(matches: &ArgMatches) { .next() .expect("Failed to extract the socket address from the iterator"); - thread::spawn(move || { - let notifier = presence::Notifier::new(is_local.clone()); - notifier.run(); - }); + let (secret_key, public_key) = sphinx::crypto::keygen(); - println!("Startup complete on: {}", socket_address); - println!("Listening for incoming packets..."); - // make sure our socket_address is equal to our predefined-hardcoded value - // assert_eq!("127.0.0.1:8080", socket_address.to_string()); - let mix = MixNode::new(socket_address, secret_key, layer); - mix.start_listening().unwrap(); + let directory_server = if is_local { + "http://localhost:8080".to_string() + } else { + "https://directory.nymtech.net".to_string() + }; + + node::Config { + directory_server, + layer, + public_key, + socket_address, + secret_key, + } } From ab02eec82424d42def186944aafab0bb2c26016d Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Mon, 16 Dec 2019 13:54:20 +0000 Subject: [PATCH 5/9] Using the node socket address to send to the directory server --- src/node/presence.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/presence.rs b/src/node/presence.rs index 3e837c33b9..c2acff1fc2 100644 --- a/src/node/presence.rs +++ b/src/node/presence.rs @@ -18,7 +18,7 @@ impl Notifier { }; let net_client = directory::Client::new(config); let presence = MixNodePresence { - host: "localhost:6666".to_string(), // send dummy address as the directory server formats the real incoming IP. + host: node_config.socket_address.to_string(), // note: the directory server formats the real incoming IP itself pub_key: node_config.public_key_string(), layer: 666, last_seen: 666, From 37a493439d767437a09adf0959ea10ae5d706e3e Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Mon, 16 Dec 2019 13:54:38 +0000 Subject: [PATCH 6/9] presence: sending URL-safe public key --- src/node/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/mod.rs b/src/node/mod.rs index d79c617291..36b1674b44 100644 --- a/src/node/mod.rs +++ b/src/node/mod.rs @@ -25,7 +25,7 @@ pub struct Config { impl Config { pub fn public_key_string(&self) -> String { let key_bytes = self.public_key.to_bytes().to_vec(); - let b64 = base64::encode(&key_bytes); + let b64 = base64::encode_config(&key_bytes, base64::URL_SAFE); b64.to_string() } } From c192afe9ebfaa20afb928e7f353b3abf02cc0ee6 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Mon, 16 Dec 2019 16:25:29 +0000 Subject: [PATCH 7/9] mix_peer: converting address to strongly typed socket address --- src/mix_peer.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mix_peer.rs b/src/mix_peer.rs index 6573c448f7..8aab5dda73 100644 --- a/src/mix_peer.rs +++ b/src/mix_peer.rs @@ -1,8 +1,9 @@ use std::error::Error; +use std::net::{Ipv4Addr, SocketAddrV4}; use tokio::prelude::*; pub struct MixPeer { - connection: String, + connection: SocketAddrV4, } impl MixPeer { From aa207e2c138b1bf44d47cc77b2b4c15cad3a8fb4 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Mon, 16 Dec 2019 16:25:49 +0000 Subject: [PATCH 8/9] mix_peer: using constant sized byte array for addressing --- src/mix_peer.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/mix_peer.rs b/src/mix_peer.rs index 8aab5dda73..053961718d 100644 --- a/src/mix_peer.rs +++ b/src/mix_peer.rs @@ -9,11 +9,12 @@ pub struct MixPeer { impl MixPeer { // note that very soon `next_hop_address` will be changed to `next_hop_metadata` pub fn new(next_hop_address: [u8; 32]) -> MixPeer { - let address = String::from_utf8_lossy(&next_hop_address) - .trim_end_matches(char::from(0)) - .to_string(); + let b = next_hop_address; + let host = Ipv4Addr::new(b[0], b[1], b[2], b[3]); + let port: u16 = u16::from_be_bytes([b[4], b[5]]); + let socket_address = SocketAddrV4::new(host, port); MixPeer { - connection: address, + connection: socket_address, } } From ddf0d762d7da440fe6a66a1f7a1a7e6a3bb4c31d Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Mon, 16 Dec 2019 16:26:30 +0000 Subject: [PATCH 9/9] mix_peer: adding a to_string() definition --- src/mix_peer.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/mix_peer.rs b/src/mix_peer.rs index 053961718d..c2fbde8477 100644 --- a/src/mix_peer.rs +++ b/src/mix_peer.rs @@ -24,4 +24,8 @@ impl MixPeer { stream.write_all(&bytes).await?; Ok(()) } + + pub fn to_string(&self) -> String { + self.connection.to_string() + } }