Skip to content

Commit

Permalink
feat(p2p): allow listener bind to be different to tor forward address
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Apr 26, 2023
1 parent 1ac81f0 commit a46555a
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 9 deletions.
1 change: 0 additions & 1 deletion base_layer/p2p/examples/gen_tor_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ async fn main() {
.with_port_mapping(port)
.with_control_server_address(tor_control_addr)
.build()
.await
.unwrap()
.create_hidden_service()
.await
Expand Down
8 changes: 6 additions & 2 deletions base_layer/p2p/src/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,16 @@ pub async fn spawn_comms_using_transport(
TransportType::Tor => {
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?;
// 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");

comms
.with_listener_address(hidden_service_ctl.proxied_address())
.with_listener_address(
listener_address_override.unwrap_or_else(|| hidden_service_ctl.proxied_address()),
)
.with_hidden_service_controller(hidden_service_ctl)
.spawn_with_transport(transport)
.await?
Expand Down Expand Up @@ -290,7 +294,7 @@ async fn initialize_hidden_service(
builder = builder.with_tor_identity(identity);
}

let hidden_svc_ctl = builder.build().await?;
let hidden_svc_ctl = builder.build()?;
Ok(hidden_svc_ctl)
}

Expand Down
6 changes: 5 additions & 1 deletion base_layer/p2p/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,11 @@ pub struct TorTransportConfig {
/// When set to true, outbound TCP connections bypass the tor proxy. Defaults to false for better privacy, setting
/// to true may improve network performance for TCP nodes.
pub proxy_bypass_for_outbound_tcp: bool,
/// If set, instructs tor to forward traffic the the provided address.
/// If set, instructs tor to forward traffic the the provided address. Otherwise, an OS-assigned port on 127.0.0.1
/// is used.
pub forward_address: Option<Multiaddr>,
/// If set, the listener will bind to this address instead of the forward_address.
pub listener_address_override: Option<Multiaddr>,
/// The tor identity to use to create the hidden service. If None, a new one will be generated.
#[serde(skip)]
pub identity: Option<TorIdentity>,
Expand Down Expand Up @@ -195,6 +198,7 @@ impl Default for TorTransportConfig {
proxy_bypass_addresses: vec![],
proxy_bypass_for_outbound_tcp: false,
forward_address: None,
listener_address_override: None,
identity: None,
}
}
Expand Down
4 changes: 3 additions & 1 deletion common/config/presets/c_base_node.toml
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,10 @@ listener_liveness_check_interval = 15
# When using the tor transport and set to true, outbound TCP connections bypass the tor proxy. Defaults to false for
# better privacy
#tor.proxy_bypass_for_outbound_tcp = false
# If set, instructs tor to forward traffic the the provided address. (e.g. "/ip4/127.0.0.1/tcp/0") (default = )
# If set, instructs tor to forward traffic the the provided address. (e.g. "/dns4/my-base-node/tcp/32123") (default = OS-assigned port)
#tor.forward_address =
# If set, the listener will bind to this address instead of the forward_address. You need to make sure that this listener is connectable from the forward_address.
#tor.listener_address_override =

# Use a SOCKS5 proxy transport. This transport recognises any addresses supported by the proxy.
# (use: type = "socks5")
Expand Down
2 changes: 1 addition & 1 deletion comms/core/examples/stress/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub async fn create(
hs_builder = hs_builder.with_tor_identity(tor_identity);
}

let mut hs_ctl = hs_builder.build().await?;
let mut hs_ctl = hs_builder.build()?;
let transport = hs_ctl.initialize_transport().await?;

builder
Expand Down
2 changes: 1 addition & 1 deletion comms/core/examples/tor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ async fn setup_node_with_tor<P: Into<tor::PortMapping>>(
hs_builder = hs_builder.with_tor_identity(ident);
}

let mut hs_controller = hs_builder.build().await?;
let mut hs_controller = hs_builder.build()?;

let node_identity = Arc::new(NodeIdentity::random(
&mut OsRng,
Expand Down
2 changes: 1 addition & 1 deletion comms/core/src/tor/hidden_service/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl HiddenServiceBuilder {

impl HiddenServiceBuilder {
/// Create a HiddenService with the given builder parameters.
pub async fn build(self) -> Result<HiddenServiceController, HiddenServiceBuilderError> {
pub fn build(self) -> Result<HiddenServiceController, HiddenServiceBuilderError> {
let proxied_port_mapping = self
.port_mapping
.ok_or(HiddenServiceBuilderError::ProxiedPortMappingNotProvided)?;
Expand Down
2 changes: 1 addition & 1 deletion comms/dht/examples/propagation/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub async fn create<P: AsRef<Path>>(
hs_builder = hs_builder.with_tor_identity(tor_identity);
}

let mut hs_ctl = hs_builder.build().await?;
let mut hs_ctl = hs_builder.build()?;
let transport = hs_ctl.initialize_transport().await?;

let comms_node = builder.with_listener_address(hs_ctl.proxied_address()).build()?;
Expand Down

0 comments on commit a46555a

Please sign in to comment.