rings_node/onion/proxy/
mod.rs1use 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#[derive(Clone, Copy, Debug, Eq, PartialEq)]
28pub enum OnionProxyProtocol {
29 TcpConnect,
32 HttpsProxy,
34 UdpDatagram,
36}
37
38impl OnionProxyProtocol {
39 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 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 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 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#[derive(Clone, Debug, Eq, PartialEq)]
83pub struct OnionProxyRoute {
84 pub target: OnionProxyTarget,
86 pub route: OnionRoute,
88}
89
90impl OnionProxyRoute {
91 pub fn exit_did(&self) -> Did {
93 self.route.exit_did()
94 }
95
96 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 #[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}