Skip to content

Commit

Permalink
fix(comms): only set final forward address if configured to port 0 (#…
Browse files Browse the repository at this point in the history
…5406)

Description
---
Sets the onion service forward address to the correct port only if
configured to port 0.

Motivation and Context
---
Fixes #5405 

How Has This Been Tested?
---
Manually, default settings were also tested.

What process can a PR reviewer use to test or verify this change?
---
```toml
# This doesnt work (no incoming connections as expected)
#tor.forward_address = "/dns4/localhost/tcp/12345"
# This does work
tor.forward_address = "/ip4/10.71.1.141/tcp/12345"
tor.listener_address_override = "/ip4/10.71.1.141/tcp/12345"
```

Breaking Changes
---

- [x] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify

<!-- Does this include a breaking change? If so, include this line as a
footer -->
<!-- BREAKING CHANGE: Description what the user should do, e.g. delete a
database, resync the chain -->

Co-authored-by: SW van Heerden <[email protected]>
  • Loading branch information
sdbondi and SWvheerden authored May 25, 2023
1 parent c704890 commit ff7fb6d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
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)
}
}

0 comments on commit ff7fb6d

Please sign in to comment.