diff --git a/swap/src/bin/asb.rs b/swap/src/bin/asb.rs index ef590dd1..c5755b81 100644 --- a/swap/src/bin/asb.rs +++ b/swap/src/bin/asb.rs @@ -33,7 +33,7 @@ use swap::protocol::alice; use swap::protocol::alice::event_loop::KrakenRate; use swap::protocol::alice::{redeem, run, EventLoop}; use swap::seed::Seed; -use swap::tor::AuthenticatedClient; +use swap::tor::{is_tor_daemon_running_on_port, AuthenticatedClient}; use swap::{asb, bitcoin, kraken, monero, tor}; use tracing::{debug, info, warn}; use tracing_subscriber::filter::LevelFilter; @@ -128,11 +128,12 @@ async fn main() -> Result<()> { let kraken_price_updates = kraken::connect()?; // setup Tor hidden services - let tor_client = - tor::Client::new(config.tor.socks5_port).with_control_port(config.tor.control_port); - let _ac = match tor_client.assert_tor_running().await { + let _ac = match is_tor_daemon_running_on_port(config.tor.socks5_port).await { Ok(_) => { tracing::info!("Tor found. Setting up hidden service"); + let tor_client = tor::Client::new(config.tor.socks5_port) + .with_control_port(config.tor.control_port); + let ac = register_tor_services(config.network.clone().listen, tor_client, &seed) .await?; diff --git a/swap/src/network/swarm.rs b/swap/src/network/swarm.rs index 37e9f219..aeda9bd0 100644 --- a/swap/src/network/swarm.rs +++ b/swap/src/network/swarm.rs @@ -1,7 +1,8 @@ use crate::protocol::alice::event_loop::LatestRate; use crate::protocol::{alice, bob}; use crate::seed::Seed; -use crate::{asb, cli, env, monero, tor}; +use crate::tor::is_tor_daemon_running_on_port; +use crate::{asb, cli, env, monero}; use anyhow::Result; use libp2p::swarm::SwarmBuilder; use libp2p::{PeerId, Swarm}; @@ -49,7 +50,7 @@ pub async fn cli( alice: PeerId, tor_socks5_port: u16, ) -> Result> { - let maybe_tor_socks5_port = match tor::Client::new(tor_socks5_port).assert_tor_running().await { + let maybe_tor_socks5_port = match is_tor_daemon_running_on_port(tor_socks5_port).await { Ok(()) => Some(tor_socks5_port), Err(_) => None, }; diff --git a/swap/src/tor.rs b/swap/src/tor.rs index 4a6b3e3e..57693693 100644 --- a/swap/src/tor.rs +++ b/swap/src/tor.rs @@ -46,23 +46,6 @@ impl Client { } } - /// checks if tor is running - pub 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.socks5_address).as_str()) - .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?; - - 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.control_port_address).await?; @@ -72,7 +55,7 @@ impl Client { /// Create a new authenticated connection to your local Tor service pub async fn into_authenticated_client(self) -> Result { - self.assert_tor_running().await?; + is_tor_daemon_running_on_port(self.socks5_address.port()).await?; let mut uc = self .init_unauthenticated_connection() @@ -126,3 +109,20 @@ impl AuthenticatedClient { .map_err(|e| anyhow!("Could not add onion service.: {:#?}", e)) } } + +/// checks if tor is running +pub async fn is_tor_daemon_running_on_port(port: u16) -> Result<()> { + // Make sure you are running tor and this is your socks port + let proxy = reqwest::Proxy::all(format!("socks5h://127.0.0.1:{}", port).as_str()) + .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?; + + if !text.contains("Congratulations. This browser is configured to use Tor.") { + bail!("Tor is currently not running") + } + + Ok(()) +}