Revert "remove hostname from config"

This commit is contained in:
Luna Yao
2026-04-04 06:01:23 +02:00
parent c8697e76b4
commit ea5e34b6af
5 changed files with 63 additions and 46 deletions
+54 -35
View File
@@ -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<String>);
fn get_netns(&self) -> Option<String>;
fn set_netns(&self, ns: Option<String>);
@@ -210,9 +214,6 @@ pub trait ConfigLoader: Send + Sync + DnsConfigLoaderExt {
}
fn set_credential_file(&self, _path: Option<std::path::PathBuf>) {}
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<String>,
hostname: Option<String>,
@@ -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::<String>();
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<String>) {
self.config.lock().unwrap().hostname = name;
}
fn get_netns(&self) -> Option<String> {
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 =