From 14f0c1afbff432f795676a93a30897cf1ccf06c3 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Thu, 12 Dec 2019 14:22:22 +0000 Subject: [PATCH 01/17] Refactored so that we have sane persistence functions --- src/commands/init.rs | 51 +++++-------------------------------- src/identity/mixnet.rs | 22 ++++++++++++++++ src/identity/mod.rs | 1 + src/identity/validators.rs | 1 + src/main.rs | 2 ++ src/persistence/mod.rs | 1 + src/persistence/pemstore.rs | 41 +++++++++++++++++++++++++++++ src/persistence/toml.rs | 1 + 8 files changed, 75 insertions(+), 45 deletions(-) create mode 100644 src/identity/mixnet.rs create mode 100644 src/identity/mod.rs create mode 100644 src/identity/validators.rs create mode 100644 src/persistence/mod.rs create mode 100644 src/persistence/pemstore.rs create mode 100644 src/persistence/toml.rs diff --git a/src/commands/init.rs b/src/commands/init.rs index 3552597aae..9bc9db962d 100644 --- a/src/commands/init.rs +++ b/src/commands/init.rs @@ -1,60 +1,21 @@ use crate::banner; +use crate::identity::mixnet; +use crate::persistence::pemstore::PemStore; use clap::ArgMatches; use dirs; -use pem::{encode, Pem}; -use std::fs::File; -use std::io::prelude::*; pub fn execute(matches: &ArgMatches) { println!("{}", banner()); println!("Initialising client..."); let id = matches.value_of("id").unwrap(); // required for now - - // don't unwrap it, pass it as it is, if it's None, choose a random - let _provider_id = matches.value_of("provider"); - let _init_local = matches.is_present("local"); - - let os_config_dir = dirs::config_dir().unwrap(); + let os_config_dir = dirs::config_dir().unwrap(); // grabs the OS default config dir let nym_client_config_dir = os_config_dir.join("nym").join("clients").join(id); println!("Writing keypairs to {:?}...", nym_client_config_dir); - write_pem_files(nym_client_config_dir); + let mix_keys = mixnet::KeyPair::new(); + let pem_store = PemStore::new(); + pem_store.write(mix_keys, nym_client_config_dir); println!("Client configuration completed.\n\n\n") } - -fn write_pem_files(nym_client_config_dir: std::path::PathBuf) { - std::fs::create_dir_all(nym_client_config_dir.clone()).unwrap(); - - let (private, public) = sphinx::crypto::keygen(); - write_pem_file( - nym_client_config_dir.clone(), - String::from("private.pem"), - private.to_bytes().to_vec(), - String::from("SPHINX CURVE25519 PRIVATE KEY"), - ); - write_pem_file( - nym_client_config_dir.clone(), - String::from("public.pem"), - public.to_bytes().to_vec(), - String::from("SPHINX CURVE25519 PUBLIC KEY"), - ); -} - -fn write_pem_file( - nym_client_config_dir: std::path::PathBuf, - filename: String, - data: Vec, - tag: String, -) { - let pem = Pem { - tag, - contents: data, - }; - let key = encode(&pem); - - let full_path = nym_client_config_dir.join(filename); - let mut file = File::create(full_path).unwrap(); - file.write_all(key.as_bytes()).unwrap(); -} diff --git a/src/identity/mixnet.rs b/src/identity/mixnet.rs new file mode 100644 index 0000000000..ae6a169d68 --- /dev/null +++ b/src/identity/mixnet.rs @@ -0,0 +1,22 @@ +use curve25519_dalek::montgomery::MontgomeryPoint; +use curve25519_dalek::scalar::Scalar; + +pub struct KeyPair { + pub private: Scalar, + pub public: MontgomeryPoint, +} + +impl KeyPair { + pub fn new() -> KeyPair { + let (private, public) = sphinx::crypto::keygen(); + KeyPair { private, public } + } + + pub fn private_bytes(&self) -> Vec { + self.private.to_bytes().to_vec() + } + + pub fn public_bytes(&self) -> Vec { + self.public.to_bytes().to_vec() + } +} diff --git a/src/identity/mod.rs b/src/identity/mod.rs new file mode 100644 index 0000000000..ae7f01c0eb --- /dev/null +++ b/src/identity/mod.rs @@ -0,0 +1 @@ +pub mod mixnet; diff --git a/src/identity/validators.rs b/src/identity/validators.rs new file mode 100644 index 0000000000..b18c85b0b2 --- /dev/null +++ b/src/identity/validators.rs @@ -0,0 +1 @@ +// TODO types for Validator keys will go in here once we hook this up. diff --git a/src/main.rs b/src/main.rs index ea6fabb444..2d2ce6f7e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,8 @@ use std::process; mod clients; mod commands; +mod identity; +mod persistence; mod utils; fn main() { diff --git a/src/persistence/mod.rs b/src/persistence/mod.rs new file mode 100644 index 0000000000..c21f39e57f --- /dev/null +++ b/src/persistence/mod.rs @@ -0,0 +1 @@ +pub mod pemstore; diff --git a/src/persistence/pemstore.rs b/src/persistence/pemstore.rs new file mode 100644 index 0000000000..e8091f091e --- /dev/null +++ b/src/persistence/pemstore.rs @@ -0,0 +1,41 @@ +use crate::identity::mixnet::KeyPair; +use pem::{encode, Pem}; +use std::fs::File; +use std::io::prelude::*; +use std::path::PathBuf; + +pub struct PemStore {} + +impl PemStore { + pub fn new() -> PemStore { + PemStore {} + } + pub fn write(&self, key_pair: KeyPair, path: PathBuf) { + std::fs::create_dir_all(path.clone()).unwrap(); + + self.write_pem_file( + path.clone(), + String::from("private.pem"), + key_pair.private_bytes(), + String::from("SPHINX CURVE25519 PRIVATE KEY"), + ); + self.write_pem_file( + path.clone(), + String::from("public.pem"), + key_pair.public_bytes(), + String::from("SPHINX CURVE25519 PUBLIC KEY"), + ); + } + + fn write_pem_file(&self, path: PathBuf, filename: String, data: Vec, tag: String) { + let pem = Pem { + tag, + contents: data, + }; + let key = encode(&pem); + + let full_path = path.join(filename); + let mut file = File::create(full_path).unwrap(); + file.write_all(key.as_bytes()).unwrap(); + } +} diff --git a/src/persistence/toml.rs b/src/persistence/toml.rs new file mode 100644 index 0000000000..f4f5578ad0 --- /dev/null +++ b/src/persistence/toml.rs @@ -0,0 +1 @@ +// TODO: we can put all the TOML config templating code in here once we get to that. From 71f06b1efcd50ec2528cdd2db5a3fb0b4469c921 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Thu, 12 Dec 2019 15:36:18 +0000 Subject: [PATCH 02/17] persistence: pem storage now working --- src/commands/init.rs | 13 +++++---- src/commands/run.rs | 7 ++--- src/identity/mixnet.rs | 13 +++++++++ src/persistence/mod.rs | 1 + src/persistence/pathfinder.rs | 21 +++++++++++++++ src/persistence/pemstore.rs | 50 ++++++++++++++++++++++++++--------- 6 files changed, 80 insertions(+), 25 deletions(-) create mode 100644 src/persistence/pathfinder.rs diff --git a/src/commands/init.rs b/src/commands/init.rs index 9bc9db962d..00af80c1f5 100644 --- a/src/commands/init.rs +++ b/src/commands/init.rs @@ -1,21 +1,20 @@ use crate::banner; use crate::identity::mixnet; +use crate::persistence::pathfinder::Pathfinder; use crate::persistence::pemstore::PemStore; use clap::ArgMatches; -use dirs; pub fn execute(matches: &ArgMatches) { println!("{}", banner()); println!("Initialising client..."); - let id = matches.value_of("id").unwrap(); // required for now - let os_config_dir = dirs::config_dir().unwrap(); // grabs the OS default config dir - let nym_client_config_dir = os_config_dir.join("nym").join("clients").join(id); + let id = matches.value_of("id").unwrap().to_string(); // required for now + let pathfinder = Pathfinder::new(id); - println!("Writing keypairs to {:?}...", nym_client_config_dir); + println!("Writing keypairs to {:?}...", pathfinder.config_dir); let mix_keys = mixnet::KeyPair::new(); - let pem_store = PemStore::new(); - pem_store.write(mix_keys, nym_client_config_dir); + let pem_store = PemStore::new(pathfinder); + pem_store.write(mix_keys); println!("Client configuration completed.\n\n\n") } diff --git a/src/commands/run.rs b/src/commands/run.rs index 0ce7a25f79..b7bec29923 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -1,5 +1,5 @@ use crate::clients::directory; -use crate::clients::directory::presence::{MixNodePresence, Topology}; +use crate::clients::directory::presence::Topology; use crate::clients::directory::requests::presence_topology_get::PresenceTopologyGetRequester; use crate::clients::directory::DirectoryClient; use crate::clients::mix::MixClient; @@ -15,10 +15,7 @@ use tokio::time::{interval_at, Instant}; pub fn execute(matches: &ArgMatches) { let custom_cfg = matches.value_of("customCfg"); - println!( - "Going to start client with custom config of: {:?}", - custom_cfg - ); + println!("Starting client with config: {:?}", custom_cfg); // Grab the network topology from the remote directory server let topology = get_topology(); diff --git a/src/identity/mixnet.rs b/src/identity/mixnet.rs index ae6a169d68..83507d7d70 100644 --- a/src/identity/mixnet.rs +++ b/src/identity/mixnet.rs @@ -1,6 +1,7 @@ use curve25519_dalek::montgomery::MontgomeryPoint; use curve25519_dalek::scalar::Scalar; +// This keypair serves as the user's identity within the Mixnet pub struct KeyPair { pub private: Scalar, pub public: MontgomeryPoint, @@ -12,6 +13,18 @@ impl KeyPair { KeyPair { private, public } } + pub fn from_bytes(private_bytes: Vec, public_bytes: Vec) -> KeyPair { + let mut bytes = [0; 32]; + bytes.copy_from_slice(&private_bytes[..]); + let private = Scalar::from_canonical_bytes(bytes).unwrap(); + + let mut bytes = [0; 32]; + bytes.copy_from_slice(&public_bytes[..]); + let public = MontgomeryPoint(bytes); + + KeyPair { private, public } + } + pub fn private_bytes(&self) -> Vec { self.private.to_bytes().to_vec() } diff --git a/src/persistence/mod.rs b/src/persistence/mod.rs index c21f39e57f..bc1ad593bd 100644 --- a/src/persistence/mod.rs +++ b/src/persistence/mod.rs @@ -1 +1,2 @@ +pub mod pathfinder; pub mod pemstore; diff --git a/src/persistence/pathfinder.rs b/src/persistence/pathfinder.rs new file mode 100644 index 0000000000..08110b9ae8 --- /dev/null +++ b/src/persistence/pathfinder.rs @@ -0,0 +1,21 @@ +use std::path::PathBuf; + +pub struct Pathfinder { + pub config_dir: PathBuf, + pub private_mix_key: PathBuf, + pub public_mix_key: PathBuf, +} + +impl Pathfinder { + pub fn new(id: String) -> Pathfinder { + let os_config_dir = dirs::config_dir().unwrap(); // grabs the OS default config dir + let config_dir = os_config_dir.join("nym").join("clients").join(id); + let private_mix_key = config_dir.join("private.pem"); + let public_mix_key = config_dir.join("public.pem"); + Pathfinder { + config_dir, + private_mix_key, + public_mix_key, + } + } +} diff --git a/src/persistence/pemstore.rs b/src/persistence/pemstore.rs index e8091f091e..0f367b48e7 100644 --- a/src/persistence/pemstore.rs +++ b/src/persistence/pemstore.rs @@ -1,41 +1,65 @@ use crate::identity::mixnet::KeyPair; -use pem::{encode, Pem}; +use crate::persistence::pathfinder::Pathfinder; +use pem::{encode, parse, Pem}; use std::fs::File; use std::io::prelude::*; use std::path::PathBuf; -pub struct PemStore {} +pub struct PemStore { + config_dir: PathBuf, + private_mix_key: PathBuf, + public_mix_key: PathBuf, +} impl PemStore { - pub fn new() -> PemStore { - PemStore {} + pub fn new(pathfinder: Pathfinder) -> PemStore { + PemStore { + config_dir: pathfinder.config_dir, + private_mix_key: pathfinder.private_mix_key, + public_mix_key: pathfinder.public_mix_key, + } } - pub fn write(&self, key_pair: KeyPair, path: PathBuf) { - std::fs::create_dir_all(path.clone()).unwrap(); + + pub fn read(&self) -> KeyPair { + let private = self.read_file(self.private_mix_key.clone()); + let public = self.read_file(self.public_mix_key.clone()); + + KeyPair::from_bytes(private, public) + } + + pub fn read_file(&self, filepath: PathBuf) -> Vec { + let mut pem_bytes = File::open(filepath).unwrap(); + let mut buf = Vec::new(); + pem_bytes.read_to_end(&mut buf).unwrap(); + let pem = parse(&buf).unwrap(); + pem.contents + } + // This should be refactored and made more generic for when we have other kinds of + // KeyPairs that we want to persist (e.g. validator keypairs, or keys for + // signing vs encryption). However, for the moment, it does the job. + pub fn write(&self, key_pair: KeyPair) { + std::fs::create_dir_all(self.config_dir.clone()).unwrap(); self.write_pem_file( - path.clone(), - String::from("private.pem"), + self.private_mix_key.clone(), key_pair.private_bytes(), String::from("SPHINX CURVE25519 PRIVATE KEY"), ); self.write_pem_file( - path.clone(), - String::from("public.pem"), + self.public_mix_key.clone(), key_pair.public_bytes(), String::from("SPHINX CURVE25519 PUBLIC KEY"), ); } - fn write_pem_file(&self, path: PathBuf, filename: String, data: Vec, tag: String) { + fn write_pem_file(&self, filepath: PathBuf, data: Vec, tag: String) { let pem = Pem { tag, contents: data, }; let key = encode(&pem); - let full_path = path.join(filename); - let mut file = File::create(full_path).unwrap(); + let mut file = File::create(filepath).unwrap(); file.write_all(key.as_bytes()).unwrap(); } } From 722f2daaef5b9d788ec8bec45881aa5b9c448c34 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Thu, 12 Dec 2019 15:40:24 +0000 Subject: [PATCH 03/17] pemstore: read_file can be private --- src/persistence/pemstore.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/persistence/pemstore.rs b/src/persistence/pemstore.rs index 0f367b48e7..c935fab437 100644 --- a/src/persistence/pemstore.rs +++ b/src/persistence/pemstore.rs @@ -27,7 +27,7 @@ impl PemStore { KeyPair::from_bytes(private, public) } - pub fn read_file(&self, filepath: PathBuf) -> Vec { + fn read_file(&self, filepath: PathBuf) -> Vec { let mut pem_bytes = File::open(filepath).unwrap(); let mut buf = Vec::new(); pem_bytes.read_to_end(&mut buf).unwrap(); From 1468deb2c0c8a032a2fd03b1a26920ac18b98507 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Thu, 12 Dec 2019 16:45:39 +0000 Subject: [PATCH 04/17] main: banner formatting --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index ea6fabb444..c9eaf03d40 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,7 +87,7 @@ fn execute(matches: ArgMatches) -> Result<(), String> { } fn usage() -> String { - banner() + "usage: --help to see available options." + banner() + "usage: --help to see available options.\n\n" } fn banner() -> String { From b0d31a88d54459fb363f5512f17be2ceceec66e1 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Fri, 13 Dec 2019 12:44:46 +0000 Subject: [PATCH 05/17] cargo: set up as a lib --- Cargo.toml | 4 ++++ src/lib.rs | 1 + 2 files changed, 5 insertions(+) create mode 100644 src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index b745efc446..42a07e2328 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,10 @@ version = "0.1.0" authors = ["Dave Hrycyszyn "] edition = "2018" +[lib] +name = "nym_client" +path = "src/lib.rs" + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000000..705f46dba5 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod clients; From f8c6f4633c18b792d5222bdb11d7e0de503c5558 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Fri, 13 Dec 2019 12:47:19 +0000 Subject: [PATCH 06/17] Passing models by reference to keep borrow checker happy --- src/clients/directory/requests/metrics_mixes_post.rs | 8 ++++---- src/clients/directory/requests/presence_coconodes_post.rs | 8 ++++---- src/clients/directory/requests/presence_mixnodes_post.rs | 8 ++++---- src/clients/directory/requests/presence_providers_post.rs | 8 ++++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/clients/directory/requests/metrics_mixes_post.rs b/src/clients/directory/requests/metrics_mixes_post.rs index 4414e8e195..b7d412af81 100644 --- a/src/clients/directory/requests/metrics_mixes_post.rs +++ b/src/clients/directory/requests/metrics_mixes_post.rs @@ -8,7 +8,7 @@ pub struct Request { pub trait MetricsMixPoster { fn new(base_url: String) -> Self; - fn post(&self, metric: MixMetric) -> Result; + fn post(&self, metric: &MixMetric) -> Result; } impl MetricsMixPoster for Request { @@ -19,7 +19,7 @@ impl MetricsMixPoster for Request { } } - fn post(&self, metric: MixMetric) -> Result { + fn post(&self, metric: &MixMetric) -> Result { let url = format!("{}{}", self.base_url, self.path); let client = reqwest::Client::new(); let mix_metric_vec = client.post(&url).json(&metric).send()?; @@ -43,7 +43,7 @@ mod metrics_get_request { let _m = mock("POST", "/api/metrics/mixes").with_status(400).create(); let req = Request::new(mockito::server_url()); let metric = fixtures::new_metric(); - let result = req.post(metric); + let result = req.post(&metric); assert_eq!(400, result.unwrap().status()); _m.assert(); } @@ -61,7 +61,7 @@ mod metrics_get_request { .create(); let req = Request::new(mockito::server_url()); let metric = fixtures::new_metric(); - let result = req.post(metric); + let result = req.post(&metric); assert_eq!(true, result.is_ok()); _m.assert(); } diff --git a/src/clients/directory/requests/presence_coconodes_post.rs b/src/clients/directory/requests/presence_coconodes_post.rs index c1e30b145e..bc5f3aaf42 100644 --- a/src/clients/directory/requests/presence_coconodes_post.rs +++ b/src/clients/directory/requests/presence_coconodes_post.rs @@ -8,7 +8,7 @@ pub struct Request { pub trait PresenceCocoNodesPoster { fn new(base_url: String) -> Self; - fn post(&self, presence: CocoPresence) -> Result; + fn post(&self, presence: &CocoPresence) -> Result; } impl PresenceCocoNodesPoster for Request { @@ -19,7 +19,7 @@ impl PresenceCocoNodesPoster for Request { } } - fn post(&self, presence: CocoPresence) -> Result { + fn post(&self, presence: &CocoPresence) -> Result { let url = format!("{}{}", self.base_url, self.path); let client = reqwest::Client::new(); let p = client.post(&url).json(&presence).send()?; @@ -45,7 +45,7 @@ mod metrics_get_request { .create(); let req = Request::new(mockito::server_url()); let presence = fixtures::new_presence(); - let result = req.post(presence); + let result = req.post(&presence); assert_eq!(400, result.unwrap().status()); _m.assert(); } @@ -65,7 +65,7 @@ mod metrics_get_request { .create(); let req = Request::new(mockito::server_url()); let presence = fixtures::new_presence(); - let result = req.post(presence); + let result = req.post(&presence); assert_eq!(true, result.is_ok()); _m.assert(); } diff --git a/src/clients/directory/requests/presence_mixnodes_post.rs b/src/clients/directory/requests/presence_mixnodes_post.rs index 4bb0686ba3..8781b9a3dc 100644 --- a/src/clients/directory/requests/presence_mixnodes_post.rs +++ b/src/clients/directory/requests/presence_mixnodes_post.rs @@ -8,7 +8,7 @@ pub struct Request { pub trait PresenceMixNodesPoster { fn new(base_url: String) -> Self; - fn post(&self, presence: MixNodePresence) -> Result; + fn post(&self, presence: &MixNodePresence) -> Result; } impl PresenceMixNodesPoster for Request { @@ -19,7 +19,7 @@ impl PresenceMixNodesPoster for Request { } } - fn post(&self, presence: MixNodePresence) -> Result { + fn post(&self, presence: &MixNodePresence) -> Result { let url = format!("{}{}", self.base_url, self.path); let client = reqwest::Client::new(); let p = client.post(&url).json(&presence).send()?; @@ -45,7 +45,7 @@ mod metrics_get_request { .create(); let req = Request::new(mockito::server_url()); let presence = fixtures::new_presence(); - let result = req.post(presence); + let result = req.post(&presence); assert_eq!(400, result.unwrap().status()); _m.assert(); } @@ -65,7 +65,7 @@ mod metrics_get_request { .create(); let req = Request::new(mockito::server_url()); let presence = fixtures::new_presence(); - let result = req.post(presence); + let result = req.post(&presence); assert_eq!(true, result.is_ok()); _m.assert(); } diff --git a/src/clients/directory/requests/presence_providers_post.rs b/src/clients/directory/requests/presence_providers_post.rs index 8819926802..c79dbd3a68 100644 --- a/src/clients/directory/requests/presence_providers_post.rs +++ b/src/clients/directory/requests/presence_providers_post.rs @@ -8,7 +8,7 @@ pub struct Request { pub trait PresenceMixProviderPoster { fn new(base_url: String) -> Self; - fn post(&self, presence: MixProviderPresence) -> Result; + fn post(&self, presence: &MixProviderPresence) -> Result; } impl PresenceMixProviderPoster for Request { @@ -19,7 +19,7 @@ impl PresenceMixProviderPoster for Request { } } - fn post(&self, presence: MixProviderPresence) -> Result { + fn post(&self, presence: &MixProviderPresence) -> Result { let url = format!("{}{}", self.base_url, self.path); let client = reqwest::Client::new(); let p = client.post(&url).json(&presence).send()?; @@ -45,7 +45,7 @@ mod metrics_get_request { .create(); let req = Request::new(mockito::server_url()); let presence = fixtures::new_presence(); - let result = req.post(presence); + let result = req.post(&presence); assert_eq!(400, result.unwrap().status()); _m.assert(); } @@ -65,7 +65,7 @@ mod metrics_get_request { .create(); let req = Request::new(mockito::server_url()); let presence = fixtures::new_presence(); - let result = req.post(presence); + let result = req.post(&presence); assert_eq!(true, result.is_ok()); _m.assert(); } From 2a7fdeeccb7aa16bda5f97c7a47e48229e6c0465 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Sat, 14 Dec 2019 15:37:07 +0000 Subject: [PATCH 07/17] presence: fixed incorrect URL for provider presence --- src/clients/directory/requests/presence_providers_post.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/clients/directory/requests/presence_providers_post.rs b/src/clients/directory/requests/presence_providers_post.rs index c79dbd3a68..5a41fac824 100644 --- a/src/clients/directory/requests/presence_providers_post.rs +++ b/src/clients/directory/requests/presence_providers_post.rs @@ -15,7 +15,7 @@ impl PresenceMixProviderPoster for Request { fn new(base_url: String) -> Self { Request { base_url, - path: "/api/presence/providers".to_string(), + path: "/api/presence/mixproviders".to_string(), } } From fdd8cc85c7aed333936f116df5a757752c7a3b10 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Sat, 14 Dec 2019 15:37:40 +0000 Subject: [PATCH 08/17] presence: added registered_clients to the provider model --- src/clients/directory/presence.rs | 7 +++++++ src/clients/directory/requests/presence_providers_post.rs | 1 + 2 files changed, 8 insertions(+) diff --git a/src/clients/directory/presence.rs b/src/clients/directory/presence.rs index 9fab6c1bd5..62968eae19 100644 --- a/src/clients/directory/presence.rs +++ b/src/clients/directory/presence.rs @@ -22,6 +22,13 @@ pub struct MixNodePresence { pub struct MixProviderPresence { pub host: String, pub pub_key: String, + pub registered_clients: Vec, +} + +#[derive(Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct MixProviderClient { + pub pub_key: String, } // Topology shows us the current state of the overall Nym network diff --git a/src/clients/directory/requests/presence_providers_post.rs b/src/clients/directory/requests/presence_providers_post.rs index 5a41fac824..dfe89f0c17 100644 --- a/src/clients/directory/requests/presence_providers_post.rs +++ b/src/clients/directory/requests/presence_providers_post.rs @@ -78,6 +78,7 @@ mod metrics_get_request { MixProviderPresence { host: "foo.com".to_string(), pub_key: "abc".to_string(), + registered_clients: vec![], } } } From 522c3d869cd646973ea9ad4c114c2572aac05139 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Sat, 14 Dec 2019 16:16:56 +0000 Subject: [PATCH 09/17] cargo: adding ws crate to enable websockets --- Cargo.lock | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index d4d5176172..ec7632c9b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -739,6 +739,11 @@ name = "lazy_static" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "lazycell" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "libc" version = "0.2.66" @@ -834,6 +839,17 @@ dependencies = [ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "mio-extras" +version = "2.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazycell 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", + "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "mio-named-pipes" version = "0.1.6" @@ -944,6 +960,7 @@ dependencies = [ "sfw-provider-requests 0.1.0", "sphinx 0.1.0", "tokio 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", + "ws 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1410,6 +1427,17 @@ dependencies = [ "sphinx 0.1.0", ] +[[package]] +name = "sha-1" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-buffer 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "sha2" version = "0.8.0" @@ -1875,6 +1903,23 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ws" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", + "httparse 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", + "mio-extras 2.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", + "sha-1 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "url 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ws2_32-sys" version = "0.2.1" @@ -1973,6 +2018,7 @@ dependencies = [ "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum keystream 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c33070833c9ee02266356de0c43f723152bd38bd96ddf52c82b3af10c9138b28" "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +"checksum lazycell 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f" "checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558" "checksum lioness 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4ae926706ba42c425c9457121178330d75e273df2e82e28b758faf3de3a9acb9" "checksum lock_api 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e57b3997725d2b60dbec1297f6c2e2957cc383db1cebd6be812163f969c7d586" @@ -1985,6 +2031,7 @@ dependencies = [ "checksum mime_guess 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1a0ed03949aef72dbdf3116a383d7b38b4768e6f960528cd6a6044aa9ed68599" "checksum miniz_oxide 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6f3f74f726ae935c3f514300cc6773a0c9492abc5e972d42ba0c0ebb88757625" "checksum mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)" = "302dec22bcf6bae6dfb69c647187f4b4d0fb6f535521f7bc022430ce8e12008f" +"checksum mio-extras 2.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "52403fe290012ce777c4626790c8951324a2b9e3316b3143779c72b029742f19" "checksum mio-named-pipes 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "f5e374eff525ce1c5b7687c4cef63943e7686524a387933ad27ca7ec43779cb3" "checksum mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "966257a94e196b11bb43aca423754d87429960a768de9414f3691d6957abf125" "checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" @@ -2044,6 +2091,7 @@ dependencies = [ "checksum serde_derive 1.0.103 (registry+https://github.com/rust-lang/crates.io-index)" = "a8c6faef9a2e64b0064f48570289b4bf8823b7581f1d6157c1b52152306651d0" "checksum serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)" = "48c575e0cc52bdd09b47f330f646cf59afc586e9c4e3ccd6fc1f625b8ea1dad7" "checksum serde_urlencoded 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "642dd69105886af2efd227f75a520ec9b44a820d65bc133a9131f7d229fd165a" +"checksum sha-1 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "23962131a91661d643c98940b20fcaffe62d776a823247be80a48fcb8b6fce68" "checksum sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7b4d8bfd0e469f417657573d8451fb33d16cfe0989359b93baf3a1ffc639543d" "checksum signal-hook-registry 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94f478ede9f64724c5d173d7bb56099ec3e2d9fc2774aac65d34b8b890405f41" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" @@ -2096,4 +2144,5 @@ dependencies = [ "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" "checksum winreg 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b2986deb581c4fe11b621998a5e53361efe6b48a151178d0cd9eeffa4dc6acc9" +"checksum ws 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a2c47b5798ccc774ffb93ff536aec7c4275d722fd9c740c83cdd1af1f2d94" "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" diff --git a/Cargo.toml b/Cargo.toml index 42a07e2328..13776b879a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,8 +20,8 @@ reqwest = "0.9.22" serde = { version = "1.0", features = ["derive"] } sphinx = { path = "../sphinx" } sfw-provider-requests = { path = "../nym-sfw-provider/sfw-provider-requests" } - tokio = { version = "0.2", features = ["full"] } +ws = "0.9.1" [dev-dependencies] mockito = "0.22.0" From 066e0fa67b71849f6ea82fd1608816b8f9dd3ae8 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Sat, 14 Dec 2019 16:57:11 +0000 Subject: [PATCH 10/17] sockets: setting up websockets feature. --- src/commands/mod.rs | 3 ++- src/commands/socket.rs | 21 --------------------- src/commands/tcpsocket.rs | 10 ++++++++++ src/commands/websocket.rs | 23 +++++++++++++++++++++++ src/sockets/mod.rs | 1 + src/sockets/tcp.rs | 0 src/sockets/ws.rs | 30 ++++++++++++++++++++++++++++++ 7 files changed, 66 insertions(+), 22 deletions(-) delete mode 100644 src/commands/socket.rs create mode 100644 src/commands/tcpsocket.rs create mode 100644 src/commands/websocket.rs create mode 100644 src/sockets/mod.rs create mode 100644 src/sockets/tcp.rs create mode 100644 src/sockets/ws.rs diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 3336430356..83edeba8a9 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,3 +1,4 @@ pub mod init; pub mod run; -pub mod socket; \ No newline at end of file +pub mod tcpsocket; +pub mod websocket; diff --git a/src/commands/socket.rs b/src/commands/socket.rs deleted file mode 100644 index 09cc09a557..0000000000 --- a/src/commands/socket.rs +++ /dev/null @@ -1,21 +0,0 @@ -use clap::ArgMatches; - -pub fn execute(matches: &ArgMatches) { - let custom_cfg = matches.value_of("customCfg"); - let socket_type = match matches.value_of("socketType").unwrap() { - TCP_SOCKET_TYPE => TCP_SOCKET_TYPE, - WEBSOCKET_SOCKET_TYPE => WEBSOCKET_SOCKET_TYPE, - other => panic!("Invalid socket type provided - {}", other), - }; - let port = match matches.value_of("port").unwrap().parse::() { - Ok(n) => n, - Err(err) => panic!("Invalid port value provided - {:?}", err), - }; - - println!( - "Going to start socket client with custom config of: {:?}", - custom_cfg - ); - println!("Using the following socket type: {:?}", socket_type); - println!("On the following port: {:?}", port); -} diff --git a/src/commands/tcpsocket.rs b/src/commands/tcpsocket.rs new file mode 100644 index 0000000000..79adfb29e6 --- /dev/null +++ b/src/commands/tcpsocket.rs @@ -0,0 +1,10 @@ +use clap::ArgMatches; + +pub fn execute(matches: &ArgMatches) { + let port = match matches.value_of("port").unwrap().parse::() { + Ok(n) => n, + Err(err) => panic!("Invalid port value provided - {:?}", err), + }; + + println!("On the following port: {:?}", port); +} diff --git a/src/commands/websocket.rs b/src/commands/websocket.rs new file mode 100644 index 0000000000..7d9889019c --- /dev/null +++ b/src/commands/websocket.rs @@ -0,0 +1,23 @@ +use crate::banner; +use crate::sockets::ws; +use clap::ArgMatches; +use std::net::ToSocketAddrs; + +pub fn execute(matches: &ArgMatches) { + let port = match matches.value_of("port").unwrap().parse::() { + Ok(n) => n, + Err(err) => panic!("Invalid port value provided - {:?}", err), + }; + + println!("{}", banner()); + println!("Starting websocket on port: {:?}", port); + println!("Listening for messages..."); + + let socket_address = ("127.0.0.1", port) + .to_socket_addrs() + .expect("Failed to combine host and port") + .next() + .expect("Failed to extract the socket address from the iterator"); + + ws::start(socket_address); +} diff --git a/src/sockets/mod.rs b/src/sockets/mod.rs new file mode 100644 index 0000000000..6757c99679 --- /dev/null +++ b/src/sockets/mod.rs @@ -0,0 +1 @@ +pub mod ws; diff --git a/src/sockets/tcp.rs b/src/sockets/tcp.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/sockets/ws.rs b/src/sockets/ws.rs new file mode 100644 index 0000000000..ee99ced4b6 --- /dev/null +++ b/src/sockets/ws.rs @@ -0,0 +1,30 @@ +use std::net::SocketAddr; +use ws::{listen, CloseCode, Handler, Message, Result, Sender}; + +struct Server { + out: Sender, +} + +impl Handler for Server { + fn on_message(&mut self, msg: Message) -> Result<()> { + foomp(); + // Echo the message back + self.out.send(msg) + } + + fn on_close(&mut self, code: CloseCode, reason: &str) { + match code { + CloseCode::Normal => println!("The client is done with the connection."), + CloseCode::Away => println!("The client is leaving the site."), + _ => println!("The client encountered an error: {}", reason), + } + } +} + +pub fn start(socket_address: SocketAddr) { + listen(socket_address, |out| Server { out: out }).unwrap() +} + +fn foomp() { + println!("FOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMMMMPPPPPPPPPPPPPPPPP"); +} From 3d812a36d6e329831d3f738ca3fcbd161638b47e Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Sat, 14 Dec 2019 16:57:32 +0000 Subject: [PATCH 11/17] main: changing structure of socket args, removing unused args --- src/main.rs | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index cf053edfac..996efb014b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod clients; mod commands; mod identity; mod persistence; +mod sockets; mod utils; fn main() { @@ -44,23 +45,20 @@ fn main() { ) ) .subcommand( - SubCommand::with_name("socket") - .about("Run a background Nym client listening on a specified socket") + SubCommand::with_name("tcpsocket") + .about("Run Nym client that listens for bytes on a TCP socket") .arg( - Arg::with_name("customCfg") - .short("cfg") - .long("customCfg") - .help("Path to custom configuration file of the client") + Arg::with_name("port") + .short("p") + .long("port") + .help("Port to listen on") .takes_value(true) + .required(true), ) - .arg( - Arg::with_name("socketType") - .short("s") - .long("socketType") - .help("Type of the socket we want to run on (tcp / websocket)") - .takes_value(true) - .required(true) - ) + ) + .subcommand( + SubCommand::with_name("websocket") + .about("Run Nym client that listens on a websocket") .arg( Arg::with_name("port") .short("p") @@ -82,8 +80,8 @@ fn execute(matches: ArgMatches) -> Result<(), String> { match matches.subcommand() { ("init", Some(m)) => Ok(commands::init::execute(m)), ("run", Some(m)) => Ok(commands::run::execute(m)), - ("socket", Some(m)) => Ok(commands::socket::execute(m)), - + ("tcpsocket", Some(m)) => Ok(commands::tcpsocket::execute(m)), + ("websocket", Some(m)) => Ok(commands::websocket::execute(m)), _ => Err(usage()), } } From 00e0a61237a807b7c2021967bd8ab0385e35fc76 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Sat, 14 Dec 2019 16:57:59 +0000 Subject: [PATCH 12/17] mix client: commenting unused code --- src/clients/mix.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/clients/mix.rs b/src/clients/mix.rs index b5f03a130f..84ea3df07c 100644 --- a/src/clients/mix.rs +++ b/src/clients/mix.rs @@ -26,8 +26,8 @@ impl MixClient { #[cfg(test)] mod sending_a_sphinx_packet { - use super::*; - use sphinx::SphinxPacket; + // use super::*; + // use sphinx::SphinxPacket; #[test] fn works() { From 13d14cff955af89f6aeade4be7a4d8b7ff0a5766 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Sat, 14 Dec 2019 16:58:31 +0000 Subject: [PATCH 13/17] presence provider: fixing tests --- src/clients/directory/requests/presence_providers_post.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/clients/directory/requests/presence_providers_post.rs b/src/clients/directory/requests/presence_providers_post.rs index dfe89f0c17..26cbf7576e 100644 --- a/src/clients/directory/requests/presence_providers_post.rs +++ b/src/clients/directory/requests/presence_providers_post.rs @@ -40,7 +40,7 @@ mod metrics_get_request { #[test] fn it_returns_an_error() { - let _m = mock("POST", "/api/presence/providers") + let _m = mock("POST", "/api/presence/mixproviders") .with_status(400) .create(); let req = Request::new(mockito::server_url()); @@ -59,7 +59,7 @@ mod metrics_get_request { let json = r#"{ "ok": true }"#; - let _m = mock("POST", "/api/presence/providers") + let _m = mock("POST", "/api/presence/mixproviders") .with_status(201) .with_body(json) .create(); From 631827f5eb2aed055d598cffd99468c56fa2d6be Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Sat, 14 Dec 2019 17:09:00 +0000 Subject: [PATCH 14/17] websocket: demonstrating send message in Rust code. --- src/sockets/ws.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/sockets/ws.rs b/src/sockets/ws.rs index ee99ced4b6..f4a08b4ceb 100644 --- a/src/sockets/ws.rs +++ b/src/sockets/ws.rs @@ -7,7 +7,7 @@ struct Server { impl Handler for Server { fn on_message(&mut self, msg: Message) -> Result<()> { - foomp(); + foomp(msg.clone()); // Echo the message back self.out.send(msg) } @@ -16,7 +16,9 @@ impl Handler for Server { match code { CloseCode::Normal => println!("The client is done with the connection."), CloseCode::Away => println!("The client is leaving the site."), - _ => println!("The client encountered an error: {}", reason), + _ => { + println!("The client encountered an error: {}", reason); + } } } } @@ -25,6 +27,8 @@ pub fn start(socket_address: SocketAddr) { listen(socket_address, |out| Server { out: out }).unwrap() } -fn foomp() { - println!("FOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMMMMPPPPPPPPPPPPPPPPP"); +// Proves we can call Rust methods from the websocket listener. Re-route it to wherever JS puts +// the `send_message` functionality. +fn foomp(msg: Message) { + println!("Foomp!: {:?}", msg); } From c3eedb709a573253d5f35b47b48c54cdb886a1d2 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Mon, 16 Dec 2019 10:55:57 +0000 Subject: [PATCH 15/17] Fixing URL for provdiders client --- src/clients/directory/requests/presence_providers_post.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/clients/directory/requests/presence_providers_post.rs b/src/clients/directory/requests/presence_providers_post.rs index dfe89f0c17..26cbf7576e 100644 --- a/src/clients/directory/requests/presence_providers_post.rs +++ b/src/clients/directory/requests/presence_providers_post.rs @@ -40,7 +40,7 @@ mod metrics_get_request { #[test] fn it_returns_an_error() { - let _m = mock("POST", "/api/presence/providers") + let _m = mock("POST", "/api/presence/mixproviders") .with_status(400) .create(); let req = Request::new(mockito::server_url()); @@ -59,7 +59,7 @@ mod metrics_get_request { let json = r#"{ "ok": true }"#; - let _m = mock("POST", "/api/presence/providers") + let _m = mock("POST", "/api/presence/mixproviders") .with_status(201) .with_body(json) .create(); From b32e1575a38a80ee5499c7c6b6e970891287a755 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Mon, 16 Dec 2019 11:41:00 +0000 Subject: [PATCH 16/17] main: removed unused customcfg option --- src/main.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index cf053edfac..e9a2a27091 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,21 +26,14 @@ fn main() { .help("Id of the provider we have preference to connect to. If left empty, a random provider will be chosen.") .takes_value(true) ) - .arg(Arg::with_name("local") - .long("local") - .help("Flag to indicate whether the client is expected to run on the local deployment.") - .takes_value(true) - ) ) .subcommand( SubCommand::with_name("run") .about("Run a persistent Nym client process") - .arg( - Arg::with_name("customCfg") - .short("cfg") - .long("customCfg") - .help("Path to custom configuration file of the client") - .takes_value(true) + .arg(Arg::with_name("local") + .long("local") + .help("Flag to indicate whether the client is expected to run on the local deployment.") + .takes_value(false) ) ) .subcommand( From d9a603aa1f394aa5ce34afd9ef580d6cad60508e Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Mon, 16 Dec 2019 11:41:18 +0000 Subject: [PATCH 17/17] run command: added the ability to start with a local directory server --- src/commands/run.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/commands/run.rs b/src/commands/run.rs index 9a72186984..c6567fe037 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -15,11 +15,11 @@ use tokio::runtime::Runtime; use tokio::time::{interval_at, Instant}; pub fn execute(matches: &ArgMatches) { - let custom_cfg = matches.value_of("customCfg"); - println!("Starting client with config: {:?}", custom_cfg); + let is_local = matches.is_present("local"); + println!("Starting client, local: {:?}", is_local); // Grab the network topology from the remote directory server - let topology = get_topology(); + let topology = get_topology(is_local); // Create the runtime, probably later move it to Client struct itself? let mut rt = Runtime::new().unwrap(); @@ -61,10 +61,14 @@ pub fn execute(matches: &ArgMatches) { }) } -fn get_topology() -> Topology { - let directory_config = directory::Config { - base_url: "https://directory.nymtech.net".to_string(), +fn get_topology(is_local: bool) -> Topology { + let url = if is_local { + "http://localhost:8080".to_string() + } else { + "https://directory.nymtech.net".to_string() }; + println!("Using directory server: {:?}", url); + let directory_config = directory::Config { base_url: url }; let directory = directory::Client::new(directory_config); let topology = directory