Skip to content

Commit

Permalink
fix loopback_only
Browse files Browse the repository at this point in the history
  • Loading branch information
ddeguglielmo committed Oct 30, 2023
1 parent 06a96b8 commit 786ac5f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
2 changes: 1 addition & 1 deletion dht-cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
25 changes: 15 additions & 10 deletions dht-cache/src/domocache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl Display for DomoCacheElement {
}
}


pub struct DomoCache {
storage: SqlxStorage,
pub cache: BTreeMap<String, BTreeMap<String, DomoCacheElement>>,
Expand All @@ -92,6 +93,7 @@ pub struct DomoCache {
client_tx_channel: Sender<DomoEvent>,
client_rx_channel: Receiver<DomoEvent>,
send_cache_state_timer: tokio::time::Instant,
loopback_peers_only: bool
}

enum Event {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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:?}");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions dht-cache/src/domolibp2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down Expand Up @@ -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)
}
Expand Down
28 changes: 28 additions & 0 deletions dht-cache/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
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
}

0 comments on commit 786ac5f

Please sign in to comment.