From ea5e34b6afa753b27807a53965ea1bf83ce729c3 Mon Sep 17 00:00:00 2001 From: Luna Yao <40349250+ZnqbuZ@users.noreply.github.com> Date: Sat, 4 Apr 2026 06:01:23 +0200 Subject: [PATCH] Revert "remove hostname from config" --- easytier/src/common/config.rs | 89 +++++++++++-------- easytier/src/core.rs | 7 +- easytier/src/instance/instance.rs | 2 +- easytier/src/launcher.rs | 7 +- easytier/src/peers/foreign_network_manager.rs | 4 +- 5 files changed, 63 insertions(+), 46 deletions(-) diff --git a/easytier/src/common/config.rs b/easytier/src/common/config.rs index 9a63fc03..ecea9fa0 100644 --- a/easytier/src/common/config.rs +++ b/easytier/src/common/config.rs @@ -1,3 +1,10 @@ +use std::{ + hash::Hasher, + net::{IpAddr, SocketAddr}, + path::PathBuf, + sync::{Arc, Mutex}, +}; + use super::env_parser; use crate::{ common::stun::StunInfoCollector, @@ -13,12 +20,6 @@ use cfg_if::cfg_if; use clap::builder::PossibleValue; use clap::ValueEnum; use serde::{Deserialize, Serialize}; -use std::{ - hash::Hasher, - net::{IpAddr, SocketAddr}, - path::PathBuf, - sync::{Arc, Mutex}, -}; use strum::{Display, EnumString, VariantArray}; use tokio::io::AsyncReadExt as _; @@ -134,6 +135,9 @@ pub trait ConfigLoader: Send + Sync + DnsConfigLoaderExt { fn get_inst_name(&self) -> String; fn set_inst_name(&self, name: String); + fn get_hostname(&self) -> String; + fn set_hostname(&self, name: Option); + fn get_netns(&self) -> Option; fn set_netns(&self, ns: Option); @@ -210,9 +214,6 @@ pub trait ConfigLoader: Send + Sync + DnsConfigLoaderExt { } fn set_credential_file(&self, _path: Option) {} - fn get_hostname(&self) -> String; - fn set_hostname(&self, hostname: &str); - fn dump(&self) -> String; } @@ -433,8 +434,7 @@ pub fn process_secure_mode_cfg(mut user_cfg: SecureModeConfig) -> anyhow::Result Ok(user_cfg) } -#[derive(Debug, Clone, PartialEq, Default, Deserialize, Serialize)] -#[serde(default)] +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] struct Config { netns: Option, hostname: Option, @@ -560,22 +560,6 @@ impl DnsConfigLoaderExt for TomlConfigLoader { } impl ConfigLoader for TomlConfigLoader { - fn get_id(&self) -> uuid::Uuid { - let mut locked_config = self.config.lock().unwrap(); - match locked_config.instance_id { - Some(id) => id, - None => { - let id = uuid::Uuid::new_v4(); - locked_config.instance_id = Some(id); - id - } - } - } - - fn set_id(&self, id: uuid::Uuid) { - self.config.lock().unwrap().instance_id = Some(id); - } - fn get_inst_name(&self) -> String { self.config .lock() @@ -589,6 +573,33 @@ impl ConfigLoader for TomlConfigLoader { self.config.lock().unwrap().instance_name = Some(name); } + fn get_hostname(&self) -> String { + let hostname = self.config.lock().unwrap().hostname.clone(); + + match hostname { + Some(hostname) => { + let hostname = hostname + .chars() + .filter(|c| !c.is_control()) + .take(32) + .collect::(); + + 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(), + } + } + + fn set_hostname(&self, name: Option) { + self.config.lock().unwrap().hostname = name; + } + fn get_netns(&self) -> Option { self.config.lock().unwrap().netns.clone() } @@ -694,6 +705,22 @@ impl ConfigLoader for TomlConfigLoader { .unwrap_or_default() } + fn get_id(&self) -> uuid::Uuid { + let mut locked_config = self.config.lock().unwrap(); + match locked_config.instance_id { + Some(id) => id, + None => { + let id = uuid::Uuid::new_v4(); + locked_config.instance_id = Some(id); + id + } + } + } + + fn set_id(&self, id: uuid::Uuid) { + self.config.lock().unwrap().instance_id = Some(id); + } + fn get_network_identity(&self) -> NetworkIdentity { self.config .lock() @@ -873,14 +900,6 @@ impl ConfigLoader for TomlConfigLoader { self.config.lock().unwrap().credential_file = path; } - fn get_hostname(&self) -> String { - self.config.lock().unwrap().dns.get_name().to_string() - } - - fn set_hostname(&self, hostname: &str) { - self.config.lock().unwrap().dns.set_name(hostname); - } - fn dump(&self) -> String { let default_flags_json = serde_json::to_string(&gen_default_flags()).unwrap(); let default_flags_hashmap = diff --git a/easytier/src/core.rs b/easytier/src/core.rs index f406f603..7b494e92 100644 --- a/easytier/src/core.rs +++ b/easytier/src/core.rs @@ -797,10 +797,9 @@ impl NetworkOptions { } fn merge_into(&self, cfg: &TomlConfigLoader) -> anyhow::Result<()> { - // TODO: remove hostname - // if self.hostname.is_some() { - // cfg.set_hostname(&self.hostname.clone()); - // } + if self.hostname.is_some() { + cfg.set_hostname(self.hostname.clone()); + } let old_ns = cfg.get_network_identity(); let network_name = self.network_name.clone().unwrap_or(old_ns.network_name); diff --git a/easytier/src/instance/instance.rs b/easytier/src/instance/instance.rs index f5d89140..7ecae865 100644 --- a/easytier/src/instance/instance.rs +++ b/easytier/src/instance/instance.rs @@ -213,7 +213,7 @@ impl InstanceConfigPatcher { let global_ctx = weak_upgrade(&self.global_ctx)?; if let Some(hostname) = patch.hostname { global_ctx.set_hostname(hostname.clone()); - global_ctx.config.set_hostname(&hostname); + global_ctx.config.set_hostname(Some(hostname)); } if let Some(ipv4) = patch.ipv4 { if !global_ctx.config.get_dhcp() { diff --git a/easytier/src/launcher.rs b/easytier/src/launcher.rs index 4296ac1f..b9061077 100644 --- a/easytier/src/launcher.rs +++ b/easytier/src/launcher.rs @@ -492,9 +492,7 @@ impl NetworkConfig { .parse() .with_context(|| format!("failed to parse instance id: {:?}", self.instance_id))?, ); - if let Some(n) = self.hostname.as_ref() { - cfg.set_hostname(n) - } + cfg.set_hostname(self.hostname.clone()); cfg.set_dhcp(self.dhcp.unwrap_or_default()); cfg.set_inst_name(self.network_name.clone().unwrap_or_default()); @@ -1035,7 +1033,8 @@ mod tests { config.set_dhcp(rng.gen_bool(0.5)); if rng.gen_bool(0.7) { - config.set_hostname(&format!("host-{}", rng.gen::())); + let hostname = format!("host-{}", rng.gen::()); + config.set_hostname(Some(hostname)); } config.set_network_identity(crate::common::config::NetworkIdentity::new( diff --git a/easytier/src/peers/foreign_network_manager.rs b/easytier/src/peers/foreign_network_manager.rs index 5507e430..cc06d4dd 100644 --- a/easytier/src/peers/foreign_network_manager.rs +++ b/easytier/src/peers/foreign_network_manager.rs @@ -239,11 +239,11 @@ impl ForeignNetworkEntry { ) -> ArcGlobalCtx { let config = TomlConfigLoader::default(); config.set_network_identity(network.clone()); - config.set_hostname(&format!( + config.set_hostname(Some(format!( "{}{}", PUBLIC_SERVER_HOSTNAME_PREFIX, global_ctx.get_hostname() - )); + ))); config.set_secure_mode(global_ctx.config.get_secure_mode()); let mut flags = config.get_flags();