From 439b8b3e64f6f4cb5a032ee3ceafb4675003f259 Mon Sep 17 00:00:00 2001 From: Neel Chauhan Date: Sat, 8 Jan 2022 13:36:42 -0800 Subject: [PATCH] Use *_with_prefs() for Option callers in TorClient::connect --- crates/arti-bench/src/main.rs | 2 +- crates/arti-client/examples/hyper.rs | 2 +- crates/arti-client/examples/readme.rs | 2 +- crates/arti-client/src/client.rs | 39 ++++++++++++++++++++------- crates/arti-client/src/lib.rs | 2 +- crates/arti/src/proxy.rs | 8 +++--- 6 files changed, 39 insertions(+), 16 deletions(-) diff --git a/crates/arti-bench/src/main.rs b/crates/arti-bench/src/main.rs index 38af85bfe..368e35ad0 100644 --- a/crates/arti-bench/src/main.rs +++ b/crates/arti-bench/src/main.rs @@ -347,7 +347,7 @@ fn main() -> Result<()> { let dp = Arc::clone(&download_payload); let stats = runtime.block_on(async move { let stream = tor_client - .connect(TorAddr::dangerously_from(connect_addr).unwrap(), None) + .connect(TorAddr::dangerously_from(connect_addr).unwrap()) .await .unwrap(); client(stream, up, dp).await diff --git a/crates/arti-client/examples/hyper.rs b/crates/arti-client/examples/hyper.rs index 598fd4144..411ac572a 100644 --- a/crates/arti-client/examples/hyper.rs +++ b/crates/arti-client/examples/hyper.rs @@ -110,7 +110,7 @@ impl Service for ArtiHttpConnector { let (host, port) = uri_to_host_port(req)?; // Initiate a new Tor connection, producing a `DataStream` if successful. let ds = client - .connect((&host as &str, port).into_tor_addr()?, None) + .connect((&host as &str, port).into_tor_addr()?) .await?; Ok(ArtiHttpConnection { inner: ds }) }) diff --git a/crates/arti-client/examples/readme.rs b/crates/arti-client/examples/readme.rs index 899d9628e..07b7ce21d 100644 --- a/crates/arti-client/examples/readme.rs +++ b/crates/arti-client/examples/readme.rs @@ -27,7 +27,7 @@ async fn main() -> Result<()> { eprintln!("connecting to example.com..."); // Initiate a connection over Tor to example.com, port 80. - let mut stream = tor_client.connect(("example.com", 80), None).await?; + let mut stream = tor_client.connect(("example.com", 80)).await?; eprintln!("sending request..."); diff --git a/crates/arti-client/src/client.rs b/crates/arti-client/src/client.rs index 84ddcec5c..ebc50f846 100644 --- a/crates/arti-client/src/client.rs +++ b/crates/arti-client/src/client.rs @@ -325,16 +325,25 @@ impl TorClient { /// /// Note that because Tor prefers to do DNS resolution on the remote /// side of the network, this function takes its address as a string. - pub async fn connect( + pub async fn connect(&self, target: A) -> Result { + self.connect_with_prefs(target, ConnectPrefs::default()) + .await + } + + /// Launch an anonymized connection to the provided address and + /// port over the Tor network with connection preference flags. + /// + /// Note that because Tor prefers to do DNS resolution on the remote + /// side of the network, this function takes its address as a string. + pub async fn connect_with_prefs( &self, target: A, - flags: Option, + flags: ConnectPrefs, ) -> Result { let addr = target.into_tor_addr()?; addr.enforce_config(&self.addrcfg.get())?; let (addr, port) = addr.into_string_and_port(); - let flags = flags.unwrap_or_default(); let exit_ports = [flags.wrap_target_port(port)]; let circ = self.get_or_launch_exit_circ(&exit_ports, &flags).await?; info!("Got a circuit for {}:{}", addr, port); @@ -350,15 +359,20 @@ impl TorClient { } /// On success, return a list of IP addresses. - pub async fn resolve( + pub async fn resolve(&self, hostname: &str) -> Result> { + self.resolve_with_prefs(hostname, ConnectPrefs::default()) + .await + } + + /// On success, return a list of IP addresses, but use flags. + pub async fn resolve_with_prefs( &self, hostname: &str, - flags: Option, + flags: ConnectPrefs, ) -> Result> { let addr = (hostname, 0).into_tor_addr()?; addr.enforce_config(&self.addrcfg.get())?; - let flags = flags.unwrap_or_default(); let circ = self.get_or_launch_exit_circ(&[], &flags).await?; let resolve_future = circ.resolve(hostname); @@ -373,12 +387,19 @@ impl TorClient { /// Perform a remote DNS reverse lookup with the provided IP address. /// /// On success, return a list of hostnames. - pub async fn resolve_ptr( + pub async fn resolve_ptr(&self, addr: IpAddr) -> Result> { + self.resolve_ptr_with_prefs(addr, ConnectPrefs::default()) + .await + } + + /// Perform a remote DNS reverse lookup with the provided IP address. + /// + /// On success, return a list of hostnames. + pub async fn resolve_ptr_with_prefs( &self, addr: IpAddr, - flags: Option, + flags: ConnectPrefs, ) -> Result> { - let flags = flags.unwrap_or_default(); let circ = self.get_or_launch_exit_circ(&[], &flags).await?; let resolve_ptr_future = circ.resolve_ptr(addr); diff --git a/crates/arti-client/src/lib.rs b/crates/arti-client/src/lib.rs index 4a5147e02..0f8c032a9 100644 --- a/crates/arti-client/src/lib.rs +++ b/crates/arti-client/src/lib.rs @@ -68,7 +68,7 @@ //! let tor_client = TorClient::bootstrap(rt, config).await?; //! //! // Initiate a connection over Tor to example.com, port 80. -//! let mut stream = tor_client.connect(("example.com", 80), None).await?; +//! let mut stream = tor_client.connect(("example.com", 80)).await?; //! //! use futures::io::{AsyncReadExt, AsyncWriteExt}; //! diff --git a/crates/arti/src/proxy.rs b/crates/arti/src/proxy.rs index cea0553cf..2cb72dc1c 100644 --- a/crates/arti/src/proxy.rs +++ b/crates/arti/src/proxy.rs @@ -187,7 +187,9 @@ where SocksCmd::CONNECT => { // The SOCKS request wants us to connect to a given address. // So, launch a connection over Tor. - let tor_stream = tor_client.connect((addr.clone(), port), Some(prefs)).await; + let tor_stream = tor_client + .connect_with_prefs((addr.clone(), port), prefs) + .await; let tor_stream = match tor_stream { Ok(s) => s, // In the case of a stream timeout, send the right SOCKS reply. @@ -224,7 +226,7 @@ where SocksCmd::RESOLVE => { // We've been asked to perform a regular hostname lookup. // (This is a tor-specific SOCKS extension.) - let addrs = tor_client.resolve(&addr, Some(prefs)).await?; + let addrs = tor_client.resolve_with_prefs(&addr, prefs).await?; if let Some(addr) = addrs.first() { let reply = request.reply( tor_socksproto::SocksStatus::SUCCEEDED, @@ -245,7 +247,7 @@ where return Err(anyhow!(e)); } }; - let hosts = tor_client.resolve_ptr(addr, Some(prefs)).await?; + let hosts = tor_client.resolve_ptr_with_prefs(addr, prefs).await?; if let Some(host) = hosts.into_iter().next() { let reply = request.reply( tor_socksproto::SocksStatus::SUCCEEDED,