Skip to main content

rings_node/onion/proxy/
mod.rs

1//! Client-side onion proxy planning.
2//!
3//! This module is runtime-neutral: native can bind it to a local HTTP CONNECT listener, while
4//! browser callers can use the same target and service mapping before handing requests to a
5//! browser-specific adapter. A proxy protocol is target-agnostic: it fixes the symbol, each
6//! request supplies its own target authority, and the pipeline's loop shape fixes the route
7//! length (#834 D5).
8
9use rings_core::dht::Did;
10
11use crate::onion::OnionExitDescriptor;
12use crate::onion::OnionExitPolicy;
13pub use crate::onion::OnionProxyTarget;
14use crate::onion::OnionRoute;
15use crate::onion::OnionServiceName;
16use crate::onion::OnionUdpBindMode;
17use crate::online::OnlineNodeType;
18
19#[cfg(rings_native)]
20pub mod http;
21
22/// Proxy protocol requested by the client ingress.
23///
24/// The protocol fixes the world-facing symbol, `TcpConnect ↦ tcp` and `HttpsProxy ↦ https`:
25/// `https` is a request/response fetch only, so every byte tunnel, TLS included, is `tcp`, and
26/// an operator who wants HTTPS-only egress registers `tcp` restricted to `*:443` (#834 D1′).
27#[derive(Clone, Copy, Debug, Eq, PartialEq)]
28pub enum OnionProxyProtocol {
29    /// HTTP CONNECT, a captured gateway flow, or any other byte tunnel. Requires a native `tcp`
30    /// exit.
31    TcpConnect,
32    /// One HTTPS request/response exchange through an `https` exit.
33    HttpsProxy,
34    /// Bound OS UDP datagram socket session. Requires a native `udp` exit.
35    UdpDatagram,
36}
37
38impl OnionProxyProtocol {
39    /// Return the world-facing symbol this protocol applies.
40    pub const fn exit_service_name(self) -> OnionServiceName {
41        match self {
42            Self::TcpConnect => OnionServiceName::tcp(),
43            Self::HttpsProxy => OnionServiceName::https(),
44            Self::UdpDatagram => OnionServiceName::udp(),
45        }
46    }
47
48    /// Return a stable diagnostic label for this proxy protocol.
49    pub const fn label(self) -> &'static str {
50        match self {
51            Self::TcpConnect => "tcp-connect",
52            Self::HttpsProxy => "https-proxy",
53            Self::UdpDatagram => "udp-datagram",
54        }
55    }
56
57    /// Whether `descriptor`'s runtime can interpret this protocol's symbol: only native and FFI
58    /// nodes have OS sockets, so only they serve `tcp` and `udp`. Route selection has already kept
59    /// only the exits that register the symbol.
60    pub(crate) fn accepts_exit_descriptor(self, descriptor: &OnionExitDescriptor) -> bool {
61        match self {
62            Self::TcpConnect | Self::UdpDatagram => matches!(
63                descriptor.node_type,
64                OnlineNodeType::Native | OnlineNodeType::Ffi
65            ),
66            Self::HttpsProxy => true,
67        }
68    }
69
70    /// Whether `policy` admits opening a session of this protocol with open `target`.
71    pub(crate) fn admits_target(self, policy: &OnionExitPolicy, target: &OnionProxyTarget) -> bool {
72        match self {
73            Self::TcpConnect | Self::HttpsProxy => policy.allows_target(target),
74            Self::UdpDatagram => {
75                OnionUdpBindMode::from_target(target).is_ok_and(|mode| policy.allows_udp_bind(mode))
76            }
77        }
78    }
79}
80
81/// A proxy route selected for a target.
82#[derive(Clone, Debug, Eq, PartialEq)]
83pub struct OnionProxyRoute {
84    /// Target requested by the local client.
85    pub target: OnionProxyTarget,
86    /// Selected route ending at the exit.
87    pub route: OnionRoute,
88}
89
90impl OnionProxyRoute {
91    /// Return the selected exit DID.
92    pub fn exit_did(&self) -> Did {
93        self.route.exit_did()
94    }
95
96    /// Return the exit service used for route selection.
97    pub fn exit_service(&self) -> &str {
98        self.route.service()
99    }
100}
101
102#[cfg(test)]
103mod tests {
104    use super::*;
105    use crate::error::Error;
106    use crate::error::Result;
107
108    /// The protocol alone fixes the symbol: a byte tunnel is always `tcp`, a fetch `https`.
109    #[test]
110    fn test_proxy_protocol_fixes_its_symbol() {
111        assert_eq!(
112            OnionProxyProtocol::TcpConnect.exit_service_name(),
113            OnionServiceName::tcp()
114        );
115        assert_eq!(
116            OnionProxyProtocol::HttpsProxy.exit_service_name(),
117            OnionServiceName::https()
118        );
119        assert_eq!(
120            OnionProxyProtocol::UdpDatagram.exit_service_name(),
121            OnionServiceName::udp()
122        );
123    }
124
125    #[test]
126    fn test_target_authority_parses_domain_targets() -> Result<()> {
127        let target = OnionProxyTarget::parse_authority("Example.COM.:443")?;
128
129        assert_eq!(target.host(), "example.com");
130        assert_eq!(target.port(), 443);
131        assert_eq!(target.authority(), "example.com:443");
132        Ok(())
133    }
134
135    #[test]
136    fn test_target_authority_parses_ipv6_targets() -> Result<()> {
137        let target = OnionProxyTarget::parse_authority("[2001:db8::1]:8443")?;
138
139        assert_eq!(target.host(), "2001:db8::1");
140        assert_eq!(target.port(), 8443);
141        assert_eq!(target.authority(), "[2001:db8::1]:8443");
142        Ok(())
143    }
144
145    #[test]
146    fn test_target_authority_rejects_missing_port() {
147        assert!(matches!(
148            OnionProxyTarget::parse_authority("example.com"),
149            Err(Error::OnionProxyTarget(
150                crate::onion::OnionProxyTargetError::MissingPort
151            ))
152        ));
153    }
154}