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) => {
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;
@@ -321,7 +322,7 @@ impl TorControllerInner {
self.connection.replace(new_connection);
// 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
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) {
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
.post(format!("http://{}/rpc/v1", onion))
.post(format!("http://{}/rpc/v1", onion_addr))
.body(
json!({
"jsonrpc": "2.0",
@@ -374,32 +375,36 @@ pub async fn tor_health_check(client: &Client, tor_controller: &TorController) {
)
.send()
.await;
match result {
// if success, do nothing
Ok(_) => {
tracing::debug!(
"Successfully verified main tor address liveness at {}",
onion
)
}
// if failure, disconnect tor control port, and restart tor controller
Err(e) => {
tracing::error!("Unable to reach self over tor: {}", e);
loop {
match tor_controller.replace().await {
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);
if let Err(e) = result {
let mut num_attempt = 1;
tracing::error!(
"Unable to reach self over tor, we will retry now...");
tracing::error!("The first TOR error: {}", e);
loop {
tracing::debug!("TOR Reconnecting retry number: {num_attempt}");
match tor_controller.replace().await {
Ok(restarted) => {
if restarted {
tracing::error!("Tor has been recently restarted, refusing to restart again right now...");
}
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
)
}
}