Use *_with_prefs() for Option<ConnectPrefs> callers in TorClient::connect

This commit is contained in:
Neel Chauhan 2022-01-08 13:36:42 -08:00
parent e9a507af67
commit 439b8b3e64
6 changed files with 39 additions and 16 deletions

View File

@ -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

View File

@ -110,7 +110,7 @@ impl<R: Runtime> Service<Uri> for ArtiHttpConnector<R> {
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 })
})

View File

@ -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...");

View File

@ -325,16 +325,25 @@ impl<R: Runtime> TorClient<R> {
///
/// 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<A: IntoTorAddr>(
pub async fn connect<A: IntoTorAddr>(&self, target: A) -> Result<DataStream> {
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<A: IntoTorAddr>(
&self,
target: A,
flags: Option<ConnectPrefs>,
flags: ConnectPrefs,
) -> Result<DataStream> {
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<R: Runtime> TorClient<R> {
}
/// On success, return a list of IP addresses.
pub async fn resolve(
pub async fn resolve(&self, hostname: &str) -> Result<Vec<IpAddr>> {
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<ConnectPrefs>,
flags: ConnectPrefs,
) -> Result<Vec<IpAddr>> {
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<R: Runtime> TorClient<R> {
/// 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<Vec<String>> {
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<ConnectPrefs>,
flags: ConnectPrefs,
) -> Result<Vec<String>> {
let flags = flags.unwrap_or_default();
let circ = self.get_or_launch_exit_circ(&[], &flags).await?;
let resolve_ptr_future = circ.resolve_ptr(addr);

View File

@ -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};
//!

View File

@ -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,