node: adding a config struct and using it to pass args around

This commit is contained in:
Dave Hrycyszyn
2019-12-16 13:30:18 +00:00
parent 9d969904f2
commit efb900a538
3 changed files with 52 additions and 36 deletions
+17 -6
View File
@@ -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,
}
}
+6 -8
View File
@@ -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,
};
+29 -22
View File
@@ -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::<u16>() {
@@ -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,
}
}