Debugging ...

This commit is contained in:
Thomas Eizinger 2021-06-25 17:30:43 +10:00
parent 89b6131252
commit 140ab200fc
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96
3 changed files with 30 additions and 62 deletions

View File

@ -13,15 +13,13 @@ use std::time::Duration;
use torut::control::AuthenticatedConn; use torut::control::AuthenticatedConn;
use torut::onion::TorSecretKeyV3; use torut::onion::TorSecretKeyV3;
use tracing_subscriber::util::SubscriberInitExt; use tracing_subscriber::util::SubscriberInitExt;
use libp2p_tor::duplex::TorutAsyncEventHandler;
#[tokio::main(flavor = "multi_thread")] #[tokio::main(flavor = "multi_thread")]
async fn main() { async fn main() {
let _guard = tracing_subscriber::fmt() tracing_subscriber::fmt()
.with_env_filter("debug,libp2p_tor=debug") // add `reqwest::connect::verbose=trace` if you want to logs of the RPC clients .with_env_filter("trace") // add `reqwest::connect::verbose=trace` if you want to logs of the RPC clients
.with_test_writer() .init();
.set_default();
tracing::debug!("test");
let key = fixed_onion_identity(); let key = fixed_onion_identity();
@ -31,37 +29,37 @@ async fn main() {
.get_address_without_dot_onion(); .get_address_without_dot_onion();
let onion_port = 7654; let onion_port = 7654;
let mut swarm = new_swarm().await; let mut client = AuthenticatedConn::new(9051).await.unwrap();
client
.add_ephemeral_service(&key, onion_port, onion_port)
.await
.unwrap();
let mut swarm = new_swarm(client, key).await;
let peer_id = *swarm.local_peer_id(); let peer_id = *swarm.local_peer_id();
println!("Peer-ID: {}", peer_id); tracing::info!("Peer-ID: {}", peer_id);
// TODO: Figure out what to with the port, we could also set it to 0 and then // TODO: Figure out what to with the port, we could also set it to 0 and then
// imply it from the assigned port swarm.listen_on(Multiaddr:: // imply it from the assigned port swarm.listen_on(Multiaddr::
// from_str(format!("/onion3/{}:{}", onion_address, // from_str(format!("/onion3/{}:{}", onion_address,
// onion_port).as_str()).unwrap()).unwrap(); // onion_port).as_str()).unwrap()).unwrap();
swarm swarm
.listen_on( .listen_on(
Multiaddr::from_str(format!("/ip4/127.0.0.1/tcp/{}", onion_port).as_str()).unwrap(), Multiaddr::from_str(format!("/onion3/{}:{}", onion_address, onion_port).as_str()).unwrap(),
) )
.unwrap(); .unwrap();
loop { loop {
match swarm.next_event().await { match swarm.next_event().await {
SwarmEvent::NewListenAddr(addr) => { SwarmEvent::NewListenAddr(addr) => {
println!("Listening on {}", addr); tracing::info!("Listening on {}", addr);
println!("Connection string: {}/p2p/{}", addr, peer_id); tracing::info!("Connection string: {}/p2p/{}", addr, peer_id);
AuthenticatedConn::new(9051)
.await
.unwrap()
.add_ephemeral_service(&key, onion_port, onion_port)
.await
.unwrap();
} }
SwarmEvent::ConnectionEstablished { SwarmEvent::ConnectionEstablished {
peer_id, endpoint, .. peer_id, endpoint, ..
} => { } => {
println!( tracing::info!(
"Connected to {} via {}", "Connected to {} via {}",
peer_id, peer_id,
endpoint.get_remote_address() endpoint.get_remote_address()
@ -69,17 +67,17 @@ async fn main() {
} }
SwarmEvent::Behaviour(PingEvent { result, peer }) => match result { SwarmEvent::Behaviour(PingEvent { result, peer }) => match result {
Ok(PingSuccess::Pong) => { Ok(PingSuccess::Pong) => {
println!("Got pong from {}", peer); tracing::info!("Got pong from {}", peer);
} }
Ok(PingSuccess::Ping { rtt }) => { Ok(PingSuccess::Ping { rtt }) => {
println!("Pinged {} with rtt of {}s", peer, rtt.as_secs()); tracing::info!("Pinged {} with rtt of {}s", peer, rtt.as_secs());
} }
Err(failure) => { Err(failure) => {
println!("Failed to ping {}: {}", peer, failure) tracing::info!("Failed to ping {}: {}", peer, failure)
} }
}, },
event => { event => {
println!("Swarm event: {:?}", event) tracing::debug!("Swarm event: {:?}", event)
} }
} }
} }
@ -90,11 +88,11 @@ async fn main() {
/// ///
/// In particular, this swarm can create ephemeral hidden services on the /// In particular, this swarm can create ephemeral hidden services on the
/// configured Tor node. /// configured Tor node.
async fn new_swarm() -> Swarm<Ping> { async fn new_swarm(client: AuthenticatedConn<tokio::net::TcpStream, TorutAsyncEventHandler>, key: TorSecretKeyV3) -> Swarm<Ping> {
let identity = fixed_libp2p_identity(); let identity = fixed_libp2p_identity();
SwarmBuilder::new( SwarmBuilder::new(
libp2p::tcp::TokioTcpConfig::new() duplex::TorConfig::new(client, key).await.unwrap()
.boxed() .boxed()
.upgrade(Version::V1) .upgrade(Version::V1)
.authenticate( .authenticate(

View File

@ -5,7 +5,7 @@ use futures::future::BoxFuture;
use futures::prelude::*; use futures::prelude::*;
use libp2p::core::multiaddr::{Multiaddr, Protocol}; use libp2p::core::multiaddr::{Multiaddr, Protocol};
use libp2p::core::transport::map_err::MapErr; use libp2p::core::transport::map_err::MapErr;
use libp2p::core::transport::{Boxed, ListenerEvent, TransportError}; use libp2p::core::transport::{ListenerEvent, TransportError};
use libp2p::core::Transport; use libp2p::core::Transport;
use libp2p::futures::stream::BoxStream; use libp2p::futures::stream::BoxStream;
use libp2p::futures::{StreamExt, TryStreamExt}; use libp2p::futures::{StreamExt, TryStreamExt};
@ -15,7 +15,7 @@ use tokio::sync::Mutex;
use torut::control::{AsyncEvent, AuthenticatedConn}; use torut::control::{AsyncEvent, AuthenticatedConn};
use torut::onion::TorSecretKeyV3; use torut::onion::TorSecretKeyV3;
type TorutAsyncEventHandler = pub type TorutAsyncEventHandler =
fn( fn(
AsyncEvent<'_>, AsyncEvent<'_>,
) -> Box<dyn Future<Output = Result<(), torut::control::ConnError>> + Unpin + Send>; ) -> Box<dyn Future<Output = Result<(), torut::control::ConnError>> + Unpin + Send>;
@ -31,13 +31,12 @@ pub struct TorConfig {
impl TorConfig { impl TorConfig {
pub async fn new( pub async fn new(
mut client: AuthenticatedConn<tokio::net::TcpStream, TorutAsyncEventHandler>, mut client: AuthenticatedConn<tokio::net::TcpStream, TorutAsyncEventHandler>,
// TODO: change to key directly
key: TorSecretKeyV3, key: TorSecretKeyV3,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
let socks_port = client.get_socks_port().await?; let socks_port = client.get_socks_port().await?;
Ok(Self { Ok(Self {
inner: TokioTcpConfig::new().map_err(Error::InnerTransport), inner: TokioTcpConfig::new().nodelay(true).map_err(Error::InnerTransport),
tor_client: Arc::new(Mutex::new(client)), tor_client: Arc::new(Mutex::new(client)),
key, key,
socks_port, socks_port,
@ -86,7 +85,7 @@ impl Transport for TorConfig {
let listener = self.inner.listen_on(localhost_tcp_random_port_addr)?; let listener = self.inner.listen_on(localhost_tcp_random_port_addr)?;
let tor_client = self.tor_client; let tor_client = self.tor_client.clone();
let listener = listener let listener = listener
.and_then({ .and_then({
@ -99,36 +98,7 @@ impl Transport for TorConfig {
async move { async move {
Ok(match event { Ok(match event {
ListenerEvent::NewAddress(address) => { ListenerEvent::NewAddress(address) => {
let local_port = address ListenerEvent::NewAddress(onion_multiaddress.clone())
.iter()
.find_map(|p| match p {
Protocol::Tcp(port) => Some(port),
_ => None,
})
.expect("TODO: Error handling");
// TODO: Don't fully understand this part, why would we have two
// different multiaddresses here? the actual onion address and the
// multiaddress would make more sense...?
tracing::debug!(
"Setting up hidden service at {} to forward to {}",
onion_multiaddress,
address
);
match tor_client
.clone()
.lock()
.await
// TODO: Potentially simplify this, in our setup the onion port
// is always equal to the local port. Otherwise we would have
// the user provide an additional port for the oion service.
.add_ephemeral_service(&key, onion_port, local_port)
.await
{
Ok(()) => ListenerEvent::NewAddress(onion_multiaddress.clone()),
Err(e) => ListenerEvent::Error(Error::Torut(e)),
}
} }
ListenerEvent::Upgrade { ListenerEvent::Upgrade {
upgrade, upgrade,

View File

@ -83,8 +83,8 @@ impl AuthenticatedConnectionExt for AuthenticatedConn<tokio::net::TcpStream, Asy
onion_port: u16, onion_port: u16,
local_port: u16, local_port: u16,
) -> Result<(), Error> { ) -> Result<(), Error> {
println!( tracing::debug!(
"Adding ephemeral service, onion port {}, local port {}", "Attempting to add ephemeral service, onion port {}, local port {}",
onion_port, local_port onion_port, local_port
); );