Skip to content

Commit 4f3ccd9

Browse files
Selecting the gateway randomly from 2 known good ones. (#355)
* Selecting the gateway randomly from 2 known good ones. There's a problem with the config file in this commit though. * Saving value of chosen gateway * Removed duplicate printing of gateway being used Co-authored-by: jstuczyn <jedrzej.stuczynski@gmail.com>
1 parent 2bb980a commit 4f3ccd9

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

clients/native/src/commands/init.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@ use directory_client::DirectoryClient;
2424
use gateway_client::GatewayClient;
2525
use gateway_requests::registration::handshake::SharedKeys;
2626
use rand::rngs::OsRng;
27+
use rand::seq::SliceRandom;
2728
use std::convert::TryInto;
2829
use std::sync::Arc;
2930
use std::time::Duration;
3031
use topology::{gateway, NymTopology};
3132

33+
const GOOD_GATEWAYS: [&str; 2] = [
34+
"D6YaMzLSY7mANtSQRKXsmMZpqgqiVkeiagKM4V4oFPFr",
35+
"5nrYxPR8gt2Gzo2BbHtsGf66KAEQY91WmM1eW78EphNy",
36+
];
37+
3238
pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> {
3339
App::new("init")
3440
.about("Initialise a Nym client. Do this first!")
@@ -40,9 +46,8 @@ pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> {
4046
)
4147
.arg(Arg::with_name("gateway")
4248
.long("gateway")
43-
.help("Id of the gateway we are going to connect to. If unsure what to put here - 'D6YaMzLSY7mANtSQRKXsmMZpqgqiVkeiagKM4V4oFPFr' is a safe choice for the testnet")
49+
.help("Id of the gateway we are going to connect to.")
4450
.takes_value(true)
45-
.required(true)
4651
)
4752
.arg(Arg::with_name("directory")
4853
.long("directory")
@@ -102,13 +107,25 @@ async fn gateway_details(directory_server: &str, gateway_id: &str) -> gateway::N
102107
.clone()
103108
}
104109

110+
fn select_gateway(arg: Option<&str>) -> &str {
111+
if let Some(gateway_id) = arg {
112+
gateway_id
113+
} else {
114+
// TODO1: this should only be done on testnet
115+
// TODO2: it should probably check if chosen gateway is actually online
116+
GOOD_GATEWAYS.choose(&mut rand::thread_rng()).unwrap()
117+
}
118+
}
119+
105120
pub fn execute(matches: &ArgMatches) {
106121
println!("Initialising client...");
107122

108123
let id = matches.value_of("id").unwrap(); // required for now
109124
let mut config = Config::new(id);
110125
let mut rng = OsRng;
111126

127+
// TODO: ideally that should be the last thing that's being done to config.
128+
// However, we are later further overriding it with gateway id
112129
config = override_config(config, matches);
113130
if matches.is_present("fastmode") {
114131
config.get_base_mut().set_high_default_traffic_volume();
@@ -117,7 +134,8 @@ pub fn execute(matches: &ArgMatches) {
117134
// create identity, encryption and ack keys.
118135
let mut key_manager = KeyManager::new(&mut rng);
119136

120-
let gateway_id = matches.value_of("gateway").unwrap();
137+
let gateway_id = select_gateway(matches.value_of("gateway"));
138+
config.get_base_mut().with_gateway_id(gateway_id);
121139

122140
let registration_fut = async {
123141
let gate_details =
@@ -146,11 +164,6 @@ pub fn execute(matches: &ArgMatches) {
146164
.save_to_file(None)
147165
.expect("Failed to save the config file");
148166
println!("Saved configuration file to {:?}", config_save_location);
149-
150-
println!(
151-
"Unless overridden in all `nym-client run` we will be talking to the following gateway: {}...",
152-
config.get_base().get_gateway_id(),
153-
);
154-
167+
println!("Using gateway: {}", config.get_base().get_gateway_id(),);
155168
println!("Client configuration completed.\n\n\n")
156169
}

clients/native/src/main.rs

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ fn main() {
2323
dotenv::dotenv().ok();
2424
setup_logging();
2525
println!("{}", banner());
26-
println!("\n\nIf unsure which gateway to choose - 'D6YaMzLSY7mANtSQRKXsmMZpqgqiVkeiagKM4V4oFPFr' is a safe choice for the testnet\n\n");
2726

2827
let arg_matches = App::new("Nym Client")
2928
.version(built_info::PKG_VERSION)

clients/socks5/src/commands/init.rs

+22-10
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ use crypto::asymmetric::identity;
2323
use directory_client::DirectoryClient;
2424
use gateway_client::GatewayClient;
2525
use gateway_requests::registration::handshake::SharedKeys;
26-
use rand::rngs::OsRng;
26+
use rand::{prelude::SliceRandom, rngs::OsRng};
2727
use std::convert::TryInto;
2828
use std::sync::Arc;
2929
use std::time::Duration;
3030
use topology::{gateway, NymTopology};
3131

32+
const GOOD_GATEWAYS: [&str; 2] = [
33+
"D6YaMzLSY7mANtSQRKXsmMZpqgqiVkeiagKM4V4oFPFr",
34+
"5nrYxPR8gt2Gzo2BbHtsGf66KAEQY91WmM1eW78EphNy",
35+
];
36+
3237
pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> {
3338
App::new("init")
3439
.about("Initialise a Nym client. Do this first!")
@@ -46,9 +51,8 @@ pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> {
4651
)
4752
.arg(Arg::with_name("gateway")
4853
.long("gateway")
49-
.help("Id of the gateway we are going to connect to. If unsure what to put here - 'D6YaMzLSY7mANtSQRKXsmMZpqgqiVkeiagKM4V4oFPFr' is a safe choice for the testnet")
54+
.help("Id of the gateway we are going to connect to.")
5055
.takes_value(true)
51-
.required(true)
5256
)
5357
.arg(Arg::with_name("directory")
5458
.long("directory")
@@ -104,6 +108,16 @@ async fn gateway_details(directory_server: &str, gateway_id: &str) -> gateway::N
104108
.clone()
105109
}
106110

111+
fn select_gateway(arg: Option<&str>) -> &str {
112+
if let Some(gateway_id) = arg {
113+
gateway_id
114+
} else {
115+
// TODO1: this should only be done on testnet
116+
// TODO2: it should probably check if chosen gateway is actually online
117+
GOOD_GATEWAYS.choose(&mut rand::thread_rng()).unwrap()
118+
}
119+
}
120+
107121
pub fn execute(matches: &ArgMatches) {
108122
println!("Initialising client...");
109123

@@ -113,6 +127,8 @@ pub fn execute(matches: &ArgMatches) {
113127
let mut config = Config::new(id, provider_address);
114128
let mut rng = OsRng;
115129

130+
// TODO: ideally that should be the last thing that's being done to config.
131+
// However, we are later further overriding it with gateway id
116132
config = override_config(config, matches);
117133
if matches.is_present("fastmode") {
118134
config.get_base_mut().set_high_default_traffic_volume();
@@ -121,7 +137,8 @@ pub fn execute(matches: &ArgMatches) {
121137
// create identity, encryption and ack keys.
122138
let mut key_manager = KeyManager::new(&mut rng);
123139

124-
let gateway_id = matches.value_of("gateway").unwrap();
140+
let gateway_id = select_gateway(matches.value_of("gateway"));
141+
config.get_base_mut().with_gateway_id(gateway_id);
125142

126143
let registration_fut = async {
127144
let gate_details =
@@ -150,11 +167,6 @@ pub fn execute(matches: &ArgMatches) {
150167
.save_to_file(None)
151168
.expect("Failed to save the config file");
152169
println!("Saved configuration file to {:?}", config_save_location);
153-
154-
println!(
155-
"Unless overridden in all `nym-socks5-client run` we will be talking to the following gateway: {}...",
156-
config.get_base().get_gateway_id(),
157-
);
158-
170+
println!("Using gateway: {}", config.get_base().get_gateway_id(),);
159171
println!("Client configuration completed.\n\n\n")
160172
}

clients/socks5/src/main.rs

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ fn main() {
2323
dotenv::dotenv().ok();
2424
setup_logging();
2525
println!("{}", banner());
26-
println!("\n\nIf unsure which gateway to choose - 'D6YaMzLSY7mANtSQRKXsmMZpqgqiVkeiagKM4V4oFPFr' is a safe choice for the testnet\n\n");
2726

2827
let arg_matches = App::new("Nym Socks5 Proxy")
2928
.version(built_info::PKG_VERSION)

0 commit comments

Comments
 (0)