Skip to content

Commit

Permalink
cli: avoid adoption of tunnels with existing host connections (micros…
Browse files Browse the repository at this point in the history
  • Loading branch information
connor4312 authored Mar 18, 2024
1 parent ff15e55 commit 7015a87
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
29 changes: 16 additions & 13 deletions cli/src/tunnels/dev_tunnels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,10 @@ impl DevTunnels {

let tunnel = match self.get_existing_tunnel_with_name(name).await? {
Some(e) => {
if tunnel_has_host_connection(&e) {
return Err(CodeError::TunnelActiveAndInUse(name.to_string()).into());
}

let loc = TunnelLocator::try_from(&e).unwrap();
info!(self.log, "Adopting existing tunnel (ID={:?})", loc);
spanf!(
Expand Down Expand Up @@ -687,13 +691,7 @@ impl DevTunnels {

let recyclable = existing_tunnels
.iter()
.filter(|t| {
t.status
.as_ref()
.and_then(|s| s.host_connection_count.as_ref())
.map(|c| c.get_count())
.unwrap_or(0) == 0
})
.filter(|t| !tunnel_has_host_connection(t))
.choose(&mut rand::thread_rng());

match recyclable {
Expand Down Expand Up @@ -764,12 +762,9 @@ impl DevTunnels {
) -> Result<String, AnyError> {
let existing_tunnels = self.list_tunnels_with_tag(&[self.tag]).await?;
let is_name_free = |n: &str| {
!existing_tunnels.iter().any(|v| {
v.status
.as_ref()
.and_then(|s| s.host_connection_count.as_ref().map(|c| c.get_count()))
.unwrap_or(0) > 0 && v.labels.iter().any(|t| t == n)
})
!existing_tunnels
.iter()
.any(|v| tunnel_has_host_connection(v) && v.labels.iter().any(|t| t == n))
};

if let Some(machine_name) = preferred_name {
Expand Down Expand Up @@ -1235,6 +1230,14 @@ fn privacy_to_tunnel_acl(privacy: PortPrivacy) -> TunnelAccessControl {
}
}

fn tunnel_has_host_connection(tunnel: &Tunnel) -> bool {
tunnel
.status
.as_ref()
.and_then(|s| s.host_connection_count.as_ref().map(|c| c.get_count() > 0))
.unwrap_or_default()
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
4 changes: 3 additions & 1 deletion cli/src/util/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,9 @@ pub enum CodeError {
#[error("Could not check for update: {0}")]
UpdateCheckFailed(String),
#[error("Could not write connection token file: {0}")]
CouldNotCreateConnectionTokenFile(std::io::Error)
CouldNotCreateConnectionTokenFile(std::io::Error),
#[error("A tunnel with the name {0} exists and is in-use. Please pick a different name or stop the existing tunnel.")]
TunnelActiveAndInUse(String),
}

makeAnyError!(
Expand Down

0 comments on commit 7015a87

Please sign in to comment.