From 56ac7ec5e1203fac519c21d57d5b547caf8b8fd2 Mon Sep 17 00:00:00 2001 From: Cyle Witruk Date: Mon, 18 Nov 2024 18:04:13 +0100 Subject: [PATCH] add config for disabling mdns --- docker/sbtc/signer/signer-config.toml | 9 +++++++++ signer/src/config/default.toml | 9 +++++++++ signer/src/config/mod.rs | 4 ++++ signer/src/network/libp2p/event_loop.rs | 9 +++++++++ 4 files changed, 31 insertions(+) diff --git a/docker/sbtc/signer/signer-config.toml b/docker/sbtc/signer/signer-config.toml index c34abdf1a..073ebf6ab 100644 --- a/docker/sbtc/signer/signer-config.toml +++ b/docker/sbtc/signer/signer-config.toml @@ -231,3 +231,12 @@ listen_on = [] # Required: false # Environment: SIGNER_SIGNER__P2P__PUBLIC_ENDPOINTS public_endpoints = [] + +# Enables/disables mDNS (multicast DNS) discovery. mDNS allows sBTC signers +# running on the same local network to discover each other without explicitly +# providing them as seed nodes. +# +# Default: false +# Required: false +# Environment: SIGNER_SIGNER__P2P__ENABLE_MDNS +enable_mdns = false \ No newline at end of file diff --git a/signer/src/config/default.toml b/signer/src/config/default.toml index 471770d80..c5ea4cc57 100644 --- a/signer/src/config/default.toml +++ b/signer/src/config/default.toml @@ -241,3 +241,12 @@ listen_on = ["tcp://0.0.0.0:4122", "quic-v1://0.0.0.0:4122"] # Required: false # Environment: SIGNER_SIGNER__P2P__PUBLIC_ENDPOINTS public_endpoints = [] + +# Enables/disables mDNS (multicast DNS) discovery. mDNS allows sBTC signers +# running on the same local network to discover each other without explicitly +# providing them as seed nodes. +# +# Default: false +# Required: false +# Environment: SIGNER_SIGNER__P2P__ENABLE_MDNS +enable_mdns = true \ No newline at end of file diff --git a/signer/src/config/mod.rs b/signer/src/config/mod.rs index e0e540f99..5d8f708f1 100644 --- a/signer/src/config/mod.rs +++ b/signer/src/config/mod.rs @@ -129,6 +129,10 @@ pub struct P2PNetworkConfig { /// public endpoint(s). #[serde(deserialize_with = "p2p_multiaddr_deserializer_vec")] pub public_endpoints: Vec, + /// Enable mDNS discovery for the P2P network. This is useful for local + /// testing and development. + #[serde(default)] + pub enable_mdns: bool, } impl Validatable for P2PNetworkConfig { diff --git a/signer/src/network/libp2p/event_loop.rs b/signer/src/network/libp2p/event_loop.rs index 6987557da..3c2444e90 100644 --- a/signer/src/network/libp2p/event_loop.rs +++ b/signer/src/network/libp2p/event_loop.rs @@ -304,6 +304,11 @@ fn handle_mdns_event(swarm: &mut Swarm, ctx: &impl Context, even // so this will never be raised for WAN peers which must otherwise // be discovered via seed nodes. Event::Discovered(peers) => { + // If we have disabled mDNS, we should not process this event. + if !ctx.config().signer.p2p.enable_mdns { + return; + } + for (peer_id, addr) in peers { if !ctx.state().current_signer_set().is_allowed_peer(&peer_id) { tracing::warn!(%peer_id, %addr, "Discovered peer via mDNS, however it is not a known signer; ignoring"); @@ -320,6 +325,10 @@ fn handle_mdns_event(swarm: &mut Swarm, ctx: &impl Context, even Event::Expired(peers) => { for (peer_id, addr) in peers { tracing::info!(%peer_id, %addr, "Expired peer via mDNS"); + swarm + .behaviour_mut() + .gossipsub + .remove_explicit_peer(&peer_id); } } }