Skip to content

Commit

Permalink
chore: Add and remove external addresses by NAT status
Browse files Browse the repository at this point in the history
  • Loading branch information
bgins committed Apr 2, 2024
1 parent 62fc71a commit 871ba53
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions homestar-runtime/src/event_handler/swarm_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use libipld::Cid;
#[cfg(feature = "websocket-notify")]
use libp2p::Multiaddr;
use libp2p::{
autonat, gossipsub, identify, kad,
autonat::{self, NatStatus},
gossipsub, identify, kad,
kad::{AddProviderOk, BootstrapOk, GetProvidersOk, GetRecordOk, PutRecordOk, QueryResult},
mdns,
multiaddr::Protocol,
Expand Down Expand Up @@ -121,8 +122,33 @@ async fn handle_swarm_event<DB: Database>(
)
}
autonat::Event::StatusChanged { old, new } => {
// TODO Add log
println!("STATUS CHANGED: Old - {old:?}, New - {new:?}");
match &new {
NatStatus::Public(address) => {
event_handler.swarm.add_external_address(address.clone());

info!(
subject = "libp2p.autonat.status_change",
category = "handle_swarm_event",
address = address.to_string(),
"Confirmed a public address",
);
}
_ => {
if let NatStatus::Public(address) = old {
// Announce addresses are configured and should not be removed
if !event_handler.announce_addresses.contains(&address) {
event_handler.swarm.remove_external_address(&address);

info!(
subject = "libp2p.autonat.status_change",
category = "handle_swarm_event",
address = address.to_string(),
"Removed an address that is no longer public",
);
}
}
}
}

#[cfg(feature = "websocket-notify")]
notification::emit_network_event(
Expand Down

0 comments on commit 871ba53

Please sign in to comment.