Files
nym/gateway/src/commands/init.rs
T
2020-11-13 10:58:01 +00:00

163 lines
5.8 KiB
Rust

// Copyright 2020 Nym Technologies SA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::commands::override_config;
use crate::config::persistence::pathfinder::GatewayPathfinder;
use crate::config::Config;
use clap::{App, Arg, ArgMatches};
use config::NymConfig;
use crypto::asymmetric::{encryption, identity};
pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> {
App::new("init")
.about("Initialise the gateway")
.arg(
Arg::with_name("id")
.long("id")
.help("Id of the gateway we want to create config for.")
.takes_value(true)
.required(true),
)
.arg(
Arg::with_name("location")
.long("location")
.help("Optional geographical location of this provider")
.takes_value(true),
)
.arg(
Arg::with_name("mix-host")
.long("mix-host")
.help("The custom host on which the gateway will be running for receiving sphinx packets")
.takes_value(true)
.required(true),
)
.arg(
Arg::with_name("mix-port")
.long("mix-port")
.help("The port on which the gateway will be listening for sphinx packets")
.takes_value(true)
)
.arg(
Arg::with_name("clients-host")
.long("clients-host")
.help("The custom host on which the gateway will be running for receiving clients gateway-requests")
.takes_value(true)
.required(true),
)
.arg(
Arg::with_name("clients-port")
.long("clients-port")
.help("The port on which the gateway will be listening for clients gateway-requests")
.takes_value(true)
)
.arg(
Arg::with_name("mix-announce-host")
.long("mix-announce-host")
.help("The host that will be reported to the directory server")
.takes_value(true),
)
.arg(
Arg::with_name("mix-announce-port")
.long("mix-announce-port")
.help("The port that will be reported to the directory server")
.takes_value(true),
)
.arg(
Arg::with_name("clients-announce-host")
.long("clients-announce-host")
.help("The host that will be reported to the directory server")
.takes_value(true),
)
.arg(
Arg::with_name("clients-announce-port")
.long("clients-announce-port")
.help("The port that will be reported to the directory server")
.takes_value(true),
)
.arg(
Arg::with_name("inboxes")
.long("inboxes")
.help("Directory with inboxes where all packets for the clients are stored")
.takes_value(true)
)
.arg(
Arg::with_name("clients-ledger")
.long("clients-ledger")
.help("Ledger directory containing registered clients")
.takes_value(true)
)
.arg(
Arg::with_name("validator")
.long("validator")
.help("REST endpoint of the validator the node is registering presence with")
.takes_value(true),
)
.arg(
Arg::with_name("incentives-address")
.long("incentives-address")
.help("Optional, if participating in the incentives program, payment address")
.takes_value(true),
)
}
pub fn execute(matches: &ArgMatches) {
let id = matches.value_of("id").unwrap();
println!("Initialising gateway {}...", id);
let already_init = if Config::default_config_file_path(id).exists() {
println!("Gateway \"{}\" was already initialised before! Config information will be overwritten (but keys will be kept)!", id);
true
} else {
false
};
let mut config = Config::new(id);
config = override_config(config, matches);
// if gateway was already initialised, don't generate new keys
if !already_init {
let identity_keys = identity::KeyPair::new();
let sphinx_keys = encryption::KeyPair::new();
let pathfinder = GatewayPathfinder::new_from_config(&config);
pemstore::store_keypair(
&sphinx_keys,
&pemstore::KeyPairPath::new(
pathfinder.private_encryption_key().to_owned(),
pathfinder.public_encryption_key().to_owned(),
),
)
.expect("Failed to save sphinx keys");
pemstore::store_keypair(
&identity_keys,
&pemstore::KeyPairPath::new(
pathfinder.private_identity_key().to_owned(),
pathfinder.public_identity_key().to_owned(),
),
)
.expect("Failed to save identity keys");
println!("Saved identity and mixnet sphinx keypairs");
}
let config_save_location = config.get_config_file_save_location();
config
.save_to_file(None)
.expect("Failed to save the config file");
println!("Saved configuration file to {:?}", config_save_location);
println!("Gateway configuration completed.\n\n\n");
}