From eb0277146cf98d2f77b06d469c33a9b147efc4fb Mon Sep 17 00:00:00 2001 From: Aiden McClelland <3732071+dr-bonez@users.noreply.github.com> Date: Wed, 17 May 2023 22:17:27 -0600 Subject: [PATCH] wait for tor (#2278) --- backend/src/init.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/backend/src/init.rs b/backend/src/init.rs index 68ac2365d..9847e10c8 100644 --- a/backend/src/init.rs +++ b/backend/src/init.rs @@ -1,5 +1,6 @@ use std::collections::{BTreeMap, HashMap}; use std::fs::Permissions; +use std::net::SocketAddr; use std::os::unix::fs::PermissionsExt; use std::path::Path; use std::process::Stdio; @@ -39,6 +40,10 @@ pub async fn check_time_is_synchronized() -> Result { == "NTPSynchronized=yes") } +pub async fn check_tor_is_ready(tor_control: SocketAddr) -> bool { + tokio::net::TcpStream::connect(tor_control).await.is_ok() +} + pub struct InitReceipts { pub server_version: LockReceipt, pub version_range: LockReceipt, @@ -404,6 +409,20 @@ pub async fn init(cfg: &RpcContextConfig) -> Result { .invoke(crate::ErrorKind::Tor) .await?; + let mut warn_tor_not_ready = true; + for _ in 0..60 { + if check_tor_is_ready(cfg.tor_control.unwrap_or(([127, 0, 0, 1], 9051).into())).await { + warn_tor_not_ready = false; + break; + } + tokio::time::sleep(Duration::from_secs(1)).await; + } + if warn_tor_not_ready { + tracing::warn!("Timed out waiting for tor to start"); + } else { + tracing::info!("Tor is started"); + } + receipts .ip_info .set(&mut handle, crate::net::dhcp::init_ips().await?)