diff --git a/README.md b/README.md index c25dc8f..e6d6d87 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,21 @@ Joining a room tells anyone present there the node's candidate addresses, its pu included, as a tracker's swarm would. `--mesh-bind` fixes its UDP port and `--mesh-max-peers` (default 16) caps its connections. +Behind a DNAT, a VM's say, STUN cannot tell the node its public address when the STUN server +is its own (that would take a UDP hairpin), and no gateway maps a port. Name the address with +`--mesh-public`, as `--bridge-public` does for the bridge: + +```sh +nfxd run --key node.key --store pkg/store --seed "$A" --relay wss://relay.example \ + --mesh-relay wss://general-relay.example --mesh-bind 0.0.0.0:7845 --mesh-public 203.0.113.7:7845 +``` + +Here `203.0.113.7:7845` stands for the public address and port forwarded to the host's UDP +port 7845; `--mesh-bind` must then be a fixed port. The node offers that address as a host +candidate and takes what arrives as sent to it, and offers no candidate address that is not +public, so its address behind the DNAT never reaches a peer. `--mesh-stun` stays optional +beside it. + ## Checks ```sh diff --git a/crates/Cargo.lock b/crates/Cargo.lock index f7dc13d..47a7f0d 100644 --- a/crates/Cargo.lock +++ b/crates/Cargo.lock @@ -4217,9 +4217,9 @@ dependencies = [ [[package]] name = "rustix" -version = "1.1.5" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "891efababe418670775f199f0d233d84843c227a0949a883ce15b37c78d6629d" +checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" dependencies = [ "bitflags 2.13.2", "errno", diff --git a/crates/desktop/Cargo.lock b/crates/desktop/Cargo.lock index 9d86921..b48a5b1 100644 --- a/crates/desktop/Cargo.lock +++ b/crates/desktop/Cargo.lock @@ -25,7 +25,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" dependencies = [ "cfg-if", - "cipher", + "cipher 0.4.4", "cpufeatures 0.2.17", ] @@ -37,7 +37,7 @@ checksum = "831010a0f742e1209b3bcea8fab6a8e149051ba6099432c8cb2cc117dec3ead1" dependencies = [ "aead", "aes", - "cipher", + "cipher 0.4.4", "ctr", "ghash", "subtle", @@ -620,7 +620,7 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26b52a9543ae338f279b96b0b9fed9c8093744685043739079ce85cd58f289a6" dependencies = [ - "cipher", + "cipher 0.4.4", ] [[package]] @@ -642,7 +642,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ae3c82e4355234767756212c570e29833699ab63e6ffd161887314cc5b43847" dependencies = [ "aead", - "cipher", + "cipher 0.4.4", "ctr", "subtle", ] @@ -693,7 +693,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3613f74bd2eac03dad61bd53dbe620703d4371614fe0bc3b9f04dd36fe4e818" dependencies = [ "cfg-if", - "cipher", + "cipher 0.4.4", "cpufeatures 0.2.17", ] @@ -704,6 +704,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" dependencies = [ "cfg-if", + "cipher 0.5.2", "cpufeatures 0.3.1", "rand_core 0.10.1", ] @@ -716,7 +717,7 @@ checksum = "10cd79432192d1c0f4e1a0fef9527696cc039165d729fb41b3f4f4f354c2dc35" dependencies = [ "aead", "chacha20 0.9.1", - "cipher", + "cipher 0.4.4", "poly1305", "zeroize", ] @@ -742,10 +743,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" dependencies = [ "crypto-common 0.1.7", - "inout", + "inout 0.1.4", "zeroize", ] +[[package]] +name = "cipher" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8cf2a2c93cd704877c0858356ed03480ff301ee950b43f1cbe4573b088bfa6c" +dependencies = [ + "block-buffer 0.12.1", + "crypto-common 0.2.2", + "inout 0.2.6", +] + [[package]] name = "cmake" version = "0.1.58" @@ -1029,7 +1041,7 @@ version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835" dependencies = [ - "cipher", + "cipher 0.4.4", ] [[package]] @@ -2793,6 +2805,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "inout" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a580c13f0ffc335ef42dd3f6b1a41b726051c5e1656d9a3aa6fddb3c5dfbbf9" +dependencies = [ + "hybrid-array", +] + [[package]] name = "inplace-vec-builder" version = "0.1.1" @@ -3918,6 +3939,7 @@ name = "nfx-node" version = "0.1.1" dependencies = [ "bytes", + "getrandom 0.4.3", "hex", "http-body-util", "hyper", @@ -3930,6 +3952,7 @@ dependencies = [ "nostr-database", "nostr-memory", "nostr-sdk", + "portmapper", "rustls-pki-types", "serde_json", "str0m", @@ -3957,6 +3980,7 @@ name = "nfx-proto" version = "0.1.0" dependencies = [ "base64 0.23.1", + "chacha20 0.10.2", "hex", "k256", "nfx-pay-wire", @@ -5368,9 +5392,9 @@ dependencies = [ [[package]] name = "rustix" -version = "1.1.5" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "891efababe418670775f199f0d233d84843c227a0949a883ce15b37c78d6629d" +checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" dependencies = [ "bitflags 2.13.2", "errno", diff --git a/crates/nfx-node/src/mesh/mod.rs b/crates/nfx-node/src/mesh/mod.rs index decee5c..51df3c6 100644 --- a/crates/nfx-node/src/mesh/mod.rs +++ b/crates/nfx-node/src/mesh/mod.rs @@ -22,6 +22,12 @@ //! [`GATHER_TIMEOUT`], and every 25 s after, which also keeps the NAT's mapping alive), and //! a port the gateway maps by UPnP, PCP or NAT-PMP when [`MeshConfig::portmapper`] is on //! and the gateway does. Every SDP sent carries every candidate (no trickle). +//! - **Behind a DNAT** ([`MeshConfig::public`]), whose public address STUN cannot report (a +//! STUN server of the node's own needs a hairpin) and no gateway maps: that address is one +//! more host candidate, the first, and every datagram the socket receives is taken as sent +//! to it, as the bridge does (`--bridge-public`). Then no candidate whose address is not +//! public ([`crate::node::is_public`]) is offered, as with iroh's external addresses: not +//! the socket's own address behind the DNAT, nor a private mapping. //! - **Serving** is the bridge's ([`crate::rtc_serve`]): only verified segments of held //! renditions, under the same per-peer caps. It never takes bytes from a peer. //! - **Discovery is by room alone** (§6): a page that plays a rendition joins its room and @@ -40,10 +46,10 @@ //! sent; every other datagram goes to the connection that accepts it, or is dropped. //! //! **Privacy.** Joining a room tells anyone who announces presence there this node's -//! candidate addresses (NFX-10 §6.7): its host address, which behind a NAT is a private one, -//! and its public mappings. Relays see the room keys (which the catalog maps back to videos), -//! the session key and who signals whom, never the SDP. The endpoint is off unless relays -//! are configured. +//! candidate addresses (NFX-10 §6.7): its host address, which behind a NAT is a private one +//! (unless [`MeshConfig::public`] is set), and its public mappings. Relays see the room keys +//! (which the catalog maps back to videos), the session key and who signals whom, never the +//! SDP. The endpoint is off unless relays are configured. pub mod book; pub mod sdp; @@ -72,6 +78,7 @@ use self::book::{ NEGOTIATIONS_PER_ROOM, OfferBackoff, PENDING_OFFERS_PER_ROOM, PresenceBook, SeenIds, SenderLimiter, }; +use crate::node::is_public; use crate::nostr::{from_nostr, to_nostr}; use crate::rtc_serve::{Conn, IDLE_TIMEOUT, Swarm, Uploader, announceable, queue, received}; use crate::store::ContentStore; @@ -114,8 +121,9 @@ const STUN_TRIES: u32 = 4; const SEND_TIMEOUT: Duration = Duration::from_secs(5); /// On shutdown, how long the `disconnect`s get. const SHUTDOWN_WAIT: Duration = Duration::from_secs(2); -/// Local candidates offered, at most (host, and one per distinct mapping). -const MAX_LOCAL_CANDIDATES: usize = 6; +/// Local candidates offered, at most (the public address, host, and one per distinct +/// mapping). +const MAX_LOCAL_CANDIDATES: usize = 7; /// How often a room whose rendition is not all in the store is checked again, so peers hear /// of segments that arrived (a pulled or fetched video fills in), as the bridge does. const REFRESH_EVERY: Duration = Duration::from_secs(10); @@ -141,11 +149,18 @@ pub struct MeshConfig { /// Ask the gateway for a port mapping (UPnP, PCP, NAT-PMP), offered as one more /// candidate when it gives one. IPv4 only. pub portmapper: bool, + /// The public address of a DNAT that forwards to `bind`'s port, when STUN cannot report + /// it: offered as the first host candidate, and every datagram the socket receives is + /// taken as sent to it. With it, no other candidate whose address is not public + /// ([`crate::node::is_public`]) is offered. An address peers can dial, of `bind`'s family; + /// whether it is public is the caller's to check (`nfxd` refuses one that is not). + /// `None`: none. + pub public: Option, } impl MeshConfig { /// Signal through `relays`, on an ephemeral IPv4 port, with no STUN server, [`MAX_PEERS`] - /// connections and no portmapper. + /// connections, no portmapper and no public address. #[must_use] pub fn new(relays: Vec) -> Self { Self { @@ -154,6 +169,7 @@ impl MeshConfig { stun: Vec::new(), max_peers: MAX_PEERS, portmapper: false, + public: None, } } } @@ -181,7 +197,8 @@ pub fn relay_url_problem(url: &str) -> Option { pub struct MeshStatus { /// Whether candidate gathering has ended (rooms are joined only after). pub gathered: bool, - /// The candidates every SDP carries, as `host ` and `srflx `. + /// The candidates every SDP carries, as `host ` and `srflx `; the + /// public address ([`MeshConfig::public`]) first, when there is one. pub candidates: Vec, /// Rooms joined. pub rooms: usize, @@ -216,6 +233,7 @@ pub struct Mesh { task: JoinHandle<()>, client: ns::Client, local: SocketAddr, + public: Option, session: String, status: Arc>, } @@ -305,14 +323,26 @@ impl Mesh { if cfg.bind.ip().is_multicast() { return bad(format!("{}: not an address to bind", cfg.bind)); } + // Checked before anything binds. str0m refuses a host candidate on an IPv4 + // link-local address too, which no DNAT forwards from. + if let Some(public) = cfg.public + && (!crate::bridge::dialable(public) + || public.is_ipv4() != cfg.bind.is_ipv4() + || Candidate::host(public, "udp").is_err()) + { + return bad(format!( + "the mesh's public address {public}: not one peers can dial, of the bind's family" + )); + } let socket = Arc::new( UdpSocket::bind(cfg.bind) .await .map_err(NodeError::transport)?, ); let local = socket.local_addr().map_err(NodeError::transport)?; - // The host candidate: the bound address, or for a wildcard the address the route out - // leaves from. Every datagram is taken as arriving there. + // The socket's own address, a host candidate (unless a public address is set and it is + // not public): the bound address, or for a wildcard the address the route out leaves + // from. Every datagram is taken as arriving there, or at the public address. let base = if local.ip().is_unspecified() { let probe = cfg.stun.first().copied().unwrap_or(if local.is_ipv4() { SocketAddr::from(([192, 0, 2, 1], 9)) @@ -328,7 +358,7 @@ impl Mesh { } else { local }; - let host = Candidate::host(base, "udp").map_err(NodeError::transport)?; + Candidate::host(base, "udp").map_err(NodeError::transport)?; let session = new_session()?; let limits = ns::RelayLimits { messages: ns::RelayMessageLimits { @@ -366,7 +396,7 @@ impl Mesh { client: client.clone(), uploader: Uploader::new(socket.clone(), store, now), base, - host, + public: cfg.public, stun: Stun::new(cfg.stun.clone(), now), portmapped: None, gathered: false, @@ -393,6 +423,7 @@ impl Mesh { task, client, local, + public: cfg.public, session: session_key, status, }) @@ -404,6 +435,12 @@ impl Mesh { self.local } + /// The public address in front of it ([`MeshConfig::public`]), when there is one. + #[must_use] + pub fn public_addr(&self) -> Option { + self.public + } + /// This run's session key (64 lowercase hex): what relays and peers see, never the /// node's identity. #[must_use] @@ -534,9 +571,11 @@ struct State { session: Session, client: ns::Client, uploader: Uploader, - /// The host candidate; every datagram is taken as arriving here. + /// The socket's own address: a host candidate unless `public` is set and it is not + /// public; what a peer's candidates are judged from ([`sdp::filter_candidates`]). base: SocketAddr, - host: Candidate, + /// The public address of a DNAT in front of the socket ([`MeshConfig::public`]). + public: Option, stun: Stun, portmapped: Option, gathered: bool, @@ -641,21 +680,50 @@ impl State { wake } - /// The local candidates every new connection gets: host, then each distinct mapping. + /// Where every datagram the socket receives is taken as sent: the public address of the + /// DNAT in front of it, or its own. + fn arrival(&self) -> SocketAddr { + self.public.unwrap_or(self.base) + } + + /// The local candidates every new connection gets, each address once: the public address + /// (host) when there is one, the socket's own (host), then each distinct mapping a STUN + /// server or the gateway reported (server reflexive). With a public address, none but it + /// whose address is not public: neither the socket's address behind the DNAT nor a + /// private mapping. A server-reflexive candidate's base is [`State::arrival`] (str0m + /// writes no `raddr` of ours, but hashes the base into the foundation), so nothing in + /// the SDP is derived from an address left out. fn candidates(&self) -> Vec { - let mut out = vec![self.host.clone()]; - let mut seen = vec![self.base]; + let only_public = self.public.is_some(); + let hosts = self + .public + .into_iter() + .chain([self.base]) + .map(|a| (a, true)); let mapped = self .stun .servers .iter() - .filter_map(|s| self.stun.mapped.get(s)); - for addr in mapped.chain(self.portmapped.as_ref()) { - if out.len() >= MAX_LOCAL_CANDIDATES || seen.contains(addr) { + .filter_map(|s| self.stun.mapped.get(s)) + .chain(self.portmapped.as_ref()) + .map(|a| (*a, false)); + let mut out = Vec::new(); + let mut seen = Vec::new(); + for (i, (addr, host)) in hosts.chain(mapped).enumerate() { + let given = i == 0 && only_public; + if out.len() >= MAX_LOCAL_CANDIDATES + || seen.contains(&addr) + || (only_public && !given && !is_public(&addr)) + { continue; } - if let Ok(c) = Candidate::server_reflexive(*addr, self.base, "udp") { - seen.push(*addr); + let c = if host { + Candidate::host(addr, "udp") + } else { + Candidate::server_reflexive(addr, self.arrival(), "udp") + }; + if let Ok(c) = c { + seen.push(addr); out.push(c); } } @@ -932,7 +1000,7 @@ impl State { self.publish_status(); return; } - let Some(input) = received(bytes, source, self.base, Instant::now()) else { + let Some(input) = received(bytes, source, self.arrival(), Instant::now()) else { return; }; if let Some(l) = self diff --git a/crates/nfx-node/src/mesh/tests.rs b/crates/nfx-node/src/mesh/tests.rs index 6dcf78f..5b000d0 100644 --- a/crates/nfx-node/src/mesh/tests.rs +++ b/crates/nfx-node/src/mesh/tests.rs @@ -39,7 +39,7 @@ async fn state(max_peers: usize) -> State { client: nostr_sdk::prelude::Client::default(), uploader: Uploader::new(socket, store, now), base, - host: Candidate::host(base, "udp").unwrap(), + public: None, stun: Stun::new(vec![], now), portmapped: None, gathered: true, @@ -316,16 +316,35 @@ async fn gathering_offers_the_mapping_a_stun_server_reports() { /// One side of the simulated punch: an ICE agent behind a NAT that maps its one socket to one /// public address for every destination (endpoint-independent mapping) and lets in only what /// comes from an address the side has sent to (address- and port-dependent filtering): the -/// NAT test's "consistent" router. +/// NAT test's "consistent" router. Or, `forwarded`, behind a DNAT that forwards its public +/// address to the socket and lets in what comes from anywhere. struct Natted { rtc: Rtc, - local: SocketAddr, public: SocketAddr, + /// Where the side takes every datagram as sent: its socket's address, or for a node told + /// its public address, that address ([`State::arrival`]). Its ICE agent sends from it. + arrives_as: SocketAddr, + /// A DNAT's forwarded port: no filtering. + forwarded: bool, /// Destinations this side has sent to, as its NAT remembers them. sent_to: Vec, open: bool, } +impl Natted { + /// A side whose socket is at `local`, behind a NAT that maps it to `public`. + fn new(rtc: Rtc, local: SocketAddr, public: SocketAddr) -> Self { + Self { + rtc, + public, + arrives_as: local, + forwarded: false, + sent_to: vec![], + open: false, + } + } +} + /// Run both sides until each sees the data channel open (true) or 30 simulated seconds pass. /// A packet to a private address never crosses (the NATs route nothing between their LANs). fn punch(a: &mut Natted, b: &mut Natted) -> bool { @@ -344,13 +363,17 @@ fn punch(a: &mut Natted, b: &mut Natted) -> bool { while let Ok(out) = src.rtc.poll_output() { match out { Output::Transmit(t) => { - assert_eq!(t.source, src.local, "every packet leaves the one socket"); + assert_eq!( + t.source, src.arrives_as, + "every packet leaves the one socket" + ); if t.destination != dst.public { continue; } src.sent_to.push(t.destination); - // The other NAT lets it in only if its side has sent to src's mapping. - if !dst.sent_to.contains(&src.public) { + // The other NAT lets it in only if its side has sent to src's mapping + // (a DNAT's forwarded port lets in everything). + if !dst.forwarded && !dst.sent_to.contains(&src.public) { continue; } let input = Input::Receive( @@ -358,7 +381,7 @@ fn punch(a: &mut Natted, b: &mut Natted) -> bool { Receive { proto: Protocol::Udp, source: src.public, - destination: dst.local, + destination: dst.arrives_as, contents: (&t.contents[..]).try_into().unwrap(), }, ); @@ -394,7 +417,6 @@ async fn full_ice_punches_through_two_consistent_nats_where_ice_lite_cannot() { let (web_local, web_public) = (sa("192.168.20.2:6000"), sa("203.0.113.12:6000")); let mut s = state(16).await; s.base = node_local; - s.host = Candidate::host(node_local, "udp").unwrap(); s.stun.servers = vec![sa("203.0.113.1:3478")]; s.stun.mapped.insert(sa("203.0.113.1:3478"), node_public); let browser_rtc = || { @@ -426,20 +448,8 @@ async fn full_ice_punches_through_two_consistent_nats_where_ice_lite_cannot() { .unwrap(), ) .unwrap(); - let mut a = Natted { - rtc: node, - local: node_local, - public: node_public, - sent_to: vec![], - open: false, - }; - let mut b = Natted { - rtc: web, - local: web_local, - public: web_public, - sent_to: vec![], - open: false, - }; + let mut a = Natted::new(node, node_local, node_public); + let mut b = Natted::new(web, web_local, web_public); assert!(punch(&mut a, &mut b), "both ends check, so both NATs open"); // Control: an ICE-lite agent (the bridge's), told as the NAT's public address the way @@ -455,19 +465,187 @@ async fn full_ice_punches_through_two_consistent_nats_where_ice_lite_cannot() { .accept_offer(SdpOffer::from_sdp_string(&offer.to_sdp_string()).unwrap()) .unwrap(); web.sdp_api().accept_answer(pending, answer).unwrap(); - let mut a = Natted { - rtc: lite, - local: node_public, - public: node_public, - sent_to: vec![], - open: false, + let mut a = Natted::new(lite, node_public, node_public); + let mut b = Natted::new(web, web_local, web_public); + assert!(!punch(&mut a, &mut b)); +} + +/// What a candidate set comes to, as the status line shows it. +fn kinds(s: &State) -> Vec { + s.candidates() + .iter() + .map(|c| format!("{} {}", c.kind(), c.addr())) + .collect() +} + +/// The `a=candidate` lines of an SDP. +fn candidate_lines(sdp: &str) -> Vec<&str> { + sdp.lines() + .filter(|l| l.starts_with("a=candidate:")) + .collect() +} + +#[tokio::test] +async fn with_a_public_address_it_is_offered_first_and_no_address_that_is_not_public() { + let sa = |s: &str| -> SocketAddr { s.parse().unwrap() }; + // Bound behind a DNAT on a private address. The public address is offered as given + // (nfxd refuses one that is not public; a documentation address stands in for it here). + let mut s = state(16).await; + s.base = sa("10.0.0.28:7845"); + assert_eq!(kinds(&s), ["host 10.0.0.28:7845"], "without one, as before"); + s.public = Some(sa("203.0.113.7:7845")); + assert_eq!(s.arrival(), sa("203.0.113.7:7845")); + assert_eq!( + kinds(&s), + ["host 203.0.113.7:7845"], + "the bound address is left out" + ); + + // STUN beside it: a mapping equal to the public address is offered once, a private one + // (a STUN server inside the DNAT) not at all, and another public one is kept. So is a + // public one the gateway maps. + let servers = [ + sa("198.51.100.1:3478"), + sa("198.51.100.2:3478"), + sa("198.51.100.3:3478"), + ]; + s.stun.servers = servers.to_vec(); + s.stun.mapped.insert(servers[0], sa("203.0.113.7:7845")); + s.stun.mapped.insert(servers[1], sa("192.168.1.2:40000")); + s.stun.mapped.insert(servers[2], sa("8.8.8.8:41000")); + s.portmapped = Some(sa("198.18.0.9:7845")); + assert_eq!( + kinds(&s), + ["host 203.0.113.7:7845", "srflx 8.8.8.8:41000"], + "a benchmarking-range mapping is not public either" + ); + s.portmapped = Some(sa("8.8.4.4:7845")); + assert_eq!( + kinds(&s), + [ + "host 203.0.113.7:7845", + "srflx 8.8.8.8:41000", + "srflx 8.8.4.4:7845" + ] + ); + + // Every SDP the node sends, offer or answer, names those and nothing derived from the + // addresses left out: str0m writes no raddr of ours, and its foundations hash the base, + // which is the public address. + let mut rtc = s.new_rtc(Instant::now()); + let mut api = rtc.sdp_api(); + api.add_channel("nfx".into()); + let (offer, _) = api.apply().unwrap(); + let offer = offer.to_sdp_string(); + let (_, _, browser) = browser_offer(); + let answer = s + .new_rtc(Instant::now()) + .sdp_api() + .accept_offer(SdpOffer::from_sdp_string(&browser).unwrap()) + .unwrap() + .to_sdp_string(); + for sdp in [&offer, &answer] { + let lines = candidate_lines(sdp); + assert_eq!(lines.len(), 3, "{sdp}"); + assert!(lines[0].contains("203.0.113.7 7845 typ host"), "{sdp}"); + assert!( + lines[1].contains("8.8.8.8 41000 typ srflx raddr 0.0.0.0"), + "{sdp}" + ); + assert!( + lines[2].contains("8.8.4.4 7845 typ srflx raddr 0.0.0.0"), + "{sdp}" + ); + for private in ["10.0.0.28", "192.168.1.2", "198.18.0.9"] { + assert!(!sdp.contains(private), "{private} in {sdp}"); + } + } + // The srflx candidates' base is the public address, as every datagram's arrival is + // (Candidate's equality takes in the base). + let srflx = |base: &str| Candidate::server_reflexive(sa("8.8.8.8:41000"), sa(base), "udp"); + assert_eq!(s.candidates()[1], srflx("203.0.113.7:7845").unwrap()); + assert_ne!(s.candidates()[1], srflx("10.0.0.28:7845").unwrap()); + + // A host on a public address keeps it, beside the public address (as iroh's external + // addresses keep a node's public direct ones). + s.stun.servers.clear(); + s.portmapped = None; + s.base = sa("8.8.4.4:7845"); + assert_eq!(kinds(&s), ["host 203.0.113.7:7845", "host 8.8.4.4:7845"]); + s.base = sa("203.0.113.7:7845"); + assert_eq!( + kinds(&s), + ["host 203.0.113.7:7845"], + "the same address once" + ); +} + +#[tokio::test] +async fn behind_a_dnat_full_ice_connects_through_the_public_address_it_is_told() { + use str0m::change::SdpAnswer; + let sa = |s: &str| -> SocketAddr { s.parse().unwrap() }; + // This node in a guest behind a DNAT: no STUN mapping (its STUN server would be itself, + // through a hairpin) and no gateway to map a port. A browser behind a consistent NAT. + let (node_local, node_public) = (sa("10.0.0.28:7845"), sa("203.0.113.11:7845")); + let (web_local, web_public) = (sa("192.168.20.2:6000"), sa("203.0.113.12:6000")); + let browser_rtc = || { + let mut rtc = Rtc::builder().build(Instant::now()); + rtc.add_local_candidate(Candidate::host(web_local, "udp").unwrap()); + rtc.add_local_candidate(Candidate::server_reflexive(web_public, web_local, "udp").unwrap()); + rtc }; - let mut b = Natted { - rtc: web, - local: web_local, - public: web_public, - sent_to: vec![], - open: false, + // Negotiate as the node offers, both sides filtering what they check. + let negotiate = |s: &State| { + let mut node = s.new_rtc(Instant::now()); + let mut api = node.sdp_api(); + api.add_channel("nfx".into()); + let (offer, pending) = api.apply().unwrap(); + let offer = offer.to_sdp_string(); + let mut web = browser_rtc(); + let answer = web + .sdp_api() + .accept_offer( + SdpOffer::from_sdp_string(&super::sdp::filter_candidates(&offer, web_local)) + .unwrap(), + ) + .unwrap() + .to_sdp_string(); + node.sdp_api() + .accept_answer( + pending, + SdpAnswer::from_sdp_string(&super::sdp::filter_candidates(&answer, s.base)) + .unwrap(), + ) + .unwrap(); + (node, web, offer) }; + let dnat = |rtc: Rtc, s: &State| Natted { + arrives_as: s.arrival(), + forwarded: true, + ..Natted::new(rtc, node_local, node_public) + }; + + let mut s = state(16).await; + s.base = node_local; + s.public = Some(node_public); + let (node, web, offer) = negotiate(&s); + assert_eq!(candidate_lines(&offer).len(), 1, "{offer}"); + assert!(offer.contains("203.0.113.11 7845 typ host"), "{offer}"); + assert!(!offer.contains("10.0.0.28"), "{offer}"); + let mut a = dnat(node, &s); + let mut b = Natted::new(web, web_local, web_public); + assert!( + punch(&mut a, &mut b), + "the browser checks the public address, the DNAT forwards it, and the node answers" + ); + + // Control: the same node not told its public address offers only the bound one, which + // the browser cannot reach; the node's own checks reach the browser's NAT from an address + // its side never sent to, so they are dropped. No connection. + s.public = None; + let (node, web, offer) = negotiate(&s); + assert!(offer.contains("10.0.0.28 7845 typ host"), "{offer}"); + let mut a = dnat(node, &s); + let mut b = Natted::new(web, web_local, web_public); assert!(!punch(&mut a, &mut b)); } diff --git a/crates/nfx-node/tests/mesh.rs b/crates/nfx-node/tests/mesh.rs index 5cd41be..e171c42 100644 --- a/crates/nfx-node/tests/mesh.rs +++ b/crates/nfx-node/tests/mesh.rs @@ -3,8 +3,10 @@ //! endpoint serving the test-vector video, and a browser-like peer built from str0m (full //! ICE), nfx-proto's signaling and nostr-sdk. The peer finds the endpoint by its room alone, //! negotiates through the relay (either side offering, by the glare rule), opens the data -//! channel, asks for a segment with p2p-media-loader's protocol and checks its sha256. Then -//! two endpoints in one room, which find and connect to each other the same way. +//! channel, asks for a segment with p2p-media-loader's protocol and checks its sha256. The +//! same behind a DNAT: the endpoint bound on loopback and told a public address, which the +//! peer alone reaches, through a DNAT in the test. Then two endpoints in one room, which find +//! and connect to each other the same way. #![allow(clippy::unwrap_used, clippy::expect_used)] @@ -102,6 +104,20 @@ struct Browser { notes: Pin + Send>>, socket: UdpSocket, addr: SocketAddr, + /// The DNAT in front of the endpoint, when there is one. + dnat: Option, +} + +/// A DNAT in front of the endpoint, as the browser's datagrams meet it. What the browser +/// sends to `public` reaches the endpoint's socket at `bind` with the browser's own source +/// address (no SNAT), and what the endpoint sends the browser arrives as from `public`, as +/// conntrack rewrites it. Nothing else crosses: the endpoint's bound address stands for a +/// private one the browser cannot reach, and the browser's own NAT lets in only what comes +/// from an address it has sent to (`opened`). +struct Dnat { + public: SocketAddr, + bind: SocketAddr, + opened: bool, } impl Browser { @@ -124,6 +140,7 @@ impl Browser { notes, socket, addr, + dnat: None, } } @@ -166,7 +183,8 @@ impl Browser { } /// Negotiate with `mesh` through the relay, whichever side the glare rule makes offer. - async fn negotiate(&mut self, mesh: &str) -> Rtc { + /// Returns the connection and the endpoint's SDP (its answer or offer). + async fn negotiate(&mut self, mesh: &str) -> (Rtc, String) { if signal::offers_to(self.session.pubkey(), mesh) { let mut rtc = self.rtc(); let mut api = rtc.sdp_api(); @@ -202,7 +220,7 @@ impl Browser { rtc.sdp_api() .accept_answer(pending, SdpAnswer::from_sdp_string(&sdp).unwrap()) .unwrap(); - return rtc; + return (rtc, sdp); } } } else { @@ -228,15 +246,37 @@ impl Browser { ) .unwrap(); self.send(&ev).await; - return rtc; + return (rtc, sdp); } } } } + /// Where a datagram for `to` goes: there, or through the DNAT; `None` where it cannot. + fn route_out(&mut self, to: SocketAddr) -> Option { + match &mut self.dnat { + None => Some(to), + Some(d) if to == d.public => { + d.opened = true; + Some(d.bind) + } + Some(_) => None, + } + } + + /// Where a datagram from `from` seems to come from: there, or through the DNAT; `None` + /// when it would not get in. + fn route_in(&self, from: SocketAddr) -> Option { + match &self.dnat { + None => Some(from), + Some(d) if from == d.bind && d.opened => Some(d.public), + Some(_) => None, + } + } + /// Drive `rtc` over the socket until the data channel is open, then ask for segment 0 /// and return its bytes once `Completed` arrives. - async fn fetch_segment_0(&self, rtc: &mut Rtc) -> Vec { + async fn fetch_segment_0(&mut self, rtc: &mut Rtc) -> Vec { let deadline = Instant::now() + WAIT; let mut channel: Option = None; let mut asked = false; @@ -250,10 +290,9 @@ impl Browser { match rtc.poll_output().unwrap() { Output::Timeout(t) => break t, Output::Transmit(t) => { - self.socket - .send_to(&t.contents, t.destination) - .await - .unwrap(); + if let Some(to) = self.route_out(t.destination) { + self.socket.send_to(&t.contents, to).await.unwrap(); + } } Output::Event(RtcEvent::ChannelOpen(id, _)) => channel = Some(id), Output::Event(RtcEvent::ChannelData(d)) => { @@ -290,8 +329,13 @@ impl Browser { let wait = wake .saturating_duration_since(Instant::now()) .min(Duration::from_millis(100)); - match tokio::time::timeout(wait, self.socket.recv_from(&mut buf)).await { - Ok(Ok((n, source))) => { + let got = tokio::time::timeout(wait, self.socket.recv_from(&mut buf)).await; + match got + .ok() + .and_then(Result::ok) + .and_then(|(n, from)| self.route_in(from).map(|source| (n, source))) + { + Some((n, source)) => { let input = Input::Receive( Instant::now(), Receive { @@ -303,20 +347,35 @@ impl Browser { ); rtc.handle_input(input).unwrap(); } - _ => rtc.handle_input(Input::Timeout(Instant::now())).unwrap(), + None => rtc.handle_input(Input::Timeout(Instant::now())).unwrap(), } } } } /// A browser offers (its key is the lower) or answers (the higher), and either way fetches a -/// verified segment from the endpoint. -async fn a_browser_fetches_a_segment(browser_offers: bool) { +/// verified segment from the endpoint. With `public`, the endpoint is told that address and +/// the browser reaches it only through a DNAT that maps it onto the endpoint's socket. +async fn a_browser_fetches_a_segment(browser_offers: bool, public: Option) { let (_relay, url) = relay().await; - let d = dir(if browser_offers { "offers" } else { "answers" }); + let d = dir(match (browser_offers, public.is_some()) { + (true, false) => "offers", + (false, false) => "answers", + (true, true) => "dnat-offers", + (false, true) => "dnat-answers", + }); let (store, v) = common::vector_store(&d); let store = Arc::new(store); - let mesh = Mesh::start(store.clone(), config(&url)).await.unwrap(); + let mesh = Mesh::start( + store.clone(), + MeshConfig { + public, + ..config(&url) + }, + ) + .await + .unwrap(); + assert_eq!(mesh.public_addr(), public); let ids = mesh.serve(&v.list, &*store, None).await.unwrap(); assert_eq!(ids, vec!["720p".to_owned()]); until(&mesh, "the room is joined", |s| s.gathered && s.rooms == 1).await; @@ -330,12 +389,36 @@ async fn a_browser_fetches_a_segment(browser_offers: bool) { } }; let mut browser = Browser::join(&url, room, session).await; - let mut rtc = browser.negotiate(mesh.session_key()).await; + browser.dnat = public.map(|public| Dnat { + public, + bind: mesh.local_addr(), + opened: false, + }); + let (mut rtc, sdp) = browser.negotiate(mesh.session_key()).await; + let candidates: Vec<&str> = sdp + .lines() + .filter(|l| l.starts_with("a=candidate:")) + .collect(); + let bound = mesh.local_addr(); + let told = public.unwrap_or(bound); + assert_eq!(candidates.len(), 1, "{sdp}"); + assert!( + candidates[0].contains(&format!("{} {} typ host", told.ip(), told.port())), + "{sdp}" + ); + if public.is_some() { + // The bound address never reaches the peer. + assert!(!sdp.contains(&bound.ip().to_string()), "{sdp}"); + } let bytes = browser.fetch_segment_0(&mut rtc).await; let want = first_segment(&v.list, &*store, "720p"); assert_eq!(hex::encode(nfx_proto::sha256(&bytes)), want); let s = until(&mesh, "served", |s| s.served >= 1 && s.open >= 1).await; assert_eq!(s.failed, 0); + assert_eq!(s.candidates, [format!("host {told}")]); + if let Some(d) = &browser.dnat { + assert!(d.opened, "the browser sent to the public address"); + } if browser_offers { assert_eq!((s.offers, s.answers), (0, 1)); } else { @@ -354,12 +437,26 @@ fn rand_bytes() -> [u8; N] { #[tokio::test(flavor = "multi_thread")] async fn a_browser_whose_key_is_lower_offers_and_fetches_a_segment() { - a_browser_fetches_a_segment(true).await; + a_browser_fetches_a_segment(true, None).await; } #[tokio::test(flavor = "multi_thread")] async fn a_browser_whose_key_is_higher_answers_and_fetches_a_segment() { - a_browser_fetches_a_segment(false).await; + a_browser_fetches_a_segment(false, None).await; +} + +/// The public address of the test's DNAT (a documentation address: nothing is ever sent to +/// it, the DNAT maps it onto the endpoint's socket). +const PUBLIC: &str = "203.0.113.7:7845"; + +#[tokio::test(flavor = "multi_thread")] +async fn behind_a_dnat_a_browser_that_offers_reaches_the_endpoint_through_its_public_address() { + a_browser_fetches_a_segment(true, Some(PUBLIC.parse().unwrap())).await; +} + +#[tokio::test(flavor = "multi_thread")] +async fn behind_a_dnat_a_browser_that_answers_reaches_the_endpoint_through_its_public_address() { + a_browser_fetches_a_segment(false, Some(PUBLIC.parse().unwrap())).await; } #[tokio::test(flavor = "multi_thread")] @@ -470,7 +567,27 @@ async fn a_stun_mapping_is_offered_and_settings_that_cannot_work_are_refused() { }, "connection cap", ), - ] { + ] + .into_iter() + .chain( + [ + "[2001:db8::7]:7845", + "203.0.113.7:0", + "0.0.0.0:7845", + "224.0.0.1:7845", + "255.255.255.255:7845", + "169.254.1.1:7845", + ] + .map(|public| { + ( + MeshConfig { + public: Some(public.parse().unwrap()), + ..config(&url) + }, + "public address", + ) + }), + ) { let err = Mesh::start(store.clone(), cfg.clone()) .await .err() diff --git a/crates/nfxd/src/daemon.rs b/crates/nfxd/src/daemon.rs index a56deca..d6717ad 100644 --- a/crates/nfxd/src/daemon.rs +++ b/crates/nfxd/src/daemon.rs @@ -24,7 +24,8 @@ //! held. //! - **mesh** ([`Config::mesh_relays`]): browsers meet the node in the rooms of the held //! renditions, signaling over Nostr (NFX-10 §6), and fetch verified segments from it over -//! WebRTC with full ICE, through NATs ([`nfx_node::mesh`]). Discovery is by room alone, so +//! WebRTC with full ICE, through NATs ([`nfx_node::mesh`]), or behind a DNAT through the +//! public address it is told ([`Config::mesh_public`]). Discovery is by room alone, so //! beacons carry nothing for it. use std::collections::BTreeMap; @@ -163,6 +164,13 @@ pub struct Config { /// Connections it holds at once (`--mesh-max-peers`, 1 to 1024; each may queue 16 MiB). /// Default: `nfx_node::mesh::MAX_PEERS` (16). Needs `mesh_relays`. pub mesh_max_peers: Option, + /// The public address of a DNAT that forwards to `mesh_bind`'s port (`--mesh-public`), + /// for a node whose public address STUN cannot report: offered as a host candidate, what + /// arrives is taken as sent to it, and no candidate address that is not public is offered + /// (`nfx_node::mesh::MeshConfig::public`). A public unicast address + /// (`nfx_node::node::is_public`) of `mesh_bind`'s family with a port. Needs `mesh_relays` + /// and a `mesh_bind` with a fixed port, the one the DNAT forwards to. + pub mesh_public: Option, /// Namespaces the embedded relay serves; by default those of the videos above. pub namespaces: Vec, /// When non-empty, the embedded relay admits only these creators (x-only pubkey, 64 @@ -233,6 +241,8 @@ pub struct Daemon { pub stun_public: Option, /// The mesh endpoint's UDP address. pub mesh_addr: Option, + /// The public address the mesh endpoint offers in front of it (`mesh_public`). + pub mesh_public: Option, /// The mesh endpoint's session key for this run (what relays and peers see). pub mesh_session: Option, pub origin_addr: Option, @@ -987,6 +997,7 @@ impl Daemon { // As iroh's: a node that may ask the gateway for a port asks for this one too. portmapper: !cfg.no_portmapper, bind: cfg.mesh_bind.unwrap_or(MeshConfig::new(vec![]).bind), + public: cfg.mesh_public, ..MeshConfig::new(cfg.mesh_relays.clone()) }; Some(Arc::new( @@ -996,6 +1007,7 @@ impl Daemon { )) }; let mesh_addr = mesh.as_ref().map(|m| m.local_addr()); + let mesh_public = mesh.as_ref().and_then(|m| m.public_addr()); let mesh_session = mesh.as_ref().map(|m| m.session_key().to_owned()); let (origin, origin_addr) = match cfg.origin { @@ -1114,6 +1126,7 @@ impl Daemon { stun_addr, stun_public, mesh_addr, + mesh_public, mesh_session, origin_addr, state, @@ -1326,6 +1339,7 @@ fn check_mesh(cfg: &Config) -> Result<()> { (cfg.mesh_bind.is_some(), "--mesh-bind"), (!cfg.mesh_stun.is_empty(), "--mesh-stun"), (cfg.mesh_max_peers.is_some(), "--mesh-max-peers"), + (cfg.mesh_public.is_some(), "--mesh-public"), ] { if set { return Err(Error::Config(format!("{flag} needs --mesh-relay"))); @@ -1364,6 +1378,31 @@ fn check_mesh(cfg: &Config) -> Result<()> { "--mesh-max-peers {n}: not 1 to {MESH_PEERS_LIMIT}" ))); } + if let Some(public) = cfg.mesh_public { + // A DNAT forwards to a port it knows: an ephemeral one changes every run, and the + // address offered would then lead nowhere. + let Some(bind) = cfg.mesh_bind else { + return Err(Error::Config( + "--mesh-public needs --mesh-bind, the address and port the DNAT forwards to".into(), + )); + }; + if bind.port() == 0 { + return Err(Error::Config(format!( + "--mesh-bind {bind}: port 0 is not a fixed port a DNAT can forward to" + ))); + } + // Every peer is told it: one that is not public would only tell them about the host. + if !nfx_node::node::is_public(&public) { + return Err(Error::Config(format!( + "--mesh-public {public}: not a public unicast address with a port" + ))); + } + if public.is_ipv4() != bind.is_ipv4() { + return Err(Error::Config(format!( + "--mesh-public {public}: not of --mesh-bind's family" + ))); + } + } Ok(()) } diff --git a/crates/nfxd/src/main.rs b/crates/nfxd/src/main.rs index 27b0c91..91fdf10 100644 --- a/crates/nfxd/src/main.rs +++ b/crates/nfxd/src/main.rs @@ -12,7 +12,7 @@ //! [--https-url ] [--embed-relay ] //! [--embed-tracker [--bridge [--bridge-public ] [--bridge-max-peers ] [--tracker-url ]]] [--namespace ]… //! [--stun [--stun-public ]] -//! [--mesh-relay … [--mesh-bind ] [--mesh-stun ]… [--mesh-max-peers ]] +//! [--mesh-relay … [--mesh-bind [--mesh-public ]] [--mesh-stun ]… [--mesh-max-peers ]] //! [--allow-creator ]… [--gossip [--gossip-peer @]…] //! [--rating-root --general-relay … [--rating-trusted ]…] //! ``` @@ -103,6 +103,15 @@ //! tells anyone who announces presence there this node's candidate addresses, its host //! address and public mappings included, as a tracker's swarm would; relays see the room keys //! (which the catalog maps back to videos) and who signals whom, never the SDP. +//! +//! Behind a DNAT, where STUN cannot report the public address (the STUN server is the node's +//! own, which a UDP hairpin would need) and no gateway maps a port, `--mesh-public ` +//! names the DNAT's public address and port, forwarded to `--mesh-bind`'s port, which must +//! then be fixed. It is offered as a host candidate and what arrives is taken as sent to it, +//! as with `--bridge-public`; then no candidate address that is not public is offered, so the +//! host's address behind the DNAT never reaches a peer. It must be a public unicast address of +//! `--mesh-bind`'s family. `--mesh-stun` stays optional beside it: a mapping that equals it is +//! offered once, one that is not public not at all. use std::path::{Path, PathBuf}; use std::process::ExitCode; @@ -121,13 +130,15 @@ const USAGE: &str = "usage: nfxd key show nfxd publish --key --relay ... --package --title [--description ] [--alt ] [--tag ]... nfxd delete --key --relay ... --a ... - nfxd run [--key ] --store [--state ] [--relay ]... [--iroh-relay ]... [--iroh-relay-ca ]... [--relay-only] [--iroh-bind [--iroh-external ]...] [--seed [#,...]]... [--fetch [#,...]]... [--pull [#,...]]... [--origin ] [--https-url ] [--embed-relay ] [--embed-tracker [--bridge [--bridge-public ] [--bridge-max-peers ] [--tracker-url ]]] [--namespace ]... [--allow-creator ]... [--gossip [--gossip-peer @]...] [--rating-root --general-relay ... [--rating-trusted ]...] [--stun [--stun-public ]] [--mesh-relay ... [--mesh-bind ] [--mesh-stun ]... [--mesh-max-peers ]] + nfxd run [--key ] --store [--state ] [--relay ]... [--iroh-relay ]... [--iroh-relay-ca ]... [--relay-only] [--iroh-bind [--iroh-external ]...] [--seed [#,...]]... [--fetch [#,...]]... [--pull [#,...]]... [--origin ] [--https-url ] [--embed-relay ] [--embed-tracker [--bridge [--bridge-public ] [--bridge-max-peers ] [--tracker-url ]]] [--namespace ]... [--allow-creator ]... [--gossip [--gossip-peer @]...] [--rating-root --general-relay ... [--rating-trusted ]...] [--stun [--stun-public ]] [--mesh-relay ... [--mesh-bind [--mesh-public ]] [--mesh-stun ]... [--mesh-max-peers ]] --mesh-relay runs the mesh endpoint (NFX-10 §6): browsers meet this node in the rooms of the renditions it holds, signaling only through those Nostr relays, and fetch verified segments over WebRTC with full ICE, through NATs (--mesh-stun: STUN servers for the public - mapping). Off by default. Privacy: joining a room tells anyone who announces presence there - this node's candidate addresses, public ones included."; + mapping; --mesh-public: behind a DNAT, its public address, forwarded to --mesh-bind's fixed + port, offered instead of any address that is not public). Off by default. Privacy: joining + a room tells anyone who announces presence there this node's candidate addresses, public + ones included."; fn usage() -> ExitCode { eprintln!("{USAGE}"); @@ -300,6 +311,7 @@ async fn run(args: &[String]) -> Result<()> { "mesh-bind", "mesh-stun", "mesh-max-peers", + "mesh-public", "namespace", "allow-creator", "gossip-peer", @@ -360,6 +372,7 @@ async fn run(args: &[String]) -> Result<()> { }) .collect::>()?, mesh_max_peers: count(&f, "mesh-max-peers")?, + mesh_public: ip_port(&f, "mesh-public")?, namespaces: all(&f, "namespace") .iter() .map(|n| Namespace::parse(n).map_err(Error::from)) @@ -409,6 +422,9 @@ async fn run(args: &[String]) -> Result<()> { } if let (Some(addr), Some(session)) = (daemon.mesh_addr, &daemon.mesh_session) { eprintln!("mesh: udp {addr}, session {session}"); + if let Some(public) = daemon.mesh_public { + eprintln!("mesh public: udp {public} (offered as a host candidate)"); + } } if gossip { let me = daemon.node().addr(); @@ -590,6 +606,17 @@ fn count(f: &[(String, String)], name: &str) -> Result> { .transpose() } +/// `--NAME ip:port`, given at most once. Only the form is checked here; `Daemon::start` +/// checks that the daemon can use it. +fn ip_port(f: &[(String, String)], name: &str) -> Result> { + one(f, name)? + .map(|v| { + v.parse() + .map_err(|_| Error::Config(format!("--{name} {v}: not an ip:port"))) + }) + .transpose() +} + /// `--bridge-max-peers N`, a count; `Daemon::start` checks its range. fn bridge_max_peers(f: &[(String, String)]) -> Result> { count(f, "bridge-max-peers") @@ -628,7 +655,9 @@ fn gossip_peers(values: &[String]) -> Result> { #[cfg(test)] mod tests { - use super::{bridge_max_peers, flags, gossip_peers, iroh_addrs, items, pubkey_hex, relay_cas}; + use super::{ + bridge_max_peers, flags, gossip_peers, ip_port, iroh_addrs, items, pubkey_hex, relay_cas, + }; fn ca_pem() -> String { use base64::Engine as _; @@ -885,4 +914,42 @@ mod tests { assert!(err.contains("given twice"), "{err}"); assert!(flags(&args(&["--bridge-max-peers"]), &known).is_err()); } + + #[test] + fn mesh_public_is_one_ip_and_port() { + let args = |s: &[&str]| s.iter().map(ToString::to_string).collect::>(); + let known = ["mesh-public"]; + let sa = |s: &str| s.parse::().unwrap(); + for v in ["203.0.113.7:7845", "[2001:db8::7]:7845"] { + let f = flags(&args(&["--mesh-public", v]), &known).unwrap(); + assert_eq!(ip_port(&f, "mesh-public").unwrap(), Some(sa(v))); + } + assert_eq!(ip_port(&[], "mesh-public").unwrap(), None); + // Only the form here: whether the daemon can use it is `Daemon::start`'s to check. + for bad in [ + "203.0.113.7", + ":7845", + "relay.example:7845", + "2001:db8::7:7845", + "203.0.113.7:65536", + "udp://203.0.113.7:7845", + ] { + let f = flags(&args(&["--mesh-public", bad]), &known).unwrap(); + let err = ip_port(&f, "mesh-public").unwrap_err().to_string(); + assert_eq!(err, format!("--mesh-public {bad}: not an ip:port")); + } + let f = flags( + &args(&[ + "--mesh-public", + "203.0.113.7:1", + "--mesh-public", + "203.0.113.7:2", + ]), + &known, + ) + .unwrap(); + let err = ip_port(&f, "mesh-public").unwrap_err().to_string(); + assert!(err.contains("--mesh-public given twice"), "{err}"); + assert!(flags(&args(&["--mesh-public"]), &known).is_err()); + } } diff --git a/crates/nfxd/tests/daemon.rs b/crates/nfxd/tests/daemon.rs index fd54589..960a0c4 100644 --- a/crates/nfxd/tests/daemon.rs +++ b/crates/nfxd/tests/daemon.rs @@ -1931,6 +1931,83 @@ async fn mesh_settings_that_cannot_work_are_refused_at_start() { }, "a relay-only node opens no direct UDP", ), + ( + "mesh-public-alone", + Config { + mesh_public: Some(sa("8.8.8.8:7845")), + ..Config::default() + }, + "--mesh-public needs --mesh-relay", + ), + ( + "mesh-public-no-bind", + Config { + mesh_relays: relay(), + mesh_public: Some(sa("8.8.8.8:7845")), + ..Config::default() + }, + "--mesh-public needs --mesh-bind", + ), + ( + "mesh-public-ephemeral-bind", + Config { + mesh_relays: relay(), + mesh_bind: Some(sa("127.0.0.1:0")), + mesh_public: Some(sa("8.8.8.8:7845")), + ..Config::default() + }, + "--mesh-bind 127.0.0.1:0: port 0 is not a fixed port", + ), + ( + "mesh-public-private", + Config { + mesh_relays: relay(), + mesh_bind: Some(sa("127.0.0.1:7845")), + mesh_public: Some(sa("10.0.0.28:7845")), + ..Config::default() + }, + "--mesh-public 10.0.0.28:7845: not a public unicast address", + ), + ( + "mesh-public-documentation", + Config { + mesh_relays: relay(), + mesh_bind: Some(sa("127.0.0.1:7845")), + mesh_public: Some(sa("203.0.113.7:7845")), + ..Config::default() + }, + "not a public unicast address", + ), + ( + "mesh-public-port-0", + Config { + mesh_relays: relay(), + mesh_bind: Some(sa("127.0.0.1:7845")), + mesh_public: Some(sa("8.8.8.8:0")), + ..Config::default() + }, + "not a public unicast address with a port", + ), + ( + "mesh-public-multicast", + Config { + mesh_relays: relay(), + mesh_bind: Some(sa("127.0.0.1:7845")), + mesh_public: Some(sa("224.0.0.1:7845")), + ..Config::default() + }, + "not a public unicast address", + ), + ( + "mesh-public-family", + Config { + mesh_relays: relay(), + mesh_bind: Some(sa("127.0.0.1:7845")), + mesh_public: Some(sa("[2001:4860:4860::8888]:7845")), + ..Config::default() + }, + "not of --mesh-bind's family", + ), ] { let err = Daemon::start(Config { keys: Some(Keys::generate()), @@ -2053,3 +2130,44 @@ async fn a_seeder_with_the_mesh_is_present_in_its_room_and_leaves_it_on_shutdown next_signal(&mut notes, room.pubkey(), &session, "disconnect").await; listener.shutdown().await; } + +#[tokio::test(flavor = "multi_thread")] +async fn with_mesh_public_the_mesh_offers_that_address_and_not_the_bound_one() { + use nostr_sdk::prelude as ns; + let general = ns::LocalRelay::builder().build(); + general.run().await.unwrap(); + let general_url = general.url().await.to_string(); + // A fixed port, as a DNAT forwards one. (Free a moment ago; the bind proves it.) The + // public address must be one the daemon takes as public; nothing is ever sent to it. + let port = std::net::UdpSocket::bind("127.0.0.1:0") + .unwrap() + .local_addr() + .unwrap() + .port(); + let bind: SocketAddr = format!("127.0.0.1:{port}").parse().unwrap(); + let public: SocketAddr = "8.8.8.8:7845".parse().unwrap(); + let node = Daemon::start(Config { + keys: Some(Keys::generate()), + store: tmp("mesh-public"), + mesh_relays: vec![general_url], + mesh_bind: Some(bind), + mesh_public: Some(public), + no_portmapper: true, + ..Config::default() + }) + .await + .unwrap(); + assert_eq!(node.mesh_addr, Some(bind)); + assert_eq!(node.mesh_public, Some(public)); + let deadline = std::time::Instant::now() + WAIT; + let status = loop { + let s = node.mesh_status().unwrap(); + if s.gathered { + break s; + } + assert!(std::time::Instant::now() < deadline, "{s:?}"); + tokio::time::sleep(Duration::from_millis(50)).await; + }; + assert_eq!(status.candidates, [format!("host {public}")]); + node.shutdown().await; +} diff --git a/spec/nfx-10-web.md b/spec/nfx-10-web.md index 161c158..ba9aa41 100644 --- a/spec/nfx-10-web.md +++ b/spec/nfx-10-web.md @@ -376,7 +376,9 @@ tracker, no TURN, no relay plugin. Any relay that carries ephemeral events (NIP- do. A scoped relay (NFX-04 §1) admits only NFX's catalog kinds and refuses kind 25050, so a page names general relays for signaling. Once two peers hold each other's complete SDP, ICE runs its connectivity checks from both ends, which is the hole punch; STUN (§1) -gives each its public mapping. +gives each its public mapping. (A native peer behind a DNAT, whose public address STUN cannot +give it, may offer that address as a host candidate and take what arrives as sent to it, as a +bridge does; nothing on the wire changes.) The wire is **signaling-compatible with the NIP-DC draft §§2, 4–6** (nostr4j's `NIP-DC.md`, 2026-11-02, commit `3a9def15`): its kind, tags, room proof, presence, offer @@ -662,3 +664,5 @@ with full ICE, and a NAT test. reachable source holds (NFX-03 §§4–5, which now let an `https` endpoint name its renditions), narrowed further only by the page's own choice, ABR free among the rest. §2: a bridge that holds only some renditions lists them and joins no other swarm. +- Draft 2026-11-16 (issue #71, editorial): §6 notes that a native peer behind a DNAT may + offer its public address as a host candidate. Nothing on the wire changes.