From 30ccfab28890a734ce3557761445f4f7877cd001 Mon Sep 17 00:00:00 2001 From: m1m1sha <18262227804@163.com> Date: Wed, 8 May 2024 16:18:09 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix:=20hostname=20empty?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- easytier/src/common/config.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/easytier/src/common/config.rs b/easytier/src/common/config.rs index 72011725..9604e373 100644 --- a/easytier/src/common/config.rs +++ b/easytier/src/common/config.rs @@ -226,17 +226,22 @@ impl ConfigLoader for TomlConfigLoader { match hostname { Some(hostname) => { - let re = regex::Regex::new(r"[^\u4E00-\u9FA5a-zA-Z0-9\-]*").unwrap(); - let mut name = re.replace_all(&hostname, "").to_string(); + if !hostname.is_empty() { + let re = regex::Regex::new(r"[^\u4E00-\u9FA5a-zA-Z0-9\-]*").unwrap(); + let mut name = re.replace_all(&hostname, "").to_string(); - if name.len() > 32 { - name = name.chars().take(32).collect::(); - } + if name.len() > 32 { + name = name.chars().take(32).collect::(); + } - if hostname != name { - self.set_hostname(Some(name.clone())); + if hostname != name { + self.set_hostname(Some(name.clone())); + } + name + } else { + self.set_hostname(None); + gethostname::gethostname().to_string_lossy().to_string() } - name } None => gethostname::gethostname().to_string_lossy().to_string(), }