From e1d8a1c39b244a66c9292563a3a0905a1d223e72 Mon Sep 17 00:00:00 2001 From: Philipp Hoenisch Date: Wed, 21 Oct 2020 09:55:47 +1100 Subject: [PATCH] PR feedback integrated --- xmr-btc/src/tor.rs | 56 ++++++++++++++++++-------------------------- xmr-btc/tests/tor.rs | 3 +-- 2 files changed, 24 insertions(+), 35 deletions(-) diff --git a/xmr-btc/src/tor.rs b/xmr-btc/src/tor.rs index c40ea4ed..3a386dff 100644 --- a/xmr-btc/src/tor.rs +++ b/xmr-btc/src/tor.rs @@ -36,63 +36,54 @@ impl UnauthenticatedConnection { } /// checks if tor is running - async fn tor_running(&self) -> Result<()> { + async fn assert_tor_running(&self) -> Result<()> { // Make sure you are running tor and this is your socks port let proxy = reqwest::Proxy::all(format!("socks5h://{}", self.tor_proxy_address).as_str()) - .expect("tor proxy should be there"); - let client = reqwest::Client::builder() - .proxy(proxy) - .build() - .expect("should be able to build reqwest client"); + .map_err(|_| anyhow!("tor proxy should be there"))?; + let client = reqwest::Client::builder().proxy(proxy).build()?; let res = client.get("https://check.torproject.org").send().await?; - let text = res.text().await?; - let is_tor = text.contains("Congratulations. This browser is configured to use Tor."); - if is_tor { - Ok(()) - } else { + if !text.contains("Congratulations. This browser is configured to use Tor.") { bail!("Tor is currently not running") } + + Ok(()) } async fn init_unauthenticated_connection(&self) -> Result> { // Connect to local tor service via control port let sock = TcpStream::connect(self.tor_control_port_address).await?; - let unauthenticated_connection = UnauthenticatedConn::new(sock); - Ok(unauthenticated_connection) + let uc = UnauthenticatedConn::new(sock); + Ok(uc) } /// Create a new authenticated connection to your local Tor service pub async fn init_authenticated_connection(self) -> Result { - self.tor_running().await?; + self.assert_tor_running().await?; - let mut unauthenticated_connection = match self.init_unauthenticated_connection().await { - Err(_) => bail!("Tor instance not running"), - Ok(unauthenticated_connection) => unauthenticated_connection, - }; + let mut uc = self + .init_unauthenticated_connection() + .await + .map_err(|_| anyhow!("Tor instance not running."))?; + + let tor_info = uc + .load_protocol_info() + .await + .map_err(|_| anyhow!("Failed to load protocol info from Tor."))?; - let tor_info = match unauthenticated_connection.load_protocol_info().await { - Ok(info) => info, - Err(_) => bail!("Failed to load protocol info from Tor."), - }; let tor_auth_data = tor_info .make_auth_data()? - .expect("Failed to make auth data."); + .ok_or_else(|| anyhow!("Failed to make auth data."))?; // Get an authenticated connection to the Tor via the Tor Controller protocol. - if unauthenticated_connection - .authenticate(&tor_auth_data) + uc.authenticate(&tor_auth_data) .await - .is_err() - { - bail!("Failed to authenticate with Tor") - } - let authenticated_connection = unauthenticated_connection.into_authenticated().await; + .map_err(|_| anyhow!("Failed to authenticate with Tor"))?; Ok(AuthenticatedConnection { - authenticated_connection, + authenticated_connection: uc.into_authenticated().await, }) } } @@ -121,7 +112,6 @@ impl AuthenticatedConnection { .iter(), ) .await - .map_err(|_| anyhow!("Could not add onion service."))?; - Ok(()) + .map_err(|_| anyhow!("Could not add onion service.")) } } diff --git a/xmr-btc/tests/tor.rs b/xmr-btc/tests/tor.rs index d36a89c8..e982e591 100644 --- a/xmr-btc/tests/tor.rs +++ b/xmr-btc/tests/tor.rs @@ -28,7 +28,6 @@ mod tor_test { rx.await.ok(); }); tokio::spawn(async { - // server.await.unwrap(); if let Err(e) = graceful.await { eprintln!("server error: {}", e); } @@ -93,7 +92,7 @@ mod tor_test { let proxy = reqwest::Proxy::all(format!("socks5h://127.0.0.1:{}", proxy_port).as_str()) .expect("tor proxy should be there"); - let client = reqwest::Client::builder().proxy(proxy).build().unwrap(); + let client = reqwest::Client::builder().proxy(proxy).build()?; let onion_address = tor_secret_key_v3.public().get_onion_address().to_string(); let onion_url = format!("http://{}:8080", onion_address);