mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-05-07 02:09:06 +00:00
🎈 perf: get hostname
This commit is contained in:
@@ -194,6 +194,7 @@ impl TomlConfigLoader {
|
|||||||
config_str, config_str
|
config_str, config_str
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
Ok(TomlConfigLoader {
|
Ok(TomlConfigLoader {
|
||||||
config: Arc::new(Mutex::new(config)),
|
config: Arc::new(Mutex::new(config)),
|
||||||
})
|
})
|
||||||
@@ -221,12 +222,24 @@ impl ConfigLoader for TomlConfigLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_hostname(&self) -> String {
|
fn get_hostname(&self) -> String {
|
||||||
self.config
|
let hostname = self.config.lock().unwrap().hostname.clone();
|
||||||
.lock()
|
|
||||||
.unwrap()
|
match hostname {
|
||||||
.hostname
|
Some(hostname) => {
|
||||||
.clone()
|
let re = regex::Regex::new(r"[^\u4E00-\u9FA5a-zA-Z0-9\-]*").unwrap();
|
||||||
.unwrap_or(gethostname::gethostname().to_string_lossy().to_string())
|
let mut name = re.replace_all(&hostname, "").to_string();
|
||||||
|
|
||||||
|
if name.len() > 32 {
|
||||||
|
name = name.chars().take(32).collect::<String>();
|
||||||
|
}
|
||||||
|
|
||||||
|
if hostname != name {
|
||||||
|
self.set_hostname(Some(name.clone()));
|
||||||
|
}
|
||||||
|
name
|
||||||
|
}
|
||||||
|
None => gethostname::gethostname().to_string_lossy().to_string(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_hostname(&self, name: Option<String>) {
|
fn set_hostname(&self, name: Option<String>) {
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ pub struct GlobalCtx {
|
|||||||
|
|
||||||
ip_collector: Arc<IPCollector>,
|
ip_collector: Arc<IPCollector>,
|
||||||
|
|
||||||
hostname: AtomicCell<Option<String>>,
|
hostname: String,
|
||||||
|
|
||||||
stun_info_collection: Box<dyn StunInfoCollectorTrait>,
|
stun_info_collection: Box<dyn StunInfoCollectorTrait>,
|
||||||
|
|
||||||
@@ -97,7 +97,7 @@ impl GlobalCtx {
|
|||||||
|
|
||||||
ip_collector: Arc::new(IPCollector::new(net_ns)),
|
ip_collector: Arc::new(IPCollector::new(net_ns)),
|
||||||
|
|
||||||
hostname: AtomicCell::new(Some(hostname)),
|
hostname,
|
||||||
|
|
||||||
stun_info_collection: Box::new(StunInfoCollector::new_with_default_servers()),
|
stun_info_collection: Box::new(StunInfoCollector::new_with_default_servers()),
|
||||||
|
|
||||||
@@ -166,29 +166,8 @@ impl GlobalCtx {
|
|||||||
self.ip_collector.clone()
|
self.ip_collector.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_hostname(&self) -> Option<String> {
|
pub fn get_hostname(&self) -> String {
|
||||||
let hostname = gethostname::gethostname().to_string_lossy().to_string();
|
return self.hostname.clone();
|
||||||
|
|
||||||
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>();
|
|
||||||
}
|
|
||||||
|
|
||||||
self.hostname.store(Some(hostname.clone()));
|
|
||||||
return Some(hostname);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_stun_info_collector(&self) -> impl StunInfoCollectorTrait + '_ {
|
pub fn get_stun_info_collector(&self) -> impl StunInfoCollectorTrait + '_ {
|
||||||
|
|||||||
@@ -181,24 +181,7 @@ impl From<Cli> for TomlConfigLoader {
|
|||||||
|
|
||||||
cfg.set_inst_name(cli.instance_name.clone());
|
cfg.set_inst_name(cli.instance_name.clone());
|
||||||
|
|
||||||
let hostname = gethostname::gethostname().to_string_lossy().to_string();
|
cfg.set_hostname(cli.hostname.clone());
|
||||||
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(
|
cfg.set_network_identity(NetworkIdentity::new(
|
||||||
cli.network_name.clone(),
|
cli.network_name.clone(),
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ struct RoutePeerInfo {
|
|||||||
cost: u8,
|
cost: u8,
|
||||||
ipv4_addr: Option<Ipv4Addr>,
|
ipv4_addr: Option<Ipv4Addr>,
|
||||||
proxy_cidrs: Vec<String>,
|
proxy_cidrs: Vec<String>,
|
||||||
hostname: Option<String>,
|
hostname: String,
|
||||||
udp_stun_info: i8,
|
udp_stun_info: i8,
|
||||||
last_update: SystemTime,
|
last_update: SystemTime,
|
||||||
version: Version,
|
version: Version,
|
||||||
@@ -82,7 +82,7 @@ impl RoutePeerInfo {
|
|||||||
cost: 0,
|
cost: 0,
|
||||||
ipv4_addr: None,
|
ipv4_addr: None,
|
||||||
proxy_cidrs: Vec::new(),
|
proxy_cidrs: Vec::new(),
|
||||||
hostname: None,
|
hostname: String::new(),
|
||||||
udp_stun_info: 0,
|
udp_stun_info: 0,
|
||||||
last_update: SystemTime::now(),
|
last_update: SystemTime::now(),
|
||||||
version: 0,
|
version: 0,
|
||||||
@@ -138,11 +138,7 @@ impl Into<crate::rpc::Route> for RoutePeerInfo {
|
|||||||
next_hop_peer_id: 0,
|
next_hop_peer_id: 0,
|
||||||
cost: self.cost as i32,
|
cost: self.cost as i32,
|
||||||
proxy_cidrs: self.proxy_cidrs.clone(),
|
proxy_cidrs: self.proxy_cidrs.clone(),
|
||||||
hostname: if let Some(hostname) = &self.hostname {
|
hostname: self.hostname,
|
||||||
hostname.clone()
|
|
||||||
} else {
|
|
||||||
"".to_string()
|
|
||||||
},
|
|
||||||
stun_info: {
|
stun_info: {
|
||||||
let mut stun_info = StunInfo::default();
|
let mut stun_info = StunInfo::default();
|
||||||
if let Ok(udp_nat_type) = NatType::try_from(self.udp_stun_info as i32) {
|
if let Ok(udp_nat_type) = NatType::try_from(self.udp_stun_info as i32) {
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ pub struct SyncPeerInfo {
|
|||||||
pub cost: u32,
|
pub cost: u32,
|
||||||
pub ipv4_addr: Option<Ipv4Addr>,
|
pub ipv4_addr: Option<Ipv4Addr>,
|
||||||
pub proxy_cidrs: Vec<String>,
|
pub proxy_cidrs: Vec<String>,
|
||||||
pub hostname: Option<String>,
|
pub hostname: String,
|
||||||
pub udp_stun_info: i8,
|
pub udp_stun_info: i8,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -585,11 +585,7 @@ impl Route for BasicRoute {
|
|||||||
route.next_hop_peer_id = route_info.peer_id;
|
route.next_hop_peer_id = route_info.peer_id;
|
||||||
route.cost = route_info.cost as i32;
|
route.cost = route_info.cost as i32;
|
||||||
route.proxy_cidrs = route_info.proxy_cidrs.clone();
|
route.proxy_cidrs = route_info.proxy_cidrs.clone();
|
||||||
route.hostname = if let Some(hostname) = &route_info.hostname {
|
route.hostname = route_info.hostname.clone();
|
||||||
hostname.clone()
|
|
||||||
} else {
|
|
||||||
"".to_string()
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut stun_info = StunInfo::default();
|
let mut stun_info = StunInfo::default();
|
||||||
if let Ok(udp_nat_type) = NatType::try_from(route_info.udp_stun_info as i32) {
|
if let Ok(udp_nat_type) = NatType::try_from(route_info.udp_stun_info as i32) {
|
||||||
|
|||||||
Reference in New Issue
Block a user