dig_nat/relay.rs
1//! Relay client — the LAST-RESORT transport + the node's persistent reachability channel.
2//!
3//! Relocated + generalized from `dig-node`'s `relay.rs`. Two responsibilities:
4//!
5//! 1. **Persistent reservation** ([`run_relay_connection`]) — a DIG Node behind NAT can't accept
6//! inbound dials, so it holds a CONSTANT registered connection with a publicly-reachable relay
7//! (default [`dig_constants::DIG_RELAY_URL`], override `DIG_RELAY_URL`, opt out with
8//! `DIG_RELAY_URL=off`). This is the reachability channel other peers reach it through and the
9//! rendezvous for relay-coordinated hole-punch.
10//! 2. **Relayed transport** — when every NAT-traversal method fails, peer traffic is tunnelled
11//! THROUGH the relay (RLY-002 `relay_message`). This is the last resort in the traversal order.
12//!
13//! **Graceful-fallback guarantees (baked in):** the reservation loop NEVER blocks startup, NEVER
14//! panics/exits, and NEVER hot-loops error-spam — failures log ONCE per state change (a transition
15//! into `Disconnected`), and every retry sleeps a bounded, capped-exponential backoff. If the relay
16//! is unreachable the node keeps serving indefinitely; the task just keeps retrying in the
17//! background. State is published through [`RelayStatus`] (a cheap atomic snapshot) as one of four
18//! [`RelayState`]s and surfaced verbatim to a `control.relayStatus`-style RPC / `/health`.
19
20use std::collections::{HashMap, HashSet};
21use std::net::{IpAddr, SocketAddr};
22use std::sync::atomic::{AtomicU32, AtomicU64, AtomicU8, Ordering};
23use std::sync::{Arc, Mutex};
24use std::time::Duration;
25
26use dig_ip::{CandidateSource, DialConfig, LocalStack, PeerCandidates};
27use futures_util::{SinkExt, StreamExt};
28use tokio::net::TcpStream;
29use tokio::sync::mpsc;
30use tokio_tungstenite::tungstenite::Message;
31use tokio_tungstenite::{client_async_tls_with_config, MaybeTlsStream, WebSocketStream};
32
33use crate::wire::{RelayMessage, RelayPeerInfo};
34
35/// Default network id a node registers under (matches dig-gossip `DEFAULT_INTRODUCER_NETWORK_ID`
36/// and dig-node's `DEFAULT_NETWORK_ID`).
37pub const DEFAULT_NETWORK_ID: &str = "DIG_MAINNET";
38
39/// Relay protocol version the node advertises in `Register` (RLY-001).
40pub const RELAY_PROTOCOL_VERSION: u32 = 1;
41
42/// Base reconnect delay (dig-gossip `RelayConfig::reconnect_delay_secs` = 5).
43const BASE_BACKOFF_SECS: u64 = 5;
44/// Cap on the exponential backoff so a long outage doesn't push the retry interval to hours.
45const MAX_BACKOFF_SECS: u64 = 300;
46/// Keepalive ping period (RLY-006; dig-gossip `PING_INTERVAL_SECS` = 30).
47const PING_INTERVAL_SECS: u64 = 30;
48/// How often the held reservation re-pulls the relay peer list (RLY-005 `GetPeers`) over the SAME
49/// persistent socket, so a peer that registers AFTER this node — or one missed on the first pull —
50/// is still discovered without ever reopening the connection (the connect-leg fix).
51const DISCOVERY_INTERVAL_SECS: u64 = 60;
52
53/// Hard cap on the peers retained in the discovered set ([`RelayStatus::known_peers`]).
54///
55/// SECURITY: the relay is an UNTRUSTED intermediary. A hostile/compromised relay can stream an
56/// unbounded flood of `PeerConnected` frames — or a single oversized `Peers` frame — with distinct
57/// fabricated `peer_id`s, so an uncapped set is a memory-exhaustion DoS. 1024 is far more than any
58/// honest relay reports for one network's live reservations (the set is folded into a peer pool that
59/// itself selects a small working subset), yet small enough that the worst case is bounded, cheap
60/// memory. Beyond the cap, further distinct peers are DROPPED rather than grown.
61pub const MAX_KNOWN_PEERS: usize = 1024;
62
63/// Hard cap on the byte length of a single RLY-002 relayed-transport payload (both directions).
64///
65/// SECURITY / backpressure: the relay is UNTRUSTED and a peer reached over relayed transport is the
66/// last-resort TURN path, so an oversized frame is refused rather than buffered — an outbound `send`
67/// larger than this errors, and an inbound frame larger than this is dropped. 1 MiB comfortably
68/// holds a sealed gossip message (NC-1 ciphertext) while bounding the worst-case per-frame memory.
69pub const MAX_RELAY_PAYLOAD: usize = 1 << 20;
70
71/// Bounded inbound capacity for one open [`RelayTunnel`]. A full channel applies backpressure — the
72/// reservation loop `try_send`s inbound relayed bytes and DROPS the frame when the consumer is not
73/// keeping up, so a hostile relay flooding one tunnel cannot exhaust memory (matches the
74/// [`MAX_KNOWN_PEERS`] bounded-set philosophy). The RLY-002 `seq` lets the consumer detect the gap.
75const RELAY_TUNNEL_INBOUND_CAP: usize = 256;
76
77/// Upper bound on concurrently-registered relay tunnels (outbound dial + inbound accept combined)
78/// before the RESPONDER path refuses to create a new inbound circuit.
79///
80/// SECURITY: the relay is UNTRUSTED. When the accept path ([`RelayStatus::enable_accept`]) is on, an
81/// inbound RLY-002 frame from an unknown peer creates a server-role tunnel + surfaces an accept — so
82/// an uncapped accept lets a hostile relay flood distinct fabricated `from` ids to spawn unbounded
83/// tunnels/accept-tasks (a memory/task-exhaustion DoS). Beyond this cap the introduced circuit is
84/// DROPPED rather than accepted. 256 is far more concurrent relayed peers than the last-resort tier
85/// ever legitimately carries, yet bounds the worst case to cheap, bounded memory.
86pub const MAX_RELAY_TUNNELS: usize = 256;
87
88/// Bounded capacity of the inbound-accept channel ([`RelayStatus::enable_accept`]). A full channel
89/// means the consumer is not accepting introduced circuits fast enough; the newest is dropped
90/// (bounded backpressure), never queued unboundedly.
91const INBOUND_ACCEPT_CAP: usize = 64;
92
93/// The mTLS role a locally-registered [`RelayTunnel`] runs — the discriminator that resolves the
94/// GLARE / simultaneous-mutual-dial case (#1536). A relay circuit needs exactly ONE mTLS client + ONE
95/// mTLS server; when two NAT'd peers fall to the relay tier and dial EACH OTHER at the same time (the
96/// common two-NAT'd-peer flywheel case), both open a `Client` tunnel and both send a ClientHello —
97/// each ClientHello would route into the OTHER side's client session (double-ClientHello deadlock).
98/// Tagging the tunnel with its role lets [`route_relayed`](RelayStatus::route_relayed) detect that a
99/// ClientHello arrived on a tunnel where WE are also the client (the glare signature) and apply the
100/// deterministic tie-break instead of feeding it to our doomed client.
101#[derive(Debug, Clone, Copy, PartialEq, Eq)]
102enum TunnelRole {
103 /// WE initiated the dial — running [`PeerSession::client`](crate::mux::PeerSession::client) over
104 /// this tunnel; inbound frames are the peer's ServerHello / server-side records.
105 Client,
106 /// WE accepted an introduced circuit — running [`PeerSession::server`](crate::mux::PeerSession::server)
107 /// over this tunnel; inbound frames are the dialing peer's client-side records.
108 Server,
109}
110
111/// A registered relayed tunnel: the inbound sink frames are routed into, the mTLS [`TunnelRole`] we
112/// run over it (for the #1536 glare tie-break), and a monotonic `id` distinguishing this registration
113/// from a later one on the SAME peer key. The id matters when the glare tie-break REPLACES our client
114/// tunnel with a server tunnel under the same `from` key: the old client [`RelayTunnel`]'s `Drop`
115/// (fired when its doomed dial fails) must not deregister the NEW server entry, so `close_tunnel` only
116/// removes when the stored id still matches.
117#[derive(Debug)]
118struct TunnelEntry {
119 sink: mpsc::Sender<Vec<u8>>,
120 role: TunnelRole,
121 id: u64,
122}
123
124/// Whether `payload` begins with a TLS handshake record whose first message is a ClientHello — a TLS
125/// record has content-type `0x16` (handshake) at byte 0 and the handshake message type at byte 5
126/// (`0x01` = ClientHello, `0x02` = ServerHello). A rustls client ships its ClientHello flight as the
127/// first `poll_write`, so the first relayed frame from a fresh dialer matches this.
128///
129/// This is the relay layer's DIRECTION discriminator, and it decides the mTLS role in both directions:
130/// a ClientHello means the remote is the circuit's client, so we are its server; anything else is not
131/// the start of an inbound circuit at all. [`RelayStatus::route_relayed`] uses it to tell a peer's
132/// GLARE ClientHello (a competing simultaneous dial) from the ServerHello / app records expected where
133/// we are the client (#1536), and [`RelayStatus::accept_introduced`] uses it to refuse to manufacture
134/// a server-role circuit out of a frame no dialer sent (#1761).
135fn is_tls_client_hello(payload: &[u8]) -> bool {
136 payload.len() >= 6 && payload[0] == 0x16 && payload[5] == 0x01
137}
138
139/// Compute the next reconnect backoff: capped exponential in the number of consecutive failures.
140/// `failures == 0` → base; doubles each failure up to [`MAX_BACKOFF_SECS`]. Pure → unit-tested.
141pub fn backoff_secs(consecutive_failures: u32) -> u64 {
142 backoff_secs_with(consecutive_failures, BASE_BACKOFF_SECS, MAX_BACKOFF_SECS)
143}
144
145/// Capped-exponential backoff with an explicit base + cap. Always returns a value in `[base, cap]`
146/// — never zero — so a failing connect can never busy-loop.
147fn backoff_secs_with(consecutive_failures: u32, base: u64, cap: u64) -> u64 {
148 let shifted = base.checked_shl(consecutive_failures).unwrap_or(cap);
149 shifted.clamp(base, cap)
150}
151
152/// Backoff schedule for the reconnect loop — production defaults, or fast values for tests.
153#[derive(Debug, Clone, Copy)]
154pub struct Backoff {
155 /// First-retry delay (seconds).
156 pub base_secs: u64,
157 /// Upper bound on the delay (seconds).
158 pub cap_secs: u64,
159}
160
161impl Default for Backoff {
162 fn default() -> Self {
163 Backoff {
164 base_secs: BASE_BACKOFF_SECS,
165 cap_secs: MAX_BACKOFF_SECS,
166 }
167 }
168}
169
170/// The four observable states of the relay reservation, surfaced verbatim (lowercase) as the
171/// `state` field of a `control.relayStatus`-style RPC.
172///
173/// - `Disabled` — reservation OFF (`DIG_RELAY_URL=off`); no task runs, no attempts made.
174/// - `Connecting` — actively dialing/registering.
175/// - `Connected` — a reservation is held (`RegisterAck{success:true}` arrived); reachable to peers.
176/// - `Disconnected` — not connected; backing off + will retry. The graceful-fallback resting state.
177#[derive(Debug, Clone, Copy, PartialEq, Eq)]
178pub enum RelayState {
179 /// Reservation OFF (`DIG_RELAY_URL=off`); no task runs, no attempts made.
180 Disabled,
181 /// Actively dialing/registering (initial attempt or a reconnect in flight).
182 Connecting,
183 /// A reservation is held (`RegisterAck{success:true}` arrived); reachable to NAT'd peers.
184 Connected,
185 /// Not connected; backing off + will retry. The graceful-fallback resting state.
186 Disconnected,
187}
188
189impl RelayState {
190 /// The stable lowercase wire string for the RPC `state` field.
191 pub fn as_str(self) -> &'static str {
192 match self {
193 RelayState::Disabled => "disabled",
194 RelayState::Connecting => "connecting",
195 RelayState::Connected => "connected",
196 RelayState::Disconnected => "disconnected",
197 }
198 }
199
200 fn to_u8(self) -> u8 {
201 match self {
202 RelayState::Disabled => 0,
203 RelayState::Connecting => 1,
204 RelayState::Connected => 2,
205 RelayState::Disconnected => 3,
206 }
207 }
208
209 fn from_u8(v: u8) -> Self {
210 match v {
211 0 => RelayState::Disabled,
212 1 => RelayState::Connecting,
213 2 => RelayState::Connected,
214 _ => RelayState::Disconnected,
215 }
216 }
217}
218
219/// The peers discovered over the live reservation socket, in insertion order with O(1) dedup +
220/// membership by `peer_id`, bounded to [`MAX_KNOWN_PEERS`].
221///
222/// `order` preserves discovery order so [`RelayStatus::known_peers`] returns a stable sequence;
223/// `ids` mirrors `order`'s `peer_id`s so dedup and removal are O(1) instead of a linear scan (the
224/// old `iter().any(...)` was O(n²) over a flood). The two are kept in lockstep — every mutation
225/// touches both.
226#[derive(Debug, Default)]
227struct DiscoveredPeers {
228 order: Vec<RelayPeerInfo>,
229 ids: HashSet<String>,
230}
231
232impl DiscoveredPeers {
233 /// Insert `peer` unless already present or the set is full. Returns nothing — a full set simply
234 /// drops the newcomer (the untrusted-relay flood defense).
235 fn insert(&mut self, peer: RelayPeerInfo) {
236 if self.order.len() >= MAX_KNOWN_PEERS {
237 return;
238 }
239 if self.ids.insert(peer.peer_id.clone()) {
240 self.order.push(peer);
241 }
242 }
243
244 /// Remove the peer with this `peer_id`, if present.
245 fn remove(&mut self, peer_id: &str) {
246 if self.ids.remove(peer_id) {
247 self.order.retain(|p| p.peer_id != peer_id);
248 }
249 }
250
251 /// Replace the whole set from a `Peers` frame, deduped + truncated to the cap.
252 fn replace(&mut self, peers: Vec<RelayPeerInfo>) {
253 self.order.clear();
254 self.ids.clear();
255 for peer in peers {
256 self.insert(peer);
257 }
258 }
259
260 fn clear(&mut self) {
261 self.order.clear();
262 self.ids.clear();
263 }
264}
265
266/// Live relay-connection status, shared (via `Arc`) between the connection task and an RPC handler.
267/// Cheap atomic reads. State setters do STATE-CHANGE-ONLY logging so a long outage never hot-loops
268/// identical error lines.
269#[derive(Debug)]
270pub struct RelayStatus {
271 state: AtomicU8,
272 reconnect_attempts: AtomicU32,
273 connected_peers: AtomicU64,
274 last_error: Mutex<Option<String>>,
275 /// Peers learned over the LIVE reservation socket — the relay's `GetPeers` response (RLY-005)
276 /// plus `PeerConnected`/`PeerDisconnected` pushes. This is the discovery output of the persistent
277 /// reservation: a consumer (dig-gossip's pool/address book) reads it instead of reopening an
278 /// ephemeral socket per pass. Keyed by `peer_id` (deduped); bounded to [`MAX_KNOWN_PEERS`] so an
279 /// untrusted relay can't exhaust memory; cleared on every reconnect so a stale list is never
280 /// served across a drop.
281 known_peers: Mutex<DiscoveredPeers>,
282 /// Sink that injects an outbound [`RelayMessage`] into the LIVE reservation socket's write half.
283 /// `Some` only while a session is held (set by `connect_once`, cleared on every drop) — this is
284 /// what lets a [`RelayTunnel`] reuse the ONE persistent reservation socket for RLY-002 relayed
285 /// transport instead of opening a second connection.
286 outbound: Mutex<Option<mpsc::UnboundedSender<RelayMessage>>>,
287 /// This node's own `peer_id` (hex), stamped as `from` on every RLY-002 frame the tunnels send.
288 /// Set when a session registers; needed because a tunnel is opened from the shared status handle.
289 local_peer_id: Mutex<Option<String>>,
290 /// The network id this reservation registered under. Echoed onto an inbound accepted tunnel (the
291 /// RLY-002 frame itself does not carry it). Set alongside [`local_peer_id`] when a session registers.
292 local_network_id: Mutex<Option<String>>,
293 /// Sink that surfaces an INTRODUCED inbound circuit — a frame from a peer with NO open outbound
294 /// tunnel — as a server-role [`RelayTunnel`] for a consumer to accept + serve
295 /// ([`crate::accept::RelayAcceptor`]). `None` (default) = the original untrusted-relay behavior:
296 /// drop an unknown-peer frame. `Some` once a consumer calls [`RelayStatus::enable_accept`].
297 ///
298 /// This is the RESPONDER counterpart to [`open_tunnel`](Self::open_tunnel): a relay circuit needs
299 /// exactly ONE mTLS client + ONE mTLS server. The DIALER calls `open_tunnel` and runs
300 /// `PeerSession::client`; the reservation-HOLDER that RECEIVES the introduced circuit accepts here
301 /// and runs `PeerSession::server`. Without this path both ends acted as TLS client and the
302 /// handshake deadlocked (`got ClientHello when expecting ServerHello`, #1536).
303 inbound_accept: Mutex<Option<mpsc::Sender<RelayTunnel>>>,
304 /// Open relayed-transport tunnels, keyed by the REMOTE peer's `peer_id` (hex). An inbound RLY-002
305 /// `relay_message` from a peer is routed to its tunnel's inbound channel; a frame from a peer with
306 /// no open tunnel is dropped (the untrusted-relay default). Entries are removed on tunnel drop.
307 /// Each entry carries the mTLS [`TunnelRole`] we run over it so `route_relayed` can resolve the
308 /// #1536 simultaneous-mutual-dial glare deterministically.
309 tunnels: Mutex<HashMap<String, TunnelEntry>>,
310 /// Monotonic per-node sequence number stamped on outbound RLY-002 frames (ordering/dedup).
311 relay_seq: AtomicU64,
312 /// Monotonic id assigned to each tunnel registration so a stale [`RelayTunnel`]'s `Drop` never
313 /// deregisters a NEWER entry under the same peer key (see [`TunnelEntry::id`]; #1536 glare replace).
314 next_tunnel_id: AtomicU64,
315}
316
317impl Default for RelayStatus {
318 fn default() -> Self {
319 RelayStatus {
320 state: AtomicU8::new(RelayState::Disconnected.to_u8()),
321 reconnect_attempts: AtomicU32::new(0),
322 connected_peers: AtomicU64::new(0),
323 last_error: Mutex::new(None),
324 known_peers: Mutex::new(DiscoveredPeers::default()),
325 outbound: Mutex::new(None),
326 local_peer_id: Mutex::new(None),
327 local_network_id: Mutex::new(None),
328 inbound_accept: Mutex::new(None),
329 tunnels: Mutex::new(HashMap::new()),
330 relay_seq: AtomicU64::new(0),
331 next_tunnel_id: AtomicU64::new(0),
332 }
333 }
334}
335
336impl RelayStatus {
337 /// A fresh status (resting `Disconnected` until the task runs / the relay is reached).
338 pub fn new() -> Arc<Self> {
339 Arc::new(RelayStatus::default())
340 }
341
342 /// Read the current state.
343 pub fn state(&self) -> RelayState {
344 RelayState::from_u8(self.state.load(Ordering::Relaxed))
345 }
346
347 /// Transition to `next`, returning `true` IFF the state actually changed. Callers use the return
348 /// to log ONCE per transition (no hot-loop spam).
349 fn transition_to(&self, next: RelayState) -> bool {
350 let prev = self.state.swap(next.to_u8(), Ordering::Relaxed);
351 prev != next.to_u8()
352 }
353
354 /// Enter `Disabled` (reservation off). Idempotent; logs only on the first entry.
355 pub fn set_disabled(&self) {
356 if self.transition_to(RelayState::Disabled) {
357 tracing::info!("relay reservation disabled (DIG_RELAY_URL=off)");
358 }
359 }
360
361 /// Enter `Connecting`. Logs only on the transition (so reconnect attempts don't spam).
362 pub fn set_connecting(&self) {
363 if self.transition_to(RelayState::Connecting) {
364 tracing::debug!("relay connecting");
365 }
366 }
367
368 /// Mark `Connected` (clears the last error, resets the attempt counter). Logs recovery once.
369 pub fn set_connected(&self, connected_peers: u64) {
370 self.connected_peers
371 .store(connected_peers, Ordering::Relaxed);
372 self.reconnect_attempts.store(0, Ordering::Relaxed);
373 *self.last_error.lock().unwrap() = None;
374 if self.transition_to(RelayState::Connected) {
375 tracing::info!(connected_peers, "relay reservation established");
376 }
377 }
378
379 /// Mark `Disconnected` with an optional error and bump the attempt counter. Logs the failure
380 /// ONLY on the transition into `Disconnected` (the first drop); subsequent failed retries while
381 /// already `Disconnected` update the error/counter SILENTLY.
382 pub fn set_disconnected(&self, error: Option<String>) {
383 self.reconnect_attempts.fetch_add(1, Ordering::Relaxed);
384 if let Some(e) = &error {
385 *self.last_error.lock().unwrap() = Some(e.clone());
386 }
387 let changed = self.transition_to(RelayState::Disconnected);
388 if changed {
389 match &error {
390 Some(e) => tracing::warn!(
391 error = %e,
392 "relay reservation lost — node still serving; retrying in background"
393 ),
394 None => tracing::info!("relay reservation closed — retrying in background"),
395 }
396 }
397 }
398
399 /// Whether a relay session is currently held.
400 pub fn is_connected(&self) -> bool {
401 self.state() == RelayState::Connected
402 }
403
404 /// The current reconnect-attempt count (for tests / RPC).
405 pub fn reconnect_attempts(&self) -> u32 {
406 self.reconnect_attempts.load(Ordering::Relaxed)
407 }
408
409 /// Snapshot of the peers discovered over the live reservation socket (RLY-005 `Peers` +
410 /// `PeerConnected` pushes, minus `PeerDisconnected`). The consumer folds these into its address
411 /// book / pool. Returns a clone so the caller holds no lock.
412 pub fn known_peers(&self) -> Vec<RelayPeerInfo> {
413 self.known_peers.lock().unwrap().order.clone()
414 }
415
416 /// Count of peers currently discovered over the live reservation socket.
417 pub fn known_peer_count(&self) -> usize {
418 self.known_peers.lock().unwrap().order.len()
419 }
420
421 /// Replace the discovered-peer set with a `GetPeers` response (RLY-005 `Peers`), deduped and
422 /// truncated to [`MAX_KNOWN_PEERS`] (an untrusted relay could send an oversized frame).
423 fn replace_known_peers(&self, peers: Vec<RelayPeerInfo>) {
424 self.known_peers.lock().unwrap().replace(peers);
425 }
426
427 /// Fold in a relay-pushed `PeerConnected` notice, deduped by `peer_id`; dropped once the set is
428 /// full ([`MAX_KNOWN_PEERS`]) so a flood can't exhaust memory.
429 fn add_known_peer(&self, peer: RelayPeerInfo) {
430 self.known_peers.lock().unwrap().insert(peer);
431 }
432
433 /// Drop a peer on a relay-pushed `PeerDisconnected` notice.
434 fn remove_known_peer(&self, peer_id: &str) {
435 self.known_peers.lock().unwrap().remove(peer_id);
436 }
437
438 /// Clear the discovered-peer set (on every reconnect — the list is per-session).
439 fn clear_known_peers(&self) {
440 self.known_peers.lock().unwrap().clear();
441 }
442
443 // -- RLY-002 relayed transport (the tier-6 TURN fallback) ------------------------------------
444 //
445 // A relayed tunnel reuses the ONE persistent reservation socket: outbound frames go through
446 // `outbound` (drained by the reservation loop's write half), inbound `relay_message` frames are
447 // routed by `from` peer_id to the matching tunnel. Available only while the reservation is held.
448
449 /// Install the live session's outbound sink + this node's `peer_id` + the registered `network_id`.
450 /// Called by `connect_once` once registered; cleared by [`clear_transport`](Self::clear_transport)
451 /// on every drop.
452 fn set_transport(
453 &self,
454 peer_id: &str,
455 network_id: &str,
456 outbound: mpsc::UnboundedSender<RelayMessage>,
457 ) {
458 *self.local_peer_id.lock().unwrap() = Some(peer_id.to_string());
459 *self.local_network_id.lock().unwrap() = Some(network_id.to_string());
460 *self.outbound.lock().unwrap() = Some(outbound);
461 }
462
463 /// Enable the RESPONDER (accept) path and return the receiver of INTRODUCED inbound circuits.
464 ///
465 /// A relayed connection needs one mTLS client + one mTLS server. The dialer opens a tunnel and
466 /// runs the client; the reservation-HOLDER calls this once at startup and, for every inbound
467 /// frame from a peer it has no open outbound tunnel to, receives a server-role [`RelayTunnel`]
468 /// here to hand to a [`crate::accept::RelayAcceptor`] (which runs `PeerSession::server`). Until a
469 /// consumer calls this, unknown-peer frames are DROPPED (the untrusted-relay default), so the
470 /// accept path is strictly opt-in. The channel is bounded ([`INBOUND_ACCEPT_CAP`]).
471 pub fn enable_accept(&self) -> mpsc::Receiver<RelayTunnel> {
472 let (tx, rx) = mpsc::channel(INBOUND_ACCEPT_CAP);
473 *self.inbound_accept.lock().unwrap() = Some(tx);
474 rx
475 }
476
477 /// Tear down the transport on session drop: drop the outbound sink (so tunnel sends fail fast)
478 /// and close every open tunnel's inbound channel (so a blocked `recv` wakes with `None`).
479 fn clear_transport(&self) {
480 *self.outbound.lock().unwrap() = None;
481 self.tunnels.lock().unwrap().clear();
482 }
483
484 /// Whether a relayed tunnel can currently be opened — a reservation is held AND its outbound sink
485 /// is live. The tier-6 [`RelayedTransport`](crate::method::relayed::RelayedTransport) gates on this.
486 pub fn relay_transport_ready(&self) -> bool {
487 self.is_connected() && self.outbound.lock().unwrap().is_some()
488 }
489
490 /// Open an RLY-002 relayed-transport tunnel to `target_peer` (hex `peer_id`) over the held
491 /// reservation socket — the traversal ladder's FINAL tier when a pair can neither direct-dial nor
492 /// hole-punch. The returned [`RelayTunnel`] sends/receives opaque payloads that the relay forwards
493 /// A→relay→B; per NC-1 the payload is END-TO-END SEALED to the recipient so the relay forwards
494 /// ciphertext only. `Err` if no reservation is held. Dropping the tunnel deregisters it.
495 pub fn open_tunnel(
496 self: &Arc<Self>,
497 target_peer: &str,
498 network_id: &str,
499 ) -> Result<RelayTunnel, String> {
500 if !self.relay_transport_ready() {
501 return Err("relay reservation not connected — cannot open relayed tunnel".into());
502 }
503 // Self / SPKI-collision guard: a relayed circuit to our OWN peer_id has no lower/higher end
504 // for the glare tie-break, so it could never converge to one-client-one-server — refuse it
505 // (#1536).
506 let local = self
507 .local_peer_id
508 .lock()
509 .unwrap()
510 .clone()
511 .unwrap_or_default();
512 if !local.is_empty() && local == target_peer {
513 return Err("refusing relayed self-dial (target == local peer_id)".into());
514 }
515 // NON-CLOBBER (#1536): if a circuit to this peer already exists — because an introduced
516 // ClientHello made us its SERVER before this dial ran (a timing-ordered glare) — do NOT open a
517 // second, conflicting circuit under the same key. The existing circuit IS the connection; a
518 // duplicate would orphan one role and leave two mTLS sessions racing to one peer. Checked +
519 // inserted under ONE lock so a concurrent `route_relayed` cannot slip a role in between.
520 let mut tunnels = self.tunnels.lock().unwrap();
521 if tunnels.contains_key(target_peer) {
522 return Err("existing relay circuit to peer — not opening a duplicate".into());
523 }
524 // A dialer runs the mTLS client over the tunnel it opens.
525 Ok(self.insert_entry(&mut tunnels, target_peer, network_id, TunnelRole::Client))
526 }
527
528 /// Register a tunnel routing entry for `target_peer` and build its [`RelayTunnel`], overwriting any
529 /// existing entry under the key. Test-only (the `open_server_tunnel` + flood-cap tests use it); the
530 /// production paths ([`open_tunnel`](Self::open_tunnel) + [`accept_introduced`](Self::accept_introduced))
531 /// go through non-clobber checks first.
532 #[cfg(test)]
533 fn register_tunnel(
534 self: &Arc<Self>,
535 target_peer: &str,
536 network_id: &str,
537 role: TunnelRole,
538 ) -> RelayTunnel {
539 let mut tunnels = self.tunnels.lock().unwrap();
540 self.insert_entry(&mut tunnels, target_peer, network_id, role)
541 }
542
543 /// Build a fresh [`RelayTunnel`] for `target_peer` with `role` and insert its entry into the
544 /// already-locked `tunnels` map (assigning a monotonic id so a stale `Drop` never evicts a newer
545 /// registration). The caller holds the lock, so the check-then-insert is atomic — the #1536
546 /// non-clobber + role-race defense.
547 fn insert_entry(
548 self: &Arc<Self>,
549 tunnels: &mut HashMap<String, TunnelEntry>,
550 target_peer: &str,
551 network_id: &str,
552 role: TunnelRole,
553 ) -> RelayTunnel {
554 let (tx, rx) = mpsc::channel(RELAY_TUNNEL_INBOUND_CAP);
555 let id = self.next_tunnel_id.fetch_add(1, Ordering::Relaxed);
556 tunnels.insert(target_peer.to_string(), TunnelEntry { sink: tx, role, id });
557 RelayTunnel {
558 target: target_peer.to_string(),
559 network_id: network_id.to_string(),
560 status: Arc::clone(self),
561 inbound: rx,
562 id,
563 }
564 }
565
566 /// Route one inbound RLY-002 `relay_message` to its tunnel by `from` peer_id. Oversized payloads
567 /// are dropped (size cap); a frame from a peer with no open tunnel becomes an introduced circuit
568 /// ONLY when it is a dialer's opening ClientHello (#1761 — see
569 /// [`accept_introduced`](Self::accept_introduced)), and is otherwise dropped, as it is when the
570 /// responder path is off or a flood cap is hit; a full inbound channel drops the frame
571 /// (backpressure). Returns silently in every drop case (untrusted relay).
572 ///
573 /// GLARE (#1536): when a ClientHello arrives on a tunnel where WE are ALSO the client — the peer
574 /// dialed us at the same time we dialed it — a deterministic tie-break makes exactly ONE side the
575 /// server: the numerically-LOWER `peer_id` becomes the server. Both ends compute the same rule, so
576 /// a crossed pair converges to one-client-one-server under ANY frame ordering with no retry loop.
577 /// The TIMING-ordered variant (a peer's ClientHello arrives BEFORE our own dial registers) cannot
578 /// produce a conflicting second circuit either: [`open_tunnel`](Self::open_tunnel) is non-clobber,
579 /// so once we serve a peer our later dial to it is refused. The per-frame role LOOKUP + any
580 /// same-frame yield (client-tunnel removal) happen under a single lock acquisition; the server
581 /// registration in [`accept_introduced`](Self::accept_introduced) re-acquires and re-checks
582 /// (non-clobber), so a dial racing between the two regions is abandoned, never a double-session.
583 fn route_relayed(self: &Arc<Self>, from: &str, payload: Vec<u8>) {
584 if payload.len() > MAX_RELAY_PAYLOAD {
585 tracing::debug!(
586 from,
587 len = payload.len(),
588 "dropping oversized relayed frame"
589 );
590 return;
591 }
592 let local = self
593 .local_peer_id
594 .lock()
595 .unwrap()
596 .clone()
597 .unwrap_or_default();
598 // Self / SPKI-collision guard: a frame stamped with our OWN id can never be a real remote peer
599 // (and the tie-break has no lower/higher end for it) — drop it rather than risk a no-server
600 // hang (#1536).
601 if !local.is_empty() && local == from {
602 tracing::debug!("dropping relayed frame stamped with our own peer_id (self/collision)");
603 return;
604 }
605
606 // Decide the action for this frame under ONE tunnels-lock so a concurrent `open_tunnel` cannot
607 // race the role assignment.
608 enum Route {
609 /// Deliver into an existing tunnel's inbound sink.
610 Deliver(mpsc::Sender<Vec<u8>>),
611 /// Drop the frame (we retain our client role, or cannot serve).
612 Ignore,
613 /// Accept as an introduced server-role circuit.
614 Accept,
615 }
616 let route = {
617 let mut tunnels = self.tunnels.lock().unwrap();
618 match tunnels.get(from).map(|e| (e.sink.clone(), e.role, e.id)) {
619 // We are the SERVER for this peer — every frame is the client's; route it.
620 Some((sink, TunnelRole::Server, _)) => Route::Deliver(sink),
621 // We are the CLIENT. A ServerHello / app record is the expected response; a ClientHello
622 // means the peer dialed us at the same time (GLARE).
623 Some((sink, TunnelRole::Client, id)) => {
624 if !is_tls_client_hello(&payload) {
625 Route::Deliver(sink)
626 } else if local.as_str() > from {
627 // Higher id → WE keep the client role; ignore the peer's competing ClientHello
628 // (the lower-id peer yields to server and answers with a ServerHello).
629 tracing::debug!(
630 from,
631 "relay glare — retaining client role (peer yields to server)"
632 );
633 Route::Ignore
634 } else if self.inbound_accept.lock().unwrap().is_none() {
635 // Lower id → should be server, but no responder path is enabled; cannot serve,
636 // so keep the (doomed) client tunnel and drop rather than tear it down.
637 tracing::debug!(
638 from,
639 "relay glare — should be server but accept path off; dropping"
640 );
641 Route::Ignore
642 } else {
643 // Lower id → yield to the server role: drop our client tunnel (only if it is
644 // still ours) and accept the peer's circuit as server below.
645 if tunnels.get(from).map(|e| e.id) == Some(id) {
646 tunnels.remove(from);
647 }
648 tracing::debug!(from, "relay glare — yielding to server role");
649 Route::Accept
650 }
651 }
652 // No circuit under this key — a candidate INTRODUCED circuit (a peer dialing us over
653 // the relay). Whether it really is one is decided by the frame's direction in
654 // `accept_introduced`, which admits only a dialer's opening ClientHello (#1761).
655 None => Route::Accept,
656 }
657 };
658
659 match route {
660 Route::Deliver(sink) => {
661 if sink.try_send(payload).is_err() {
662 tracing::debug!(from, "relayed tunnel inbound full/closed — frame dropped");
663 }
664 }
665 Route::Ignore => {}
666 Route::Accept => self.accept_introduced(from, payload),
667 }
668 }
669
670 /// Accept an INTRODUCED inbound circuit from `from` as a server-role tunnel and surface it to the
671 /// consumer's [`RelayAcceptor`](crate::accept::RelayAcceptor) — the RESPONDER path. Gated on: the
672 /// frame actually being a dialer's OPENING HANDSHAKE (below), the responder path being enabled
673 /// ([`enable_accept`](Self::enable_accept)), NON-CLOBBER (a circuit already under this key is kept,
674 /// never replaced by a racing registration — the #1536 double-session defense), and the flood cap
675 /// ([`MAX_RELAY_TUNNELS`]). The opening frame (the dialer's ClientHello) is delivered into the fresh
676 /// tunnel so the server handshake sees it; a full accept channel drops the newest circuit (bounded
677 /// backpressure).
678 ///
679 /// This is the ONE place a server-role circuit is created, so the mTLS role of a relayed circuit is
680 /// decided HERE and only here, from the circuit's own direction — never from what the accept path
681 /// happens to be handed.
682 fn accept_introduced(self: &Arc<Self>, from: &str, payload: Vec<u8>) {
683 // #1761: ONLY a client's opening handshake may open an inbound circuit. A relayed frame from a
684 // peer we hold no tunnel to is otherwise NOT the start of a circuit — it is a frame belonging to
685 // a circuit that no longer exists here, or one that was never ours. The whole class must be
686 // dropped, not just the observed instance: a peer's ServerHello or application record arriving
687 // after `fast_connect` released the per-peer tunnel on a relayed→direct promotion, a frame that
688 // outlived a timed-out or torn-down circuit, and any garbage an untrusted relay injects. Accepting
689 // any of them stands up a TLS SERVER against a peer that is itself a server, which is the live
690 // `got ServerHello when expecting ClientHello` / `UnexpectedMessage` deadlock — and it also let a
691 // single arbitrary byte cost a tunnel slot plus an accept-task.
692 if !is_tls_client_hello(&payload) {
693 // The relay-supplied `from` is deliberately NOT logged: it is an untrusted, unbounded
694 // peer-controlled string, and this line is reachable by any frame a hostile relay cares to
695 // send. The frame length is the only diagnostic that cannot carry attacker text.
696 tracing::debug!(
697 len = payload.len(),
698 "dropping a relayed frame that is not a dialer's opening handshake — no circuit is open \
699 for this peer"
700 );
701 return;
702 }
703 let Some(accept_tx) = self.inbound_accept.lock().unwrap().clone() else {
704 return;
705 };
706 let network_id = self
707 .local_network_id
708 .lock()
709 .unwrap()
710 .clone()
711 .unwrap_or_default();
712 let tunnel = {
713 let mut tunnels = self.tunnels.lock().unwrap();
714 if tunnels.contains_key(from) {
715 // A circuit to this peer already exists (a racing dial claimed the key) — do NOT
716 // clobber it into a conflicting second session.
717 return;
718 }
719 if tunnels.len() >= MAX_RELAY_TUNNELS {
720 tracing::debug!(
721 from,
722 "inbound relay accept cap reached — dropping introduced circuit"
723 );
724 return;
725 }
726 self.insert_entry(&mut tunnels, from, &network_id, TunnelRole::Server)
727 };
728 // Deliver the opening frame so the server-side handshake sees the ClientHello.
729 if let Some(sink) = self
730 .tunnels
731 .lock()
732 .unwrap()
733 .get(from)
734 .map(|e| e.sink.clone())
735 {
736 let _ = sink.try_send(payload);
737 }
738 // Hand the server-role tunnel to the consumer to run `PeerSession::server` over. A full/closed
739 // accept channel drops the tunnel here — its `Drop` deregisters the routing.
740 if accept_tx.try_send(tunnel).is_err() {
741 tracing::debug!(
742 from,
743 "inbound accept channel full/closed — dropping introduced circuit"
744 );
745 }
746 }
747
748 /// Remove a tunnel's routing entry (called on [`RelayTunnel`] drop) — but ONLY when the stored
749 /// entry is still THIS registration (`id` matches). A glare tie-break can replace our client
750 /// tunnel with a server tunnel under the same peer key (#1536); the old client tunnel's `Drop`
751 /// must not then evict the newer server entry.
752 fn close_tunnel(&self, target_peer: &str, id: u64) {
753 let mut tunnels = self.tunnels.lock().unwrap();
754 if tunnels.get(target_peer).map(|e| e.id) == Some(id) {
755 tunnels.remove(target_peer);
756 }
757 }
758
759 /// Whether a relayed tunnel to `target_peer` is currently registered — the test hook fast-connect
760 /// uses to assert the per-peer tunnel was released (dropped) after a relayed→direct promotion,
761 /// while the reservation itself stays held.
762 #[cfg(test)]
763 pub(crate) fn open_tunnel_exists(&self, target_peer: &str) -> bool {
764 self.tunnels.lock().unwrap().contains_key(target_peer)
765 }
766
767 /// Test-only: register a SERVER-role tunnel to `target_peer` directly, for tests that drive an mTLS
768 /// SERVER over a hand-wired relay tunnel (the production server path is [`enable_accept`] +
769 /// [`route_relayed`]'s accept branch). A server-role tunnel routes an incoming ClientHello straight
770 /// through instead of treating it as the #1536 glare signal, so these tests mirror a real server
771 /// receiving a dialer's ClientHello.
772 #[cfg(test)]
773 pub(crate) fn open_server_tunnel(
774 self: &Arc<Self>,
775 target_peer: &str,
776 network_id: &str,
777 ) -> RelayTunnel {
778 self.register_tunnel(target_peer, network_id, TunnelRole::Server)
779 }
780
781 /// A JSON snapshot for a `control.relayStatus`-style RPC. `state` is the canonical truth;
782 /// `connected` is a convenience boolean (== `state == connected`).
783 pub fn snapshot_json(&self, endpoint: &str, peer_id: &str) -> serde_json::Value {
784 let state = self.state();
785 serde_json::json!({
786 "state": state.as_str(),
787 "connected": state == RelayState::Connected,
788 "endpoint": endpoint,
789 "peer_id": peer_id,
790 "reconnect_attempts": self.reconnect_attempts.load(Ordering::Relaxed),
791 "connected_peers": self.connected_peers.load(Ordering::Relaxed),
792 "last_error": *self.last_error.lock().unwrap(),
793 })
794 }
795}
796
797/// A live RLY-002 relayed-transport tunnel to one peer, multiplexed over the node's persistent relay
798/// reservation socket (the tier-6 TURN fallback). Writes are framed as RLY-002 `relay_message` to the
799/// target and forwarded A→relay→B; reads are the payloads the relay forwards back from that peer.
800///
801/// Per NC-1 the payload MUST be END-TO-END SEALED to the recipient's key by the caller — the relay is
802/// an untrusted forwarder that sees only ciphertext. Dropping the tunnel deregisters its routing.
803pub struct RelayTunnel {
804 /// The remote peer's `peer_id` (hex) — the RLY-002 `to`, and the routing key for inbound frames.
805 target: String,
806 /// The network the tunnel is scoped to (echoed for the consumer; relay routes by peer_id).
807 network_id: String,
808 /// Shared status handle — provides the live outbound sink, this node's `peer_id`, and the seq.
809 status: Arc<RelayStatus>,
810 /// Inbound payloads the relay forwarded from `target`, in arrival order (bounded — see
811 /// [`RELAY_TUNNEL_INBOUND_CAP`]).
812 inbound: mpsc::Receiver<Vec<u8>>,
813 /// This registration's monotonic id — so `Drop` only deregisters when the map still holds THIS
814 /// entry (a glare replace may have superseded it under the same key; #1536).
815 id: u64,
816}
817
818impl RelayTunnel {
819 /// The remote peer this tunnel forwards to/from (hex `peer_id`).
820 pub fn target(&self) -> &str {
821 &self.target
822 }
823
824 /// The network the tunnel is scoped to.
825 pub fn network_id(&self) -> &str {
826 &self.network_id
827 }
828
829 /// Send `payload` to the target peer through the relay (RLY-002 `relay_message`). `payload` MUST
830 /// already be sealed to the recipient (NC-1). `Err` if the reservation dropped (send after the
831 /// session closed) or `payload` exceeds [`MAX_RELAY_PAYLOAD`].
832 pub fn send(&self, payload: Vec<u8>) -> Result<(), String> {
833 if payload.len() > MAX_RELAY_PAYLOAD {
834 return Err(format!(
835 "relayed payload {} exceeds cap {MAX_RELAY_PAYLOAD}",
836 payload.len()
837 ));
838 }
839 let from = self
840 .status
841 .local_peer_id
842 .lock()
843 .unwrap()
844 .clone()
845 .ok_or("relay reservation not connected — no local peer_id")?;
846 let seq = self.status.relay_seq.fetch_add(1, Ordering::Relaxed);
847 let frame = RelayMessage::RelayGossipMessage {
848 from,
849 to: self.target.clone(),
850 payload,
851 seq,
852 };
853 let guard = self.status.outbound.lock().unwrap();
854 let sink = guard
855 .as_ref()
856 .ok_or("relay reservation not connected — cannot send relayed frame")?;
857 sink.send(frame)
858 .map_err(|_| "relay reservation write half closed".to_string())
859 }
860
861 /// Await the next payload the relay forwards from the target peer. `None` once the reservation
862 /// drops (the session closed) — the caller should re-open the tunnel after the relay reconnects.
863 pub async fn recv(&mut self) -> Option<Vec<u8>> {
864 self.inbound.recv().await
865 }
866
867 /// Poll for the next inbound payload. This is the non-`async` primitive the
868 /// [`RelayTunnelStream`](crate::tunnel::RelayTunnelStream) `AsyncRead` adapter drives so an mTLS
869 /// session can run OVER the relay tunnel. `Poll::Ready(None)` once the reservation drops.
870 pub(crate) fn poll_recv(
871 &mut self,
872 cx: &mut std::task::Context<'_>,
873 ) -> std::task::Poll<Option<Vec<u8>>> {
874 self.inbound.poll_recv(cx)
875 }
876}
877
878impl Drop for RelayTunnel {
879 fn drop(&mut self) {
880 self.status.close_tunnel(&self.target, self.id);
881 }
882}
883
884/// Resolve the relay endpoint: `DIG_RELAY_URL` if set + non-empty (and not the opt-out token), else
885/// the canonical [`dig_constants::DIG_RELAY_URL`].
886pub fn relay_url_from_env() -> String {
887 std::env::var("DIG_RELAY_URL")
888 .ok()
889 .filter(|s| !s.trim().is_empty())
890 .filter(|s| !is_off_token(s))
891 .unwrap_or_else(|| dig_constants::DIG_RELAY_URL.to_string())
892}
893
894/// Whether the relay connection is enabled. Disabled when `DIG_RELAY_URL` is `off`/`disabled`/
895/// empty-after-trim — an explicit opt-out for air-gapped/standalone nodes.
896pub fn relay_enabled() -> bool {
897 match std::env::var("DIG_RELAY_URL") {
898 Ok(v) => !is_off_token(&v),
899 Err(_) => true,
900 }
901}
902
903/// `true` if `v` is the reservation opt-out token (`off`/`disabled`, case-insensitive, trimmed).
904fn is_off_token(v: &str) -> bool {
905 let v = v.trim();
906 v.eq_ignore_ascii_case("off") || v.eq_ignore_ascii_case("disabled")
907}
908
909/// Current unix time (seconds), saturating.
910fn now_secs() -> u64 {
911 std::time::SystemTime::now()
912 .duration_since(std::time::UNIX_EPOCH)
913 .map(|d| d.as_secs())
914 .unwrap_or(0)
915}
916
917/// Maintain a CONSTANT relay reservation forever: connect, register, keepalive, and on any drop
918/// reconnect with capped exponential backoff. Spawned as a background task; tolerates the relay
919/// being down (retries forever, never crashes). `peer_id` is the node's stable identity hex.
920pub async fn run_relay_connection(
921 endpoint: String,
922 peer_id: String,
923 network_id: String,
924 listen_addrs: Vec<SocketAddr>,
925 status: Arc<RelayStatus>,
926) {
927 run_relay_connection_with(
928 endpoint,
929 peer_id,
930 network_id,
931 listen_addrs,
932 status,
933 Backoff::default(),
934 )
935 .await
936}
937
938/// [`run_relay_connection`] with an explicit backoff schedule (tests pass tiny values for fast,
939/// deterministic reconnect timing; the LOGIC is identical — only the sleep durations differ).
940pub async fn run_relay_connection_with(
941 endpoint: String,
942 peer_id: String,
943 network_id: String,
944 listen_addrs: Vec<SocketAddr>,
945 status: Arc<RelayStatus>,
946 backoff: Backoff,
947) {
948 let mut consecutive_failures: u32 = 0;
949 loop {
950 status.set_connecting();
951 match connect_once(&endpoint, &peer_id, &network_id, &listen_addrs, &status).await {
952 Ok(()) => {
953 consecutive_failures = 0;
954 status.set_disconnected(None);
955 }
956 Err(e) => {
957 consecutive_failures = consecutive_failures.saturating_add(1);
958 status.set_disconnected(Some(e));
959 }
960 }
961 // ALWAYS sleep a bounded backoff before retrying — prevents a busy error loop.
962 let delay = backoff_secs_with(consecutive_failures, backoff.base_secs, backoff.cap_secs);
963 tokio::time::sleep(Duration::from_secs(delay)).await;
964 }
965}
966
967/// A relay WebSocket endpoint parsed into the pieces the happy-eyeballs dial needs: the host to
968/// resolve and the TCP port. The scheme (`ws`/`wss`) only selects the default port here — the
969/// plaintext-vs-TLS choice is re-derived from the URL by [`client_async_tls_with_config`] during the
970/// handshake, so a single code path serves both.
971#[derive(Debug, PartialEq, Eq)]
972struct RelayEndpoint {
973 host: String,
974 port: u16,
975}
976
977/// Parse a relay endpoint URL (`ws://host[:port][/path]` / `wss://host[:port][/path]`, IPv6 hosts in
978/// `[…]`) into its host + port. Only the authority is needed for the dial; any path/query/fragment and
979/// userinfo are ignored (the full URL is still handed to the WS handshake for the correct `Host`/SNI).
980fn parse_relay_endpoint(endpoint: &str) -> Result<RelayEndpoint, String> {
981 let (scheme, rest) = endpoint
982 .split_once("://")
983 .ok_or_else(|| format!("relay endpoint missing scheme: {endpoint}"))?;
984 let default_port = match scheme.to_ascii_lowercase().as_str() {
985 "ws" => 80,
986 "wss" => 443,
987 other => return Err(format!("unsupported relay scheme: {other}")),
988 };
989 // Authority only: drop any path/query/fragment, then any `userinfo@`.
990 let authority = rest.split(['/', '?', '#']).next().unwrap_or(rest);
991 let authority = authority
992 .rsplit_once('@')
993 .map(|(_, h)| h)
994 .unwrap_or(authority);
995
996 let (host, port) = if let Some(stripped) = authority.strip_prefix('[') {
997 // Bracketed IPv6 literal: `[addr]` or `[addr]:port`.
998 let (h, after) = stripped
999 .split_once(']')
1000 .ok_or_else(|| format!("malformed IPv6 authority: {authority}"))?;
1001 let port = match after.strip_prefix(':') {
1002 Some(p) => p.parse().map_err(|_| format!("bad relay port: {after}"))?,
1003 None => default_port,
1004 };
1005 (h.to_string(), port)
1006 } else if let Some((h, p)) = authority.rsplit_once(':') {
1007 (
1008 h.to_string(),
1009 p.parse().map_err(|_| format!("bad relay port: {p}"))?,
1010 )
1011 } else {
1012 (authority.to_string(), default_port)
1013 };
1014
1015 if host.is_empty() {
1016 return Err(format!("relay endpoint missing host: {endpoint}"));
1017 }
1018 Ok(RelayEndpoint { host, port })
1019}
1020
1021/// Resolve a relay host to its family-tagged dial candidates: a literal IP yields one candidate (no
1022/// DNS), a hostname is resolved to its full A + AAAA set. The candidates feed `dig_ip::connect`, which
1023/// applies the §5.2 IPv6-first preference + local∩peer family intersection, so no ordering is imposed
1024/// here — the addresses are added as resolved and tagged by family for observability.
1025async fn resolve_relay_candidates(host: &str, port: u16) -> Result<PeerCandidates, String> {
1026 let mut candidates = PeerCandidates::new();
1027 let source_for = |ip: &IpAddr| {
1028 if ip.is_ipv6() {
1029 CandidateSource::DnsAAAA
1030 } else {
1031 CandidateSource::DnsA
1032 }
1033 };
1034 if let Ok(ip) = host.parse::<IpAddr>() {
1035 candidates.add(SocketAddr::new(ip, port), source_for(&ip));
1036 } else {
1037 let resolved = tokio::net::lookup_host((host, port))
1038 .await
1039 .map_err(|e| format!("resolve {host}:{port}: {e}"))?;
1040 for addr in resolved {
1041 candidates.add(addr, source_for(&addr.ip()));
1042 }
1043 }
1044 if candidates.is_empty() {
1045 return Err(format!("no addresses resolved for {host}:{port}"));
1046 }
1047 Ok(candidates)
1048}
1049
1050/// Race the relay `candidates` IPv6-first with graceful IPv4 fallback via `dig_ip::connect` (§5.2,
1051/// RFC 8305). The transport connect stays a caller-supplied closure so the racing logic is unit-tested
1052/// with a fake dial (no real DNS/sockets) exactly as the direct-peer dialer does in `dialer.rs`; the
1053/// production caller ([`open_relay_ws`]) hands it a real [`TcpStream::connect`].
1054async fn race_relay_candidates<C, F, Fut>(
1055 local: &LocalStack,
1056 candidates: &PeerCandidates,
1057 config: DialConfig,
1058 dial_fn: F,
1059) -> Result<dig_ip::DialWinner<C>, String>
1060where
1061 F: Fn(SocketAddr) -> Fut + Sync,
1062 Fut: std::future::Future<Output = Result<C, String>> + Send,
1063 C: Send,
1064{
1065 dig_ip::connect(local, candidates, config, dial_fn)
1066 .await
1067 .map_err(|e| format!("relay happy-eyeballs dial: {e}"))
1068}
1069
1070/// Open the relay WebSocket over an IPv6-first happy-eyeballs TCP race (§5.2), matching the direct-peer
1071/// dial path in `dialer.rs`: resolve the endpoint host to its A + AAAA candidates, race the TCP connect
1072/// via `dig_ip::connect` (IPv6-first, fast IPv4 fallback), then run the WS handshake over the WINNING
1073/// socket — TLS-over-that-stream for `wss://`, plaintext for `ws://` (the mode is taken from the URL by
1074/// [`client_async_tls_with_config`]). Replaces `tokio_tungstenite::connect_async`, whose sequential,
1075/// single-family resolve-and-connect contradicted the IPv6-first reservation guarantee.
1076async fn open_relay_ws(
1077 endpoint: &str,
1078) -> Result<WebSocketStream<MaybeTlsStream<TcpStream>>, String> {
1079 let parsed = parse_relay_endpoint(endpoint)?;
1080 let candidates = resolve_relay_candidates(&parsed.host, parsed.port).await?;
1081 let local = LocalStack::cached();
1082 let winner = race_relay_candidates(
1083 &local,
1084 &candidates,
1085 DialConfig::default(),
1086 |addr| async move {
1087 TcpStream::connect(addr)
1088 .await
1089 .map_err(|e| format!("tcp connect {addr}: {e}"))
1090 },
1091 )
1092 .await?;
1093 let (ws, _resp) = client_async_tls_with_config(endpoint, winner.conn, None, None)
1094 .await
1095 .map_err(|e| format!("ws handshake: {e}"))?;
1096 Ok(ws)
1097}
1098
1099/// One connect → register → serve cycle. Returns `Ok` on a clean close, `Err(reason)` on failure.
1100async fn connect_once(
1101 endpoint: &str,
1102 peer_id: &str,
1103 network_id: &str,
1104 listen_addrs: &[SocketAddr],
1105 status: &Arc<RelayStatus>,
1106) -> Result<(), String> {
1107 // Each session's discovered-peer set + transport are independent — never carry state across a
1108 // drop. `clear_transport` also runs at the end so a dropped session's tunnels/sink never linger.
1109 status.clear_known_peers();
1110 status.clear_transport();
1111
1112 let ws = open_relay_ws(endpoint).await?;
1113 let (mut write, mut read) = ws.split();
1114
1115 // RLY-001: register immediately so the relay holds our reservation, advertising the node's gossip
1116 // listen candidates (B1) so the relay can hand other peers a dialable candidate (§5.2 IPv6-first).
1117 let register = RelayMessage::Register {
1118 peer_id: peer_id.to_string(),
1119 network_id: network_id.to_string(),
1120 protocol_version: RELAY_PROTOCOL_VERSION,
1121 listen_addrs: listen_addrs.to_vec(),
1122 };
1123 send(&mut write, ®ister).await?;
1124
1125 // Publish the outbound sink so RLY-002 relayed tunnels can reuse THIS persistent socket. Drained
1126 // in the select loop below; cleared when the session ends.
1127 let (out_tx, mut out_rx) = mpsc::unbounded_channel::<RelayMessage>();
1128 status.set_transport(peer_id, network_id, out_tx);
1129
1130 // RLY-005: pull the current peer list right away, then again periodically — all over THIS
1131 // persistent socket, so discovery never requires reopening a connection.
1132 let get_peers = RelayMessage::GetPeers {
1133 network_id: Some(network_id.to_string()),
1134 };
1135 send(&mut write, &get_peers).await?;
1136
1137 let mut ping = tokio::time::interval(Duration::from_secs(PING_INTERVAL_SECS));
1138 ping.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
1139 ping.tick().await; // skip the immediate first tick
1140
1141 let mut discovery = tokio::time::interval(Duration::from_secs(DISCOVERY_INTERVAL_SECS));
1142 discovery.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
1143 discovery.tick().await; // skip the immediate first tick (we already pulled once above)
1144
1145 // Run the session; whatever the outcome, tear the transport down so a dropped session never
1146 // leaves a stale outbound sink or open tunnels behind (they'd send into a closed socket).
1147 let result = serve_session(
1148 &mut write,
1149 &mut read,
1150 &mut ping,
1151 &mut discovery,
1152 &mut out_rx,
1153 network_id,
1154 status,
1155 )
1156 .await;
1157 status.clear_transport();
1158 result
1159}
1160
1161/// The connected-session select loop: keepalive pings, periodic RLY-005 discovery, draining the
1162/// outbound relayed-transport sink onto the socket, and handling inbound frames. Returns `Ok` on a
1163/// clean close, `Err(reason)` on a failure. Split out of `connect_once` so its caller can always run
1164/// transport teardown regardless of how the session ends.
1165#[allow(clippy::too_many_arguments)]
1166async fn serve_session<W, R>(
1167 write: &mut W,
1168 read: &mut R,
1169 ping: &mut tokio::time::Interval,
1170 discovery: &mut tokio::time::Interval,
1171 out_rx: &mut mpsc::UnboundedReceiver<RelayMessage>,
1172 network_id: &str,
1173 status: &Arc<RelayStatus>,
1174) -> Result<(), String>
1175where
1176 W: SinkExt<Message> + Unpin,
1177 <W as futures_util::Sink<Message>>::Error: std::fmt::Display,
1178 R: StreamExt<Item = Result<Message, tokio_tungstenite::tungstenite::Error>> + Unpin,
1179{
1180 loop {
1181 tokio::select! {
1182 _ = ping.tick() => {
1183 send(write, &RelayMessage::Ping { timestamp: now_secs() }).await?;
1184 }
1185 _ = discovery.tick() => {
1186 send(write, &RelayMessage::GetPeers {
1187 network_id: Some(network_id.to_string()),
1188 }).await?;
1189 }
1190 // A relayed tunnel queued an RLY-002 frame — forward it over THIS persistent socket.
1191 Some(frame) = out_rx.recv() => {
1192 send(write, &frame).await?;
1193 }
1194 frame = read.next() => {
1195 match frame {
1196 None => return Ok(()),
1197 Some(Err(e)) => return Err(format!("read: {e}")),
1198 Some(Ok(Message::Close(_))) => return Ok(()),
1199 Some(Ok(Message::Ping(p))) => {
1200 write.send(Message::Pong(p)).await.map_err(|e| format!("pong: {e}"))?;
1201 }
1202 Some(Ok(Message::Pong(_))) | Some(Ok(Message::Frame(_))) => {}
1203 Some(Ok(Message::Text(t))) => {
1204 handle_incoming(t.into_bytes(), write, status).await?;
1205 }
1206 Some(Ok(Message::Binary(b))) => {
1207 handle_incoming(b, write, status).await?;
1208 }
1209 }
1210 }
1211 }
1212 }
1213}
1214
1215/// Handle one decoded inbound relay frame: track RegisterAck (→ connected), answer relay Pings.
1216async fn handle_incoming<W>(
1217 bytes: Vec<u8>,
1218 write: &mut W,
1219 status: &Arc<RelayStatus>,
1220) -> Result<(), String>
1221where
1222 W: SinkExt<Message> + Unpin,
1223 <W as futures_util::Sink<Message>>::Error: std::fmt::Display,
1224{
1225 let Ok(msg) = serde_json::from_slice::<RelayMessage>(&bytes) else {
1226 return Ok(()); // ignore anything we can't parse; the relay is untrusted
1227 };
1228 match msg {
1229 RelayMessage::RegisterAck {
1230 success,
1231 message,
1232 connected_peers,
1233 } => {
1234 if success {
1235 status.set_connected(connected_peers as u64);
1236 } else {
1237 return Err(format!("register rejected: {message}"));
1238 }
1239 }
1240 RelayMessage::Ping { timestamp } => {
1241 send(write, &RelayMessage::Pong { timestamp }).await?;
1242 }
1243 // RLY-005 + push notices: fold peers discovered over the live socket into the status so the
1244 // consumer's pool/address book sees them without opening an ephemeral discovery connection.
1245 RelayMessage::Peers { peers } => status.replace_known_peers(peers),
1246 RelayMessage::PeerConnected { peer } => status.add_known_peer(peer),
1247 RelayMessage::PeerDisconnected { peer_id } => status.remove_known_peer(&peer_id),
1248 // RLY-002 relayed transport (tier-6 TURN): route a payload the relay forwarded from `from` to
1249 // that peer's open tunnel. Unknown-peer / oversized / full-channel frames are dropped inside
1250 // `route_relayed` (untrusted-relay defense). `to`/`seq` are the relay's concern; we key on
1251 // `from`. Per NC-1 `payload` is sealed ciphertext the relay could not read.
1252 RelayMessage::RelayGossipMessage { from, payload, .. } => {
1253 status.route_relayed(&from, payload)
1254 }
1255 RelayMessage::Error { code, message } => {
1256 return Err(format!("relay error {code}: {message}"));
1257 }
1258 other => tracing::debug!(?other, "relay message ignored by reservation loop"),
1259 }
1260 Ok(())
1261}
1262
1263/// Wire two in-memory relay reservations to forward RLY-002 frames to each OTHER — a loopback relay
1264/// with no real network. Each returned [`RelayStatus`] is `Connected` with a live outbound sink whose
1265/// frames are routed into the peer's tunnels by `from` peer_id, exactly as a real relay would forward
1266/// A→relay→B. Used to prove a full mTLS session round-trips over [`RelayTunnel`]s (see `tunnel.rs`).
1267///
1268/// `a` opens tunnels targeting `b_id`; `b` opens tunnels targeting `a_id`.
1269#[cfg(test)]
1270pub(crate) fn loopback_reservation_pair(
1271 a_id: &str,
1272 b_id: &str,
1273) -> (Arc<RelayStatus>, Arc<RelayStatus>) {
1274 let a = RelayStatus::new();
1275 let b = RelayStatus::new();
1276 a.set_connected(1);
1277 b.set_connected(1);
1278
1279 let (a_tx, mut a_rx) = mpsc::unbounded_channel::<RelayMessage>();
1280 let (b_tx, mut b_rx) = mpsc::unbounded_channel::<RelayMessage>();
1281 a.set_transport(a_id, DEFAULT_NETWORK_ID, a_tx);
1282 b.set_transport(b_id, DEFAULT_NETWORK_ID, b_tx);
1283
1284 // Drain a's outbound → forward into b (route by `from`), and symmetrically b → a. This is the
1285 // relay's forwarding role, in-process.
1286 let b_route = Arc::clone(&b);
1287 tokio::spawn(async move {
1288 while let Some(RelayMessage::RelayGossipMessage { from, payload, .. }) = a_rx.recv().await {
1289 b_route.route_relayed(&from, payload);
1290 }
1291 });
1292 let a_route = Arc::clone(&a);
1293 tokio::spawn(async move {
1294 while let Some(RelayMessage::RelayGossipMessage { from, payload, .. }) = b_rx.recv().await {
1295 a_route.route_relayed(&from, payload);
1296 }
1297 });
1298
1299 (a, b)
1300}
1301
1302/// Serialize + send one `RelayMessage` as a WebSocket text frame.
1303async fn send<W>(write: &mut W, msg: &RelayMessage) -> Result<(), String>
1304where
1305 W: SinkExt<Message> + Unpin,
1306 <W as futures_util::Sink<Message>>::Error: std::fmt::Display,
1307{
1308 let txt = serde_json::to_string(msg).map_err(|e| format!("encode: {e}"))?;
1309 write
1310 .send(Message::Text(txt))
1311 .await
1312 .map_err(|e| format!("send: {e}"))
1313}
1314
1315#[cfg(test)]
1316mod tests {
1317 use super::*;
1318 use dig_ip::Family;
1319 use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering};
1320 use std::sync::Mutex as StdMutex;
1321
1322 #[test]
1323 fn parses_wss_host_and_explicit_port() {
1324 let ep = parse_relay_endpoint("wss://relay.dig.net:443").unwrap();
1325 assert_eq!(ep.host, "relay.dig.net");
1326 assert_eq!(ep.port, 443);
1327 }
1328
1329 #[test]
1330 fn parses_default_ports_by_scheme() {
1331 assert_eq!(
1332 parse_relay_endpoint("wss://relay.dig.net").unwrap().port,
1333 443
1334 );
1335 assert_eq!(parse_relay_endpoint("ws://relay.dig.net").unwrap().port, 80);
1336 }
1337
1338 #[test]
1339 fn parses_bracketed_ipv6_authority_with_and_without_port() {
1340 let with_port = parse_relay_endpoint("wss://[2001:db8::1]:8443").unwrap();
1341 assert_eq!(with_port.host, "2001:db8::1");
1342 assert_eq!(with_port.port, 8443);
1343 let no_port = parse_relay_endpoint("wss://[2001:db8::1]/ws").unwrap();
1344 assert_eq!(no_port.host, "2001:db8::1");
1345 assert_eq!(no_port.port, 443);
1346 }
1347
1348 #[test]
1349 fn ignores_path_query_and_userinfo() {
1350 let ep = parse_relay_endpoint("wss://user@relay.dig.net:9443/ws?x=1#f").unwrap();
1351 assert_eq!(ep.host, "relay.dig.net");
1352 assert_eq!(ep.port, 9443);
1353 }
1354
1355 #[test]
1356 fn rejects_malformed_endpoints() {
1357 assert!(parse_relay_endpoint("relay.dig.net:443").is_err()); // no scheme
1358 assert!(parse_relay_endpoint("http://relay.dig.net").is_err()); // wrong scheme
1359 assert!(parse_relay_endpoint("wss://relay.dig.net:notaport").is_err());
1360 }
1361
1362 #[tokio::test]
1363 async fn resolve_relay_candidates_handles_ip_literals_without_dns() {
1364 let v6 = resolve_relay_candidates("2001:db8::1", 443).await.unwrap();
1365 assert_eq!(v6.all().len(), 1);
1366 assert_eq!(v6.all()[0].family, Family::V6);
1367 assert_eq!(v6.all()[0].source, CandidateSource::DnsAAAA);
1368
1369 let v4 = resolve_relay_candidates("203.0.113.7", 443).await.unwrap();
1370 assert_eq!(v4.all()[0].family, Family::V4);
1371 assert_eq!(v4.all()[0].source, CandidateSource::DnsA);
1372 }
1373
1374 /// The relay dial races BOTH families and falls back to IPv4 when the IPv6 candidate is dead —
1375 /// the §5.2 happy-eyeballs guarantee, proven with a FAKE dial closure (no real DNS/sockets). A
1376 /// dead IPv6 candidate + a live IPv4 candidate on a dual-stack host must yield the IPv4 winner,
1377 /// and BOTH families must have been attempted.
1378 #[tokio::test]
1379 async fn relay_dial_races_both_families_and_falls_back_to_ipv4() {
1380 let mut candidates = PeerCandidates::new();
1381 let v6: SocketAddr = "[2001:db8::1]:443".parse().unwrap();
1382 let v4: SocketAddr = "203.0.113.7:443".parse().unwrap();
1383 candidates.add(v6, CandidateSource::DnsAAAA);
1384 candidates.add(v4, CandidateSource::DnsA);
1385
1386 let dual = LocalStack::from_flags(true, true);
1387 let attempted: StdMutex<Vec<SocketAddr>> = StdMutex::new(Vec::new());
1388 // Fast attempt-delay so the hedged IPv4 starts promptly once the IPv6 attempt fails.
1389 let cfg = DialConfig {
1390 per_attempt_timeout: Duration::from_secs(1),
1391 attempt_delay: Duration::from_millis(5),
1392 };
1393
1394 let winner = race_relay_candidates(&dual, &candidates, cfg, |addr| {
1395 let attempted = &attempted;
1396 async move {
1397 attempted.lock().unwrap().push(addr);
1398 if addr.is_ipv6() {
1399 Err(format!("simulated dead IPv6 {addr}"))
1400 } else {
1401 Ok(addr) // the fake "connection" is just the address that won
1402 }
1403 }
1404 })
1405 .await
1406 .expect("IPv4 fallback wins when IPv6 is dead");
1407
1408 assert_eq!(winner.conn, v4, "the live IPv4 candidate won");
1409 assert_eq!(winner.family, Family::V4);
1410 let tried = attempted.lock().unwrap();
1411 assert!(
1412 tried.contains(&v6),
1413 "the IPv6 candidate was attempted first"
1414 );
1415 assert!(
1416 tried.contains(&v4),
1417 "the IPv4 candidate was attempted as fallback"
1418 );
1419 }
1420
1421 /// IPv6 is the PREFERENCE, not merely first-attempted: with both families live on a dual-stack
1422 /// host, the IPv6 candidate wins the race (IPv4 is only a fallback).
1423 #[tokio::test]
1424 async fn relay_dial_prefers_ipv6_when_both_live() {
1425 let mut candidates = PeerCandidates::new();
1426 let v6: SocketAddr = "[2001:db8::2]:443".parse().unwrap();
1427 let v4: SocketAddr = "203.0.113.8:443".parse().unwrap();
1428 candidates.add(v6, CandidateSource::DnsAAAA);
1429 candidates.add(v4, CandidateSource::DnsA);
1430
1431 let dual = LocalStack::from_flags(true, true);
1432 let calls = AtomicUsize::new(0);
1433 let winner = race_relay_candidates(&dual, &candidates, DialConfig::default(), |addr| {
1434 calls.fetch_add(1, AtomicOrdering::Relaxed);
1435 async move { Ok::<SocketAddr, String>(addr) }
1436 })
1437 .await
1438 .unwrap();
1439
1440 assert_eq!(winner.conn, v6, "IPv6 preferred when both are viable");
1441 assert_eq!(winner.family, Family::V6);
1442 }
1443
1444 /// A minimal TLS handshake record whose first message is a ClientHello — the ONLY frame that opens
1445 /// an introduced circuit (#1761), so every test that drives the responder path must send this shape.
1446 fn client_hello_frame() -> Vec<u8> {
1447 vec![0x16, 0x03, 0x01, 0x00, 0x05, 0x01, 0, 0, 0, 0]
1448 }
1449
1450 /// Build a `Connected` status with a live (dummy) outbound sink + local identity, so
1451 /// `route_relayed`'s introduced-circuit path can run without a real relay socket.
1452 fn connected_status(local_id: &str) -> Arc<RelayStatus> {
1453 let status = RelayStatus::new();
1454 status.set_connected(1);
1455 let (out_tx, _out_rx) = mpsc::unbounded_channel::<RelayMessage>();
1456 status.set_transport(local_id, DEFAULT_NETWORK_ID, out_tx);
1457 // These tests exercise the introduced-circuit ROUTING (register/accept/drop), which never uses
1458 // the outbound sink, so the receiver may drop at end of scope — the sink stays `Some`.
1459 status
1460 }
1461
1462 /// SECURITY (accept OFF): with NO responder path enabled, an introduced RLY-002 frame from an
1463 /// unknown peer is DROPPED — no tunnel is created and nothing is surfaced. This is the
1464 /// untrusted-relay default: a node that never opted into accepting relayed circuits cannot be made
1465 /// to spawn one by a hostile relay.
1466 #[test]
1467 fn introduced_frame_dropped_when_accept_disabled() {
1468 let status = connected_status("00aa");
1469 // A ClientHello-shaped frame from a peer we hold NO tunnel to — the introduced-circuit trigger.
1470 status.route_relayed("ffbb", client_hello_frame());
1471 assert!(
1472 !status.open_tunnel_exists("ffbb"),
1473 "no tunnel surfaced for an introduced circuit while accept is off"
1474 );
1475 assert_eq!(
1476 status.tunnels.lock().unwrap().len(),
1477 0,
1478 "accept-off drops the introduced circuit entirely"
1479 );
1480 }
1481
1482 /// SECURITY (flood defense, tunnel cap): once [`MAX_RELAY_TUNNELS`] tunnels are open, a further
1483 /// introduced circuit from a new peer is DROPPED rather than registered — a hostile relay flooding
1484 /// distinct fabricated `from` ids cannot spawn unbounded server tunnels/accept-tasks.
1485 #[test]
1486 fn introduced_circuit_dropped_at_max_tunnels_cap() {
1487 let status = connected_status("00aa");
1488 let mut accept_rx = status.enable_accept();
1489 // Saturate the tunnel table at the cap (held open by the returned RelayTunnels).
1490 let mut held = Vec::new();
1491 for i in 0..MAX_RELAY_TUNNELS {
1492 held.push(status.register_tunnel(
1493 &format!("peer{i:05}"),
1494 DEFAULT_NETWORK_ID,
1495 TunnelRole::Server,
1496 ));
1497 }
1498 assert_eq!(status.tunnels.lock().unwrap().len(), MAX_RELAY_TUNNELS);
1499
1500 status.route_relayed("overflowpeer", client_hello_frame());
1501 assert!(
1502 !status.open_tunnel_exists("overflowpeer"),
1503 "an introduced circuit beyond the tunnel cap is dropped, not registered"
1504 );
1505 assert_eq!(
1506 status.tunnels.lock().unwrap().len(),
1507 MAX_RELAY_TUNNELS,
1508 "tunnel count never grows past the cap"
1509 );
1510 assert!(
1511 accept_rx.try_recv().is_err(),
1512 "the capped circuit is never surfaced to the acceptor"
1513 );
1514 drop(held);
1515 }
1516
1517 /// SECURITY (flood defense, accept channel): when the bounded inbound-accept channel
1518 /// ([`INBOUND_ACCEPT_CAP`]) is full — the consumer is not accepting fast enough — a further
1519 /// introduced circuit is DROPPED (its freshly-registered tunnel is torn down), bounded
1520 /// backpressure rather than unbounded queueing.
1521 #[test]
1522 fn introduced_circuit_dropped_when_accept_channel_full() {
1523 let status = connected_status("00aa");
1524 let mut accept_rx = status.enable_accept();
1525 // Fill the accept channel to capacity WITHOUT draining it — each surfaced circuit occupies one
1526 // slot and keeps its server tunnel registered (the RelayTunnel lives in the channel).
1527 for i in 0..INBOUND_ACCEPT_CAP {
1528 status.route_relayed(&format!("in{i:05}"), client_hello_frame());
1529 }
1530 assert_eq!(
1531 status.tunnels.lock().unwrap().len(),
1532 INBOUND_ACCEPT_CAP,
1533 "each surfaced circuit registered exactly one server tunnel"
1534 );
1535
1536 // One more: the accept channel is full → the tunnel is registered then immediately dropped,
1537 // so its routing is deregistered and nothing new is surfaced.
1538 status.route_relayed("overflow", client_hello_frame());
1539 assert!(
1540 !status.open_tunnel_exists("overflow"),
1541 "an introduced circuit is dropped when the accept channel is full"
1542 );
1543
1544 let mut surfaced = 0;
1545 while accept_rx.try_recv().is_ok() {
1546 surfaced += 1;
1547 }
1548 assert_eq!(
1549 surfaced, INBOUND_ACCEPT_CAP,
1550 "exactly the channel capacity surfaced — never the overflow circuit"
1551 );
1552 }
1553
1554 /// REGRESSION (#1536 glare, TIMING order): a peer's ClientHello arrives BEFORE our own dial to it
1555 /// registers. We accept it as a server; our later dial to the SAME peer MUST then be refused
1556 /// (non-clobber) so no conflicting second circuit / double mTLS session is created. This is the
1557 /// deeper ordering the first tie-break missed (role was decided by who-registered-first).
1558 #[test]
1559 fn clienthello_before_local_dial_does_not_double_register() {
1560 let status = connected_status("bbbb");
1561 let mut accept_rx = status.enable_accept();
1562
1563 // The peer's introduced ClientHello arrives first → we accept it as a server-role circuit.
1564 status.route_relayed("aaaa", client_hello_frame());
1565 assert!(status.open_tunnel_exists("aaaa"));
1566 let _server_tunnel = accept_rx
1567 .try_recv()
1568 .expect("introduced circuit surfaced as a server");
1569
1570 // Our own dial to that peer now must be REFUSED — the existing circuit is the connection.
1571 let dial = status.open_tunnel("aaaa", DEFAULT_NETWORK_ID);
1572 assert!(
1573 dial.is_err(),
1574 "a second dial to a peer we already serve is refused (no double-session)"
1575 );
1576 assert_eq!(
1577 status.tunnels.lock().unwrap().len(),
1578 1,
1579 "exactly one circuit per peer — never a conflicting client+server pair"
1580 );
1581 }
1582
1583 /// REGRESSION (#1536 equal-id): a relayed self-dial, or a frame stamped with our OWN peer_id
1584 /// (theoretical SPKI collision / a hostile relay reflecting our id), has no lower/higher end for
1585 /// the tie-break — it MUST be rejected outright, never producing a no-server hang.
1586 #[test]
1587 fn self_dial_and_self_stamped_frame_rejected() {
1588 let status = connected_status("cccc");
1589 assert!(
1590 status.open_tunnel("cccc", DEFAULT_NETWORK_ID).is_err(),
1591 "a relayed self-dial (target == local id) is refused"
1592 );
1593
1594 let mut accept_rx = status.enable_accept();
1595 status.route_relayed("cccc", client_hello_frame());
1596 assert!(
1597 !status.open_tunnel_exists("cccc"),
1598 "a frame stamped with our own id is dropped, never registered"
1599 );
1600 assert!(
1601 accept_rx.try_recv().is_err(),
1602 "a self-stamped frame is never surfaced as an accept"
1603 );
1604 }
1605
1606 /// SECURITY (#1536 relay-injected-ClientHello DoS): an untrusted relay can inject a bogus
1607 /// ClientHello on a lower-id node's client tunnel to force it to yield its outbound dial to a
1608 /// server accept that no real peer completes. mTLS identity is never bypassed, but the outbound
1609 /// dial MUST NOT be permanently lost — once the bogus (never-completing) server circuit is dropped,
1610 /// the peer key frees and a fresh dial is possible.
1611 #[test]
1612 fn injected_clienthello_yield_does_not_permanently_block_redial() {
1613 // local id "00aa" is numerically lower than the peer "ffff", so we are the yield-to-server side.
1614 let status = connected_status("00aa");
1615 let mut accept_rx = status.enable_accept();
1616
1617 let client_tunnel = status
1618 .open_tunnel("ffff", DEFAULT_NETWORK_ID)
1619 .expect("outbound relayed dial opens");
1620 assert!(status.open_tunnel_exists("ffff"));
1621
1622 // The relay injects a bogus ClientHello with from=ffff → glare on our client tunnel → we (the
1623 // lower id) yield: drop the client tunnel, surface a server accept.
1624 status.route_relayed("ffff", client_hello_frame());
1625 let server_tunnel = accept_rx
1626 .try_recv()
1627 .expect("the injected ClientHello yielded a server accept");
1628
1629 // The original outbound dial is cancelled; dropping its handle must NOT evict the newer server
1630 // entry (generation-id guard).
1631 drop(client_tunnel);
1632 assert!(
1633 status.open_tunnel_exists("ffff"),
1634 "the server circuit survives the cancelled client dial's drop"
1635 );
1636
1637 // The bogus circuit completes no handshake; once its tunnel is dropped the key frees...
1638 drop(server_tunnel);
1639 assert!(
1640 !status.open_tunnel_exists("ffff"),
1641 "dropping the never-completing server circuit releases the peer key"
1642 );
1643 // ...and a fresh dial is possible — no permanent lockout from the injected frame.
1644 assert!(
1645 status.open_tunnel("ffff", DEFAULT_NETWORK_ID).is_ok(),
1646 "a fresh outbound dial succeeds after the bogus injection is cleaned up"
1647 );
1648 }
1649
1650 /// REGRESSION (#1761): only a client's OPENING HANDSHAKE may create a server-role circuit.
1651 ///
1652 /// A relayed frame from a peer we hold no tunnel to used to be accepted as an introduced circuit
1653 /// WHATEVER its content, so any frame that is not a dialer's ClientHello manufactured a bogus
1654 /// server-role circuit — and the mTLS server behind it then failed with the live
1655 /// `got ServerHello when expecting ClientHello` (both ends running the TLS server role).
1656 ///
1657 /// The frames covered here are the whole CLASS of "not the start of an inbound circuit", each with
1658 /// a real production or adversarial origin: a peer's ServerHello or application record arriving
1659 /// after we released our client tunnel (a `fast_connect` relayed→direct promotion drops the
1660 /// per-peer tunnel while the peer's frames are still in flight), a truncated record too short to
1661 /// classify, and arbitrary relay garbage. The final ClientHello is the truthful CONTROL: the
1662 /// responder path still works, and dropping the stray frames never blacklists the peer.
1663 #[test]
1664 fn only_a_clienthello_creates_an_introduced_circuit() {
1665 let status = connected_status("00aa");
1666 let mut accept_rx = status.enable_accept();
1667
1668 // A TLS handshake record whose message type is ServerHello (0x02) — the live #1761 frame.
1669 let server_hello = vec![0x16, 0x03, 0x03, 0x00, 0x05, 0x02, 0, 0, 0, 0];
1670 // A TLS application-data record (0x17) — a mid-session frame from a released tunnel.
1671 let app_data = vec![0x17, 0x03, 0x03, 0x00, 0x04, 0xde, 0xad, 0xbe, 0xef];
1672 // A record header truncated before the handshake-message byte — unclassifiable, so not an
1673 // opening handshake.
1674 let truncated = vec![0x16, 0x03, 0x03, 0x00, 0x05];
1675 // Not TLS at all.
1676 let garbage = vec![0x00, 0x01, 0x02, 0x03, 0x04, 0x05];
1677
1678 for (label, frame) in [
1679 ("ServerHello", server_hello),
1680 ("application record", app_data),
1681 ("truncated record", truncated),
1682 ("garbage", garbage),
1683 ] {
1684 status.route_relayed("ffbb", frame);
1685 assert!(
1686 !status.open_tunnel_exists("ffbb"),
1687 "a {label} frame must not register a server-role circuit"
1688 );
1689 assert!(
1690 accept_rx.try_recv().is_err(),
1691 "a {label} frame must not surface an accept"
1692 );
1693 }
1694
1695 // CONTROL: the peer's genuine ClientHello still opens the circuit — the responder path is
1696 // intact and the earlier drops left no per-peer state behind.
1697 status.route_relayed("ffbb", client_hello_frame());
1698 assert!(
1699 status.open_tunnel_exists("ffbb"),
1700 "a genuine ClientHello still opens an introduced circuit"
1701 );
1702 assert!(
1703 accept_rx.try_recv().is_ok(),
1704 "a genuine ClientHello still surfaces an accept"
1705 );
1706 }
1707}