rings_node/onion/
gateway.rs1use 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
20const ONION_SESSION_FAILED: &str = "the onion session failed closed";
22
23pub struct NativeOnionGatewayConnector {
25 processor: Arc<Processor>,
26 onion: NativeOnionCircuitHandle,
27}
28
29impl NativeOnionGatewayConnector {
30 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
44struct NativeOnionStreamProbe(OnionSessionWitness);
46
47impl OnionStreamProbe for NativeOnionStreamProbe {
48 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 fn reset(self) {}
84}