From 497732e74a934993754aec3b079edf084a390a70 Mon Sep 17 00:00:00 2001 From: SW van Heerden Date: Mon, 22 Jan 2024 17:40:45 +0200 Subject: [PATCH] first compiling draft --- Cargo.lock | 107 ++++++++++++++---- .../minotari_console_wallet/src/init/mod.rs | 10 +- applications/minotari_node/src/bootstrap.rs | 21 ++-- .../src/chat_client/src/networking.rs | 23 ++-- base_layer/p2p/src/initialization.rs | 24 ++-- base_layer/wallet/src/wallet.rs | 13 ++- comms/core/src/builder/comms_node.rs | 46 +------- .../core/src/tor/hidden_service/controller.rs | 5 +- .../transports/hidden_service_transport.rs | 59 ++++++---- comms/core/src/transports/mod.rs | 3 +- 10 files changed, 177 insertions(+), 134 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6baf7f37f1..ac607c1d89 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -95,9 +95,9 @@ checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" [[package]] name = "anstream" -version = "0.6.4" +version = "0.6.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44" +checksum = "6e2e1ebcb11de5c03c67de28a7df593d32191b44939c482e97702baaaa6ab6a5" dependencies = [ "anstyle", "anstyle-parse", @@ -115,30 +115,30 @@ checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" [[package]] name = "anstyle-parse" -version = "0.2.2" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "317b9a89c1868f5ea6ff1d9539a69f45dffc21ce321ac1fd1160dfa48c8e2140" +checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" dependencies = [ "utf8parse", ] [[package]] name = "anstyle-query" -version = "1.0.0" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" dependencies = [ - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] name = "anstyle-wincon" -version = "3.0.1" +version = "3.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628" +checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" dependencies = [ "anstyle", - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] @@ -2162,15 +2162,15 @@ dependencies = [ [[package]] name = "globset" -version = "0.4.13" +version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "759c97c1e17c55525b57192c06a267cda0ac5210b222d6b82189a2338fa1c13d" +checksum = "57da3b9b5b85bd66f31093f8c408b90a74431672542466497dcbdfdc02034be1" dependencies = [ "aho-corasick", "bstr", - "fnv", "log", - "regex", + "regex-automata 0.4.3", + "regex-syntax 0.8.2", ] [[package]] @@ -2500,17 +2500,16 @@ dependencies = [ [[package]] name = "ignore" -version = "0.4.20" +version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbe7873dab538a9a44ad79ede1faf5f30d49f9a5c883ddbab48bce81b64b7492" +checksum = "b46810df39e66e925525d6e38ce1e7f6e1d208f72dc39757880fcb66e2c58af1" dependencies = [ + "crossbeam-deque", "globset", - "lazy_static", "log", "memchr", - "regex", + "regex-automata 0.4.3", "same-file", - "thread_local", "walkdir", "winapi-util", ] @@ -2610,9 +2609,9 @@ checksum = "8bb03732005da905c88227371639bf1ad885cc712789c011c31c5fb3ab3ccf02" [[package]] name = "inventory" -version = "0.3.13" +version = "0.3.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0508c56cfe9bfd5dfeb0c22ab9a6abfda2f27bdca422132e494266351ed8d83c" +checksum = "c8573b2b1fb643a372c73b23f4da5f888677feef3305146d68a539250a9bccc7" [[package]] name = "ipnet" @@ -7191,6 +7190,15 @@ dependencies = [ "windows-targets 0.48.5", ] +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.0", +] + [[package]] name = "windows-targets" version = "0.42.2" @@ -7221,6 +7229,21 @@ dependencies = [ "windows_x86_64_msvc 0.48.5", ] +[[package]] +name = "windows-targets" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" +dependencies = [ + "windows_aarch64_gnullvm 0.52.0", + "windows_aarch64_msvc 0.52.0", + "windows_i686_gnu 0.52.0", + "windows_i686_msvc 0.52.0", + "windows_x86_64_gnu 0.52.0", + "windows_x86_64_gnullvm 0.52.0", + "windows_x86_64_msvc 0.52.0", +] + [[package]] name = "windows_aarch64_gnullvm" version = "0.42.2" @@ -7233,6 +7256,12 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" + [[package]] name = "windows_aarch64_msvc" version = "0.42.2" @@ -7245,6 +7274,12 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" + [[package]] name = "windows_i686_gnu" version = "0.42.2" @@ -7257,6 +7292,12 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" +[[package]] +name = "windows_i686_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" + [[package]] name = "windows_i686_msvc" version = "0.42.2" @@ -7269,6 +7310,12 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" +[[package]] +name = "windows_i686_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" + [[package]] name = "windows_x86_64_gnu" version = "0.42.2" @@ -7281,6 +7328,12 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" + [[package]] name = "windows_x86_64_gnullvm" version = "0.42.2" @@ -7293,6 +7346,12 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" + [[package]] name = "windows_x86_64_msvc" version = "0.42.2" @@ -7305,6 +7364,12 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" + [[package]] name = "winnow" version = "0.5.18" diff --git a/applications/minotari_console_wallet/src/init/mod.rs b/applications/minotari_console_wallet/src/init/mod.rs index 636faa2567..d37acb7763 100644 --- a/applications/minotari_console_wallet/src/init/mod.rs +++ b/applications/minotari_console_wallet/src/init/mod.rs @@ -22,8 +22,7 @@ #![allow(dead_code, unused)] -use std::{fs, path::PathBuf, str::FromStr, sync::Arc}; -use std::time::Instant; +use std::{fs, path::PathBuf, str::FromStr, sync::Arc, time::Instant}; use log::*; use minotari_app_utilities::identity_management::setup_node_identity; @@ -466,13 +465,6 @@ pub async fn init_wallet( WalletError::CommsInitializationError(cie) => cie.to_exit_error(), e => ExitError::new(ExitCode::WalletError, format!("Error creating Wallet Container: {}", e)), })?; - // TODO: fix this - // if let Some(hs) = wallet.comms.hidden_service() { - // wallet - // .db - // .set_tor_identity(hs.tor_identity().clone()) - // .map_err(|e| ExitError::new(ExitCode::WalletError, format!("Problem writing tor identity. {}", e)))?; - // } error!( target: LOG_TARGET, diff --git a/applications/minotari_node/src/bootstrap.rs b/applications/minotari_node/src/bootstrap.rs index dce1a5b28c..fdc1d3d6ff 100644 --- a/applications/minotari_node/src/bootstrap.rs +++ b/applications/minotari_node/src/bootstrap.rs @@ -164,10 +164,20 @@ where B: BlockchainBackend + 'static let comms = comms.add_protocol_extension(mempool_protocol); let comms = Self::setup_rpc_services(comms, &handles, self.db.into(), &p2p_config); - let comms = initialization::spawn_comms_using_transport(comms, p2p_config.transport.clone()) - .await - .map_err(|e| e.to_exit_error())?; + let comms = if p2p_config.transport.transport_type == TransportType::Tor { + let path = base_node_config.tor_identity_file.clone(); + let after_comms = move |identity| { + let _result = identity_management::save_as_json(&path, &identity); + trace!(target: LOG_TARGET, "resave the chat tor identity {:?}", identity); + }; + initialization::spawn_comms_using_transport(comms, p2p_config.transport.clone(), after_comms).await + } else { + let after_comms = |_identity| {}; + initialization::spawn_comms_using_transport(comms, p2p_config.transport.clone(), after_comms).await + }; + + let comms = comms.map_err(|e| e.to_exit_error())?; // Save final node identity after comms has initialized. This is required because the public_address can be // changed by comms during initialization when using tor. match p2p_config.transport.transport_type { @@ -177,11 +187,6 @@ where B: BlockchainBackend + 'static .map_err(|e| ExitError::new(ExitCode::IdentityError, e))?; }, }; - todo!("Fix this"); - // if let Some(hs) = comms.hidden_service() { - // identity_management::save_as_json(&base_node_config.tor_identity_file, hs.tor_identity()) - // .map_err(|e| ExitError::new(ExitCode::IdentityError, e))?; - // } handles.register(comms); diff --git a/base_layer/contacts/src/chat_client/src/networking.rs b/base_layer/contacts/src/chat_client/src/networking.rs index b42d9a41be..1193fdee00 100644 --- a/base_layer/contacts/src/chat_client/src/networking.rs +++ b/base_layer/contacts/src/chat_client/src/networking.rs @@ -29,7 +29,7 @@ pub use tari_comms::{ multiaddr::Multiaddr, peer_manager::{NodeIdentity, PeerFeatures}, }; -use tari_comms::{peer_manager::Peer, CommsNode, UnspawnedCommsNode}; +use tari_comms::{peer_manager::Peer, CommsNode, UnspawnedCommsNode}; use tari_contacts::contacts_service::{handle::ContactsServiceHandle, ContactsServiceInitializer}; use tari_p2p::{ comms_connector::pubsub_connector, @@ -109,10 +109,17 @@ pub async fn start( for peer in seed_peers { peer_manager.add_peer(peer).await?; } - - let comms = spawn_comms_using_transport(comms, p2p_config.transport.clone()).await?; - - // Save final node identity after comms has initialized. This is required because the public_address can be + let comms = if p2p_config.transport.transport_type == TransportType::Tor { + let path = config.chat_client.tor_identity_file.clone(); + let after_comms = move |identity| { + let _result = identity_management::save_as_json(&path, &identity); + trace!(target: LOG_TARGET, "resave the chat tor identity {:?}", identity); + }; + spawn_comms_using_transport(comms, p2p_config.transport.clone(), after_comms).await? + } else { + let after_comms = |_identity| {}; + spawn_comms_using_transport(comms, p2p_config.transport.clone(), after_comms).await? + }; // changed by comms during initialization when using tor. match p2p_config.transport.transport_type { TransportType::Tcp => {}, // Do not overwrite TCP public_address in the base_node_id! @@ -121,11 +128,7 @@ pub async fn start( trace!(target: LOG_TARGET, "save chat identity file"); }, }; - todo!("Fix this"); - // if let Some(hs) = comms.hidden_service() { - // identity_management::save_as_json(&config.chat_client.tor_identity_file, hs.tor_identity())?; - // trace!(target: LOG_TARGET, "resave the chat tor identity {:?}", hs.tor_identity()); - // } + handles.register(comms); let comms = handles.expect_handle::(); diff --git a/base_layer/p2p/src/initialization.rs b/base_layer/p2p/src/initialization.rs index 5514e963c6..d4cf62ca97 100644 --- a/base_layer/p2p/src/initialization.rs +++ b/base_layer/p2p/src/initialization.rs @@ -51,8 +51,15 @@ use tari_comms::{ ProtocolId, }, tor, - tor::HiddenServiceControllerError, - transports::{predicate::FalsePredicate, MemoryTransport, SocksConfig, SocksTransport, TcpWithTorTransport}, + tor::{HiddenServiceControllerError, TorIdentity}, + transports::{ + predicate::FalsePredicate, + HiddenServiceTransport, + MemoryTransport, + SocksConfig, + SocksTransport, + TcpWithTorTransport, + }, utils::cidr::parse_cidrs, CommsBuilder, CommsBuilderError, @@ -70,7 +77,6 @@ use tari_storage::{ use thiserror::Error; use tokio::sync::{broadcast, mpsc}; use tower::ServiceBuilder; -use tari_comms::transports::HiddenServiceTransport; use crate::{ comms_connector::{InboundDomainConnector, PubsubDomainConnector}, @@ -210,9 +216,10 @@ pub async fn initialize_local_test_comms>( Ok((comms, dht, event_sender)) } -pub async fn spawn_comms_using_transport( +pub async fn spawn_comms_using_transport( comms: UnspawnedCommsNode, transport_config: TransportConfig, + after_comms: F, ) -> Result { let comms = match transport_config.transport_type { TransportType::Memory => { @@ -250,19 +257,16 @@ pub async fn spawn_comms_using_transport( let tor_config = transport_config.tor; debug!(target: LOG_TARGET, "Building TOR comms stack ({:?})", tor_config); let listener_address_override = tor_config.listener_address_override.clone(); - let mut hidden_service_ctl = initialize_hidden_service(tor_config)?; + let hidden_service_ctl = initialize_hidden_service(tor_config)?; // Set the listener address to be the address (usually local) to which tor will forward all traffic let instant = Instant::now(); - let transport = HiddenServiceTransport::new(hidden_service_ctl); - error!(target: LOG_TARGET, "TOR transport initialized in {:.0?}", instant.elapsed()); - - + let transport = HiddenServiceTransport::new(hidden_service_ctl, after_comms); + debug!(target: LOG_TARGET, "TOR transport initialized in {:.0?}", instant.elapsed()); comms .with_listener_address( listener_address_override.unwrap_or_else(|| multiaddr![Ip4([127, 0, 0, 1]), Tcp(0u16)]), ) - // .with_hidden_service_controller(hidden_service_ctl) .spawn_with_transport(transport) .await? }, diff --git a/base_layer/wallet/src/wallet.rs b/base_layer/wallet/src/wallet.rs index ce2d79f9e1..947937f151 100644 --- a/base_layer/wallet/src/wallet.rs +++ b/base_layer/wallet/src/wallet.rs @@ -36,6 +36,7 @@ use tari_comms::{ multiaddr::Multiaddr, net_address::{MultiaddressesWithStats, PeerAddressSource}, peer_manager::{NodeId, Peer, PeerFeatures, PeerFlags}, + tor::TorIdentity, types::{CommsPublicKey, CommsSecretKey}, CommsNode, NodeIdentity, @@ -72,6 +73,7 @@ use tari_p2p::{ initialization::P2pInitializer, services::liveness::{config::LivenessConfig, LivenessInitializer}, PeerSeedsConfig, + TransportType, }; use tari_script::{one_sided_payment_script, ExecutionStack, TariScript}; use tari_service_framework::StackBuilder; @@ -255,7 +257,16 @@ where let comms = handles .take_handle::() .expect("P2pInitializer was not added to the stack"); - let comms = initialization::spawn_comms_using_transport(comms, config.p2p.transport).await?; + let comms = if config.p2p.transport.transport_type == TransportType::Tor { + let wallet_db = wallet_database.clone(); + let after_comms = move |identity: TorIdentity| { + let _result = wallet_db.set_tor_identity(identity.clone()); + }; + initialization::spawn_comms_using_transport(comms, config.p2p.transport, after_comms).await? + } else { + let after_comms = |_identity| {}; + initialization::spawn_comms_using_transport(comms, config.p2p.transport, after_comms).await? + }; let mut output_manager_handle = handles.expect_handle::(); let key_manager_handle = handles.expect_handle::(); diff --git a/comms/core/src/builder/comms_node.rs b/comms/core/src/builder/comms_node.rs index 3b2d8e4c6c..92824ee1b8 100644 --- a/comms/core/src/builder/comms_node.rs +++ b/comms/core/src/builder/comms_node.rs @@ -21,10 +21,8 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use std::{iter, sync::Arc, time::Duration}; -use std::time::Instant; use log::*; -use multiaddr::{multiaddr, Protocol}; use tari_shutdown::ShutdownSignal; use tokio::{ io::{AsyncRead, AsyncWrite}, @@ -38,7 +36,6 @@ use crate::{ ConnectionManagerEvent, ConnectionManagerRequest, ConnectionManagerRequester, - ListenerInfo, LivenessCheck, LivenessStatus, }, @@ -144,7 +141,7 @@ impl UnspawnedCommsNode { let UnspawnedCommsNode { builder, connection_manager_request_rx, - mut connection_manager_requester, + connection_manager_requester, connectivity_requester, connectivity_rx, node_identity, @@ -156,7 +153,6 @@ impl UnspawnedCommsNode { let CommsBuilder { dial_backoff, - hidden_service_ctl, connection_manager_config, connectivity_config, .. @@ -218,31 +214,6 @@ impl UnspawnedCommsNode { "Your node's network ID is '{}'", node_identity.node_id() ); - - // let instant = Instant::now(); - // - // let listening_info = connection_manager_requester.wait_until_listening().await?; - // error!(target: LOG_TARGET, "Waited for {} to connect", instant.elapsed().as_millis()); - // Final setup of the hidden service. - // let mut hidden_service = None; - // if let Some(mut ctl) = hidden_service_ctl { - // // Only set the address to the bind address it is set to TCP port 0 - // let mut proxied_addr = ctl.proxied_address(); - // if proxied_addr.ends_with(&multiaddr!(Tcp(0u16))) { - // // Remove the TCP port 0 address and replace it with the actual listener port - // if let Some(Protocol::Tcp(port)) = listening_info.bind_address().iter().last() { - // proxied_addr.pop(); - // proxied_addr.push(Protocol::Tcp(port)); - // ctl.set_proxied_addr(&proxied_addr); - // } - // } - // let hs = ctl.create_hidden_service().await?; - // let onion_addr = hs.get_onion_address(); - // if !node_identity.public_addresses().contains(&onion_addr) { - // node_identity.add_public_address(onion_addr); - // } - // hidden_service = Some(hs); - // } info!( target: LOG_TARGET, "Your node's public addresses are '{}'", @@ -272,7 +243,6 @@ impl UnspawnedCommsNode { node_identity, peer_manager, liveness_watch, - // hidden_service, complete_signals: ext_context.drain_complete_signals(), }) } @@ -314,12 +284,8 @@ pub struct CommsNode { node_identity: Arc, /// Shared PeerManager instance peer_manager: Arc, - /// The bind addresses of the listener(s) - // listening_info: ListenerInfo, /// Current liveness status liveness_watch: watch::Receiver, - /// `Some` if the comms node is configured to run via a hidden service, otherwise `None` - //hidden_service: Option, /// The 'reciprocal' shutdown signals for each comms service complete_signals: Vec, } @@ -350,16 +316,6 @@ impl CommsNode { &self.node_identity } - /// Return the Ip/Tcp address that this node is listening on - // pub fn listening_address(&self) -> &Multiaddr { - // self.listening_info.bind_address() - // } - - /// Return [ListenerInfo] - // pub fn listening_info(&self) -> &ListenerInfo { - // &self.listening_info - // } - /// Returns the current liveness status pub fn liveness_status(&self) -> LivenessStatus { *self.liveness_watch.borrow() diff --git a/comms/core/src/tor/hidden_service/controller.rs b/comms/core/src/tor/hidden_service/controller.rs index d5a60089ca..87da0b61df 100644 --- a/comms/core/src/tor/hidden_service/controller.rs +++ b/comms/core/src/tor/hidden_service/controller.rs @@ -83,7 +83,7 @@ pub struct HiddenServiceController { proxied_port_mapping: PortMapping, socks_address_override: Option, socks_auth: socks::Authentication, - identity: Option, + pub identity: Option, hs_flags: HsFlags, is_authenticated: bool, proxy_opts: TorProxyOpts, @@ -124,9 +124,7 @@ impl HiddenServiceController { } pub async fn initialize_transport(&mut self) -> Result { - dbg!("here3"); self.connect_and_auth().await?; - dbg!("here4"); let socks_addr = self.get_socks_address().await?; Ok(SocksTransport::new(SocksConfig { proxy_address: socks_addr, @@ -237,7 +235,6 @@ impl HiddenServiceController { } fn client_mut(&mut self) -> Result<&mut TorControlPortClient, HiddenServiceControllerError> { - dbg!("here5"); self.client .as_mut() .filter(|c| c.is_connected()) diff --git a/comms/core/src/transports/hidden_service_transport.rs b/comms/core/src/transports/hidden_service_transport.rs index d5f94400da..f8736cf591 100644 --- a/comms/core/src/transports/hidden_service_transport.rs +++ b/comms/core/src/transports/hidden_service_transport.rs @@ -20,14 +20,16 @@ // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use std::io; -use std::sync::Arc; -use log::info; +use std::{io, io::ErrorKind, sync::Arc}; + +use log::*; use multiaddr::Multiaddr; use tokio::sync::RwLock; -use crate::tor::HiddenServiceController; -use crate::transports::{SocksTransport, TcpTransport, Transport}; -use crate::transports::tcp::TcpInbound; + +use crate::{ + tor::{HiddenServiceController, TorIdentity}, + transports::{SocksTransport, Transport}, +}; const LOG_TARGET: &str = "comms::transports::hidden_service_transport"; @@ -37,27 +39,27 @@ pub enum HiddenServiceTransportError { HiddenServiceControllerError(#[from] crate::tor::HiddenServiceControllerError), #[error("Tor hidden service socks error: `{0}`")] SocksTransportError(#[from] io::Error), - } struct HiddenServiceTransportInner { socks_transport: Option, - hidden_service_ctl: HiddenServiceController - + hidden_service_ctl: HiddenServiceController, } #[derive(Clone)] -pub struct HiddenServiceTransport { - inner: Arc> +pub struct HiddenServiceTransport { + inner: Arc>, + after_init: F, } -impl HiddenServiceTransport { - pub fn new(hidden_service_ctl: HiddenServiceController) -> Self { +impl HiddenServiceTransport { + pub fn new(hidden_service_ctl: HiddenServiceController, after_init: F) -> Self { Self { - inner : Arc::new(RwLock::new(HiddenServiceTransportInner { + inner: Arc::new(RwLock::new(HiddenServiceTransportInner { socks_transport: None, - hidden_service_ctl - })) + hidden_service_ctl, + })), + after_init, } } @@ -67,7 +69,21 @@ impl HiddenServiceTransport { drop(inner); let mut mut_inner = self.inner.write().await; if mut_inner.socks_transport.is_none() { - let transport = mut_inner.hidden_service_ctl.initialize_transport().await.expect("TODO NEED TO MAP THESE ERRORS SOMEHOW"); + let transport = mut_inner.hidden_service_ctl.initialize_transport().await.map_err(|e| { + error!( + target: LOG_TARGET, + "Error initializing hidden transport service stack{}", + e + ); + io::Error::new(ErrorKind::Other, e.to_string()) + })?; + (self.after_init)( + mut_inner + .hidden_service_ctl + .identity + .clone() + .ok_or(io::Error::new(ErrorKind::Other, "Missing tor identity".to_string()))?, + ); mut_inner.socks_transport = Some(transport); } } @@ -75,20 +91,15 @@ impl HiddenServiceTransport { } } #[crate::async_trait] -impl Transport for HiddenServiceTransport { - type Output = ::Output; +impl Transport for HiddenServiceTransport { type Error = ::Error; type Listener = ::Listener; + type Output = ::Output; async fn listen(&self, addr: &Multiaddr) -> Result<(Self::Listener, Multiaddr), Self::Error> { self.ensure_initialized().await?; let inner = self.inner.read().await; - // info!( - // target: LOG_TARGET, - // "Tor hidden service initialized. proxied_address = '{:?}'", - // inner.proxied_address(), - // ); Ok(inner.socks_transport.as_ref().unwrap().listen(addr).await?) } diff --git a/comms/core/src/transports/mod.rs b/comms/core/src/transports/mod.rs index 6419985302..1c4d40dd1b 100644 --- a/comms/core/src/transports/mod.rs +++ b/comms/core/src/transports/mod.rs @@ -47,10 +47,9 @@ pub use socks::{SocksConfig, SocksTransport}; mod tcp; pub use tcp::TcpTransport; -mod tcp_with_tor; mod hidden_service_transport; +mod tcp_with_tor; pub use hidden_service_transport::HiddenServiceTransport; - pub use tcp_with_tor::TcpWithTorTransport; /// Defines an abstraction for implementations that can dial and listen for connections over a provided address.