From 37958009758cb4cb5bd98f56c309541533951c89 Mon Sep 17 00:00:00 2001 From: Luna Yao <40349250+ZnqbuZ@users.noreply.github.com> Date: Fri, 17 Apr 2026 18:43:16 +0200 Subject: [PATCH] replace gethostname with hostname --- easytier/Cargo.toml | 2 +- easytier/src/common/config.rs | 35 +++++++++++++++------------------- easytier/src/dns/config/dns.rs | 3 +-- easytier/src/web_client/mod.rs | 5 +---- 4 files changed, 18 insertions(+), 27 deletions(-) diff --git a/easytier/Cargo.toml b/easytier/Cargo.toml index 3c65d370..0eaf8b90 100644 --- a/easytier/Cargo.toml +++ b/easytier/Cargo.toml @@ -56,7 +56,7 @@ itertools = "0.14.0" strum = { version = "0.27.2", features = ["derive"] } -gethostname = "1.1.0" +hostname = "0.4.2" futures = { version = "0.3", features = ["bilock", "unstable"] } diff --git a/easytier/src/common/config.rs b/easytier/src/common/config.rs index 5b3ec43d..dc5f1a0f 100644 --- a/easytier/src/common/config.rs +++ b/easytier/src/common/config.rs @@ -6,16 +6,15 @@ use std::{ }; use anyhow::Context; -use base64::{Engine as _, prelude::BASE64_STANDARD}; -use clap::ValueEnum; +use base64::{prelude::BASE64_STANDARD, Engine as _}; use clap::builder::PossibleValue; +use clap::ValueEnum; use serde::{Deserialize, Serialize}; use strum::{Display, EnumString, VariantArray}; use tokio::io::AsyncReadExt as _; use crate::{ common::stun::StunInfoCollector, - instance::dns_server::DEFAULT_ET_DNS_ZONE, proto::{ acl::Acl, common::{CompressionAlgoPb, PortForwardConfigPb, SecureModeConfig, SocketType}, @@ -570,26 +569,22 @@ impl ConfigLoader for TomlConfigLoader { } fn get_hostname(&self) -> String { - let hostname = self.config.lock().unwrap().hostname.clone(); - - match hostname { - Some(hostname) => { - let hostname = hostname - .chars() + let hostname = self + .config + .lock() + .unwrap() + .hostname + .clone() + .map(|h| { + h.chars() .filter(|c| !c.is_control()) .take(32) - .collect::(); + .collect::() + }) + .filter(|h| !h.is_empty()); - if !hostname.is_empty() { - self.set_hostname(Some(hostname.clone())); - hostname - } else { - self.set_hostname(None); - gethostname::gethostname().to_string_lossy().to_string() - } - } - None => gethostname::gethostname().to_string_lossy().to_string(), - } + self.set_hostname(hostname.clone()); + hostname.unwrap_or_else(|| hostname::get().unwrap_or_default().to_string_lossy().into_owned()) } fn set_hostname(&self, name: Option) { diff --git a/easytier/src/dns/config/dns.rs b/easytier/src/dns/config/dns.rs index 6b096d11..702b3590 100644 --- a/easytier/src/dns/config/dns.rs +++ b/easytier/src/dns/config/dns.rs @@ -6,7 +6,6 @@ use crate::dns::utils::addr::NameServerAddrGroup; use crate::dns::utils::parse; use crate::proto::dns::GetExportConfigResponse; use derivative::Derivative; -use gethostname::gethostname; use hickory_proto::rr::{LowerName, Name}; use hickory_proto::xfer::Protocol; use serde::{Deserialize, Deserializer, Serialize}; @@ -52,7 +51,7 @@ impl DnsConfig { impl DnsConfig { pub fn get_name(&self) -> LowerName { if self.name.is_empty() { - parse(gethostname().to_string_lossy().as_ref()) + parse(hostname::get().unwrap_or_default().to_string_lossy().as_ref()) } else { self.name.clone() } diff --git a/easytier/src/web_client/mod.rs b/easytier/src/web_client/mod.rs index c487d13f..9db012f3 100644 --- a/easytier/src/web_client/mod.rs +++ b/easytier/src/web_client/mod.rs @@ -225,10 +225,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 = match hostname { - None => gethostname::gethostname().to_string_lossy().to_string(), - Some(hostname) => hostname, - }; + let hostname = hostname.unwrap_or_else(|| hostname::get().unwrap_or_default().to_string_lossy().into_owned()); Ok(WebClient::new( create_connector_by_url(c_url.as_str(), &global_ctx, IpVersion::Both).await?, token.to_string(),