Move Tor running check out of client

To perform this check, we don't need an authenticated connection to
Tor, just the socks5 proxy. This function is misplaced on the client.
debug-remodel-tor
Thomas Eizinger 3 years ago
parent 085848d646
commit 83c4be1657
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96

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

@ -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<Swarm<bob::Behaviour>> {
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,
};

@ -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<UnauthenticatedConn<TcpStream>> {
// 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<AuthenticatedClient> {
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(())
}

Loading…
Cancel
Save