From a4808635f931c08cd384f3e0062db3bb520a99f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogdan-=C8=98tefan=20Neac=C5=9Fu?= Date: Thu, 16 May 2024 13:13:58 +0000 Subject: [PATCH] Support nym node first --- common/types/src/gateway.rs | 1 - common/wireguard-types/src/config.rs | 44 ------------------------ common/wireguard-types/src/lib.rs | 1 - gateway/src/commands/helpers.rs | 4 --- gateway/src/config/mod.rs | 14 -------- gateway/src/config/old_config_v1_1_36.rs | 8 +---- gateway/src/helpers.rs | 16 +-------- gateway/src/http/mod.rs | 30 ++-------------- gateway/src/node/helpers.rs | 27 --------------- nym-node/Cargo.toml | 3 ++ nym-node/src/cli/commands/migrate.rs | 11 +----- nym-node/src/config/exit_gateway.rs | 16 +++++++++ nym-node/src/config/helpers.rs | 8 ----- nym-node/src/config/mod.rs | 7 ++++ nym-node/src/config/persistence.rs | 15 ++++++-- nym-node/src/config/template.rs | 7 ++-- 16 files changed, 48 insertions(+), 164 deletions(-) delete mode 100644 common/wireguard-types/src/config.rs diff --git a/common/types/src/gateway.rs b/common/types/src/gateway.rs index de9de0c9c5..c78de5203a 100644 --- a/common/types/src/gateway.rs +++ b/common/types/src/gateway.rs @@ -89,7 +89,6 @@ pub struct GatewayNodeDetailsResponse { pub network_requester: Option, pub ip_packet_router: Option, - pub wireguard: Option, } impl fmt::Display for GatewayNodeDetailsResponse { diff --git a/common/wireguard-types/src/config.rs b/common/wireguard-types/src/config.rs deleted file mode 100644 index 8ce79b805d..0000000000 --- a/common/wireguard-types/src/config.rs +++ /dev/null @@ -1,44 +0,0 @@ -use std::{ - io, - net::{IpAddr, Ipv4Addr, SocketAddr}, - path::Path, -}; - -use nym_network_defaults::WG_PORT; -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Deserialize, PartialEq, Serialize)] -#[serde(deny_unknown_fields)] -pub struct Wireguard { - /// Specifies whether the wireguard service is enabled on this node. - pub enabled: bool, - - /// Socket address this node will use for binding its wireguard interface. - /// default: `0.0.0.0:51822` - pub bind_address: SocketAddr, - - /// Port announced to external clients wishing to connect to the wireguard interface. - /// Useful in the instances where the node is behind a proxy. - pub announced_port: u16, - - /// The prefix denoting the maximum number of the clients that can be connected via Wireguard. - /// The maximum value for IPv4 is 32 and for IPv6 is 128 - pub private_network_prefix: u8, -} - -impl Default for Wireguard { - fn default() -> Self { - Wireguard { - enabled: false, - bind_address: SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), WG_PORT), - announced_port: WG_PORT, - private_network_prefix: 16, - } - } -} - -impl Wireguard { - pub fn read_from_toml_file>(path: P) -> io::Result { - nym_config::read_config_from_toml_file(path) - } -} diff --git a/common/wireguard-types/src/lib.rs b/common/wireguard-types/src/lib.rs index 73042267ea..acb3366f86 100644 --- a/common/wireguard-types/src/lib.rs +++ b/common/wireguard-types/src/lib.rs @@ -1,7 +1,6 @@ // Copyright 2023 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 -pub mod config; pub mod error; pub mod public_key; pub mod registration; diff --git a/gateway/src/commands/helpers.rs b/gateway/src/commands/helpers.rs index 44e1b7d7c3..ff487dc7c2 100644 --- a/gateway/src/commands/helpers.rs +++ b/gateway/src/commands/helpers.rs @@ -101,10 +101,6 @@ impl OverrideConfig { config = config.with_default_ip_packet_router_config_path(); } - if config.wireguard.enabled && config.storage_paths.wireguard_config.is_none() { - config = config.with_default_wireguard_config_path(); - } - Ok(config) } } diff --git a/gateway/src/config/mod.rs b/gateway/src/config/mod.rs index b377504f40..bd5b579a3f 100644 --- a/gateway/src/config/mod.rs +++ b/gateway/src/config/mod.rs @@ -12,7 +12,6 @@ use nym_config::{ DEFAULT_CONFIG_DIR, DEFAULT_CONFIG_FILENAME, DEFAULT_DATA_DIR, NYM_DIR, }; use nym_network_defaults::{mainnet, DEFAULT_NYM_NODE_HTTP_PORT}; -use nym_wireguard_types::config::Wireguard; use serde::{Deserialize, Serialize}; use std::io; use std::net::{IpAddr, Ipv4Addr, SocketAddr}; @@ -94,9 +93,6 @@ pub struct Config { #[serde(default)] pub ip_packet_router: IpPacketRouter, - #[serde(default)] - pub wireguard: Wireguard, - #[serde(default)] pub logging: LoggingSettings, @@ -122,7 +118,6 @@ impl Config { }, http: Default::default(), gateway: default_gateway, - wireguard: Default::default(), storage_paths: GatewayPaths::new_default(id.as_ref()), network_requester: Default::default(), ip_packet_router: Default::default(), @@ -136,7 +131,6 @@ impl Config { host: impl Into, http: impl Into, gateway: impl Into, - wireguard: impl Into, storage_paths: impl Into, network_requester: impl Into, ip_packet_router: impl Into, @@ -148,7 +142,6 @@ impl Config { host: host.into(), http: http.into(), gateway: gateway.into(), - wireguard: wireguard.into(), storage_paths: storage_paths.into(), network_requester: network_requester.into(), ip_packet_router: ip_packet_router.into(), @@ -228,11 +221,6 @@ impl Config { self } - pub fn with_enabled_wireguard(mut self, enabled_wireguard: bool) -> Self { - self.wireguard.enabled = enabled_wireguard; - self - } - pub fn with_default_wireguard_config_path(mut self) -> Self { self.storage_paths = self .storage_paths @@ -275,8 +263,6 @@ impl Config { let http_port = self.http.bind_address.port(); self.http.bind_address = SocketAddr::new(listening_address, http_port); - let wg_port = self.wireguard.bind_address.port(); - self.wireguard.bind_address = SocketAddr::new(listening_address, wg_port); self } diff --git a/gateway/src/config/old_config_v1_1_36.rs b/gateway/src/config/old_config_v1_1_36.rs index affff83ccc..64e44b4578 100644 --- a/gateway/src/config/old_config_v1_1_36.rs +++ b/gateway/src/config/old_config_v1_1_36.rs @@ -15,7 +15,7 @@ use std::time::Duration; use url::Url; use super::persistence::paths::KeysPaths; -use super::{Config, Debug, Gateway, Host, Http, IpPacketRouter, NetworkRequester, Wireguard}; +use super::{Config, Debug, Gateway, Host, Http, IpPacketRouter, NetworkRequester}; const DEFAULT_GATEWAYS_DIR: &str = "gateways"; @@ -128,12 +128,6 @@ impl From for Config { nyxd_urls: value.gateway.nyxd_urls, cosmos_mnemonic: value.gateway.cosmos_mnemonic, }, - wireguard: Wireguard { - enabled: value.wireguard.enabled, - bind_address: value.wireguard.bind_address, - announced_port: value.wireguard.announced_port, - private_network_prefix: value.wireguard.private_network_prefix, - }, storage_paths: GatewayPaths { keys: KeysPaths { private_identity_key_file: value.storage_paths.keys.private_identity_key_file, diff --git a/gateway/src/helpers.rs b/gateway/src/helpers.rs index 9271526428..d63cc2e394 100644 --- a/gateway/src/helpers.rs +++ b/gateway/src/helpers.rs @@ -1,9 +1,9 @@ // Copyright 2024 - Nym Technologies SA // SPDX-License-Identifier: GPL-3.0-only +use crate::config::Config; use crate::node::helpers::load_keypair; use crate::GatewayError; -use crate::{config::Config, node::helpers::load_wireguard_config}; use nym_config::OptionalSet; use nym_crypto::asymmetric::{encryption, identity}; use nym_pemstore::traits::PemStorableKey; @@ -11,7 +11,6 @@ use nym_pemstore::KeyPairPath; use nym_sphinx::addressing::clients::Recipient; use nym_types::gateway::{ GatewayIpPacketRouterDetails, GatewayNetworkRequesterDetails, GatewayNodeDetailsResponse, - GatewayWireguardDetails, }; use std::path::Path; @@ -193,18 +192,6 @@ pub async fn node_details(config: &Config) -> Result Result Result { - let wireguard = if config.wireguard.enabled { - Some(api_requests::v1::gateway::models::Wireguard { - port: config.wireguard.announced_port, - public_key: "placeholder key value".to_string(), - }) - } else { - None - }; - Ok(api_requests::v1::gateway::models::Gateway { enforces_zk_nyms: config.gateway.only_coconut_credentials, client_interfaces: api_requests::v1::gateway::models::ClientInterfaces { - wireguard, + wireguard: None, mixnet_websockets: Some(api_requests::v1::gateway::models::WebSockets { ws_port: config.gateway.clients_port, wss_port: config.gateway.clients_wss_port, @@ -281,22 +269,8 @@ impl<'a> HttpApiBuilder<'a> { )?); } - let wireguard_private_network = IpNetwork::new( - IpAddr::from(Ipv4Addr::new(10, 1, 0, 0)), - self.gateway_config.wireguard.private_network_prefix, - )?; - let wg_state = self.client_registry.and_then(|client_registry| { - WireguardAppState::new( - client_registry, - Default::default(), - self.gateway_config.wireguard.bind_address.port(), - wireguard_private_network, - ) - .ok() - }); - let bind_address = self.gateway_config.http.bind_address; - let router = nym_node_http_api::NymNodeRouter::new(config, None, wg_state); + let router = nym_node_http_api::NymNodeRouter::new(config, None, None); tokio::spawn(async move { let server = match router.build_server(&bind_address).await { diff --git a/gateway/src/node/helpers.rs b/gateway/src/node/helpers.rs index d47ffc8ee1..0c920181e0 100644 --- a/gateway/src/node/helpers.rs +++ b/gateway/src/node/helpers.rs @@ -37,19 +37,6 @@ pub async fn load_ip_packet_router_config>( read_ip_packet_router_config(id, path) } -pub async fn load_wireguard_config>( - id: &str, - path: P, -) -> Result { - let path = path.as_ref(); - if let Ok(cfg) = read_wireguard_config(id, path) { - return Ok(cfg); - } - - nym_ip_packet_router::config::helpers::try_upgrade_config(path).await?; - read_wireguard_config(id, path) -} - pub fn read_network_requester_config>( id: &str, path: P, @@ -78,20 +65,6 @@ pub fn read_ip_packet_router_config>( }) } -pub fn read_wireguard_config>( - id: &str, - path: P, -) -> Result { - let path = path.as_ref(); - nym_wireguard_types::config::Wireguard::read_from_toml_file(path).map_err(|err| { - GatewayError::WireguardConfigLoadFailure { - id: id.to_string(), - path: path.to_path_buf(), - source: err, - } - }) -} - pub(crate) async fn initialise_main_storage( config: &Config, ) -> Result { diff --git a/nym-node/Cargo.toml b/nym-node/Cargo.toml index 5aec940189..8e718da3f3 100644 --- a/nym-node/Cargo.toml +++ b/nym-node/Cargo.toml @@ -60,3 +60,6 @@ nym-ip-packet-router = { path = "../service-providers/ip-packet-router" } [build-dependencies] # temporary bonding information v1 (to grab and parse nym-mixnode and nym-gateway package versions) cargo_metadata = "0.18.1" + +[features] +wireguard = [] diff --git a/nym-node/src/cli/commands/migrate.rs b/nym-node/src/cli/commands/migrate.rs index b4ca40e429..c93dada287 100644 --- a/nym-node/src/cli/commands/migrate.rs +++ b/nym-node/src/cli/commands/migrate.rs @@ -18,8 +18,8 @@ use nym_mixnode::MixnodeError; use nym_network_requester::{CustomGatewayDetails, GatewayDetails}; use nym_node::config; use nym_node::config::mixnode::DEFAULT_VERLOC_PORT; +use nym_node::config::Config; use nym_node::config::{default_config_filepath, ConfigBuilder, NodeMode}; -use nym_node::config::{Config, DEFAULT_WIREGUARD_NETWORK_IP}; use nym_node::error::{EntryGatewayError, ExitGatewayError, NymNodeError}; use nym_node_http_api::api::api_requests::v1::node::models::NodeDescription; use rand::rngs::OsRng; @@ -400,15 +400,6 @@ async fn migrate_gateway(mut args: Args) -> Result<(), NymNodeError> { }, ..config::MixnodeConfig::new_default() })) - .with_wireguard(args.wireguard.override_config_section(config::Wireguard { - enabled: cfg.wireguard.enabled, - bind_address: cfg.wireguard.bind_address, - private_network_ip: DEFAULT_WIREGUARD_NETWORK_IP, - announced_port: cfg.wireguard.announced_port, - private_network_prefix: cfg.wireguard.private_network_prefix, - // this is fine as currently the paths stored inside gateway itself are empty - storage_paths: config::persistence::WireguardPaths::new(&data_dir), - })) .with_entry_gateway(args.entry_gateway.override_config_section( config::EntryGatewayConfig { storage_paths: config::persistence::EntryGatewayPaths::new(&data_dir), diff --git a/nym-node/src/config/exit_gateway.rs b/nym-node/src/config/exit_gateway.rs index f24dd90a2e..0c2502376b 100644 --- a/nym-node/src/config/exit_gateway.rs +++ b/nym-node/src/config/exit_gateway.rs @@ -13,6 +13,8 @@ use serde::{Deserialize, Serialize}; use std::path::Path; use url::Url; +use super::LocalWireguardOpts; + #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct ExitGatewayConfig { @@ -136,6 +138,7 @@ pub struct EphemeralConfig { pub gateway: nym_gateway::config::Config, pub nr_opts: LocalNetworkRequesterOpts, pub ipr_opts: LocalIpPacketRouterOpts, + pub wg_opts: LocalWireguardOpts, } fn base_client_config(config: &Config) -> nym_client_core_config_types::Client { @@ -241,6 +244,18 @@ pub fn ephemeral_exit_gateway_config( let ipr_enabled = config.exit_gateway.ip_packet_router.debug.enabled; let nr_enabled = config.exit_gateway.network_requester.debug.enabled; + let wg_opts = LocalWireguardOpts { + config: super::Wireguard { + enabled: config.wireguard.enabled, + bind_address: config.wireguard.bind_address, + private_network_ip: config.wireguard.private_network_ip, + announced_port: config.wireguard.announced_port, + private_network_prefix: config.wireguard.private_network_prefix, + storage_paths: config.wireguard.storage_paths.clone(), + }, + custom_mixnet_path: None, + }; + let mut gateway = ephemeral_gateway_config(config, mnemonic)?; gateway.ip_packet_router.enabled = ipr_enabled; gateway.network_requester.enabled = nr_enabled; @@ -253,6 +268,7 @@ pub fn ephemeral_exit_gateway_config( Ok(EphemeralConfig { nr_opts, ipr_opts, + wg_opts, gateway, }) } diff --git a/nym-node/src/config/helpers.rs b/nym-node/src/config/helpers.rs index 6b9c4e91b9..bb1ec6e0e8 100644 --- a/nym-node/src/config/helpers.rs +++ b/nym-node/src/config/helpers.rs @@ -57,18 +57,10 @@ pub fn ephemeral_gateway_config( cosmos_mnemonic: mnemonic.clone(), }; - let wireguard = nym_wireguard_types::config::Wireguard { - enabled: config.wireguard.enabled, - bind_address: config.wireguard.bind_address, - announced_port: config.wireguard.announced_port, - private_network_prefix: config.wireguard.private_network_prefix, - }; - Ok(nym_gateway::config::Config::externally_loaded( host, http, gateway, - wireguard, nym_gateway::config::GatewayPaths::new_empty(), nym_gateway::config::NetworkRequester { enabled: false }, nym_gateway::config::IpPacketRouter { enabled: false }, diff --git a/nym-node/src/config/mod.rs b/nym-node/src/config/mod.rs index 0b8e6f5da6..82a5914455 100644 --- a/nym-node/src/config/mod.rs +++ b/nym-node/src/config/mod.rs @@ -531,3 +531,10 @@ impl Wireguard { } } } + +#[derive(Debug, Clone)] +pub struct LocalWireguardOpts { + pub config: Wireguard, + + pub custom_mixnet_path: Option, +} diff --git a/nym-node/src/config/persistence.rs b/nym-node/src/config/persistence.rs index 530f815aef..1afb2bb8a1 100644 --- a/nym-node/src/config/persistence.rs +++ b/nym-node/src/config/persistence.rs @@ -43,6 +43,10 @@ pub const DEFAULT_IPR_ACK_KEY_FILENAME: &str = "aes128ctr_ipr_ack"; pub const DEFAULT_IPR_REPLY_SURB_DB_FILENAME: &str = "ipr_persistent_reply_store.sqlite"; pub const DEFAULT_IPR_GATEWAYS_DB_FILENAME: &str = "ipr_gateways_info_store.sqlite"; +// Wireguard +pub const DEFAULT_X25519_WG_DH_KEY_FILENAME: &str = "x25519_wg_dh"; +pub const DEFAULT_X25519_WG_PUBLIC_DH_KEY_FILENAME: &str = "x25519_wg_dh.pub"; + #[derive(Debug, Clone, Deserialize, PartialEq, Eq, Serialize)] #[serde(deny_unknown_fields)] pub struct NymNodePaths { @@ -366,11 +370,16 @@ impl ExitGatewayPaths { #[derive(Debug, Clone, Deserialize, PartialEq, Eq, Serialize)] #[serde(deny_unknown_fields)] pub struct WireguardPaths { - // pub keys: + pub private_diffie_hellman_key_file: PathBuf, + pub public_diffie_hellman_key_file: PathBuf, } impl WireguardPaths { - pub fn new>(_data_dir: P) -> Self { - WireguardPaths {} + pub fn new>(data_dir: P) -> Self { + let data_dir = data_dir.as_ref(); + WireguardPaths { + private_diffie_hellman_key_file: data_dir.join(DEFAULT_X25519_WG_DH_KEY_FILENAME), + public_diffie_hellman_key_file: data_dir.join(DEFAULT_X25519_WG_PUBLIC_DH_KEY_FILENAME), + } } } diff --git a/nym-node/src/config/template.rs b/nym-node/src/config/template.rs index 2b053b759c..5fe2c2e2a3 100644 --- a/nym-node/src/config/template.rs +++ b/nym-node/src/config/template.rs @@ -127,9 +127,12 @@ announced_port = {{ wireguard.announced_port }} # The maximum value for IPv4 is 32 and for IPv6 is 128 private_network_prefix = {{ wireguard.private_network_prefix }} -# Paths for wireguard keys, client registries, etc. [wireguard.storage_paths] -# currently empty +# Path to file containing wireguard x25519 diffie hellman private key. +private_x25519_diffie_hellman_key_file = '{{ wireguard.storage_paths.private_diffie_hellman_key_file }}' + +# Path to file containing wireguard x25519 diffie hellman public key. +public_x25519_diffie_hellman_key_file = '{{ wireguard.storage_paths.public_diffie_hellman_key_file }}' ##### mixnode mode nym-node config options #####