node-device-session 7.7.9

Shared server for /econ-v1/device-session/1 behind transport-neutral traits (C-5)
Documentation
//! Carrier-specific inputs for the shared WebRTC answerer.

use super::portmap::{NoSessionPortMapper, SessionPortMapper};
use super::WebRtcHandle;
use crate::messages::OpenOk;
use async_trait::async_trait;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Instant;

/// What a carrier must supply to run the shared WebRTC answerer.
///
/// Wire protocol, str0m session, DataChannel framing, and HTTP/WS tunnels are
/// shared. Only ICE environment and session bookkeeping stay carrier-owned.
#[async_trait]
pub trait WebRtcCarrier: Send + Sync {
    /// Instant used for str0m and ICE timers.
    fn now(&self) -> Instant {
        Instant::now()
    }

    /// STUN server addresses used while gathering (no TURN).
    async fn stun_servers(&self) -> Vec<SocketAddr>;

    /// Optional forced bind for the answerer's ICE UDP sockets.
    fn bind_addr(&self) -> Option<SocketAddr> {
        None
    }

    /// IPv4 session-port mapper (UPnP / carrier portmap). Default: unavailable.
    fn port_mapper(&self) -> Arc<dyn SessionPortMapper> {
        Arc::new(NoSessionPortMapper)
    }

    /// Description string passed to [`SessionPortMapper::map`].
    fn mapping_description(&self) -> &str {
        "webrtc"
    }

    /// Called after the handle is created and before the answerer runs.
    ///
    /// Carriers bind the handle into a session registry (revocation / replace).
    /// Return `false` to abort without answering.
    async fn bind_session(
        &self,
        _identity: &OpenOk,
        _peer: [u8; 32],
        _handle: &WebRtcHandle,
    ) -> bool {
        true
    }
}

/// Minimal carrier: no STUN, no portmap, system clock.
#[derive(Debug, Default, Clone, Copy)]
pub struct NullWebRtcCarrier;

#[async_trait]
impl WebRtcCarrier for NullWebRtcCarrier {
    async fn stun_servers(&self) -> Vec<SocketAddr> {
        Vec::new()
    }
}

/// Carrier with fixed STUN servers and an optional port mapper (tests / simple hosts).
#[derive(Clone)]
pub struct StaticWebRtcCarrier {
    /// STUN addresses returned by [`WebRtcCarrier::stun_servers`].
    pub stun: Vec<SocketAddr>,
    /// Optional ICE UDP bind.
    pub bind: Option<SocketAddr>,
    /// Port mapper (defaults to [`NoSessionPortMapper`]).
    pub mapper: Arc<dyn SessionPortMapper>,
    /// Mapper description (e.g. `"iroh-webrtc"` / `"p2p-webrtc"`).
    pub mapping_description: &'static str,
}

impl Default for StaticWebRtcCarrier {
    fn default() -> Self {
        Self {
            stun: Vec::new(),
            bind: None,
            mapper: Arc::new(NoSessionPortMapper),
            mapping_description: "webrtc",
        }
    }
}

#[async_trait]
impl WebRtcCarrier for StaticWebRtcCarrier {
    async fn stun_servers(&self) -> Vec<SocketAddr> {
        self.stun.clone()
    }

    fn bind_addr(&self) -> Option<SocketAddr> {
        self.bind
    }

    fn port_mapper(&self) -> Arc<dyn SessionPortMapper> {
        self.mapper.clone()
    }

    fn mapping_description(&self) -> &str {
        self.mapping_description
    }
}