mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-12-24 23:19:34 -05:00
Split authenticated struct into unauthenticated struct to make it usage more obvious
This commit is contained in:
parent
a73f1fcc6f
commit
e67e940768
@ -31,6 +31,7 @@ bitcoin-harness = { git = "https://github.com/coblox/bitcoin-harness-rs", rev =
|
||||
futures = "0.3"
|
||||
hyper = "0.13"
|
||||
monero-harness = { path = "../monero-harness" }
|
||||
port_check = "0.1"
|
||||
spectral = "0.6"
|
||||
testcontainers = "0.10"
|
||||
tracing = "0.1"
|
||||
|
@ -1,5 +1,4 @@
|
||||
use anyhow::{anyhow, bail, Result};
|
||||
use lazy_static::lazy_static;
|
||||
use std::{
|
||||
future::Future,
|
||||
net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4},
|
||||
@ -10,33 +9,32 @@ use torut::{
|
||||
onion::TorSecretKeyV3,
|
||||
};
|
||||
|
||||
// lazy_static! {
|
||||
// The default TOR socks5 proxy address, `127.0.0.1:9050`.
|
||||
// pub static ref TOR_PROXY_ADDR: SocketAddrV4 =
|
||||
// SocketAddrV4::new(Ipv4Addr::LOCALHOST, 9050); The default TOR Controller
|
||||
// Protocol address, `127.0.0.1:9051`. pub static ref TOR_CP_ADDR: SocketAddr =
|
||||
// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 9051)); }
|
||||
|
||||
type Handler = fn(AsyncEvent<'_>) -> Box<dyn Future<Output = Result<(), ConnError>> + Unpin>;
|
||||
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct AuthenticatedConnection {
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct UnauthenticatedConnection {
|
||||
tor_proxy_address: SocketAddrV4,
|
||||
tor_control_port_address: SocketAddr,
|
||||
authenticated_connection: Option<AuthenticatedConn<TcpStream, Handler>>,
|
||||
}
|
||||
|
||||
impl Default for AuthenticatedConnection {
|
||||
impl Default for UnauthenticatedConnection {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
tor_proxy_address: SocketAddrV4::new(Ipv4Addr::LOCALHOST, 9050),
|
||||
tor_control_port_address: SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 9051)),
|
||||
authenticated_connection: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AuthenticatedConnection {
|
||||
impl UnauthenticatedConnection {
|
||||
pub fn with_ports(proxy_port: u16, control_port: u16) -> Self {
|
||||
Self {
|
||||
tor_proxy_address: SocketAddrV4::new(Ipv4Addr::LOCALHOST, proxy_port),
|
||||
tor_control_port_address: SocketAddr::V4(SocketAddrV4::new(
|
||||
Ipv4Addr::LOCALHOST,
|
||||
control_port,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
/// checks if tor is running
|
||||
async fn tor_running(&self) -> Result<()> {
|
||||
// Make sure you are running tor and this is your socks port
|
||||
@ -60,25 +58,14 @@ impl AuthenticatedConnection {
|
||||
}
|
||||
|
||||
async fn init_unauthenticated_connection(&self) -> Result<UnauthenticatedConn<TcpStream>> {
|
||||
// try to connect to local tor service via control port
|
||||
// Connect to local tor service via control port
|
||||
let sock = TcpStream::connect(self.tor_control_port_address).await?;
|
||||
let unauthenticated_connection = UnauthenticatedConn::new(sock);
|
||||
Ok(unauthenticated_connection)
|
||||
}
|
||||
|
||||
pub fn with_ports(proxy_port: u16, control_port: u16) -> Self {
|
||||
Self {
|
||||
tor_proxy_address: SocketAddrV4::new(Ipv4Addr::LOCALHOST, proxy_port),
|
||||
tor_control_port_address: SocketAddr::V4(SocketAddrV4::new(
|
||||
Ipv4Addr::LOCALHOST,
|
||||
control_port,
|
||||
)),
|
||||
authenticated_connection: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new authenticated connection to your local Tor service
|
||||
pub async fn connect(self) -> Result<Self> {
|
||||
pub async fn init_authenticated_connection(self) -> Result<AuthenticatedConnection> {
|
||||
self.tor_running().await?;
|
||||
|
||||
let mut unauthenticated_connection = match self.init_unauthenticated_connection().await {
|
||||
@ -105,17 +92,23 @@ impl AuthenticatedConnection {
|
||||
let authenticated_connection = unauthenticated_connection.into_authenticated().await;
|
||||
|
||||
Ok(AuthenticatedConnection {
|
||||
authenticated_connection: Some(authenticated_connection),
|
||||
..self
|
||||
authenticated_connection,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type Handler = fn(AsyncEvent<'_>) -> Box<dyn Future<Output = Result<(), ConnError>> + Unpin>;
|
||||
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct AuthenticatedConnection {
|
||||
authenticated_connection: AuthenticatedConn<TcpStream, Handler>,
|
||||
}
|
||||
|
||||
impl AuthenticatedConnection {
|
||||
/// Add an ephemeral tor service on localhost with the provided key
|
||||
pub async fn add_service(&mut self, port: u16, tor_key: &TorSecretKeyV3) -> Result<()> {
|
||||
match self.authenticated_connection {
|
||||
None => bail!("Not connected to local tor instance"),
|
||||
Some(ref mut aut) => {
|
||||
aut.add_onion_v3(
|
||||
self.authenticated_connection
|
||||
.add_onion_v3(
|
||||
tor_key,
|
||||
false,
|
||||
false,
|
||||
@ -129,9 +122,6 @@ impl AuthenticatedConnection {
|
||||
)
|
||||
.await
|
||||
.map_err(|_| anyhow!("Could not add onion service."))?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ mod tor_test {
|
||||
onion::TorSecretKeyV3,
|
||||
utils::{run_tor, AutoKillChild},
|
||||
};
|
||||
use xmr_btc::tor::AuthenticatedConnection;
|
||||
use xmr_btc::tor::UnauthenticatedConnection;
|
||||
|
||||
async fn hello_world(
|
||||
_req: hyper::Request<hyper::Body>,
|
||||
@ -36,8 +36,16 @@ mod tor_test {
|
||||
}
|
||||
|
||||
fn run_tmp_tor() -> (Child, u16, u16) {
|
||||
let control_port = 9051;
|
||||
let proxy_port = 9050;
|
||||
let control_port = if port_check::is_local_port_free(9051) {
|
||||
9051
|
||||
} else {
|
||||
port_check::free_local_port().unwrap()
|
||||
};
|
||||
let proxy_port = if port_check::is_local_port_free(9050) {
|
||||
9050
|
||||
} else {
|
||||
port_check::free_local_port().unwrap()
|
||||
};
|
||||
|
||||
(
|
||||
run_tor(
|
||||
@ -47,6 +55,8 @@ mod tor_test {
|
||||
"1",
|
||||
"--ControlPort",
|
||||
control_port.to_string().as_str(),
|
||||
"--SocksPort",
|
||||
proxy_port.to_string().as_str(),
|
||||
]
|
||||
.iter(),
|
||||
)
|
||||
@ -69,8 +79,8 @@ mod tor_test {
|
||||
|
||||
// Connect to local Tor service
|
||||
let mut authenticated_connection =
|
||||
AuthenticatedConnection::with_ports(proxy_port, control_port)
|
||||
.connect()
|
||||
UnauthenticatedConnection::with_ports(proxy_port, control_port)
|
||||
.init_authenticated_connection()
|
||||
.await?;
|
||||
|
||||
// Expose an onion service that re-directs to the echo server.
|
||||
|
Loading…
Reference in New Issue
Block a user