Skip to content

Commit 7761ff1

Browse files
committed
Farcasterd: Add config for Tor hidden service creation
1 parent 56cb365 commit 7761ff1

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

src/config.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,32 @@ impl Config {
139139
}
140140
}
141141

142+
/// Returns the addr of the tor control socket if it is set, or the default
143+
/// socket at localhost:9051 if not.
144+
pub fn get_tor_control_socket(&self) -> Result<InetSocketAddr, Error> {
145+
if let Some(FarcasterdConfig {
146+
tor_control_socket: Some(addr),
147+
..
148+
}) = &self.farcasterd
149+
{
150+
Ok(InetSocketAddr::from_str(addr)?)
151+
} else {
152+
Ok(InetSocketAddr::from_str("127.0.0.1:9051")?)
153+
}
154+
}
155+
156+
pub fn create_hidden_service(&self) -> bool {
157+
if let Some(FarcasterdConfig {
158+
create_hidden_service: Some(create_hidden_service),
159+
..
160+
}) = &self.farcasterd
161+
{
162+
*create_hidden_service
163+
} else {
164+
false
165+
}
166+
}
167+
142168
/// Returns the swap config for the specified network and arbitrating/accordant blockchains
143169
pub fn get_swap_config(
144170
&self,
@@ -230,6 +256,11 @@ pub struct FarcasterdConfig {
230256
pub bind_ip: Option<String>,
231257
/// Whether checkpoints should be auto restored at start-up, or not
232258
pub auto_restore: Option<bool>,
259+
/// Tor control socket for creating the hidden service
260+
pub tor_control_socket: Option<String>,
261+
/// Whether to create a hidden service or not. If set, the node will only
262+
/// run in hidden service mode
263+
pub create_hidden_service: Option<bool>,
233264
}
234265

235266
#[derive(Deserialize, Serialize, Debug, Clone)]
@@ -432,6 +463,8 @@ impl Default for FarcasterdConfig {
432463
// write the default port and ip in the generated config
433464
bind_port: Some(FARCASTER_BIND_PORT),
434465
bind_ip: Some(FARCASTER_BIND_IP.to_string()),
466+
tor_control_socket: None,
467+
create_hidden_service: None,
435468
}
436469
}
437470
}

src/farcasterd/runtime.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,6 @@ impl Runtime {
11341134
endpoints: &mut Endpoints,
11351135
bind_addr: InetSocketAddr,
11361136
public_port: u16,
1137-
tor_control_socket: InetSocketAddr,
11381137
) -> Result<(InetSocketAddr, NodeId), Error> {
11391138
self.services_ready()?;
11401139
let (peer_secret_key, peer_public_key) = self.peer_keys_ready()?;
@@ -1165,7 +1164,7 @@ impl Runtime {
11651164
let public_onion_address = create_v3_onion_service(
11661165
bind_addr,
11671166
public_port,
1168-
tor_control_socket,
1167+
self.config.get_tor_control_socket()?,
11691168
&self.old_hidden_services,
11701169
)
11711170
.unwrap();

src/farcasterd/trade_state_machine.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,19 @@ fn attempt_transition_to_make_deal(
459459
}
460460
Ok(bind_addr) => bind_addr,
461461
};
462-
match runtime.listen(bind_addr) {
462+
let res = if runtime.config.create_hidden_service() {
463+
runtime.listen_tor(
464+
event.endpoints,
465+
bind_addr,
466+
public_addr.port().ok_or(Error::Farcaster(
467+
"Cannot combine create hidden service with passed in Tor public address"
468+
.to_string(),
469+
))?,
470+
)
471+
} else {
472+
runtime.listen(bind_addr).map(|n| (public_addr, n))
473+
};
474+
match res {
463475
Err(err) => {
464476
warn!("Failed to start peerd listen, cannot make deal: {}", err);
465477
event.complete_client_ctl(CtlMsg::Failure(Failure {
@@ -468,7 +480,7 @@ fn attempt_transition_to_make_deal(
468480
}))?;
469481
Ok(None)
470482
}
471-
Ok(node_id) => {
483+
Ok((public_addr, node_id)) => {
472484
let deal = deal_parameters.to_v1(node_id.public_key(), public_addr);
473485
let msg = s!("Deal registered, please share with taker.");
474486
info!(

0 commit comments

Comments
 (0)