Skip to content

Commit

Permalink
Fix: Unify client and exit wg port constants and literals
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jkilpatr committed Nov 21, 2024
1 parent 05cfe6e commit a7fa72e
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 33 deletions.
13 changes: 9 additions & 4 deletions integration_tests/src/debts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 9 additions & 4 deletions integration_tests/src/five_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions integration_tests/src/mutli_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 9 additions & 4 deletions integration_tests/src/payments_althea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 9 additions & 4 deletions integration_tests/src/payments_eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 7 additions & 3 deletions integration_tests/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions rita_client/src/exit_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions rita_client/src/exit_manager/requests.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -131,7 +131,7 @@ pub async fn exit_setup_request(code: Option<String>) -> Result<(), RitaClientEr
));
}
},
wg_port: DEFAULT_WG_LISTEN_PORT,
wg_port: CLIENT_WG_PORT,
reg_details,
};

Expand Down Expand Up @@ -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,
};

Expand Down
8 changes: 6 additions & 2 deletions rita_client/src/exit_manager/utils.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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(
Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions rita_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions rita_exit/src/database/ipddr_assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion settings/src/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a7fa72e

Please sign in to comment.