From 786ac5f9fdce748064dc7705ee5e5e632d860ba5 Mon Sep 17 00:00:00 2001 From: Domenico De Guglielmo Date: Mon, 30 Oct 2023 10:08:19 +0100 Subject: [PATCH] fix loopback_only --- dht-cache/Cargo.toml | 2 +- dht-cache/src/domocache.rs | 25 +++++++++++++++---------- dht-cache/src/domolibp2p.rs | 11 +++-------- dht-cache/src/utils.rs | 28 ++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/dht-cache/Cargo.toml b/dht-cache/Cargo.toml index 1098d70..2951b6a 100644 --- a/dht-cache/Cargo.toml +++ b/dht-cache/Cargo.toml @@ -28,7 +28,7 @@ pem-rfc7468 = { version = "0.7", features = ["alloc"] } sifis-config = { path = "../dht-config" } openssl-sys = "*" libsqlite3-sys = "*" - +local-ip-address = "0.4.9" [package.metadata.cargo-udeps.ignore] normal = ["openssl-sys", "libsqlite3-sys", "libc"] diff --git a/dht-cache/src/domocache.rs b/dht-cache/src/domocache.rs index 027bd75..73c1f58 100644 --- a/dht-cache/src/domocache.rs +++ b/dht-cache/src/domocache.rs @@ -80,6 +80,7 @@ impl Display for DomoCacheElement { } } + pub struct DomoCache { storage: SqlxStorage, pub cache: BTreeMap>, @@ -92,6 +93,7 @@ pub struct DomoCache { client_tx_channel: Sender, client_rx_channel: Receiver, send_cache_state_timer: tokio::time::Instant, + loopback_peers_only: bool } enum Event { @@ -311,14 +313,10 @@ impl DomoCache { m.peer_id ); - println!("INSERTING {}", m.peer_id); - let mut need_check = false; - println!("{:?}", self.peers_caches_state); - if let Some(old) = self.peers_caches_state.get(&m.peer_id) { - log::info!("NOW {} OLD {}", get_epoch_ms(), old.publication_timestamp); + if old.publication_timestamp < (get_epoch_ms() - 2 * 1000 * u128::from(SEND_CACHE_HASH_PERIOD) ) { need_check = true; } @@ -412,19 +410,19 @@ impl DomoCache { log::debug!("Address {address:?} expired"); } SwarmEvent::ConnectionEstablished { peer_id, connection_id, endpoint, .. } => { - log::debug!("Connection established {peer_id:?}, {connection_id:?}, {endpoint:?}"); + log::info!("Connection established {peer_id:?}, {connection_id:?}, {endpoint:?}"); } SwarmEvent::ConnectionClosed { peer_id, connection_id, endpoint, num_established: _, cause } => { - log::debug!("Connection closed {peer_id:?}, {connection_id:?}, {endpoint:?} -> {cause:?}"); + log::info!("Connection closed {peer_id:?}, {connection_id:?}, {endpoint:?} -> {cause:?}"); } SwarmEvent::ListenerError { listener_id, error } => { log::warn!("Listener Error {listener_id:?} -> {error:?}"); } SwarmEvent::OutgoingConnectionError { connection_id, peer_id, error } => { - log::warn!("Outgoing connection error {peer_id:?}, {connection_id:?} -> {error:?}"); + log::info!("Outgoing connection error {peer_id:?}, {connection_id:?} -> {error:?}"); } SwarmEvent::ListenerClosed { .. } => { - log::debug!("Listener Closed"); + log::info!("Listener Closed"); } SwarmEvent::NewListenAddr { address, .. } => { log::info!("Listening in {address:?}"); @@ -466,7 +464,13 @@ impl DomoCache { mdns::Event::Discovered(list), )) => { let local = OffsetDateTime::now_utc(); - for (peer, _) in list { + for (peer, multiaddr) in list { + log::info!("{}", multiaddr); + let is_local_peer = utils::is_local_peer(&multiaddr.to_string()); + if self.loopback_peers_only && !is_local_peer { + log::info!("Skipping peer since it is not local"); + continue; + } self.swarm .behaviour_mut() .gossipsub @@ -565,6 +569,7 @@ impl DomoCache { client_tx_channel, client_rx_channel, send_cache_state_timer, + loopback_peers_only: loopback_only }; // Populate the cache with the sqlite contents diff --git a/dht-cache/src/domolibp2p.rs b/dht-cache/src/domolibp2p.rs index 6196ec7..e3aa8e1 100644 --- a/dht-cache/src/domolibp2p.rs +++ b/dht-cache/src/domolibp2p.rs @@ -90,7 +90,7 @@ pub async fn start( let mdnsconf = mdns::Config { ttl: Duration::from_secs(600), query_interval: Duration::from_secs(30), - enable_ipv6: false, + enable_ipv6: false }; let mdns = mdns::tokio::Behaviour::new(mdnsconf, local_peer_id)?; @@ -134,13 +134,8 @@ pub async fn start( SwarmBuilder::with_tokio_executor(transport, behaviour, local_peer_id).build() }; - if !loopback_only { - // Listen on all interfaces and whatever port the OS assigns. - swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?; - } else { - // Listen only on loopack interface - swarm.listen_on("/ip4/127.0.0.1/tcp/0".parse()?)?; - } + // Listen on all interfaces and whatever port the OS assigns. + swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?; Ok(swarm) } diff --git a/dht-cache/src/utils.rs b/dht-cache/src/utils.rs index 76d6f69..51d4fa8 100644 --- a/dht-cache/src/utils.rs +++ b/dht-cache/src/utils.rs @@ -1,7 +1,35 @@ use std::time::{SystemTime, UNIX_EPOCH}; +use local_ip_address::list_afinet_netifas; +use std::net::IpAddr; + pub fn get_epoch_ms() -> u128 { SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap() .as_millis() } + +pub fn get_addresses_of_local_interfaces() -> Vec { + let network_interfaces = list_afinet_netifas().unwrap(); + + let mut to_ret = Vec::new(); + + for (_name, ip) in network_interfaces.iter() { + if ip.to_string() != "127.0.0.1" && matches!(ip, IpAddr::V4(_)) { + to_ret.push(ip.to_string()); + } + } + + to_ret +} + +pub fn is_local_peer(multiaddr: &str) -> bool { + let my_addresses = get_addresses_of_local_interfaces(); + log::info!("My addresses {:?}", my_addresses); + for ip in my_addresses { + if multiaddr.to_string().contains(&ip) { + return true; + } + } + false +} \ No newline at end of file