Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
binarybaron committed Nov 19, 2024
1 parent 0a5c9bd commit bb1fda1
Show file tree
Hide file tree
Showing 11 changed files with 2,607 additions and 292 deletions.
2,753 changes: 2,534 additions & 219 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions swap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tauri = [ "dep:tauri" ]

[dependencies]
anyhow = "1"
arti-client = "0.24.0"
arti-client = { version = "0.24.0", features = ["static-sqlite"] }
async-compression = { version = "0.3", features = [ "bzip2", "tokio" ] }
async-trait = "0.1"
asynchronous-codec = "0.7.0"
Expand Down Expand Up @@ -46,6 +46,7 @@ itertools = "0.13"
jsonrpsee = { version = "0.16.2", features = [ "server" ] }
jsonrpsee-core = "0.16.2"
libp2p = { version = "0.53.2", features = [ "tcp", "yamux", "dns", "noise", "request-response", "ping", "rendezvous", "identify", "macros", "cbor", "json", "tokio", "serde", "rsa" ] }
libp2p-community-tor = { git = "https://github.com/UnstoppableSwap/libp2p-tor.git", branch = "upgrade/arti_client", version = "0.2.0" }
monero = { version = "0.12", features = [ "serde_support" ] }
monero-rpc = { path = "../monero-rpc" }
once_cell = "1.19"
Expand Down Expand Up @@ -74,10 +75,9 @@ sigma_fun = { version = "0.7", default-features = false, features = [
"secp256k1",
"alloc",
] }
sqlx = { version = "0.6.3", features = [
sqlx = { version = "0.8", features = [
"sqlite",
"runtime-tokio-rustls",
"offline",
] }
structopt = "0.3"
strum = { version = "0.26", features = [ "derive" ] }
Expand All @@ -100,6 +100,7 @@ tokio-socks = "0.5"
tokio-tungstenite = { version = "0.15", features = [ "rustls-tls" ] }
tokio-util = { version = "0.7", features = [ "io", "codec" ] }
toml = "0.8"
tor-rtcompat = "0.24.0"
torut = { version = "0.2", default-features = false, features = [
"v3",
"control",
Expand Down
1 change: 1 addition & 0 deletions swap/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod cancel_and_refund;
pub mod command;
mod event_loop;
mod list_sellers;
mod tor;
pub mod transport;
pub mod watcher;

Expand Down
9 changes: 9 additions & 0 deletions swap/src/cli/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::seed::Seed;
use crate::{bitcoin, common, monero};
use anyhow::anyhow;
use anyhow::{bail, Context as AnyContext, Error, Result};
use arti_client::TorClient;
use futures::future::try_join_all;
use std::fmt;
use std::future::Future;
Expand All @@ -22,11 +23,13 @@ use tauri_bindings::{
};
use tokio::sync::{broadcast, broadcast::Sender, Mutex as TokioMutex, RwLock};
use tokio::task::JoinHandle;
use tor_rtcompat::tokio::TokioRustlsRuntime;
use tracing::level_filters::LevelFilter;
use tracing::Level;
use url::Url;
use uuid::Uuid;

use super::tor::init_tor_client;
use super::watcher::Watcher;

static START: Once = Once::new();
Expand Down Expand Up @@ -188,6 +191,7 @@ pub struct Context {
bitcoin_wallet: Option<Arc<bitcoin::Wallet>>,
monero_wallet: Option<Arc<monero::Wallet>>,
monero_rpc_process: Option<Arc<SyncMutex<monero::WalletRpcProcess>>>,
tor_client: Option<Arc<TorClient<TokioRustlsRuntime>>>,
}

/// A conveniant builder struct for [`Context`].
Expand Down Expand Up @@ -343,6 +347,7 @@ impl ContextBuilder {
self.tauri_handle.clone(),
)
.await?;

(Some(Arc::new(wlt)), Some(Arc::new(SyncMutex::new(prc))))
} else {
(None, None)
Expand Down Expand Up @@ -372,6 +377,8 @@ impl ContextBuilder {
}
}

let tor = init_tor_client(&data_dir).await?;

let context = Context {
db,
bitcoin_wallet,
Expand All @@ -390,6 +397,7 @@ impl ContextBuilder {
swap_lock,
tasks: Arc::new(PendingTaskList::default()),
tauri_handle: self.tauri_handle,
tor_client: Some(tor),
};

Ok(context)
Expand Down Expand Up @@ -423,6 +431,7 @@ impl Context {
swap_lock: Arc::new(SwapLock::new()),
tasks: Arc::new(PendingTaskList::default()),
tauri_handle: None,
tor_client: None,
}
}

Expand Down
9 changes: 2 additions & 7 deletions swap/src/cli/api/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,12 +623,7 @@ pub async fn buy_xmr(
(seed.derive_libp2p_identity(), context.config.namespace),
);

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

swarm.add_peer_address(seller_peer_id, seller);

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

tracing::debug!(peer_id = %our_peer_id, "Network layer initialized");
Expand Down
2 changes: 1 addition & 1 deletion swap/src/cli/list_sellers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub async fn list_sellers(
quote: quote::cli(),
ping: ping::Behaviour::new(ping::Config::new().with_timeout(Duration::from_secs(60))),
};
let mut swarm = swarm::cli(identity, tor_socks5_port, behaviour).await?;
let mut swarm = swarm::cli(identity, None, behaviour).await?;

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

Expand Down
13 changes: 8 additions & 5 deletions swap/src/cli/tor.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
use std::path::PathBuf;
use std::sync::Arc;

use arti::socks::run_socks_proxy;
use arti_client::{config::TorClientConfigBuilder, TorClient, TorClientBuilder};
use arti_client::{config::TorClientConfigBuilder, Error, TorClient};
use tor_rtcompat::tokio::TokioRustlsRuntime;

pub async fn init_tor_client(tor_socks5_port: u16, data_dir: PathBuf) -> Result<Arc<TorClient>> {
pub async fn init_tor_client(
data_dir: &PathBuf,
) -> Result<Arc<TorClient<TokioRustlsRuntime>>, Error> {
let data_dir = data_dir.join("tor");
let state_dir = data_dir.join("state");
let cache_dir = data_dir.join("cache");

// The client configuration describes how to connect to the Tor network,
// and what directories to use for storing persistent state.
let config = TorClientConfigBuilder::from_directories(state_dir, cache_dir).build()?;
let config = TorClientConfigBuilder::from_directories(state_dir, cache_dir)
.build()
.unwrap();

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

run_socks_proxy(tor_client, tor_socks5_port).await?;
Ok(Arc::new(tor_client))
}
15 changes: 11 additions & 4 deletions swap/src/cli/transport.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use crate::network::tor_transport::TorDialOnlyTransport;
use std::sync::Arc;

use crate::network::transport::authenticate_and_multiplex;
use anyhow::Result;
use arti_client::TorClient;
use libp2p::core::muxing::StreamMuxerBox;
use libp2p::core::transport::{Boxed, OptionalTransport};
use libp2p::dns;
use libp2p::tcp;
use libp2p::{identity, PeerId, Transport};
use libp2p_community_tor::{AddressConversion, TorTransport};
use tor_rtcompat::tokio::TokioRustlsRuntime;

/// Creates the libp2p transport for the swap CLI.
///
Expand All @@ -17,13 +21,16 @@ use libp2p::{identity, PeerId, Transport};
/// TCP transport.
pub fn new(
identity: &identity::Keypair,
maybe_tor_socks5_port: Option<u16>,
maybe_tor_client: Option<Arc<TorClient<TokioRustlsRuntime>>>,
) -> Result<Boxed<(PeerId, StreamMuxerBox)>> {
let tcp = tcp::tokio::Transport::new(tcp::Config::new().nodelay(true));
let tcp_with_dns = dns::tokio::Transport::system(tcp)?;

let maybe_tor_transport = match maybe_tor_socks5_port {
Some(port) => OptionalTransport::some(TorDialOnlyTransport::new(port)),
let maybe_tor_transport: OptionalTransport<TorTransport> = match maybe_tor_client {
Some(client) => OptionalTransport::some(libp2p_community_tor::TorTransport {
client,
conversion_mode: AddressConversion::IpAndDns,
}),
None => OptionalTransport::none(),
};

Expand Down
2 changes: 2 additions & 0 deletions swap/src/common/tracing_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ fn env_filter(level_filter: LevelFilter) -> Result<EnvFilter> {
Ok(EnvFilter::from_default_env()
.add_directive(Directive::from_str(&format!("asb={}", &level_filter))?)
.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!(
"unstoppableswap-gui-rs={}",
&level_filter
Expand Down
Loading

0 comments on commit bb1fda1

Please sign in to comment.