From df670985b05fd3e247a76b33ec2bc9e1addd89d0 Mon Sep 17 00:00:00 2001 From: Benno Zeeman Date: Fri, 19 Jul 2024 23:03:16 +0200 Subject: [PATCH 1/3] feat(identify): add hide_listen_addrs config option --- protocols/identify/CHANGELOG.md | 1 + protocols/identify/src/behaviour.rs | 23 ++++++++-- protocols/identify/tests/smoke.rs | 71 +++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 5 deletions(-) diff --git a/protocols/identify/CHANGELOG.md b/protocols/identify/CHANGELOG.md index b8ab3f8df50..4983dfcd31d 100644 --- a/protocols/identify/CHANGELOG.md +++ b/protocols/identify/CHANGELOG.md @@ -2,6 +2,7 @@ - Add `ConnectionId` in `Event`. See [PR 4981](https://github.com/libp2p/rust-libp2p/pull/4981). +- Add `hide_listen_addrs` option to prevent leaking (local) listen addresses. ## 0.44.2 diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index 92a0dc46103..42a7e9b6e01 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -97,6 +97,12 @@ pub struct Config { /// /// Disabled by default. pub cache_size: usize, + + /// Whether to include our listen addresses in our responses. If enabled, + /// we will effectively only share our external addresses. + /// + /// Disabled by default. + pub hide_listen_addrs: bool, } impl Config { @@ -110,6 +116,7 @@ impl Config { interval: Duration::from_secs(5 * 60), push_listen_addr_updates: false, cache_size: 100, + hide_listen_addrs: false, } } @@ -139,6 +146,12 @@ impl Config { self.cache_size = cache_size; self } + + /// Configures whether we prevent sending out our listen addresses. + pub fn with_hide_listen_addrs(mut self, b: bool) -> Self { + self.hide_listen_addrs = b; + self + } } impl Behaviour { @@ -207,11 +220,11 @@ impl Behaviour { } fn all_addresses(&self) -> HashSet { - self.listen_addresses - .iter() - .chain(self.external_addresses.iter()) - .cloned() - .collect() + let mut addrs = HashSet::from_iter(self.external_addresses.iter().cloned()); + if !self.config.hide_listen_addrs { + addrs.extend(self.listen_addresses.iter().cloned()); + }; + addrs } } diff --git a/protocols/identify/tests/smoke.rs b/protocols/identify/tests/smoke.rs index dd92d10979a..6bf9ebe11c0 100644 --- a/protocols/identify/tests/smoke.rs +++ b/protocols/identify/tests/smoke.rs @@ -228,6 +228,77 @@ async fn emits_unique_listen_addresses() { assert!(reported_addrs.contains(&(swarm2_peer_id, swarm2_tcp_listen_addr))); } +#[async_std::test] +async fn hides_listen_addresses() { + let _ = tracing_subscriber::fmt() + .with_env_filter(EnvFilter::from_default_env()) + .try_init(); + + let mut swarm1 = Swarm::new_ephemeral(|identity| { + identify::Behaviour::new( + identify::Config::new("a".to_string(), identity.public()) + .with_agent_version("b".to_string()) + .with_interval(Duration::from_secs(1)) + .with_cache_size(10), + ) + }); + let mut swarm2 = Swarm::new_ephemeral(|identity| { + identify::Behaviour::new( + identify::Config::new("c".to_string(), identity.public()) + .with_agent_version("d".to_string()) + .with_hide_listen_addrs(true), + ) + }); + + let (_swarm2_mem_listen_addr, swarm2_tcp_listen_addr) = + swarm2.listen().with_tcp_addr_external().await; + let swarm2_peer_id = *swarm2.local_peer_id(); + swarm1.connect(&mut swarm2).await; + + async_std::task::spawn(swarm2.loop_on_next()); + + let swarm_events = futures::stream::poll_fn(|cx| swarm1.poll_next_unpin(cx)) + .take(8) + .collect::>() + .await; + + let infos = swarm_events + .iter() + .filter_map(|e| match e { + SwarmEvent::Behaviour(identify::Event::Received { info, .. }) => Some(info.clone()), + _ => None, + }) + .collect::>(); + + assert!( + infos.len() > 1, + "should exchange identify payload more than once" + ); + + let listen_addrs = infos + .iter() + .map(|i| i.listen_addrs.clone()) + .collect::>(); + + for addrs in listen_addrs { + assert_eq!(addrs.len(), 1); + assert!(addrs.contains(&swarm2_tcp_listen_addr)); + } + + let reported_addrs = swarm_events + .iter() + .filter_map(|e| match e { + SwarmEvent::NewExternalAddrOfPeer { peer_id, address } => { + Some((*peer_id, address.clone())) + } + _ => None, + }) + .collect::>(); + + assert_eq!(reported_addrs.len(), 1, "To have one TCP address of remote"); + assert!(reported_addrs.contains(&(swarm2_peer_id, swarm2_tcp_listen_addr))); +} + #[async_std::test] async fn identify_push() { let _ = tracing_subscriber::fmt() From bb26949ff64aa58634aa87f217c7d6062a98683b Mon Sep 17 00:00:00 2001 From: Benno Zeeman Date: Fri, 19 Jul 2024 23:13:58 +0200 Subject: [PATCH 2/3] docs(identify): add PR link to CHANGELOG entry --- protocols/identify/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/protocols/identify/CHANGELOG.md b/protocols/identify/CHANGELOG.md index 4983dfcd31d..e843973393b 100644 --- a/protocols/identify/CHANGELOG.md +++ b/protocols/identify/CHANGELOG.md @@ -3,6 +3,7 @@ - Add `ConnectionId` in `Event`. See [PR 4981](https://github.com/libp2p/rust-libp2p/pull/4981). - Add `hide_listen_addrs` option to prevent leaking (local) listen addresses. + See [PR 5507](https://github.com/libp2p/rust-libp2p/pull/5507). ## 0.44.2 From 1eb27f229b8e5824c3d503df22fb2dd435b10aa3 Mon Sep 17 00:00:00 2001 From: Benno Zeeman Date: Wed, 28 Aug 2024 13:51:56 +0200 Subject: [PATCH 3/3] chore(identify): make change part of 0.45.1 --- Cargo.lock | 2 +- Cargo.toml | 2 +- protocols/identify/CHANGELOG.md | 7 +++++-- protocols/identify/Cargo.toml | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 41ed8883c52..02360c33ddb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2875,7 +2875,7 @@ dependencies = [ [[package]] name = "libp2p-identify" -version = "0.45.0" +version = "0.45.1" dependencies = [ "async-std", "asynchronous-codec", diff --git a/Cargo.toml b/Cargo.toml index c23bb8650f3..ed9977f45a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,7 +84,7 @@ libp2p-dcutr = { version = "0.12.0", path = "protocols/dcutr" } libp2p-dns = { version = "0.42.0", path = "transports/dns" } libp2p-floodsub = { version = "0.45.0", path = "protocols/floodsub" } libp2p-gossipsub = { version = "0.47.0", path = "protocols/gossipsub" } -libp2p-identify = { version = "0.45.0", path = "protocols/identify" } +libp2p-identify = { version = "0.45.1", path = "protocols/identify" } libp2p-identity = { version = "0.2.9" } libp2p-kad = { version = "0.47.0", path = "protocols/kad" } libp2p-mdns = { version = "0.46.0", path = "protocols/mdns" } diff --git a/protocols/identify/CHANGELOG.md b/protocols/identify/CHANGELOG.md index a02a4e3fb47..c5778ff92ee 100644 --- a/protocols/identify/CHANGELOG.md +++ b/protocols/identify/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.45.1 + +- Add `hide_listen_addrs` option to prevent leaking (local) listen addresses. + See [PR 5507](https://github.com/libp2p/rust-libp2p/pull/5507). + ## 0.45.0 - Address translation is moved here from `libp2p-core`. @@ -7,8 +12,6 @@ - Add `ConnectionId` in `Event`. See [PR 4981](https://github.com/libp2p/rust-libp2p/pull/4981). -- Add `hide_listen_addrs` option to prevent leaking (local) listen addresses. - See [PR 5507](https://github.com/libp2p/rust-libp2p/pull/5507). ## 0.44.2 diff --git a/protocols/identify/Cargo.toml b/protocols/identify/Cargo.toml index cdc5ce587de..c3fb585c99c 100644 --- a/protocols/identify/Cargo.toml +++ b/protocols/identify/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-identify" edition = "2021" rust-version = { workspace = true } description = "Nodes identification protocol for libp2p" -version = "0.45.0" +version = "0.45.1" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p"