Skip to content

Commit

Permalink
cli: use hostname-based generation instead of bird names (microsoft#1…
Browse files Browse the repository at this point in the history
…73220)

Fixes microsoft#167708 by using a more predictable naming scheme.

```
me> Write a haiku about the extinction of birds
chatgpt>
	Silent skies above,
	Once lively songs now gone,
	Fading memories.
```
  • Loading branch information
connor4312 committed Feb 2, 2023
1 parent ddd71a2 commit 953a039
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 231 deletions.
5 changes: 2 additions & 3 deletions cli/src/tunnels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ pub mod paths;
pub mod shutdown_signal;

mod control_server;
mod name_generator;
mod nosleep;
#[cfg(target_os = "linux")]
mod nosleep_linux;
#[cfg(target_os = "macos")]
mod nosleep_macos;
#[cfg(target_os = "windows")]
mod nosleep_windows;
#[cfg(target_os = "linux")]
mod nosleep_linux;
mod port_forwarder;
mod protocol;
#[cfg_attr(unix, path = "tunnels/server_bridge_unix.rs")]
Expand Down
77 changes: 67 additions & 10 deletions cli/src/tunnels/dev_tunnels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*--------------------------------------------------------------------------------------------*/
use crate::auth;
use crate::constants::{
CONTROL_PORT, PROTOCOL_VERSION_TAG, PROTOCOL_VERSION_TAG_PREFIX, TUNNEL_SERVICE_USER_AGENT, IS_INTERACTIVE_CLI,
CONTROL_PORT, IS_INTERACTIVE_CLI, PROTOCOL_VERSION_TAG, PROTOCOL_VERSION_TAG_PREFIX,
TUNNEL_SERVICE_USER_AGENT,
};
use crate::state::{LauncherPaths, PersistedState};
use crate::util::errors::{
Expand All @@ -31,8 +32,6 @@ use tunnels::management::{
NO_REQUEST_OPTIONS,
};

use super::name_generator;

#[derive(Clone, Serialize, Deserialize)]
pub struct PersistedTunnel {
pub name: String,
Expand Down Expand Up @@ -636,10 +635,14 @@ impl DevTunnels {
mut use_random_name: bool,
) -> Result<String, AnyError> {
let existing_tunnels = self.list_all_server_tunnels().await?;
println!("{:?}", existing_tunnels);
let is_name_free = |n: &str| {
!existing_tunnels
.iter()
.any(|v| v.tags.iter().any(|t| t == n))
!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.tags.iter().any(|t| t == n)
})
};

if let Some(machine_name) = preferred_name {
Expand All @@ -658,11 +661,19 @@ impl DevTunnels {
use_random_name = true;
}

let mut placeholder_name = name_generator::generate_name(MAX_TUNNEL_NAME_LENGTH);
if use_random_name || !*IS_INTERACTIVE_CLI {
while !is_name_free(&placeholder_name) {
placeholder_name = name_generator::generate_name(MAX_TUNNEL_NAME_LENGTH);
let mut placeholder_name =
clean_hostname_for_tunnel(&gethostname::gethostname().to_string_lossy());
if !is_name_free(&placeholder_name) {
for i in 2.. {
let fixed_name = format!("{}{}", placeholder_name, i);
if is_name_free(&fixed_name) {
placeholder_name = fixed_name;
break;
}
}
}

if use_random_name || !*IS_INTERACTIVE_CLI {
return Ok(placeholder_name);
}

Expand Down Expand Up @@ -959,3 +970,49 @@ impl Backoff {
self.failures = 0;
}
}

/// Cleans up the hostname so it can be used as a tunnel name.
/// See TUNNEL_NAME_PATTERN in the tunnels SDK for the rules we try to use.
fn clean_hostname_for_tunnel(hostname: &str) -> String {
let mut out = String::new();
for char in hostname.chars().take(60) {
match char {
'-' | '_' | ' ' => {
out.push('-');
}
'0'..='9' | 'a'..='z' | 'A'..='Z' => {
out.push(char);
}
_ => {}
}
}

let trimmed = out.trim_matches('-');
if trimmed.len() < 2 {
"remote-machine".to_string() // placeholder if the result was empty
} else {
trimmed.to_owned()
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_clean_hostname_for_tunnel() {
assert_eq!(
clean_hostname_for_tunnel("hello123"),
"hello123".to_string()
);
assert_eq!(
clean_hostname_for_tunnel("-cool-name-"),
"cool-name".to_string()
);
assert_eq!(
clean_hostname_for_tunnel("cool!name with_chars"),
"coolname-with-chars".to_string()
);
assert_eq!(clean_hostname_for_tunnel("z"), "remote-machine".to_string());
}
}
218 changes: 0 additions & 218 deletions cli/src/tunnels/name_generator.rs

This file was deleted.

0 comments on commit 953a039

Please sign in to comment.