From e701c8fdbd89eda3ba3ac17ab7328b52113dbf59 Mon Sep 17 00:00:00 2001 From: Bo QIU <35757521+boqiu@users.noreply.github.com> Date: Mon, 14 Oct 2024 14:57:42 +0800 Subject: [PATCH] Supports custom public ip to announce file (#233) * Supports custom public ip to announce file * Fix comment --- node/router/src/lib.rs | 4 +++- node/router/src/libp2p_event_handler.rs | 19 ++++++++++++++----- node/src/config/convert.rs | 7 +++++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/node/router/src/lib.rs b/node/router/src/lib.rs index 5cd21c7..4bb746c 100644 --- a/node/router/src/lib.rs +++ b/node/router/src/lib.rs @@ -10,7 +10,7 @@ mod service; use duration_str::deserialize_duration; use network::Multiaddr; use serde::Deserialize; -use std::time::Duration; +use std::{net::IpAddr, time::Duration}; pub use crate::service::RouterService; @@ -26,6 +26,7 @@ pub struct Config { pub libp2p_nodes: Vec, pub private_ip_enabled: bool, pub check_announced_ip: bool, + pub public_address: Option, // batcher /// Timeout to publish messages in batch @@ -47,6 +48,7 @@ impl Default for Config { libp2p_nodes: vec![], private_ip_enabled: false, check_announced_ip: false, + public_address: None, batcher_timeout: Duration::from_secs(1), batcher_file_capacity: 1, diff --git a/node/router/src/libp2p_event_handler.rs b/node/router/src/libp2p_event_handler.rs index 5ed5f71..d1b09cc 100644 --- a/node/router/src/libp2p_event_handler.rs +++ b/node/router/src/libp2p_event_handler.rs @@ -348,17 +348,26 @@ impl Libp2pEventHandler { } } - async fn get_listen_addr_or_add(&self) -> Option { + async fn construct_announced_ip(&self) -> Option { + // public address configured + if let Some(ip) = self.config.public_address { + let mut addr = Multiaddr::empty(); + addr.push(ip.into()); + addr.push(Protocol::Tcp(self.network_globals.listen_port_tcp())); + return Some(addr); + } + + // public listen address if let Some(addr) = self.get_listen_addr() { return Some(addr); } + // auto detect public IP address let ipv4_addr = public_ip::addr_v4().await?; let mut addr = Multiaddr::empty(); addr.push(Protocol::Ip4(ipv4_addr)); addr.push(Protocol::Tcp(self.network_globals.listen_port_tcp())); - addr.push(Protocol::P2p(self.network_globals.local_peer_id().into())); self.network_globals .listen_multiaddrs @@ -420,7 +429,7 @@ impl Libp2pEventHandler { let peer_id = *self.network_globals.peer_id.read(); - let addr = self.get_listen_addr_or_add().await?; + let addr = self.construct_announced_ip().await?; let timestamp = timestamp_now(); let shard_config = self.store.get_store().get_shard_config(); @@ -452,7 +461,7 @@ impl Libp2pEventHandler { shard_config: ShardConfig, ) -> Option { let peer_id = *self.network_globals.peer_id.read(); - let addr = self.get_listen_addr_or_add().await?; + let addr = self.construct_announced_ip().await?; let timestamp = timestamp_now(); let msg = AnnounceShardConfig { @@ -528,7 +537,7 @@ impl Libp2pEventHandler { index_end: u64, ) -> Option { let peer_id = *self.network_globals.peer_id.read(); - let addr = self.get_listen_addr_or_add().await?; + let addr = self.construct_announced_ip().await?; let timestamp = timestamp_now(); let msg = AnnounceChunks { diff --git a/node/src/config/convert.rs b/node/src/config/convert.rs index 34dc3d3..c31679d 100644 --- a/node/src/config/convert.rs +++ b/node/src/config/convert.rs @@ -200,6 +200,13 @@ impl ZgsConfig { pub fn router_config(&self, network_config: &NetworkConfig) -> Result { let mut router_config = self.router.clone(); router_config.libp2p_nodes = network_config.libp2p_nodes.to_vec(); + + if router_config.public_address.is_none() { + if let Some(addr) = &self.network_enr_address { + router_config.public_address = Some(addr.parse().unwrap()); + } + } + Ok(router_config) }