Skip to main content

node_device_session/
lib.rs

1//! Shared device-session server (C-5): transport-neutral `/econ-v1/device-session/1`.
2//!
3//! Carriers implement [`SessionStreams`] + [`HostBus`] and call [`serve`] or
4//! [`serve_with_registry`]. Pass [`WebRtcSignaling`] when the carrier can
5//! answer `webrtc.signal/1`; otherwise that method is refused with
6//! `unsupported_transport`.
7//!
8//! The shared WebRTC answerer lives in [`webrtc`]: wire a [`webrtc::WebRtcAnswerer`]
9//! with a carrier-specific [`webrtc::WebRtcCarrier`] (STUN, portmap, clock,
10//! session bind). Iroh/p2p-only pieces (QUIC `ProtocolHandler`,
11//! gossip STUN discovery adapters) stay in the carrier apps. Live-session
12//! revalidation is shared here via [`SessionRegistry`].
13
14#![deny(missing_docs)]
15
16mod messages;
17mod registry;
18mod revalidate;
19mod serve;
20mod stream_wire;
21mod traits;
22mod tunnel;
23mod types;
24pub mod webrtc;
25mod wire;
26mod wire_stream;
27
28pub use messages::{OpenMode, OpenOk};
29pub use registry::{SessionClose, SessionRegistry, REVOKED_CLOSE_CODE, SUPERSEDED_CLOSE_CODE};
30pub use revalidate::{is_definitive_refusal_code, verify_refusal_code, RevalidateReport};
31pub use serve::{serve, serve_with_options, serve_with_registry, ServeOptions};
32pub use traits::{BoxRecv, BoxSend, HostBus, SessionStreams, WebRtcSignaling};
33pub use types::{
34    IngressSocket, PairingAdmission, Refusal, Session, SessionEnd, SessionPath, WireSocket,
35    PAIRING_DOOR_HEADER, PAIRING_ENDPOINT_HEADER, PATH_HEADER, SESSION_HEADER,
36};
37pub use webrtc::{
38    ByteBudget, MapRefusal, MappedSessionPort, NoSessionPortMapper, NullWebRtcCarrier,
39    SessionPortMapper, StaticWebRtcCarrier, WebRtcAnswerer, WebRtcCarrier, WebRtcHandle,
40    SESSION_BYTES,
41};
42pub use wire::{
43    decode_request, decode_response, encode_request, encode_response, peek_method, FrameError,
44    ResponseError, ALPN, DEVICE_CALL_METHOD, HTTP_REQUEST_METHOD, MAX_FRAME_BYTES,
45    RESPONSE_STATUS_ERR, RESPONSE_STATUS_OK, SESSION_OPEN_METHOD, SESSION_PAIR_METHOD,
46    SESSION_RENEW_METHOD, WEBRTC_SIGNAL_METHOD, WIRE_METHOD, WS_STREAM_METHOD,
47};
48pub use wire_stream::{
49    preamble_line, splice_to_wire_socket, wire_socket_path, DEFAULT_WIRE_SOCKET, MAX_PREAMBLE,
50};
51
52/// Capability names the host bus is expected to map.
53pub mod caps {
54    /// `core.device_session.verify`
55    pub const VERIFY: &str = "core.device_session.verify";
56    /// `core.device_session.renew`
57    pub const RENEW: &str = "core.device_session.renew";
58    /// `core.device_session.admit_renewal`
59    pub const ADMIT_RENEWAL: &str = "core.device_session.admit_renewal";
60    /// `core.device_session.admit_pairing`
61    pub const ADMIT_PAIRING: &str = "core.device_session.admit_pairing";
62}
63
64/// Encode an `http.request/1` body for tests and carriers.
65pub fn encode_http_tunnel_request(
66    method: &str,
67    path: &str,
68    headers: std::collections::HashMap<String, String>,
69    body: &[u8],
70) -> Vec<u8> {
71    use base64::{engine::general_purpose::STANDARD, Engine as _};
72    let request = stream_wire::StreamRequest {
73        method: method.into(),
74        path: path.into(),
75        headers,
76        body_b64: STANDARD.encode(body),
77    };
78    stream_wire::encode_request(&request).expect("http tunnel request encodes")
79}