diff --git a/src/handler.rs b/src/handler.rs index 70dd225..c5880ec 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -116,6 +116,18 @@ impl Handler { warn!("User not set"); } } + Input::Char('r') => { + let Some(selected) = self.selected else { + return Ok(false); + }; + + let Some(tunnel) = self.tunnels.get_mut(selected) else { + warn!("Trying to retry invalid tunnel"); + return Ok(false); + }; + + *tunnel = self.all_tunnels.retry_tunnel(tunnel.clone()).await; + } Input::Delete => { let Some(selected) = self.selected else { return Ok(false); @@ -285,7 +297,7 @@ impl russh::server::Handler for Handler { let tunnel = self .all_tunnels - .add_tunnel(session.handle(), address, *port, user) + .create_tunnel(session.handle(), address, *port, user) .await; self.tunnels.push(tunnel); diff --git a/src/tui.rs b/src/tui.rs index 5f8464d..51e5f3d 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -42,11 +42,12 @@ impl Renderer { command("esc", "deselect"), command("↓/j", "move down"), command("↑/k", "move up"), - command("del", "close tunnel"), vec![], command("p", "make private"), command("ctrl-p", "make protected"), command("shift-p", "make public"), + command("del", "remove"), + command("r", "retry"), ] } else { vec![ diff --git a/src/tunnel.rs b/src/tunnel.rs index ac12805..eb55bad 100644 --- a/src/tunnel.rs +++ b/src/tunnel.rs @@ -85,7 +85,7 @@ impl Tunnels { } } - pub async fn add_tunnel( + pub async fn create_tunnel( &mut self, handle: Handle, name: impl Into, @@ -117,8 +117,12 @@ impl Tunnels { trace!(tunnel = tunnel.name, "Already in use, picking new name"); } }; - let address = tunnel.get_address().expect("domain is set"); + self.add_tunnel(tunnel).await + } + + async fn add_tunnel(&mut self, mut tunnel: Tunnel) -> Tunnel { + let address = tunnel.get_address().expect("domain is set"); if let Entry::Vacant(e) = self.tunnels.write().await.entry(address) { trace!(tunnel = tunnel.name, "Adding tunnel"); e.insert(tunnel.clone()); @@ -130,12 +134,21 @@ impl Tunnels { tunnel } - pub async fn remove_tunnel(&mut self, tunnel: Tunnel) { + pub async fn remove_tunnel(&mut self, mut tunnel: Tunnel) -> Tunnel { let mut all_tunnels = self.tunnels.write().await; if let Some(address) = tunnel.get_address() { trace!(tunnel.name, "Removing tunnel"); all_tunnels.remove(&address); } + tunnel.domain = None; + tunnel + } + + pub async fn retry_tunnel(&mut self, tunnel: Tunnel) -> Tunnel { + let mut tunnel = self.remove_tunnel(tunnel).await; + tunnel.domain = Some(self.domain.clone()); + + self.add_tunnel(tunnel).await } }