Skip to content

Commit

Permalink
Farcasterd: Add config for Tor hidden service creation
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCharlatan committed Dec 27, 2022
1 parent 56cb365 commit 9fe6f0e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/bus/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub struct WrapOnionAddressV3(pub OnionAddressV3);
impl StrictEncode for WrapOnionAddressV3 {
fn strict_encode<E: std::io::Write>(&self, mut e: E) -> Result<usize, strict_encoding::Error> {
let address_encoding = self.0.get_public_key().as_bytes().to_vec();
Ok(address_encoding.strict_encode(&mut e)?)
address_encoding.strict_encode(&mut e)
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,32 @@ impl Config {
}
}

/// Returns the addr of the tor control socket if it is set, or the default
/// socket at localhost:9051 if not.
pub fn get_tor_control_socket(&self) -> Result<InetSocketAddr, Error> {
if let Some(FarcasterdConfig {
tor_control_socket: Some(addr),
..
}) = &self.farcasterd
{
Ok(InetSocketAddr::from_str(addr)?)
} else {
Ok(InetSocketAddr::from_str("127.0.0.1:9051")?)
}
}

pub fn create_hidden_service(&self) -> bool {
if let Some(FarcasterdConfig {
create_hidden_service: Some(create_hidden_service),
..
}) = &self.farcasterd
{
*create_hidden_service
} else {
false
}
}

/// Returns the swap config for the specified network and arbitrating/accordant blockchains
pub fn get_swap_config(
&self,
Expand Down Expand Up @@ -230,6 +256,11 @@ pub struct FarcasterdConfig {
pub bind_ip: Option<String>,
/// Whether checkpoints should be auto restored at start-up, or not
pub auto_restore: Option<bool>,
/// Tor control socket for creating the hidden service
pub tor_control_socket: Option<String>,
/// Whether to create a hidden service or not. If set, the node will only
/// run in hidden service mode
pub create_hidden_service: Option<bool>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
Expand Down Expand Up @@ -432,6 +463,8 @@ impl Default for FarcasterdConfig {
// write the default port and ip in the generated config
bind_port: Some(FARCASTER_BIND_PORT),
bind_ip: Some(FARCASTER_BIND_IP.to_string()),
tor_control_socket: None,
create_hidden_service: None,
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/farcasterd/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,6 @@ impl Runtime {
endpoints: &mut Endpoints,
bind_addr: InetSocketAddr,
public_port: u16,
tor_control_socket: InetSocketAddr,
) -> Result<(InetSocketAddr, NodeId), Error> {
self.services_ready()?;
let (peer_secret_key, peer_public_key) = self.peer_keys_ready()?;
Expand All @@ -1158,14 +1157,14 @@ impl Runtime {
);

let address = bind_addr.address();
let port = bind_addr.port().ok_or(Error::Farcaster(
"listen requires the port to listen on".to_string(),
))?;
let port = bind_addr
.port()
.ok_or_else(|| Error::Farcaster("listen requires the port to listen on".to_string()))?;

let public_onion_address = create_v3_onion_service(
bind_addr,
public_port,
tor_control_socket,
self.config.get_tor_control_socket()?,
&self.old_hidden_services,
)
.unwrap();
Expand Down Expand Up @@ -1201,7 +1200,7 @@ impl Runtime {
debug!("Instantiating peerd...");
let child = launch(
"peerd",
&[
[
"--listen",
&format!("{}", address),
"--port",
Expand Down
18 changes: 16 additions & 2 deletions src/farcasterd/trade_state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,21 @@ fn attempt_transition_to_make_deal(
}
Ok(bind_addr) => bind_addr,
};
match runtime.listen(bind_addr) {
let res = if runtime.config.create_hidden_service() {
runtime.listen_tor(
event.endpoints,
bind_addr,
public_addr.port().ok_or_else(|| {
Error::Farcaster(
"Cannot combine create hidden service with passed in Tor public address"
.to_string(),
)
})?,
)
} else {
runtime.listen(bind_addr).map(|n| (public_addr, n))
};
match res {
Err(err) => {
warn!("Failed to start peerd listen, cannot make deal: {}", err);
event.complete_client_ctl(CtlMsg::Failure(Failure {
Expand All @@ -468,7 +482,7 @@ fn attempt_transition_to_make_deal(
}))?;
Ok(None)
}
Ok(node_id) => {
Ok((public_addr, node_id)) => {
let deal = deal_parameters.to_v1(node_id.public_key(), public_addr);
let msg = s!("Deal registered, please share with taker.");
info!(
Expand Down

0 comments on commit 9fe6f0e

Please sign in to comment.