diff --git a/easytier/src/common/config.rs b/easytier/src/common/config.rs index 405fdc91..8df84eb3 100644 --- a/easytier/src/common/config.rs +++ b/easytier/src/common/config.rs @@ -22,6 +22,7 @@ use crate::{ common::{CompressionAlgoPb, PortForwardConfigPb, SecureModeConfig, SocketType}, }, tunnel::generate_digest_from_str, + utils, }; pub type Flags = crate::proto::common::FlagsInConfig; @@ -580,14 +581,7 @@ impl ConfigLoader for TomlConfigLoader { .filter(|h| !h.is_empty()); self.set_hostname(hostname.clone()); - hostname.unwrap_or_else(|| { - sanitize( - hostname::get() - .unwrap_or_else(|_| "localhost".into()) - .to_string_lossy() - .as_ref(), - ) - }) + hostname.unwrap_or_else(|| sanitize(utils::hostname())) } fn set_hostname(&self, name: Option) { diff --git a/easytier/src/utils/mod.rs b/easytier/src/utils/mod.rs index 0b2a6ad9..1e227b59 100644 --- a/easytier/src/utils/mod.rs +++ b/easytier/src/utils/mod.rs @@ -23,6 +23,13 @@ pub fn weak_upgrade(weak: &Weak) -> anyhow::Result> { .ok_or_else(|| anyhow::anyhow!("{} not available", std::any::type_name::())) } +pub fn hostname() -> String { + hostname::get() + .unwrap_or_else(|_| "localhost".into()) + .to_string_lossy() + .into_owned() +} + pub trait BoxExt: Sized { fn boxed(self) -> Box { Box::new(self) diff --git a/easytier/src/web_client/mod.rs b/easytier/src/web_client/mod.rs index 5a61aaa7..fb50ab43 100644 --- a/easytier/src/web_client/mod.rs +++ b/easytier/src/web_client/mod.rs @@ -9,6 +9,7 @@ use crate::{ instance_manager::{DaemonGuard, NetworkInstanceManager}, proto::common::NatType, tunnel::{IpVersion, TunnelConnector}, + utils, }; use anyhow::{Context as _, Result}; use async_trait::async_trait; @@ -225,12 +226,7 @@ pub async fn run_web_client( let mut flags = global_ctx.get_flags(); flags.bind_device = false; global_ctx.set_flags(flags); - let hostname = hostname.unwrap_or_else(|| { - hostname::get() - .unwrap_or_else(|_| "localhost".into()) - .to_string_lossy() - .into_owned() - }); + let hostname = hostname.unwrap_or_else(utils::hostname); Ok(WebClient::new( create_connector_by_url(c_url.as_str(), &global_ctx, IpVersion::Both).await?, token.to_string(),