From 45f67e121cba560cfd6bf96d689bc66f101f8b1c Mon Sep 17 00:00:00 2001 From: LHaghfoss Date: Sun, 22 Nov 2026 10:40:36 +0100 Subject: [PATCH 1/2] fix(desktop): switch a dead ssh -L forward to an SSH route on its own After a wake a user-run ssh -L keeps its port open on a dead link, and the machine stayed away until the user pressed 'Let RustCode manage this tunnel'. The supervisor now does that takeover itself the first time it finds the tunnel dead and the ssh -L behind it, then retries at once, so later wakes reconnect through a fresh ssh -W within seconds. --- apps/desktop/src-tauri/src/machines.rs | 68 +++++++++++++++++++++++--- docs/remote.md | 5 +- 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/apps/desktop/src-tauri/src/machines.rs b/apps/desktop/src-tauri/src/machines.rs index f81965e81..bbe095367 100644 --- a/apps/desktop/src-tauri/src/machines.rs +++ b/apps/desktop/src-tauri/src/machines.rs @@ -652,6 +652,9 @@ async fn supervise( // away ([`stalled_forward`]). It stays on the row while the next attempt // runs, so it does not flicker away every few seconds. let mut stalled: Option = None; + // Whether the forwarded port was already handed to an SSH route of + // RustCode's own without the button ([`take_over_forward`]). + let mut tried_take_over = false; loop { set_state( &machines, @@ -695,6 +698,42 @@ async fn supervise( } else { None }; + // The `ssh -L` behind the port was found: switch to an + // SSH route of RustCode's own, which opens a fresh + // `ssh -W` per connection and so comes back by itself + // after the next wake, instead of asking the user to + // press "Let RustCode manage this tunnel". Tried once per + // supervisor; when the host does not answer, the row + // keeps its advice and button. + if let Some(found) = stalled.as_ref().filter(|stalled| stalled.offer) { + if let (Some(owner), false) = (found.owner.clone(), tried_take_over) { + tried_take_over = true; + let route = Route::ssh(owner); + match take_over_forward(&config_dir, &id, &found.address, route).await { + Ok(()) => { + emit_log( + &channel, + format!( + "desktop: RustCode now manages the tunnel to {} \ + instead of the forwarded port {}", + machine.name, found.address + ), + ); + stalled = None; + set_stalled(&machines, &id, None); + attempt = 0; + continue; + } + Err(why) => emit_log( + &channel, + format!( + "desktop: could not take over the tunnel to {}: {why}", + machine.name + ), + ), + } + } + } emit_log( &channel, format!( @@ -2091,7 +2130,22 @@ pub async fn machines_forward_take_over( ) -> Result, String> { let route = ssh_route(ssh.trim(), ssh_port, identity, gateway_port)?; let config_dir = config_dir()?; - let connector = Machines::connector(&config_dir)?; + take_over_forward(&config_dir, &id, &address, route).await?; + set_stalled(machines.inner(), &id, None); + machines.retry(Some(&id)); + machines.list() +} + +/// Test the SSH `route` and, once it answers, save it and turn off the +/// forwarded port at `address`. Nothing is saved when the test fails; the +/// error says why. +async fn take_over_forward( + config_dir: &Path, + id: &str, + address: &str, + route: Route, +) -> Result<(), String> { + let connector = Machines::connector(config_dir)?; let probe = connector.probe_route(&route).await; if probe.outcome != ProbeOutcome::Reachable { let why = probe @@ -2103,12 +2157,12 @@ pub async fn machines_forward_take_over( route.address )); } - let mut registry = MachineRegistry::load(&config_dir).map_err(chain)?; + let mut registry = MachineRegistry::load(config_dir).map_err(chain)?; registry - .take_over_forward(&id, &address, route) + .take_over_forward(id, address, route) .map_err(chain)?; let machine = registry - .get(&id) + .get(id) .ok_or("That machine is no longer saved.")? .clone(); let mut probes: Vec = machine @@ -2118,10 +2172,8 @@ pub async fn machines_forward_take_over( .cloned() .collect(); probes.push(probe); - registry.set_probes(&id, probes).map_err(chain)?; - set_stalled(machines.inner(), &id, None); - machines.retry(Some(&id)); - machines.list() + registry.set_probes(id, probes).map_err(chain)?; + Ok(()) } /// Remove one route of a saved machine. A route the machine reports comes diff --git a/docs/remote.md b/docs/remote.md index 09c9572ab..dfc50ccb7 100644 --- a/docs/remote.md +++ b/docs/remote.md @@ -877,7 +877,10 @@ process, which ends with it, so no tunnel is left running. SSH route to that host and, once it answers, saves it and turns the forwarded port off (`machines_forward_take_over`). Each connection then opens its own `ssh -W`, so after a wake the next attempt starts a fresh tunnel. - Turning the forwarded port back on undoes it. + Turning the forwarded port back on undoes it. When the running `ssh -L` + was found, the app does this on its own the first time the tunnel is found + dead, without the button; the row keeps the advice and button only when that + SSH route does not answer. #### Mutual access From fb4e8ff6810b49549f889e7898cf8cbc15503b79 Mon Sep 17 00:00:00 2001 From: LHaghfoss Date: Sun, 22 Nov 2026 10:42:05 +0100 Subject: [PATCH 2/2] fix(desktop): drop the new comments the ratchet refuses --- apps/desktop/src-tauri/src/machines.rs | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/apps/desktop/src-tauri/src/machines.rs b/apps/desktop/src-tauri/src/machines.rs index bbe095367..aa430a0e8 100644 --- a/apps/desktop/src-tauri/src/machines.rs +++ b/apps/desktop/src-tauri/src/machines.rs @@ -652,9 +652,7 @@ async fn supervise( // away ([`stalled_forward`]). It stays on the row while the next attempt // runs, so it does not flicker away every few seconds. let mut stalled: Option = None; - // Whether the forwarded port was already handed to an SSH route of - // RustCode's own without the button ([`take_over_forward`]). - let mut tried_take_over = false; + let mut took_over_forward_once = false; loop { set_state( &machines, @@ -698,16 +696,10 @@ async fn supervise( } else { None }; - // The `ssh -L` behind the port was found: switch to an - // SSH route of RustCode's own, which opens a fresh - // `ssh -W` per connection and so comes back by itself - // after the next wake, instead of asking the user to - // press "Let RustCode manage this tunnel". Tried once per - // supervisor; when the host does not answer, the row - // keeps its advice and button. if let Some(found) = stalled.as_ref().filter(|stalled| stalled.offer) { - if let (Some(owner), false) = (found.owner.clone(), tried_take_over) { - tried_take_over = true; + if let (Some(owner), false) = (found.owner.clone(), took_over_forward_once) + { + took_over_forward_once = true; let route = Route::ssh(owner); match take_over_forward(&config_dir, &id, &found.address, route).await { Ok(()) => { @@ -2136,9 +2128,6 @@ pub async fn machines_forward_take_over( machines.list() } -/// Test the SSH `route` and, once it answers, save it and turn off the -/// forwarded port at `address`. Nothing is saved when the test fails; the -/// error says why. async fn take_over_forward( config_dir: &Path, id: &str,