Skip to content

Commit

Permalink
Add config http api port to gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
neacsu committed Oct 18, 2023
1 parent 4141a78 commit 6af10da
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 9 deletions.
1 change: 1 addition & 0 deletions common/types/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub struct GatewayNodeDetailsResponse {
pub bind_address: String,
pub mix_port: u16,
pub clients_port: u16,
pub http_api_port: u16,
pub config_path: String,
pub data_store: String,

Expand Down
2 changes: 2 additions & 0 deletions gateway/src/commands/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub(crate) struct OverrideConfig {
pub(crate) host: Option<IpAddr>,
pub(crate) mix_port: Option<u16>,
pub(crate) clients_port: Option<u16>,
pub(crate) http_api_port: Option<u16>,
pub(crate) datastore: Option<PathBuf>,
pub(crate) enabled_statistics: Option<bool>,
pub(crate) statistics_service_url: Option<url::Url>,
Expand All @@ -44,6 +45,7 @@ impl OverrideConfig {
.with_optional(Config::with_listening_address, self.host)
.with_optional(Config::with_mix_port, self.mix_port)
.with_optional(Config::with_clients_port, self.clients_port)
.with_optional(Config::with_http_api_port, self.http_api_port)
.with_optional_custom_env(
Config::with_custom_nym_apis,
self.nym_apis,
Expand Down
5 changes: 5 additions & 0 deletions gateway/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ pub struct Init {
#[arg(long)]
clients_port: Option<u16>,

/// The port on which the gateway will be listening for http requests
#[clap(long)]
http_api_port: Option<u16>,

/// Path to sqlite database containing all gateway persistent data
#[arg(long)]
datastore: Option<PathBuf>,
Expand Down Expand Up @@ -127,6 +131,7 @@ impl From<Init> for OverrideConfig {
host: Some(init_config.host),
mix_port: init_config.mix_port,
clients_port: init_config.clients_port,
http_api_port: init_config.http_api_port,
datastore: init_config.datastore,
nym_apis: init_config.nym_apis,
mnemonic: init_config.mnemonic,
Expand Down
5 changes: 5 additions & 0 deletions gateway/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ pub struct Run {
#[clap(long)]
mix_port: Option<u16>,

/// The port on which the gateway will be listening for http requests
#[clap(long)]
http_api_port: Option<u16>,

/// The port on which the gateway will be listening for clients gateway-requests
#[clap(long)]
clients_port: Option<u16>,
Expand Down Expand Up @@ -124,6 +128,7 @@ impl From<Run> for OverrideConfig {
host: run_config.host,
mix_port: run_config.mix_port,
clients_port: run_config.clients_port,
http_api_port: run_config.http_api_port,
datastore: run_config.datastore,
nym_apis: run_config.nym_apis,
mnemonic: run_config.mnemonic,
Expand Down
34 changes: 33 additions & 1 deletion gateway/src/commands/upgrade_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use crate::config::old_config_v1_1_20::ConfigV1_1_20;
use crate::config::old_config_v1_1_28::ConfigV1_1_28;
use crate::config::old_config_v1_1_30::ConfigV1_1_30;
use crate::config::{default_config_filepath, Config};
use crate::error::GatewayError;
use log::info;
Expand All @@ -20,7 +21,8 @@ fn try_upgrade_v1_1_20_config(id: &str) -> Result<bool, GatewayError> {
info!("It is going to get updated to the current specification.");

let updated_step1: ConfigV1_1_28 = old_config.into();
let updated: Config = updated_step1.into();
let updated_step2: ConfigV1_1_30 = updated_step1.into();
let updated: Config = updated_step2.into();
updated
.save_to_default_location()
.map_err(|err| GatewayError::ConfigSaveFailure {
Expand All @@ -42,6 +44,33 @@ fn try_upgrade_v1_1_28_config(id: &str) -> Result<bool, GatewayError> {
info!("It seems the gateway is using <= v1.1.28 config template.");
info!("It is going to get updated to the current specification.");

let updated_step1: ConfigV1_1_30 = old_config.into();
let updated: Config = updated_step1.into();
updated
.save_to_default_location()
.map_err(|err| GatewayError::ConfigSaveFailure {
path: default_config_filepath(id),
id: id.to_string(),
source: err,
})?;

Ok(true)
}

fn try_upgrade_v1_1_30_config(id: &str) -> Result<bool, GatewayError> {
// if current configuration can be loaded, there is no need to try the upgrade
if Config::read_from_default_path(id).is_ok() {
return Ok(true);
}
// explicitly load it as v1.1.32 (which is incompatible with the current, i.e. 1.1.33+)
let Ok(old_config) = ConfigV1_1_30::read_from_default_path(id) else {
// if we failed to load it, there might have been nothing to upgrade
// or maybe it was an even older file. in either way. just ignore it and carry on with our day
return Ok(false);
};
info!("It seems the gateway is using <= v1.1.32 config template.");
info!("It is going to get updated to the current specification.");

let updated: Config = old_config.into();
updated
.save_to_default_location()
Expand All @@ -61,6 +90,9 @@ pub(crate) fn try_upgrade_config(id: &str) -> Result<(), GatewayError> {
if try_upgrade_v1_1_28_config(id)? {
return Ok(());
}
if try_upgrade_v1_1_30_config(id)? {
return Ok(());
}

Ok(())
}
15 changes: 14 additions & 1 deletion gateway/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::config::persistence::paths::GatewayPaths;
use crate::config::template::CONFIG_TEMPLATE;
use log::{debug, warn};
use nym_bin_common::logging::LoggingSettings;
use nym_config::defaults::{DEFAULT_CLIENT_LISTENING_PORT, DEFAULT_MIX_LISTENING_PORT};
use nym_config::defaults::{
DEFAULT_CLIENT_LISTENING_PORT, DEFAULT_HTTP_API_LISTENING_PORT, DEFAULT_MIX_LISTENING_PORT,
};
use nym_config::helpers::inaddr_any;
use nym_config::{
must_get_home, read_config_from_toml_file, save_formatted_config_to_file, NymConfigTemplate,
Expand All @@ -22,6 +24,7 @@ use zeroize::{Zeroize, ZeroizeOnDrop};

pub(crate) mod old_config_v1_1_20;
pub(crate) mod old_config_v1_1_28;
pub(crate) mod old_config_v1_1_30;
pub mod persistence;
mod template;

Expand Down Expand Up @@ -197,6 +200,11 @@ impl Config {
self
}

pub fn with_http_api_port(mut self, port: u16) -> Self {
self.gateway.http_api_port = port;
self
}

pub fn with_custom_persistent_store(mut self, store_dir: PathBuf) -> Self {
self.storage_paths.clients_storage = store_dir;
self
Expand Down Expand Up @@ -245,6 +253,10 @@ pub struct Gateway {
/// (default: 9000)
pub clients_port: u16,

/// Port used for listening for http requests.
/// (default: 8000)
pub http_api_port: u16,

/// Whether gateway collects and sends anonymized statistics
pub enabled_statistics: bool,

Expand Down Expand Up @@ -279,6 +291,7 @@ impl Gateway {
listening_address: inaddr_any(),
mix_port: DEFAULT_MIX_LISTENING_PORT,
clients_port: DEFAULT_CLIENT_LISTENING_PORT,
http_api_port: DEFAULT_HTTP_API_LISTENING_PORT,
enabled_statistics: false,
statistics_service_url: mainnet::STATISTICS_SERVICE_DOMAIN_ADDRESS
.parse()
Expand Down
9 changes: 5 additions & 4 deletions gateway/src/config/old_config_v1_1_28.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright 2020-2023 - Nym Technologies SA <[email protected]>
// SPDX-License-Identifier: Apache-2.0

use crate::config::old_config_v1_1_30::{ConfigV1_1_30, GatewayV1_1_30};
use crate::config::persistence::paths::{GatewayPaths, KeysPaths};
use crate::config::{Config, Debug, Gateway};
use crate::config::Debug;
use nym_bin_common::logging::LoggingSettings;
use nym_config::{
must_get_home, read_config_from_toml_file, DEFAULT_CONFIG_DIR, DEFAULT_CONFIG_FILENAME, NYM_DIR,
Expand Down Expand Up @@ -84,11 +85,11 @@ impl ConfigV1_1_28 {
}
}

impl From<ConfigV1_1_28> for Config {
impl From<ConfigV1_1_28> for ConfigV1_1_30 {
fn from(value: ConfigV1_1_28) -> Self {
Config {
ConfigV1_1_30 {
save_path: None,
gateway: Gateway {
gateway: GatewayV1_1_30 {
version: value.gateway.version,
id: value.gateway.id,
only_coconut_credentials: value.gateway.only_coconut_credentials,
Expand Down
110 changes: 110 additions & 0 deletions gateway/src/config/old_config_v1_1_30.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2020-2023 - Nym Technologies SA <[email protected]>
// SPDX-License-Identifier: Apache-2.0

use crate::config::default_config_filepath;
use crate::config::persistence::paths::GatewayPaths;
use crate::config::{Config, Debug, Gateway, NetworkRequester};
use nym_bin_common::logging::LoggingSettings;
use nym_config::read_config_from_toml_file;
use nym_network_defaults::DEFAULT_HTTP_API_LISTENING_PORT;
use serde::{Deserialize, Serialize};
use std::io;
use std::net::IpAddr;
use std::path::{Path, PathBuf};
use url::Url;

#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ConfigV1_1_30 {
// additional metadata holding on-disk location of this config file
#[serde(skip)]
pub(crate) save_path: Option<PathBuf>,

pub gateway: GatewayV1_1_30,

pub storage_paths: GatewayPaths,

pub network_requester: NetworkRequester,

#[serde(default)]
pub logging: LoggingSettings,

#[serde(default)]
pub debug: Debug,
}

impl ConfigV1_1_30 {
pub fn read_from_default_path<P: AsRef<Path>>(id: P) -> io::Result<Self> {
read_config_from_toml_file(default_config_filepath(id))
}
}

impl From<ConfigV1_1_30> for Config {
fn from(value: ConfigV1_1_30) -> Self {
Config {
save_path: value.save_path,
gateway: Gateway {
version: value.gateway.version,
id: value.gateway.id,
only_coconut_credentials: value.gateway.only_coconut_credentials,
listening_address: value.gateway.listening_address,
mix_port: value.gateway.mix_port,
http_api_port: DEFAULT_HTTP_API_LISTENING_PORT,
clients_port: value.gateway.clients_port,
enabled_statistics: value.gateway.enabled_statistics,
nym_api_urls: value.gateway.nym_api_urls,
nyxd_urls: value.gateway.nyxd_urls,
statistics_service_url: value.gateway.statistics_service_url,
cosmos_mnemonic: value.gateway.cosmos_mnemonic,
},
storage_paths: value.storage_paths,
network_requester: value.network_requester,
logging: value.logging,
debug: value.debug,
}
}
}

#[derive(Debug, Deserialize, PartialEq, Eq, Serialize)]
pub struct GatewayV1_1_30 {
/// Version of the gateway for which this configuration was created.
pub version: String,

/// ID specifies the human readable ID of this particular gateway.
pub id: String,

/// Indicates whether this gateway is accepting only coconut credentials for accessing the
/// the mixnet, or if it also accepts non-paying clients
#[serde(default)]
pub only_coconut_credentials: bool,

/// Address to which this mixnode will bind to and will be listening for packets.
pub listening_address: IpAddr,

/// Port used for listening for all mixnet traffic.
/// (default: 1789)
pub mix_port: u16,

/// Port used for listening for all client-related traffic.
/// (default: 9000)
pub clients_port: u16,

/// Whether gateway collects and sends anonymized statistics
pub enabled_statistics: bool,

/// Domain address of the statistics service
pub statistics_service_url: Url,

/// Addresses to APIs from which the node gets the view of the network.
#[serde(alias = "validator_api_urls")]
pub nym_api_urls: Vec<Url>,

/// Addresses to validators which the node uses to check for double spending of ERC20 tokens.
#[serde(alias = "validator_nymd_urls")]
pub nyxd_urls: Vec<Url>,

/// Mnemonic of a cosmos wallet used in checking for double spending.
// #[deprecated(note = "move to storage")]
// TODO: I don't think this should be stored directly in the config...
pub cosmos_mnemonic: bip39::Mnemonic,
}
4 changes: 4 additions & 0 deletions gateway/src/config/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ mix_port = {{ gateway.mix_port }}
# (default: 9000)
clients_port = {{ gateway.clients_port }}
# Port used for listening for http requests.
# (default: 8000)
http_api_port = {{ gateway.http_api_port }}
# Wheather gateway collects and sends anonymized statistics
enabled_statistics = {{ gateway.enabled_statistics }}
Expand Down
1 change: 1 addition & 0 deletions gateway/src/node/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub(crate) fn node_details(config: &Config) -> Result<GatewayNodeDetailsResponse
bind_address: config.gateway.listening_address.to_string(),
mix_port: config.gateway.mix_port,
clients_port: config.gateway.clients_port,
http_api_port: config.gateway.http_api_port,
config_path: display_maybe_path(config.save_path.as_ref()),
data_store: display_path(&config.storage_paths.clients_storage),
network_requester,
Expand Down
4 changes: 1 addition & 3 deletions gateway/src/node/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ fn router_with_state(state: Arc<ApiState>) -> Router {
}

pub(crate) async fn start_http_api(
port: u16,
client_registry: Arc<ClientRegistry>,
sphinx_key_pair: Arc<encryption::KeyPair>,
) {
// Port should be 80 post smoosh
let port = 8000;

info!("Started HTTP API on port {}", port);

let client_registry = Arc::clone(&client_registry);
Expand Down
1 change: 1 addition & 0 deletions gateway/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ impl<St> Gateway<St> {

// This should likely be wireguard feature gated, but its easier to test if it hangs in here
tokio::spawn(start_http_api(
self.config.gateway.http_api_port,
Arc::clone(&self.client_registry),
Arc::clone(&self.sphinx_keypair),
));
Expand Down

0 comments on commit 6af10da

Please sign in to comment.