Skip to main content

rings_node/onion/
gateway.rs

1//! Native node adapter for the runtime-neutral gateway Onion boundary.
2
3use std::sync::Arc;
4
5use rings_gateway::BoxGatewayDuplex;
6use rings_gateway::BoxOnionStreamProbe;
7use rings_gateway::FlowId;
8use rings_gateway::GatewayError;
9use rings_gateway::OnionStreamCondition;
10use rings_gateway::OnionStreamConnector;
11use rings_gateway::OnionStreamProbe;
12
13use crate::onion::native::NativeOnionCircuitHandle;
14use crate::onion::session::witness::OnionSessionCondition;
15use crate::onion::session::witness::OnionSessionWitness;
16use crate::onion::tcp::OnionLocalStream;
17use crate::onion::OnionProxyTarget;
18use crate::processor::Processor;
19
20/// The diagnostic a captured flow's reset carries when its session failed closed.
21const ONION_SESSION_FAILED: &str = "the onion session failed closed";
22
23/// Native connector that maps one captured target to one Rings Onion TCP stream.
24pub struct NativeOnionGatewayConnector {
25    processor: Arc<Processor>,
26    onion: NativeOnionCircuitHandle,
27}
28
29impl NativeOnionGatewayConnector {
30    /// Bind a native processor and its installed Onion runtime: every captured flow opens a
31    /// `tcp` session ([`NativeOnionCircuitHandle::open_tcp_target`]).
32    pub fn new(processor: Arc<Processor>, onion: NativeOnionCircuitHandle) -> Self {
33        Self { processor, onion }
34    }
35
36    fn onion_error(flow: FlowId, error: impl std::fmt::Display) -> GatewayError {
37        GatewayError::OnionUnavailable {
38            target: flow.target,
39            message: error.to_string(),
40        }
41    }
42}
43
44/// The gateway's probe of one captured flow's session: its witness, read at any instant.
45struct NativeOnionStreamProbe(OnionSessionWitness);
46
47impl OnionStreamProbe for NativeOnionStreamProbe {
48    /// The session witness's condition, in the gateway's terms.
49    fn condition(&self) -> OnionStreamCondition {
50        match self.0.condition() {
51            OnionSessionCondition::Live => OnionStreamCondition::Live,
52            OnionSessionCondition::Stalled => OnionStreamCondition::Stalled,
53            OnionSessionCondition::Failed => {
54                OnionStreamCondition::Failed(ONION_SESSION_FAILED.to_string())
55            }
56        }
57    }
58}
59
60#[async_trait::async_trait]
61impl OnionStreamConnector for NativeOnionGatewayConnector {
62    async fn open_stream(
63        &self,
64        flow: FlowId,
65        stream: BoxGatewayDuplex,
66    ) -> Result<BoxOnionStreamProbe, GatewayError> {
67        let target = OnionProxyTarget::parse_authority(&flow.target.to_string())
68            .map_err(|error| Self::onion_error(flow, error))?;
69        let opened = self
70            .onion
71            .open_tcp_target(&self.processor, target)
72            .await
73            .map_err(|error| Self::onion_error(flow, error))?;
74        Ok(Box::new(NativeOnionStreamProbe(
75            opened.relay_witnessed(stream),
76        )))
77    }
78}
79
80impl OnionLocalStream for BoxGatewayDuplex {
81    /// A drop: a duplex expresses no abortive close, so the failure crosses the gateway
82    /// boundary as the probe, which reads `Failed` before this drop (#902, #950).
83    fn reset(self) {}
84}