From a7fa72e8dc9e9fe8fe14387b14260e3d64f73cfa Mon Sep 17 00:00:00 2001 From: Justin Kilpatrick Date: Wed, 20 Nov 2024 23:14:13 -0500 Subject: [PATCH] Fix: Unify client and exit wg port constants and literals Since the exit registration smart contract does not store data about the client wireguard listen port it exists only as a constant. Before this patch it was two constants one in the exit code and one in the client. It's not strictly required to set the client listen port correctly, the server listen port is passed through the registration contract and is sufficient to generate a handshake. But obviously there's no reason to have two constants for the same value around. The issue that actually generated errors was in the integration test, where the wg_exit listen port for smart contract registration was hardcoded indepenently of the default value in the rita_exit settings. By unifying these constants a confusing integration test error is no longer possible. --- integration_tests/src/debts.rs | 13 +++++++++---- integration_tests/src/five_nodes.rs | 13 +++++++++---- integration_tests/src/mutli_exit.rs | 9 +++++++-- integration_tests/src/payments_althea.rs | 13 +++++++++---- integration_tests/src/payments_eth.rs | 13 +++++++++---- integration_tests/src/utils.rs | 10 +++++++--- rita_client/src/exit_manager/mod.rs | 3 --- rita_client/src/exit_manager/requests.rs | 6 +++--- rita_client/src/exit_manager/utils.rs | 8 ++++++-- rita_common/src/lib.rs | 5 +++++ rita_exit/src/database/ipddr_assignment.rs | 4 +--- settings/src/exit.rs | 2 +- 12 files changed, 66 insertions(+), 33 deletions(-) diff --git a/integration_tests/src/debts.rs b/integration_tests/src/debts.rs index 82ce533bb..cee5d3c7f 100644 --- a/integration_tests/src/debts.rs +++ b/integration_tests/src/debts.rs @@ -59,13 +59,18 @@ pub async fn run_debts_test() { info!("Starting root server!"); spawn_exit_root_of_trust(db_addr).await; - let rita_identities = - thread_spawner(namespaces.clone(), client_settings, exit_settings, db_addr) - .expect("Could not spawn Rita threads"); + let rita_identities = thread_spawner( + namespaces.clone(), + client_settings, + exit_settings.clone(), + db_addr, + ) + .expect("Could not spawn Rita threads"); info!("Thread Spawner: {res:?}"); // Add exits to the contract exit list so clients get the propers exits they can migrate to - add_exits_contract_exit_list(db_addr, rita_identities.clone()).await; + add_exits_contract_exit_list(db_addr, exit_settings.exit_network, rita_identities.clone()) + .await; populate_routers_eth(rita_identities, exit_root_addr).await; diff --git a/integration_tests/src/five_nodes.rs b/integration_tests/src/five_nodes.rs index 7a7fd169b..ecd16a4a1 100644 --- a/integration_tests/src/five_nodes.rs +++ b/integration_tests/src/five_nodes.rs @@ -28,13 +28,18 @@ pub async fn run_five_node_test_scenario() { info!("Starting root server!"); spawn_exit_root_of_trust(db_addr).await; - let rita_identities = - thread_spawner(namespaces.clone(), client_settings, exit_settings, db_addr) - .expect("Could not spawn Rita threads"); + let rita_identities = thread_spawner( + namespaces.clone(), + client_settings, + exit_settings.clone(), + db_addr, + ) + .expect("Could not spawn Rita threads"); info!("Thread Spawner: {res:?}"); // Add exits to the contract exit list so clients get the propers exits they can migrate to - add_exits_contract_exit_list(db_addr, rita_identities.clone()).await; + add_exits_contract_exit_list(db_addr, exit_settings.exit_network, rita_identities.clone()) + .await; // this sleep is for debugging so that the container can be accessed to poke around in //thread::sleep(SETUP_WAIT * 500); diff --git a/integration_tests/src/mutli_exit.rs b/integration_tests/src/mutli_exit.rs index aa44987be..a411a2a7c 100644 --- a/integration_tests/src/mutli_exit.rs +++ b/integration_tests/src/mutli_exit.rs @@ -53,14 +53,19 @@ pub async fn run_multi_exit_test() { let rita_identities = thread_spawner( namespaces.clone(), rita_client_settings, - rita_exit_settings, + rita_exit_settings.clone(), db_addr, ) .expect("Could not spawn Rita threads"); info!("Thread Spawner: {res:?}"); // Add exits to the contract exit list so clients get the propers exits they can migrate to - add_exits_contract_exit_list(db_addr, rita_identities.clone()).await; + add_exits_contract_exit_list( + db_addr, + rita_exit_settings.exit_network, + rita_identities.clone(), + ) + .await; populate_routers_eth(rita_identities, exit_root_addr).await; diff --git a/integration_tests/src/payments_althea.rs b/integration_tests/src/payments_althea.rs index e0fc57553..154239f5a 100644 --- a/integration_tests/src/payments_althea.rs +++ b/integration_tests/src/payments_althea.rs @@ -62,13 +62,18 @@ pub async fn run_althea_payments_test_scenario() { let (client_settings, exit_settings) = althea_payments_map(&mut client_settings, &mut exit_settings); - let rita_identities = - thread_spawner(namespaces.clone(), client_settings, exit_settings, db_addr) - .expect("Could not spawn Rita threads"); + let rita_identities = thread_spawner( + namespaces.clone(), + client_settings, + exit_settings.clone(), + db_addr, + ) + .expect("Could not spawn Rita threads"); info!("Thread Spawner: {res:?}"); // Add exits to the contract exit list so clients get the propers exits they can migrate to - add_exits_contract_exit_list(db_addr, rita_identities.clone()).await; + add_exits_contract_exit_list(db_addr, exit_settings.exit_network, rita_identities.clone()) + .await; populate_routers_eth(rita_identities.clone(), exit_root_addr).await; diff --git a/integration_tests/src/payments_eth.rs b/integration_tests/src/payments_eth.rs index 7170075ee..58428a53a 100644 --- a/integration_tests/src/payments_eth.rs +++ b/integration_tests/src/payments_eth.rs @@ -62,13 +62,18 @@ pub async fn run_eth_payments_test_scenario() { info!("Starting root server!"); spawn_exit_root_of_trust(db_addr).await; - let rita_identities = - thread_spawner(namespaces.clone(), client_settings, exit_settings, db_addr) - .expect("Could not spawn Rita threads"); + let rita_identities = thread_spawner( + namespaces.clone(), + client_settings, + exit_settings.clone(), + db_addr, + ) + .expect("Could not spawn Rita threads"); info!("Thread Spawner: {res:?}"); // Add exits to the contract exit list so clients get the propers exits they can migrate to - add_exits_contract_exit_list(db_addr, rita_identities.clone()).await; + add_exits_contract_exit_list(db_addr, exit_settings.exit_network, rita_identities.clone()) + .await; populate_routers_eth(rita_identities, exit_root_addr).await; diff --git a/integration_tests/src/utils.rs b/integration_tests/src/utils.rs index dcf933977..42fabcd29 100644 --- a/integration_tests/src/utils.rs +++ b/integration_tests/src/utils.rs @@ -1184,7 +1184,11 @@ pub async fn populate_routers_eth(rita_identities: InstanceData, exit_root_addr: send_eth_bulk((ONE_ETH * 50).into(), &to_top_up, &web3).await; } -pub async fn add_exits_contract_exit_list(db_addr: Address, rita_identities: InstanceData) { +pub async fn add_exits_contract_exit_list( + db_addr: Address, + exit_settings: ExitNetworkSettings, + rita_identities: InstanceData, +) { let web3 = Web3::new(&get_eth_node(), WEB3_TIMEOUT); let miner_private_key: clarity::PrivateKey = REGISTRATION_SERVER_KEY.parse().unwrap(); let miner_pub_key = miner_private_key.to_address(); @@ -1207,8 +1211,8 @@ pub async fn add_exits_contract_exit_list(db_addr: Address, rita_identities: Ins mesh_ip: id.mesh_ip, wg_key: id.wg_public_key, eth_addr: id.eth_address, - registration_port: 4875, - wg_exit_listen_port: 59998, + registration_port: exit_settings.exit_hello_port, + wg_exit_listen_port: exit_settings.wg_tunnel_port, allowed_regions: { let mut ret = HashSet::new(); ret.insert(Regions::UnitedStates); diff --git a/rita_client/src/exit_manager/mod.rs b/rita_client/src/exit_manager/mod.rs index abb2c1cf0..5f7731c73 100644 --- a/rita_client/src/exit_manager/mod.rs +++ b/rita_client/src/exit_manager/mod.rs @@ -30,9 +30,6 @@ use std::collections::HashMap; use std::time::Duration; use std::time::Instant; -/// TODO replace with a component in the exit config struct -const DEFAULT_WG_LISTEN_PORT: u16 = 59998; - /// Data to use identity whether a clients wg exit tunnel needs to be setup up again across ticks #[derive(Clone)] pub struct LastExitStates { diff --git a/rita_client/src/exit_manager/requests.rs b/rita_client/src/exit_manager/requests.rs index ca003215f..28b27c730 100644 --- a/rita_client/src/exit_manager/requests.rs +++ b/rita_client/src/exit_manager/requests.rs @@ -1,5 +1,4 @@ use super::get_current_exit; -use super::DEFAULT_WG_LISTEN_PORT; use crate::rita_loop::CLIENT_LOOP_TIMEOUT; use crate::RitaClientError; use actix_web::Result; @@ -9,6 +8,7 @@ use althea_types::ExitIdentity; use althea_types::SignedExitServerList; use althea_types::WgKey; use althea_types::{ExitClientIdentity, ExitRegistrationDetails, ExitState}; +use rita_common::CLIENT_WG_PORT; use settings::exit::EXIT_LIST_IP; use settings::exit::EXIT_LIST_PORT; use settings::get_registration_details; @@ -131,7 +131,7 @@ pub async fn exit_setup_request(code: Option) -> Result<(), RitaClientEr )); } }, - wg_port: DEFAULT_WG_LISTEN_PORT, + wg_port: CLIENT_WG_PORT, reg_details, }; @@ -209,7 +209,7 @@ pub async fn exit_status_request(exit: ExitIdentity) -> Result<(), RitaClientErr )); } }, - wg_port: DEFAULT_WG_LISTEN_PORT, + wg_port: CLIENT_WG_PORT, reg_details, }; diff --git a/rita_client/src/exit_manager/utils.rs b/rita_client/src/exit_manager/utils.rs index 8d945fc46..c9abda1af 100644 --- a/rita_client/src/exit_manager/utils.rs +++ b/rita_client/src/exit_manager/utils.rs @@ -1,5 +1,4 @@ use super::LastExitStates; -use crate::exit_manager::DEFAULT_WG_LISTEN_PORT; use crate::heartbeat::get_exit_registration_state; use crate::rita_loop::CLIENT_LOOP_TIMEOUT; use crate::RitaClientError; @@ -21,6 +20,7 @@ use babel_monitor::open_babel_stream; use babel_monitor::parse_routes; use babel_monitor::structs::Route; use ipnetwork::IpNetwork; +use rita_common::CLIENT_WG_PORT; use std::net::SocketAddr; pub fn linux_setup_exit_tunnel( @@ -40,11 +40,15 @@ pub fn linux_setup_exit_tunnel( return Err(RitaClientError::MiscStringError(v)); } + error!( + "Got wg exit listen port as: {}", + selected_exit.wg_exit_listen_port + ); let args = ClientExitTunnelConfig { endpoint: SocketAddr::new(selected_exit.mesh_ip, selected_exit.wg_exit_listen_port), pubkey: selected_exit.wg_key, private_key_path: network.wg_private_key_path.clone(), - listen_port: DEFAULT_WG_LISTEN_PORT, + listen_port: CLIENT_WG_PORT, local_ip: our_details.client_internal_ip, netmask: general_details.netmask, rita_hello_port: network.rita_hello_port, diff --git a/rita_common/src/lib.rs b/rita_common/src/lib.rs index 5bc2a76e2..b96a64a38 100644 --- a/rita_common/src/lib.rs +++ b/rita_common/src/lib.rs @@ -11,6 +11,11 @@ extern crate arrayvec; pub static DROPBEAR_CONFIG: &str = "/etc/config/dropbear"; pub static DROPBEAR_AUTHORIZED_KEYS: &str = "/etc/dropbear/authorized_keys"; +/// Default wg_exit port on the client side, by default the client reaches out to the server on the port +/// provided in th exit entry. But the exit can reach out to the client provided it knows the port +/// the client is listening on. Which will be this value. +pub const CLIENT_WG_PORT: u16 = 59999; + pub mod blockchain_oracle; pub mod dashboard; pub mod debt_keeper; diff --git a/rita_exit/src/database/ipddr_assignment.rs b/rita_exit/src/database/ipddr_assignment.rs index c14130eed..a03920c54 100644 --- a/rita_exit/src/database/ipddr_assignment.rs +++ b/rita_exit/src/database/ipddr_assignment.rs @@ -4,15 +4,13 @@ use crate::RitaExitError; use althea_kernel_interface::ExitClient; use althea_types::{ExitClientDetails, ExitClientIdentity, ExitState, Identity}; use ipnetwork::{IpNetwork, Ipv6Network}; +use rita_common::CLIENT_WG_PORT; use settings::exit::{ExitInternalIpv4Settings, ExitIpv4RoutingSettings, ExitIpv6RoutingSettings}; use settings::get_rita_exit; use std::collections::{HashMap, HashSet}; use std::fmt::Write; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; -/// Wg exit port on client side -pub const CLIENT_WG_PORT: u16 = 59999; - /// Max number of time we try to generate a valid ip addr before returning an eror pub const MAX_IP_RETRIES: u8 = 10; diff --git a/settings/src/exit.rs b/settings/src/exit.rs index 1dd758325..974cfcc9f 100644 --- a/settings/src/exit.rs +++ b/settings/src/exit.rs @@ -261,7 +261,7 @@ impl ExitNetworkSettings { pub fn test_default() -> Self { ExitNetworkSettings { exit_hello_port: 4875, - wg_tunnel_port: 59998, + wg_tunnel_port: 59999, exit_price: 10, geoip_api_user: None, geoip_api_key: None,