Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(comms): only set final forward address if configured to port 0 #5406

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions base_layer/p2p/src/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use tari_common::{
};
use tari_comms::{
backoff::ConstantBackoff,
multiaddr::multiaddr,
peer_manager::{NodeIdentity, Peer, PeerFeatures, PeerFlags, PeerManagerError},
pipeline,
protocol::{
Expand Down Expand Up @@ -249,14 +250,20 @@ 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).await?;
let mut 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 transport = hidden_service_ctl.initialize_transport().await?;
debug!(target: LOG_TARGET, "Comms and DHT configured");

info!(
target: LOG_TARGET,
"Tor hidden service initialized. proxied_address = '{:?}', listener_override_address = {:?}",
hidden_service_ctl.proxied_address(),
listener_address_override,
);

comms
.with_listener_address(
listener_address_override.unwrap_or_else(|| hidden_service_ctl.proxied_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)
Expand All @@ -275,7 +282,7 @@ pub async fn spawn_comms_using_transport(
Ok(comms)
}

async fn initialize_hidden_service(
fn initialize_hidden_service(
mut config: TorTransportConfig,
) -> Result<tor::HiddenServiceController, CommsInitializationError> {
let mut builder = tor::HiddenServiceBuilder::new()
Expand Down
14 changes: 13 additions & 1 deletion comms/core/src/builder/comms_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use std::{iter, sync::Arc, time::Duration};

use log::*;
use multiaddr::{multiaddr, Protocol};
use tari_shutdown::ShutdownSignal;
use tokio::{
io::{AsyncRead, AsyncWrite},
Expand Down Expand Up @@ -222,9 +223,20 @@ impl UnspawnedCommsNode {
);

let listening_info = connection_manager_requester.wait_until_listening().await?;

// Final setup of the hidden service.
let mut hidden_service = None;
if let Some(mut ctl) = hidden_service_ctl {
ctl.set_proxied_addr(listening_info.bind_address());
// 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) {
Expand Down
2 changes: 1 addition & 1 deletion comms/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub mod traits;

pub mod multiaddr {
// Re-export so that client code does not have to have multiaddr as a dependency
pub use ::multiaddr::{Error, Multiaddr, Protocol};
pub use ::multiaddr::{multiaddr, Error, Multiaddr, Protocol};
}

pub use async_trait::async_trait;
Expand Down
8 changes: 7 additions & 1 deletion comms/core/src/tor/control_client/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub enum PrivateKey {

/// Represents a mapping between an onion port and a proxied address (usually 127.0.0.1:xxxx).
/// If the proxied_address is not specified, the default `127.0.0.1:[onion_port]` will be used.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[derive(Clone, Copy, Serialize, Deserialize)]
pub struct PortMapping {
onion_port: u16,
proxied_address: SocketAddr,
Expand Down Expand Up @@ -146,3 +146,9 @@ impl fmt::Display for PortMapping {
write!(f, "PortMapping [{} -> {}]", self.onion_port, self.proxied_address)
}
}

impl fmt::Debug for PortMapping {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}