Skip to content

Commit

Permalink
feat: pass tor client tor all instantioators of transport
Browse files Browse the repository at this point in the history
  • Loading branch information
binarybaron committed Nov 20, 2024
1 parent a61d27c commit 3fbb357
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion swap/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod tests {
rendezvous_peer_id,
rendezvous_address,
namespace,
0,
None,
identity::Keypair::generate_ed25519(),
);
let sellers = tokio::time::timeout(Duration::from_secs(15), list_sellers)
Expand Down
11 changes: 8 additions & 3 deletions swap/src/cli/api/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,12 @@ pub async fn buy_xmr(
(seed.derive_libp2p_identity(), context.config.namespace),
);

let mut swarm = swarm::cli(seed.derive_libp2p_identity(), None, behaviour).await?;
let mut swarm = swarm::cli(
seed.derive_libp2p_identity(),
context.tor_client.clone(),
behaviour,
)
.await?;

swarm.add_peer_address(seller_peer_id, seller);

Expand Down Expand Up @@ -808,7 +813,7 @@ pub async fn resume_swap(
),
(seed.clone(), context.config.namespace),
);
let mut swarm = swarm::cli(seed.clone(), None, behaviour).await?;
let mut swarm = swarm::cli(seed.clone(), context.tor_client.clone(), behaviour).await?;
let our_peer_id = swarm.local_peer_id();

tracing::debug!(peer_id = %our_peer_id, "Network layer initialized");
Expand Down Expand Up @@ -1078,7 +1083,7 @@ pub async fn list_sellers(
rendezvous_node_peer_id,
rendezvous_point,
context.config.namespace,
context.config.tor_socks5_port,
context.tor_client.clone(),
identity,
)
.await?;
Expand Down
7 changes: 5 additions & 2 deletions swap/src/cli/list_sellers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::network::quote::BidQuote;
use crate::network::rendezvous::XmrBtcNamespace;
use crate::network::{quote, swarm};
use anyhow::{Context, Result};
use arti_client::TorClient;
use futures::StreamExt;
use libp2p::multiaddr::Protocol;
use libp2p::request_response;
Expand All @@ -12,7 +13,9 @@ use serde::Serialize;
use serde_with::{serde_as, DisplayFromStr};
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tor_rtcompat::tokio::TokioRustlsRuntime;
use typeshare::typeshare;

/// Returns sorted list of sellers, with [Online](Status::Online) listed first.
Expand All @@ -25,15 +28,15 @@ pub async fn list_sellers(
rendezvous_node_peer_id: PeerId,
rendezvous_node_addr: Multiaddr,
namespace: XmrBtcNamespace,
tor_socks5_port: u16,
maybe_tor_client: Option<Arc<TorClient<TokioRustlsRuntime>>>,
identity: identity::Keypair,
) -> Result<Vec<Seller>> {
let behaviour = Behaviour {
rendezvous: rendezvous::client::Behaviour::new(identity.clone()),
quote: quote::cli(),
ping: ping::Behaviour::new(ping::Config::new().with_timeout(Duration::from_secs(60))),
};
let mut swarm = swarm::cli(identity, None, behaviour).await?;
let mut swarm = swarm::cli(identity, maybe_tor_client, behaviour).await?;

swarm.add_peer_address(rendezvous_node_peer_id, rendezvous_node_addr.clone());

Expand Down
3 changes: 2 additions & 1 deletion swap/src/cli/tor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use tor_rtcompat::tokio::TokioRustlsRuntime;
pub async fn init_tor_client(
data_dir: &PathBuf,
) -> Result<Arc<TorClient<TokioRustlsRuntime>>, Error> {
// We store the Tor state in the data directory
let data_dir = data_dir.join("tor");
let state_dir = data_dir.join("state");
let cache_dir = data_dir.join("cache");
Expand All @@ -20,7 +21,7 @@ pub async fn init_tor_client(
// Start the Arti client, and let it bootstrap a connection to the Tor network.
// (This takes a while to gather the necessary directory information.
// It uses cached information when possible.)
let tor_client = TorClient::create_bootstrapped(config).await?;
let tor_client = TorClient::<TokioRustlsRuntime>::create_bootstrapped(config).await?;

Ok(Arc::new(tor_client))
}
12 changes: 8 additions & 4 deletions swap/src/cli/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ pub fn new(
let tcp_with_dns = dns::tokio::Transport::system(tcp)?;

let maybe_tor_transport: OptionalTransport<TorTransport> = match maybe_tor_client {
Some(client) => OptionalTransport::some(libp2p_community_tor::TorTransport {
client,
conversion_mode: AddressConversion::IpAndDns,
}),
Some(client) => {
println!("Using Tor transport");

OptionalTransport::some(libp2p_community_tor::TorTransport::from_client(
client,
AddressConversion::IpAndDns,
))
}
None => OptionalTransport::none(),
};

Expand Down
4 changes: 4 additions & 0 deletions swap/src/common/tracing_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ fn env_filter(level_filter: LevelFilter) -> Result<EnvFilter> {
.add_directive(Directive::from_str(&format!("swap={}", &level_filter))?)
.add_directive(Directive::from_str(&format!("arti={}", &level_filter))?)
.add_directive(Directive::from_str(&format!("libp2p={}", &level_filter))?)
.add_directive(Directive::from_str(&format!(
"libp2p_community_tor={}",
&level_filter
))?)
.add_directive(Directive::from_str(&format!(
"unstoppableswap-gui-rs={}",
&level_filter
Expand Down
2 changes: 1 addition & 1 deletion swap/src/network/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::asb::{LatestRate, RendezvousNode};
use crate::libp2p_ext::MultiAddrExt;
use crate::network::rendezvous::XmrBtcNamespace;
use crate::seed::Seed;
use crate::{asb, bitcoin, cli, env, tor};
use crate::{asb, bitcoin, cli, env};
use anyhow::Result;
use arti_client::TorClient;
use libp2p::swarm::NetworkBehaviour;
Expand Down

0 comments on commit 3fbb357

Please sign in to comment.