Tor daemon fix (#1934)

* tor restart fix poc

* code cleanup

* Update backend/src/net/tor.rs

Co-authored-by: Aiden McClelland <3732071+dr-bonez@users.noreply.github.com>

* add info statement

* Update backend/src/net/tor.rs

Co-authored-by: Aiden McClelland <3732071+dr-bonez@users.noreply.github.com>
This commit is contained in:
Stephen Chavez
2022-11-10 17:19:56 +00:00
committed by Aiden McClelland
parent 05b29a7e9a
commit 20b93e9fba

View File

@@ -313,6 +313,7 @@ impl TorControllerInner {
} }
Err(e) => { Err(e) => {
tracing::info!("Failed to reconnect to tor control socket: {}", e); tracing::info!("Failed to reconnect to tor control socket: {}", e);
tracing::info!("Trying again in one second");
} }
} }
tokio::time::sleep(Duration::from_secs(1)).await; tokio::time::sleep(Duration::from_secs(1)).await;
@@ -321,7 +322,7 @@ impl TorControllerInner {
self.connection.replace(new_connection); self.connection.replace(new_connection);
// swap empty map for owned old service map // swap empty map for owned old service map
let old_services = std::mem::replace(&mut self.services, BTreeMap::new()); let old_services = std::mem::take(&mut self.services);
// re add all of the services on the new control socket // re add all of the services on the new control socket
for ((package_id, interface_id), (tor_key, tor_cfg, ipv4)) in old_services { for ((package_id, interface_id), (tor_key, tor_cfg, ipv4)) in old_services {
@@ -360,9 +361,9 @@ impl TorControllerInner {
pub async fn tor_health_check(client: &Client, tor_controller: &TorController) { pub async fn tor_health_check(client: &Client, tor_controller: &TorController) {
tracing::debug!("Attempting to self-check tor address"); tracing::debug!("Attempting to self-check tor address");
let onion = tor_controller.embassyd_onion().await; let onion_addr = tor_controller.embassyd_onion().await;
let result = client let result = client
.post(format!("http://{}/rpc/v1", onion)) .post(format!("http://{}/rpc/v1", onion_addr))
.body( .body(
json!({ json!({
"jsonrpc": "2.0", "jsonrpc": "2.0",
@@ -374,32 +375,36 @@ pub async fn tor_health_check(client: &Client, tor_controller: &TorController) {
) )
.send() .send()
.await; .await;
match result { if let Err(e) = result {
// if success, do nothing let mut num_attempt = 1;
Ok(_) => { tracing::error!(
tracing::debug!( "Unable to reach self over tor, we will retry now...");
"Successfully verified main tor address liveness at {}", tracing::error!("The first TOR error: {}", e);
onion
) loop {
} tracing::debug!("TOR Reconnecting retry number: {num_attempt}");
// if failure, disconnect tor control port, and restart tor controller
Err(e) => { match tor_controller.replace().await {
tracing::error!("Unable to reach self over tor: {}", e); Ok(restarted) => {
loop { if restarted {
match tor_controller.replace().await { tracing::error!("Tor has been recently restarted, refusing to restart again right now...");
Ok(restarted) => {
if restarted {
tracing::error!("Tor has been recently restarted, refusing to restart");
}
break;
}
Err(e) => {
tracing::error!("Unable to restart tor: {}", e);
tracing::debug!("{:?}", e);
} }
break;
}
Err(e) => {
tracing::error!("TOR retry error: {}", e);
tracing::error!("Unable to restart tor on attempt {num_attempt}...Retrying");
num_attempt += 1;
continue;
} }
} }
} }
} else {
tracing::debug!(
"Successfully verified main tor address liveness at {}",
onion_addr
)
} }
} }