diff --git a/bin/periphery/src/config.rs b/bin/periphery/src/config.rs index 66f65c504..94d016773 100644 --- a/bin/periphery/src/config.rs +++ b/bin/periphery/src/config.rs @@ -164,11 +164,13 @@ pub fn periphery_config() -> &'static PeripheryConfig { .or(config.passkeys), core_addresses: env .periphery_core_addresses - .or(config.core_addresses), + .unwrap_or(config.core_addresses), core_tls_insecure_skip_verify: env .periphery_core_tls_insecure_skip_verify .unwrap_or(config.core_tls_insecure_skip_verify), - connect_as: env.periphery_connect_as.or(config.connect_as), + connect_as: env + .periphery_connect_as + .unwrap_or(config.connect_as), server_enabled: env .periphery_server_enabled .unwrap_or(config.server_enabled), diff --git a/bin/periphery/src/connection/client.rs b/bin/periphery/src/connection/client.rs index 3f8a91f4a..08140ca61 100644 --- a/bin/periphery/src/connection/client.rs +++ b/bin/periphery/src/connection/client.rs @@ -21,13 +21,13 @@ use crate::{ connection::{CorePublicKeyValidator, core_channels}, }; -pub async fn handler( - address: &str, - connect_as: &str, -) -> anyhow::Result<()> { +pub async fn handler(address: &str) -> anyhow::Result<()> { let address = fix_ws_address(address); let identifiers = AddressConnectionIdentifiers::extract(&address)?; - let query = format!("server={}", urlencoding::encode(connect_as)); + let query = format!( + "server={}", + urlencoding::encode(&periphery_config().connect_as) + ); let endpoint = format!("{address}/ws/periphery?{query}"); info!("Initiating outbound connection to {endpoint}"); @@ -47,7 +47,7 @@ pub async fn handler( loop { let (mut socket, accept) = - match connect_websocket(&endpoint, connect_as).await { + match connect_websocket(&endpoint).await { Ok(res) => res, Err(e) => { if !already_logged_connection_error { @@ -129,9 +129,7 @@ pub async fn handler( } // Creation &[1] => { - if let Err(e) = - handle_onboarding(connect_as, socket, identifiers).await - { + if let Err(e) = handle_onboarding(socket, identifiers).await { if !already_logged_onboarding_error { error!("{e:#}"); already_logged_onboarding_error = true; @@ -162,14 +160,14 @@ pub async fn handler( } async fn handle_onboarding( - connect_as: &str, mut socket: TungsteniteWebsocket, identifiers: ConnectionIdentifiers<'_>, ) -> anyhow::Result<()> { - let onboarding_key = periphery_config() + let config = periphery_config(); + let onboarding_key = config .onboarding_key .as_deref() - .with_context(|| format!("Server {connect_as} does not exist, and no PERIPHERY_ONBOARDING_KEY is provided."))?; + .with_context(|| format!("Server {} does not exist, and no PERIPHERY_ONBOARDING_KEY is provided.", config.connect_as))?; ClientLoginFlow::login(LoginFlowArgs { private_key: onboarding_key, @@ -196,7 +194,8 @@ async fn handle_onboarding( match res.last().map(|byte| MessageState::from_byte(*byte)) { Some(MessageState::Successful) => { info!( - "Server onboarding flow for '{connect_as}' successful ✅" + "Server onboarding flow for '{}' successful ✅", + config.connect_as ); Ok(()) } @@ -211,14 +210,14 @@ async fn handle_onboarding( async fn connect_websocket( url: &str, - connect_as: &str, ) -> anyhow::Result<(TungsteniteWebsocket, HeaderValue)> { - TungsteniteWebsocket::connect_maybe_tls_insecure(url, periphery_config().core_tls_insecure_skip_verify) + let config = periphery_config(); + TungsteniteWebsocket::connect_maybe_tls_insecure(url, config.core_tls_insecure_skip_verify) .await .map_err(|e| match e.status { - StatusCode::NOT_FOUND => anyhow!("404 Not Found: Server '{connect_as}' does not exist."), - StatusCode::BAD_REQUEST => anyhow!("400 Bad Request: Server '{connect_as}' is disabled or configured to make Core → Periphery connection"), - StatusCode::UNAUTHORIZED => anyhow!("401 Unauthorized: Only one Server connected as '{connect_as}' is allowed. Or the Core reverse proxy needs to forward host and websocket headers."), + StatusCode::NOT_FOUND => anyhow!("404 Not Found: Server '{}' does not exist.", config.connect_as), + StatusCode::BAD_REQUEST => anyhow!("400 Bad Request: Server '{}' is disabled or configured to make Core → Periphery connection", config.connect_as), + StatusCode::UNAUTHORIZED => anyhow!("401 Unauthorized: Only one Server connected as '{}' is allowed. Or the Core reverse proxy needs to forward host and websocket headers.", config.connect_as), _ => e.error, }) } diff --git a/bin/periphery/src/connection/mod.rs b/bin/periphery/src/connection/mod.rs index 00497919e..a7d80f31f 100644 --- a/bin/periphery/src/connection/mod.rs +++ b/bin/periphery/src/connection/mod.rs @@ -92,10 +92,10 @@ async fn handle_socket( info!( "Logged in to Komodo Core {} websocket{}", args.core, - if config.core_addresses.is_some() - && let Some(connect_as) = &config.connect_as + if !config.core_addresses.is_empty() + && !config.connect_as.is_empty() { - format!(" as Server {connect_as}") + format!(" as Server {}", config.connect_as) } else { String::new() } diff --git a/bin/periphery/src/main.rs b/bin/periphery/src/main.rs index 1bf69f29c..751f6132c 100644 --- a/bin/periphery/src/main.rs +++ b/bin/periphery/src/main.rs @@ -39,20 +39,16 @@ async fn app() -> anyhow::Result<()> { let mut handles = FuturesUnordered::new(); // Spawn client side connections - match (&config.core_addresses, &config.connect_as) { - (Some(addresses), Some(connect_as)) => { - for address in addresses { - handles.push(tokio::spawn(connection::client::handler( - address, connect_as, - ))); - } + if !config.core_addresses.is_empty() && config.connect_as.is_empty() + { + warn!( + "'core_addresses' are defined for outbound connection, but missing 'connect_as' (PERIPHERY_CONNECT_AS)." + ); + } else { + for address in &config.core_addresses { + handles + .push(tokio::spawn(connection::client::handler(address))); } - (Some(_), None) => { - warn!( - "'core_addresses' are defined for outbound connection, but missing 'connect_as' (PERIPHERY_CONNECT_AS)." - ); - } - _ => {} } // Spawn server connection handler diff --git a/client/core/rs/src/entities/config/periphery.rs b/client/core/rs/src/entities/config/periphery.rs index 7d60e0449..4fd76c9e3 100644 --- a/client/core/rs/src/entities/config/periphery.rs +++ b/client/core/rs/src/entities/config/periphery.rs @@ -18,7 +18,10 @@ use serde::Deserialize; use std::{collections::HashMap, path::PathBuf}; use crate::{ - deserializers::{ForgivingVec, option_string_list_deserializer}, + deserializers::{ + ForgivingVec, option_string_list_deserializer, + string_list_deserializer, + }, entities::{ Timelength, logger::{LogConfig, LogLevel, StdioLogMode}, @@ -258,10 +261,9 @@ pub struct PeripheryConfig { #[serde( default, alias = "core_address", - deserialize_with = "option_string_list_deserializer", - skip_serializing_if = "Option::is_none" + deserialize_with = "string_list_deserializer" )] - pub core_addresses: Option>, + pub core_addresses: Vec, /// Allow Periphery to connect to Core /// without validating the Core certs @@ -269,8 +271,8 @@ pub struct PeripheryConfig { pub core_tls_insecure_skip_verify: bool, /// Server name / id to connect as - #[serde(skip_serializing_if = "Option::is_none")] - pub connect_as: Option, + #[serde(default)] + pub connect_as: String, // ====================== // = INBOUND CONNECTION = @@ -443,9 +445,9 @@ impl Default for PeripheryConfig { onboarding_key: None, core_public_keys: None, passkeys: None, - core_addresses: None, + core_addresses: Default::default(), core_tls_insecure_skip_verify: Default::default(), - connect_as: None, + connect_as: Default::default(), server_enabled: default_server_enabled(), port: default_periphery_port(), bind_ip: default_periphery_bind_ip(),