From 94231356d1be4c86e1f952a5f850ab46b9170018 Mon Sep 17 00:00:00 2001 From: Jedrzej Stuczynski Date: Mon, 9 Dec 2019 10:47:04 +0000 Subject: [PATCH 1/2] slightly increased default interval --- src/main.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4cf3bae6b9..bdc2ace554 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,8 +10,8 @@ use tokio::time::{interval_at, Instant}; #[tokio::main] async fn main() { let start = Instant::now() + Duration::from_nanos(1000); - let mut interval = interval_at(start, Duration::from_nanos(1000)); - let mut i = 0; + let mut interval = interval_at(start, Duration::from_millis(5000)); + let mut i: usize = 0; loop { interval.tick().await; let message = format!("Hello, Sphinx {}", i).as_bytes().to_vec(); @@ -22,6 +22,7 @@ async fn main() { let destination = directory.get_destination(); let delays = sphinx::header::delays::generate(2); + println!("delays: {:?}",delays); // build the packet let packet = sphinx::SphinxPacket::new(message, &route[..], &destination, &delays).unwrap(); From f92e2119603d6554a1acd81a7b0771e6af4d282c Mon Sep 17 00:00:00 2001 From: Jedrzej Stuczynski Date: Mon, 9 Dec 2019 12:33:06 +0000 Subject: [PATCH 2/2] Argument parsing --- Cargo.lock | 62 ++++++++++++++++ Cargo.toml | 3 +- src/clients/mix.rs | 3 +- src/main.rs | 177 ++++++++++++++++++++++++++++++++++++++------- 4 files changed, 214 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 81c39a2055..154aa1844c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -31,6 +31,14 @@ dependencies = [ "stream-cipher 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "arc-swap" version = "0.4.4" @@ -41,6 +49,15 @@ name = "arrayref" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "atty" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "bitflags" version = "1.2.1" @@ -126,6 +143,20 @@ dependencies = [ "keystream 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "clap" +version = "2.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "clear_on_drop" version = "0.2.3" @@ -396,6 +427,7 @@ dependencies = [ name = "nym-client" version = "0.1.0" dependencies = [ + "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "sphinx 0.1.0", "tokio 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -578,6 +610,11 @@ dependencies = [ "generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "subtle" version = "1.0.0" @@ -598,6 +635,14 @@ dependencies = [ "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tokio" version = "0.2.1" @@ -634,11 +679,21 @@ name = "typenum" version = "1.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "unicode-width" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unicode-xid" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "vec_map" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "wasi" version = "0.7.0" @@ -686,8 +741,10 @@ dependencies = [ "checksum aes-ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d2e5b0458ea3beae0d1d8c0f3946564f8e10f90646cf78c06b4351052058d1ee" "checksum aes-soft 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cfd7e7ae3f9a1fb5c03b389fc6bb9a51400d0c13053f0dca698c832bfd893a0d" "checksum aesni 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2f70a6b5f971e473091ab7cfb5ffac6cde81666c4556751d8d5620ead8abf100" +"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" "checksum arc-swap 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d7b8a9123b8027467bce0099fe556c628a53c8d83df0507084c31e9ba2e39aff" "checksum arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0d382e583f07208808f6b1249e60848879ba3543f57c32277bf52d69c2f0f0ee" +"checksum atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1803c647a3ec87095e7ae7acfca019e98de5ec9a7d01343f611cf3152ed71a90" "checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" "checksum blake2 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "94cb07b0da6a73955f8fb85d24c466778e70cda767a568229b104f0264089330" "checksum block-buffer 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" @@ -700,6 +757,7 @@ dependencies = [ "checksum cc 1.0.47 (registry+https://github.com/rust-lang/crates.io-index)" = "aa87058dce70a3ff5621797f1506cb837edd02ac4c0ae642b4542dce802908b8" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" "checksum chacha 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ddf3c081b5fba1e5615640aae998e0fbd10c24cbd897ee39ed754a77601a4862" +"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" "checksum clear_on_drop 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "97276801e127ffb46b66ce23f35cc96bd454fa311294bced4bbace7baa8b1d17" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum crypto-mac 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4434400df11d95d556bac068ddfedd482915eb18fe8bea89bc80b6e4b1c179e5" @@ -752,13 +810,17 @@ dependencies = [ "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" "checksum socket2 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)" = "e8b74de517221a2cb01a53349cf54182acdc31a074727d3079068448c0676d85" "checksum stream-cipher 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8131256a5896cabcf5eb04f4d6dacbe1aefda854b0d9896e09cb58829ec5638c" +"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" "checksum subtle 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee" "checksum subtle 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ab3af2eb31c42e8f0ccf43548232556c42737e01a96db6e1777b0be108e79799" "checksum syn 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "661641ea2aa15845cddeb97dad000d22070bb5c1fb456b96c1cba883ec691e92" +"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" "checksum tokio 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "beeef686ef92a222de07e089f455d9f8478bbba9651718f9e4b276babe829082" "checksum tokio-macros 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d5795a71419535c6dcecc9b6ca95bdd3c2d6142f7e8343d7beb9923f129aa87e" "checksum typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9" +"checksum unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" +"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" "checksum wasi 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b89c3ce4ce14bdc6fb6beaf9ec7928ca331de5df7e5ea278375642a2f478570d" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" diff --git a/Cargo.toml b/Cargo.toml index d9825f5f10..6d643fe588 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ edition = "2018" [dependencies] sphinx = { path = "../sphinx" } -tokio = { version = "0.2", features = ["full"] } \ No newline at end of file +tokio = { version = "0.2", features = ["full"] } +clap = "2.33.0" \ No newline at end of file diff --git a/src/clients/mix.rs b/src/clients/mix.rs index 22802482c6..af7844dc02 100644 --- a/src/clients/mix.rs +++ b/src/clients/mix.rs @@ -1,7 +1,6 @@ use crate::clients::directory::DirectoryClient; use sphinx::SphinxPacket; use sphinx::route::Node as MixNode; -use tokio::net::TcpStream; use tokio::prelude::*; pub struct MixClient {} @@ -15,7 +14,7 @@ impl MixClient { pub async fn send(&self, packet: SphinxPacket, mix: &MixNode) -> Result<(), Box>{ let bytes = packet.to_bytes(); - let mut stream = TcpStream::connect("127.0.0.1:8080").await?; + let mut stream = tokio::net::TcpStream::connect("127.0.0.1:8080").await?; stream.write_all(&bytes[..]).await?; Ok(()) } diff --git a/src/main.rs b/src/main.rs index bdc2ace554..1fbe250580 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,37 +1,158 @@ +use std::process; +use std::time::Duration; + +use clap::{App, Arg, ArgMatches, SubCommand}; +use tokio::runtime::Runtime; +use tokio::time::{Instant, interval_at}; + +use crate::clients::directory::DirectoryClient; +use crate::clients::mix::MixClient; + mod clients; -use tokio::prelude::*; +const TCP_SOCKET_TYPE: &str = "tcp"; +const WEBSOCKET_SOCKET_TYPE: &str = "websocket"; -use crate::clients::mix::MixClient; -use crate::clients::directory::DirectoryClient; -use std::time::{ Duration}; -use tokio::time::{interval_at, Instant}; -#[tokio::main] -async fn main() { - let start = Instant::now() + Duration::from_nanos(1000); - let mut interval = interval_at(start, Duration::from_millis(5000)); - let mut i: usize = 0; - loop { - interval.tick().await; - let message = format!("Hello, Sphinx {}", i).as_bytes().to_vec(); +fn execute(matches: ArgMatches) -> Result<(), String> { + match matches.subcommand() { + ("init", Some(m)) => Ok(init(m)), + ("run", Some(m)) => Ok(run(m)), + ("socket", Some(m)) => Ok(socket(m)), - // set up the route - let directory = DirectoryClient::new(); - let route = directory.get_mixes(); - let destination = directory.get_destination(); - let delays = sphinx::header::delays::generate(2); - - println!("delays: {:?}",delays); - // build the packet - let packet = sphinx::SphinxPacket::new(message, &route[..], &destination, &delays).unwrap(); - - // send to mixnet - let mix_client = MixClient::new(); - let result = mix_client.send(packet, route.first().unwrap()).await; - println!("packet sent: {:?}", i); - i += 1; + _ => Err(String::from("Unknown command")), } } +fn init(matches: &ArgMatches) { + println!("Running client init!"); + // don't unwrap it, pass it as it is, if it's None, choose a random + let provider_id = matches.value_of("providerID"); + let init_local = matches.is_present("local"); + + println!( + "client init goes here with providerID: {:?} and running locally: {:?}", + provider_id, init_local + ); +} + +fn run(matches: &ArgMatches) { + let custom_cfg = matches.value_of("customCfg"); + println!("Going to start client with custom config of: {:?}", custom_cfg); + + // Create the runtime, probably later move it to Client struct itself? + let mut rt = Runtime::new().unwrap(); + + // Spawn the root task + rt.block_on(async { + let start = Instant::now() + Duration::from_nanos(1000); + let mut interval = interval_at(start, Duration::from_millis(5000)); + let mut i: usize = 0; + loop { + interval.tick().await; + let message = format!("Hello, Sphinx {}", i).as_bytes().to_vec(); + + // set up the route + let directory = DirectoryClient::new(); + let route = directory.get_mixes(); + let destination = directory.get_destination(); + let delays = sphinx::header::delays::generate(2); + + println!("delays: {:?}", delays); + // build the packet + let packet = sphinx::SphinxPacket::new(message, &route[..], &destination, &delays).unwrap(); + + // send to mixnet + let mix_client = MixClient::new(); + let result = mix_client.send(packet, route.first().unwrap()).await; + println!("packet sent: {:?}", i); + i += 1; + } + }) +} + +fn socket(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); + +} + +// TODO: perhaps more subcommands and/or args to distinguish between coco client and mix client +fn main() { + let arg_matches = App::new("Nym Client") + .version("0.1.0") + .author("Nymtech") + .about("Implementation of the Nym Client") + .subcommand( + SubCommand::with_name("init") + .about("Initialise a Nym client") + .arg(Arg::with_name("providerID") + .short("pid") + .long("providerID") + .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") + .short("loc") + .long("local") + .help("Flag to indicate whether the client is expected to run on the local deployment") + ) + ) + .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) + ) + ) + .subcommand( + SubCommand::with_name("socket") + .about("Run a background Nym client listening on a specified socket") + .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("socketType") + .short("s") + .long("socketType") + .help("Type of the socket we want to run on (tcp / websocket)") + .takes_value(true) + .required(true) + ) + .arg( + Arg::with_name("port") + .short("p") + .long("port") + .help("Port to listen on") + .takes_value(true) + .required(true), + ) + ) + .get_matches(); + + if let Err(e) = execute(arg_matches) { + println!("Application error: {}", e); + process::exit(1); + } +}