use rings_core::dht::Did;
use crate::onion::OnionExitDescriptor;
pub use crate::onion::OnionProxyTarget;
use crate::onion::OnionRoute;
use crate::onion::OnionServiceName;
use crate::online::OnlineNodeType;
#[cfg(rings_native)]
pub mod http;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum OnionProxyProtocol {
TcpConnect,
HttpsProxy,
}
impl OnionProxyProtocol {
pub const fn exit_service_name(self) -> OnionServiceName {
match self {
Self::TcpConnect => OnionServiceName::tcp(),
Self::HttpsProxy => OnionServiceName::https(),
}
}
pub const fn label(self) -> &'static str {
match self {
Self::TcpConnect => "tcp-connect",
Self::HttpsProxy => "https-proxy",
}
}
pub(crate) fn accepts_exit_descriptor(self, descriptor: &OnionExitDescriptor) -> bool {
match self {
Self::TcpConnect => matches!(
descriptor.node_type,
OnlineNodeType::Native | OnlineNodeType::Ffi
),
Self::HttpsProxy => true,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OnionProxyRoute {
pub target: OnionProxyTarget,
pub route: OnionRoute,
}
impl OnionProxyRoute {
pub fn exit_did(&self) -> Did {
self.route.exit_did()
}
pub fn exit_service(&self) -> &str {
self.route.service()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::error::Error;
use crate::error::Result;
#[test]
fn test_proxy_protocol_fixes_its_symbol() {
assert_eq!(
OnionProxyProtocol::TcpConnect.exit_service_name(),
OnionServiceName::tcp()
);
assert_eq!(
OnionProxyProtocol::HttpsProxy.exit_service_name(),
OnionServiceName::https()
);
}
#[test]
fn test_target_authority_parses_domain_targets() -> Result<()> {
let target = OnionProxyTarget::parse_authority("Example.COM.:443")?;
assert_eq!(target.host(), "example.com");
assert_eq!(target.port(), 443);
assert_eq!(target.authority(), "example.com:443");
Ok(())
}
#[test]
fn test_target_authority_parses_ipv6_targets() -> Result<()> {
let target = OnionProxyTarget::parse_authority("[2001:db8::1]:8443")?;
assert_eq!(target.host(), "2001:db8::1");
assert_eq!(target.port(), 8443);
assert_eq!(target.authority(), "[2001:db8::1]:8443");
Ok(())
}
#[test]
fn test_target_authority_rejects_missing_port() {
assert!(matches!(
OnionProxyTarget::parse_authority("example.com"),
Err(Error::OnionProxyTarget(
crate::onion::OnionProxyTargetError::MissingPort
))
));
}
}