mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-09-03 17:45:44 +00:00
Revert "remove hostname from config"
This commit is contained in:
@@ -1,3 +1,10 @@
|
|||||||
|
use std::{
|
||||||
|
hash::Hasher,
|
||||||
|
net::{IpAddr, SocketAddr},
|
||||||
|
path::PathBuf,
|
||||||
|
sync::{Arc, Mutex},
|
||||||
|
};
|
||||||
|
|
||||||
use super::env_parser;
|
use super::env_parser;
|
||||||
use crate::{
|
use crate::{
|
||||||
common::stun::StunInfoCollector,
|
common::stun::StunInfoCollector,
|
||||||
@@ -13,12 +20,6 @@ use cfg_if::cfg_if;
|
|||||||
use clap::builder::PossibleValue;
|
use clap::builder::PossibleValue;
|
||||||
use clap::ValueEnum;
|
use clap::ValueEnum;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{
|
|
||||||
hash::Hasher,
|
|
||||||
net::{IpAddr, SocketAddr},
|
|
||||||
path::PathBuf,
|
|
||||||
sync::{Arc, Mutex},
|
|
||||||
};
|
|
||||||
use strum::{Display, EnumString, VariantArray};
|
use strum::{Display, EnumString, VariantArray};
|
||||||
use tokio::io::AsyncReadExt as _;
|
use tokio::io::AsyncReadExt as _;
|
||||||
|
|
||||||
@@ -134,6 +135,9 @@ pub trait ConfigLoader: Send + Sync + DnsConfigLoaderExt {
|
|||||||
fn get_inst_name(&self) -> String;
|
fn get_inst_name(&self) -> String;
|
||||||
fn set_inst_name(&self, name: 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 get_netns(&self) -> Option<String>;
|
||||||
fn set_netns(&self, ns: 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 set_credential_file(&self, _path: Option<std::path::PathBuf>) {}
|
||||||
|
|
||||||
fn get_hostname(&self) -> String;
|
|
||||||
fn set_hostname(&self, hostname: &str);
|
|
||||||
|
|
||||||
fn dump(&self) -> String;
|
fn dump(&self) -> String;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -433,8 +434,7 @@ pub fn process_secure_mode_cfg(mut user_cfg: SecureModeConfig) -> anyhow::Result
|
|||||||
Ok(user_cfg)
|
Ok(user_cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Default, Deserialize, Serialize)]
|
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
|
||||||
#[serde(default)]
|
|
||||||
struct Config {
|
struct Config {
|
||||||
netns: Option<String>,
|
netns: Option<String>,
|
||||||
hostname: Option<String>,
|
hostname: Option<String>,
|
||||||
@@ -560,22 +560,6 @@ impl DnsConfigLoaderExt for TomlConfigLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ConfigLoader 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 {
|
fn get_inst_name(&self) -> String {
|
||||||
self.config
|
self.config
|
||||||
.lock()
|
.lock()
|
||||||
@@ -589,6 +573,33 @@ impl ConfigLoader for TomlConfigLoader {
|
|||||||
self.config.lock().unwrap().instance_name = Some(name);
|
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> {
|
fn get_netns(&self) -> Option<String> {
|
||||||
self.config.lock().unwrap().netns.clone()
|
self.config.lock().unwrap().netns.clone()
|
||||||
}
|
}
|
||||||
@@ -694,6 +705,22 @@ impl ConfigLoader for TomlConfigLoader {
|
|||||||
.unwrap_or_default()
|
.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 {
|
fn get_network_identity(&self) -> NetworkIdentity {
|
||||||
self.config
|
self.config
|
||||||
.lock()
|
.lock()
|
||||||
@@ -873,14 +900,6 @@ impl ConfigLoader for TomlConfigLoader {
|
|||||||
self.config.lock().unwrap().credential_file = path;
|
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 {
|
fn dump(&self) -> String {
|
||||||
let default_flags_json = serde_json::to_string(&gen_default_flags()).unwrap();
|
let default_flags_json = serde_json::to_string(&gen_default_flags()).unwrap();
|
||||||
let default_flags_hashmap =
|
let default_flags_hashmap =
|
||||||
|
|||||||
@@ -797,10 +797,9 @@ impl NetworkOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn merge_into(&self, cfg: &TomlConfigLoader) -> anyhow::Result<()> {
|
fn merge_into(&self, cfg: &TomlConfigLoader) -> anyhow::Result<()> {
|
||||||
// TODO: remove hostname
|
if self.hostname.is_some() {
|
||||||
// if self.hostname.is_some() {
|
cfg.set_hostname(self.hostname.clone());
|
||||||
// cfg.set_hostname(&self.hostname.clone());
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
let old_ns = cfg.get_network_identity();
|
let old_ns = cfg.get_network_identity();
|
||||||
let network_name = self.network_name.clone().unwrap_or(old_ns.network_name);
|
let network_name = self.network_name.clone().unwrap_or(old_ns.network_name);
|
||||||
|
|||||||
@@ -213,7 +213,7 @@ impl InstanceConfigPatcher {
|
|||||||
let global_ctx = weak_upgrade(&self.global_ctx)?;
|
let global_ctx = weak_upgrade(&self.global_ctx)?;
|
||||||
if let Some(hostname) = patch.hostname {
|
if let Some(hostname) = patch.hostname {
|
||||||
global_ctx.set_hostname(hostname.clone());
|
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 let Some(ipv4) = patch.ipv4 {
|
||||||
if !global_ctx.config.get_dhcp() {
|
if !global_ctx.config.get_dhcp() {
|
||||||
|
|||||||
@@ -492,9 +492,7 @@ impl NetworkConfig {
|
|||||||
.parse()
|
.parse()
|
||||||
.with_context(|| format!("failed to parse instance id: {:?}", self.instance_id))?,
|
.with_context(|| format!("failed to parse instance id: {:?}", self.instance_id))?,
|
||||||
);
|
);
|
||||||
if let Some(n) = self.hostname.as_ref() {
|
cfg.set_hostname(self.hostname.clone());
|
||||||
cfg.set_hostname(n)
|
|
||||||
}
|
|
||||||
cfg.set_dhcp(self.dhcp.unwrap_or_default());
|
cfg.set_dhcp(self.dhcp.unwrap_or_default());
|
||||||
cfg.set_inst_name(self.network_name.clone().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));
|
config.set_dhcp(rng.gen_bool(0.5));
|
||||||
|
|
||||||
if rng.gen_bool(0.7) {
|
if rng.gen_bool(0.7) {
|
||||||
config.set_hostname(&format!("host-{}", rng.gen::<u16>()));
|
let hostname = format!("host-{}", rng.gen::<u16>());
|
||||||
|
config.set_hostname(Some(hostname));
|
||||||
}
|
}
|
||||||
|
|
||||||
config.set_network_identity(crate::common::config::NetworkIdentity::new(
|
config.set_network_identity(crate::common::config::NetworkIdentity::new(
|
||||||
|
|||||||
@@ -239,11 +239,11 @@ impl ForeignNetworkEntry {
|
|||||||
) -> ArcGlobalCtx {
|
) -> ArcGlobalCtx {
|
||||||
let config = TomlConfigLoader::default();
|
let config = TomlConfigLoader::default();
|
||||||
config.set_network_identity(network.clone());
|
config.set_network_identity(network.clone());
|
||||||
config.set_hostname(&format!(
|
config.set_hostname(Some(format!(
|
||||||
"{}{}",
|
"{}{}",
|
||||||
PUBLIC_SERVER_HOSTNAME_PREFIX,
|
PUBLIC_SERVER_HOSTNAME_PREFIX,
|
||||||
global_ctx.get_hostname()
|
global_ctx.get_hostname()
|
||||||
));
|
)));
|
||||||
config.set_secure_mode(global_ctx.config.get_secure_mode());
|
config.set_secure_mode(global_ctx.config.get_secure_mode());
|
||||||
|
|
||||||
let mut flags = config.get_flags();
|
let mut flags = config.get_flags();
|
||||||
|
|||||||
Reference in New Issue
Block a user