mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-08-21 20:48:37 -04:00
Replace tor::Client with extension trait
The types provided by torut already provide a fair amount of type-safety. We don't need to wrap them again in a type of ours. To extend them with functionality, an extension trait will also do and keeps the hierarchy flat.
This commit is contained in:
parent
83c4be1657
commit
89b6131252
5 changed files with 159 additions and 170 deletions
|
@ -14,11 +14,9 @@
|
||||||
|
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
use libp2p::core::multiaddr::Protocol;
|
use libp2p::core::multiaddr::Protocol;
|
||||||
use libp2p::core::Multiaddr;
|
|
||||||
use libp2p::Swarm;
|
use libp2p::Swarm;
|
||||||
use prettytable::{row, Table};
|
use prettytable::{row, Table};
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use structopt::clap;
|
use structopt::clap;
|
||||||
use structopt::clap::ErrorKind;
|
use structopt::clap::ErrorKind;
|
||||||
|
@ -33,8 +31,9 @@ use swap::protocol::alice;
|
||||||
use swap::protocol::alice::event_loop::KrakenRate;
|
use swap::protocol::alice::event_loop::KrakenRate;
|
||||||
use swap::protocol::alice::{redeem, run, EventLoop};
|
use swap::protocol::alice::{redeem, run, EventLoop};
|
||||||
use swap::seed::Seed;
|
use swap::seed::Seed;
|
||||||
use swap::tor::{is_tor_daemon_running_on_port, AuthenticatedClient};
|
use swap::torut_ext::AuthenticatedConnectionExt;
|
||||||
use swap::{asb, bitcoin, kraken, monero, tor};
|
use swap::{asb, bitcoin, kraken, monero, tor};
|
||||||
|
use torut::control::AuthenticatedConn;
|
||||||
use tracing::{debug, info, warn};
|
use tracing::{debug, info, warn};
|
||||||
use tracing_subscriber::filter::LevelFilter;
|
use tracing_subscriber::filter::LevelFilter;
|
||||||
|
|
||||||
|
@ -127,19 +126,38 @@ async fn main() -> Result<()> {
|
||||||
|
|
||||||
let kraken_price_updates = kraken::connect()?;
|
let kraken_price_updates = kraken::connect()?;
|
||||||
|
|
||||||
// setup Tor hidden services
|
let local_listening_tcp_port = config.network.listen.iter().find_map(|address| {
|
||||||
let _ac = match is_tor_daemon_running_on_port(config.tor.socks5_port).await {
|
address.iter().find_map(|protocol| match protocol {
|
||||||
Ok(_) => {
|
Protocol::Tcp(port) => Some(port),
|
||||||
tracing::info!("Tor found. Setting up hidden service");
|
_ => None,
|
||||||
let tor_client = tor::Client::new(config.tor.socks5_port)
|
})
|
||||||
.with_control_port(config.tor.control_port);
|
});
|
||||||
|
|
||||||
let ac =
|
// setup Tor hidden services
|
||||||
register_tor_services(config.network.clone().listen, tor_client, &seed)
|
let _ac = match (
|
||||||
.await?;
|
tor::is_daemon_running_on_port(config.tor.socks5_port).await,
|
||||||
Some(ac)
|
local_listening_tcp_port,
|
||||||
|
) {
|
||||||
|
(Ok(_), Some(port)) => {
|
||||||
|
tracing::info!("Tor found. Setting up hidden service");
|
||||||
|
|
||||||
|
let mut connection = AuthenticatedConn::new(config.tor.control_port).await?;
|
||||||
|
|
||||||
|
let tor_secret_key = seed.derive_torv3_key();
|
||||||
|
connection
|
||||||
|
.add_ephemeral_service(&tor_secret_key, port, port)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let onion_address = tor_secret_key.public().get_onion_address();
|
||||||
|
tracing::info!(%onion_address);
|
||||||
|
|
||||||
|
Some(connection)
|
||||||
}
|
}
|
||||||
Err(_) => {
|
(Ok(_), None) => {
|
||||||
|
tracing::warn!("Tor running but no local listening addresses available, unable to set up hidden service");
|
||||||
|
None
|
||||||
|
}
|
||||||
|
(Err(_), _) => {
|
||||||
tracing::warn!("Tor not found. Running on clear net");
|
tracing::warn!("Tor not found. Running on clear net");
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -334,46 +352,3 @@ async fn init_monero_wallet(
|
||||||
|
|
||||||
Ok(wallet)
|
Ok(wallet)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Registers a hidden service for each network.
|
|
||||||
/// Note: Once ac goes out of scope, the services will be de-registered.
|
|
||||||
async fn register_tor_services(
|
|
||||||
networks: Vec<Multiaddr>,
|
|
||||||
tor_client: tor::Client,
|
|
||||||
seed: &Seed,
|
|
||||||
) -> Result<AuthenticatedClient> {
|
|
||||||
let mut ac = tor_client.into_authenticated_client().await?;
|
|
||||||
|
|
||||||
let hidden_services_details = networks
|
|
||||||
.iter()
|
|
||||||
.flat_map(|network| {
|
|
||||||
network.iter().map(|protocol| match protocol {
|
|
||||||
Protocol::Tcp(port) => Some((
|
|
||||||
port,
|
|
||||||
SocketAddr::new(IpAddr::from(Ipv4Addr::new(127, 0, 0, 1)), port),
|
|
||||||
)),
|
|
||||||
_ => {
|
|
||||||
// We only care for Tcp for now.
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.flatten()
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
let key = seed.derive_torv3_key();
|
|
||||||
|
|
||||||
ac.add_services(&hidden_services_details, &key).await?;
|
|
||||||
|
|
||||||
let onion_address = key
|
|
||||||
.public()
|
|
||||||
.get_onion_address()
|
|
||||||
.get_address_without_dot_onion();
|
|
||||||
|
|
||||||
hidden_services_details.iter().for_each(|(port, _)| {
|
|
||||||
let onion_address = format!("/onion3/{}:{}", onion_address, port);
|
|
||||||
tracing::info!(%onion_address);
|
|
||||||
});
|
|
||||||
|
|
||||||
Ok(ac)
|
|
||||||
}
|
|
||||||
|
|
|
@ -28,5 +28,6 @@ pub mod network;
|
||||||
pub mod protocol;
|
pub mod protocol;
|
||||||
pub mod seed;
|
pub mod seed;
|
||||||
pub mod tor;
|
pub mod tor;
|
||||||
|
pub mod torut_ext;
|
||||||
|
|
||||||
mod monero_ext;
|
mod monero_ext;
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
use crate::protocol::alice::event_loop::LatestRate;
|
use crate::protocol::alice::event_loop::LatestRate;
|
||||||
use crate::protocol::{alice, bob};
|
use crate::protocol::{alice, bob};
|
||||||
use crate::seed::Seed;
|
use crate::seed::Seed;
|
||||||
use crate::tor::is_tor_daemon_running_on_port;
|
use crate::{asb, cli, env, monero, tor};
|
||||||
use crate::{asb, cli, env, monero};
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use libp2p::swarm::SwarmBuilder;
|
use libp2p::swarm::SwarmBuilder;
|
||||||
use libp2p::{PeerId, Swarm};
|
use libp2p::{PeerId, Swarm};
|
||||||
|
@ -50,7 +49,7 @@ pub async fn cli(
|
||||||
alice: PeerId,
|
alice: PeerId,
|
||||||
tor_socks5_port: u16,
|
tor_socks5_port: u16,
|
||||||
) -> Result<Swarm<bob::Behaviour>> {
|
) -> Result<Swarm<bob::Behaviour>> {
|
||||||
let maybe_tor_socks5_port = match is_tor_daemon_running_on_port(tor_socks5_port).await {
|
let maybe_tor_socks5_port = match tor::is_daemon_running_on_port(tor_socks5_port).await {
|
||||||
Ok(()) => Some(tor_socks5_port),
|
Ok(()) => Some(tor_socks5_port),
|
||||||
Err(_) => None,
|
Err(_) => None,
|
||||||
};
|
};
|
||||||
|
|
113
swap/src/tor.rs
113
swap/src/tor.rs
|
@ -1,117 +1,10 @@
|
||||||
use anyhow::{anyhow, bail, Context, Result};
|
use anyhow::{anyhow, bail, Result};
|
||||||
use std::future::Future;
|
|
||||||
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
|
|
||||||
use tokio::net::TcpStream;
|
|
||||||
use torut::control::{AsyncEvent, AuthenticatedConn, ConnError, UnauthenticatedConn};
|
|
||||||
use torut::onion::TorSecretKeyV3;
|
|
||||||
|
|
||||||
pub const DEFAULT_SOCKS5_PORT: u16 = 9050;
|
pub const DEFAULT_SOCKS5_PORT: u16 = 9050;
|
||||||
pub const DEFAULT_CONTROL_PORT: u16 = 9051;
|
pub const DEFAULT_CONTROL_PORT: u16 = 9051;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
/// Check if Tor daemon is running on the given port.
|
||||||
pub struct Client {
|
pub async fn is_daemon_running_on_port(port: u16) -> Result<()> {
|
||||||
socks5_address: SocketAddrV4,
|
|
||||||
control_port_address: SocketAddr,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Client {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
socks5_address: SocketAddrV4::new(Ipv4Addr::LOCALHOST, DEFAULT_SOCKS5_PORT),
|
|
||||||
control_port_address: SocketAddr::V4(SocketAddrV4::new(
|
|
||||||
Ipv4Addr::LOCALHOST,
|
|
||||||
DEFAULT_CONTROL_PORT,
|
|
||||||
)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Client {
|
|
||||||
pub fn new(socks5_port: u16) -> Self {
|
|
||||||
Self {
|
|
||||||
socks5_address: SocketAddrV4::new(Ipv4Addr::LOCALHOST, socks5_port),
|
|
||||||
control_port_address: SocketAddr::V4(SocketAddrV4::new(
|
|
||||||
Ipv4Addr::LOCALHOST,
|
|
||||||
DEFAULT_CONTROL_PORT,
|
|
||||||
)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn with_control_port(self, control_port: u16) -> Self {
|
|
||||||
Self {
|
|
||||||
control_port_address: SocketAddr::V4(SocketAddrV4::new(
|
|
||||||
Ipv4Addr::LOCALHOST,
|
|
||||||
control_port,
|
|
||||||
)),
|
|
||||||
..self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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?;
|
|
||||||
let uc = UnauthenticatedConn::new(sock);
|
|
||||||
Ok(uc)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create a new authenticated connection to your local Tor service
|
|
||||||
pub async fn into_authenticated_client(self) -> Result<AuthenticatedClient> {
|
|
||||||
is_tor_daemon_running_on_port(self.socks5_address.port()).await?;
|
|
||||||
|
|
||||||
let mut uc = self
|
|
||||||
.init_unauthenticated_connection()
|
|
||||||
.await
|
|
||||||
.map_err(|_| anyhow!("Could not connect to Tor. Tor might not be running or the control port is incorrect."))?;
|
|
||||||
|
|
||||||
let tor_info = uc
|
|
||||||
.load_protocol_info()
|
|
||||||
.await
|
|
||||||
.map_err(|_| anyhow!("Failed to load protocol info from Tor."))?;
|
|
||||||
|
|
||||||
let tor_auth_data = tor_info
|
|
||||||
.make_auth_data()?
|
|
||||||
.context("Failed to make Tor auth data.")?;
|
|
||||||
|
|
||||||
// Get an authenticated connection to the Tor via the Tor Controller protocol.
|
|
||||||
uc.authenticate(&tor_auth_data)
|
|
||||||
.await
|
|
||||||
.map_err(|_| anyhow!("Failed to authenticate with Tor"))?;
|
|
||||||
|
|
||||||
Ok(AuthenticatedClient {
|
|
||||||
inner: uc.into_authenticated().await,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn tor_proxy_port(&self) -> u16 {
|
|
||||||
self.socks5_address.port()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Handler = fn(AsyncEvent<'_>) -> Box<dyn Future<Output = Result<(), ConnError>> + Unpin>;
|
|
||||||
|
|
||||||
#[allow(missing_debug_implementations)]
|
|
||||||
pub struct AuthenticatedClient {
|
|
||||||
inner: AuthenticatedConn<TcpStream, Handler>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AuthenticatedClient {
|
|
||||||
/// Add an ephemeral tor service on localhost with the provided key
|
|
||||||
/// `service_port` and `onion_port` can be different but don't have to as
|
|
||||||
/// they are on different networks.
|
|
||||||
pub async fn add_services(
|
|
||||||
&mut self,
|
|
||||||
services: &[(u16, SocketAddr)],
|
|
||||||
tor_key: &TorSecretKeyV3,
|
|
||||||
) -> Result<()> {
|
|
||||||
let mut listeners = services.iter();
|
|
||||||
self.inner
|
|
||||||
.add_onion_v3(tor_key, false, false, false, None, &mut listeners)
|
|
||||||
.await
|
|
||||||
.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
|
// 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())
|
let proxy = reqwest::Proxy::all(format!("socks5h://127.0.0.1:{}", port).as_str())
|
||||||
.map_err(|_| anyhow!("tor proxy should be there"))?;
|
.map_err(|_| anyhow!("tor proxy should be there"))?;
|
||||||
|
|
121
swap/src/torut_ext.rs
Normal file
121
swap/src/torut_ext.rs
Normal file
|
@ -0,0 +1,121 @@
|
||||||
|
use std::borrow::Cow;
|
||||||
|
use std::future::Future;
|
||||||
|
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||||
|
use std::num::ParseIntError;
|
||||||
|
use std::{io, iter};
|
||||||
|
use torut::control::{AsyncEvent, AuthenticatedConn, TorAuthData, UnauthenticatedConn};
|
||||||
|
use torut::onion::TorSecretKeyV3;
|
||||||
|
|
||||||
|
pub type AsyncEventHandler =
|
||||||
|
fn(
|
||||||
|
AsyncEvent<'_>,
|
||||||
|
) -> Box<dyn Future<Output = Result<(), torut::control::ConnError>> + Unpin + Send>;
|
||||||
|
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
pub enum Error {
|
||||||
|
#[error("Failed to connect to Tor control port")]
|
||||||
|
FailedToConnect(#[source] io::Error),
|
||||||
|
#[error("Failed to read Tor auth-cookie filed")]
|
||||||
|
FailedToReadCookieFile(#[source] io::Error),
|
||||||
|
#[error("No authentication information could be found")]
|
||||||
|
NoAuthData,
|
||||||
|
#[error("Failed to communicate with Tor control port")]
|
||||||
|
Connection(torut::control::ConnError),
|
||||||
|
#[error("Failed to add hidden service")]
|
||||||
|
FailedToAddHiddenService(torut::control::ConnError),
|
||||||
|
#[error("Failed to parse port")]
|
||||||
|
FailedToParsePort(#[from] ParseIntError),
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Use #[from] once available: https://github.com/teawithsand/torut/issues/12
|
||||||
|
impl From<torut::control::ConnError> for Error {
|
||||||
|
fn from(e: torut::control::ConnError) -> Self {
|
||||||
|
Error::Connection(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait AuthenticatedConnectionExt: Sized {
|
||||||
|
async fn new(control_port: u16) -> Result<Self, Error>;
|
||||||
|
async fn with_password(control_port: u16, password: &str) -> Result<Self, Error>;
|
||||||
|
async fn add_ephemeral_service(
|
||||||
|
&mut self,
|
||||||
|
key: &TorSecretKeyV3,
|
||||||
|
onion_port: u16,
|
||||||
|
local_port: u16,
|
||||||
|
) -> Result<(), Error>;
|
||||||
|
async fn get_socks_port(&mut self) -> Result<u16, Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl AuthenticatedConnectionExt for AuthenticatedConn<tokio::net::TcpStream, AsyncEventHandler> {
|
||||||
|
async fn new(control_port: u16) -> Result<Self, Error> {
|
||||||
|
let stream = tokio::net::TcpStream::connect(format!("127.0.0.1:{}", control_port))
|
||||||
|
.await
|
||||||
|
.map_err(Error::FailedToConnect)?;
|
||||||
|
let mut uac = UnauthenticatedConn::new(stream);
|
||||||
|
|
||||||
|
let tor_info = uac.load_protocol_info().await?;
|
||||||
|
|
||||||
|
let tor_auth_data = tor_info
|
||||||
|
.make_auth_data()
|
||||||
|
.map_err(Error::FailedToReadCookieFile)?
|
||||||
|
.ok_or(Error::NoAuthData)?;
|
||||||
|
|
||||||
|
uac.authenticate(&tor_auth_data).await?;
|
||||||
|
|
||||||
|
Ok(uac.into_authenticated().await)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn with_password(control_port: u16, password: &str) -> Result<Self, Error> {
|
||||||
|
let stream = tokio::net::TcpStream::connect(format!("127.0.0.1:{}", control_port))
|
||||||
|
.await
|
||||||
|
.map_err(Error::FailedToConnect)?;
|
||||||
|
let mut uac = UnauthenticatedConn::new(stream);
|
||||||
|
|
||||||
|
uac.authenticate(&TorAuthData::HashedPassword(Cow::Borrowed(password)))
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(uac.into_authenticated().await)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn add_ephemeral_service(
|
||||||
|
&mut self,
|
||||||
|
key: &TorSecretKeyV3,
|
||||||
|
onion_port: u16,
|
||||||
|
local_port: u16,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
tracing::debug!(
|
||||||
|
"Adding ephemeral service, onion port {}, local port {}",
|
||||||
|
onion_port,
|
||||||
|
local_port
|
||||||
|
);
|
||||||
|
|
||||||
|
self.add_onion_v3(
|
||||||
|
&key,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
None,
|
||||||
|
&mut iter::once(&(
|
||||||
|
onion_port,
|
||||||
|
SocketAddr::new(IpAddr::from(Ipv4Addr::new(127, 0, 0, 1)), local_port),
|
||||||
|
)),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map_err(Error::FailedToAddHiddenService)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_socks_port(&mut self) -> Result<u16, Error> {
|
||||||
|
const DEFAULT_SOCKS_PORT: u16 = 9050;
|
||||||
|
|
||||||
|
let mut vec = self.get_conf("SocksPort").await?;
|
||||||
|
|
||||||
|
let first_element = vec
|
||||||
|
.pop()
|
||||||
|
.expect("exactly one element because we requested one config option");
|
||||||
|
let port = first_element.map_or(Ok(DEFAULT_SOCKS_PORT), |port| port.parse())?; // if config is empty, we are listing on the default port
|
||||||
|
|
||||||
|
Ok(port)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue