mirror of
https://mirror.suhoan.cn/https://github.com/EasyTier/EasyTier.git
synced 2025-12-14 13:47:24 +08:00
✨ feat: custom hostname
This commit is contained in:
@@ -14,6 +14,9 @@ pub trait ConfigLoader: Send + Sync {
|
||||
fn get_id(&self) -> uuid::Uuid;
|
||||
fn set_id(&self, id: uuid::Uuid);
|
||||
|
||||
fn get_hostname(&self) -> String;
|
||||
fn set_hostname(&self, name: Option<String>);
|
||||
|
||||
fn get_inst_name(&self) -> String;
|
||||
fn set_inst_name(&self, name: String);
|
||||
|
||||
@@ -152,6 +155,7 @@ pub struct Flags {
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
|
||||
struct Config {
|
||||
netns: Option<String>,
|
||||
hostname: Option<String>,
|
||||
instance_name: Option<String>,
|
||||
instance_id: Option<uuid::Uuid>,
|
||||
ipv4: Option<String>,
|
||||
@@ -216,6 +220,19 @@ impl ConfigLoader for TomlConfigLoader {
|
||||
self.config.lock().unwrap().instance_name = Some(name);
|
||||
}
|
||||
|
||||
fn get_hostname(&self) -> String {
|
||||
self.config
|
||||
.lock()
|
||||
.unwrap()
|
||||
.hostname
|
||||
.clone()
|
||||
.unwrap_or(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()
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ pub struct GlobalCtx {
|
||||
|
||||
ip_collector: Arc<IPCollector>,
|
||||
|
||||
hotname: AtomicCell<Option<String>>,
|
||||
hostname: AtomicCell<Option<String>>,
|
||||
|
||||
stun_info_collection: Box<dyn StunInfoCollectorTrait>,
|
||||
|
||||
@@ -80,6 +80,7 @@ impl GlobalCtx {
|
||||
let id = config_fs.get_id();
|
||||
let network = config_fs.get_network_identity();
|
||||
let net_ns = NetNS::new(config_fs.get_netns());
|
||||
let hostname = config_fs.get_hostname();
|
||||
|
||||
let (event_bus, _) = tokio::sync::broadcast::channel(100);
|
||||
|
||||
@@ -96,7 +97,7 @@ impl GlobalCtx {
|
||||
|
||||
ip_collector: Arc::new(IPCollector::new(net_ns)),
|
||||
|
||||
hotname: AtomicCell::new(None),
|
||||
hostname: AtomicCell::new(Some(hostname)),
|
||||
|
||||
stun_info_collection: Box::new(StunInfoCollector::new_with_default_servers()),
|
||||
|
||||
@@ -166,13 +167,27 @@ impl GlobalCtx {
|
||||
}
|
||||
|
||||
pub fn get_hostname(&self) -> Option<String> {
|
||||
if let Some(hostname) = self.hotname.take() {
|
||||
self.hotname.store(Some(hostname.clone()));
|
||||
return Some(hostname);
|
||||
let hostname = gethostname::gethostname().to_string_lossy().to_string();
|
||||
|
||||
let hostname = match self.hostname.take() {
|
||||
Some(name) => {
|
||||
// when allowing custom hostname, there may be empty
|
||||
if name.is_empty() {
|
||||
hostname
|
||||
} else {
|
||||
name
|
||||
}
|
||||
},
|
||||
None => hostname,
|
||||
};
|
||||
|
||||
let re = regex::Regex::new(r"[^\u4E00-\u9FA5a-zA-Z0-9\-]*").unwrap();
|
||||
let mut hostname = re.replace_all(&hostname, "").to_string();
|
||||
if hostname.len() > 32 {
|
||||
hostname = hostname.chars().take(32).collect::<String>();
|
||||
}
|
||||
|
||||
let hostname = gethostname::gethostname().to_string_lossy().to_string();
|
||||
self.hotname.store(Some(hostname.clone()));
|
||||
self.hostname.store(Some(hostname.clone()));
|
||||
return Some(hostname);
|
||||
}
|
||||
|
||||
|
||||
@@ -111,6 +111,9 @@ struct Cli {
|
||||
#[arg(long, help = "directory to store log files")]
|
||||
file_log_dir: Option<String>,
|
||||
|
||||
#[arg(long, help = "host name to identify this device")]
|
||||
hostname: Option<String>,
|
||||
|
||||
#[arg(
|
||||
short = 'm',
|
||||
long,
|
||||
@@ -177,6 +180,26 @@ impl From<Cli> for TomlConfigLoader {
|
||||
let cfg = TomlConfigLoader::default();
|
||||
|
||||
cfg.set_inst_name(cli.instance_name.clone());
|
||||
|
||||
let hostname = gethostname::gethostname().to_string_lossy().to_string();
|
||||
let hostname = match cli.hostname {
|
||||
Some(name) => {
|
||||
// when allowing custom hostname, there may be empty
|
||||
if name.is_empty() {
|
||||
hostname
|
||||
} else {
|
||||
name
|
||||
}
|
||||
}
|
||||
None => hostname,
|
||||
};
|
||||
let re = regex::Regex::new(r"[^\u4E00-\u9FA5a-zA-Z0-9\-]*").unwrap();
|
||||
let mut hostname = re.replace_all(&hostname, "").to_string();
|
||||
if hostname.len() > 32 {
|
||||
hostname = hostname.chars().take(32).collect::<String>();
|
||||
}
|
||||
cfg.set_hostname(Some(hostname));
|
||||
|
||||
cfg.set_network_identity(NetworkIdentity::new(
|
||||
cli.network_name.clone(),
|
||||
cli.network_secret.clone(),
|
||||
|
||||
Reference in New Issue
Block a user