replace gethostname with hostname

This commit is contained in:
Luna Yao
2026-04-17 18:43:16 +02:00
parent ba7fc1098b
commit 3795800975
4 changed files with 18 additions and 27 deletions
+1 -1
View File
@@ -56,7 +56,7 @@ itertools = "0.14.0"
strum = { version = "0.27.2", features = ["derive"] } strum = { version = "0.27.2", features = ["derive"] }
gethostname = "1.1.0" hostname = "0.4.2"
futures = { version = "0.3", features = ["bilock", "unstable"] } futures = { version = "0.3", features = ["bilock", "unstable"] }
+15 -20
View File
@@ -6,16 +6,15 @@ use std::{
}; };
use anyhow::Context; use anyhow::Context;
use base64::{Engine as _, prelude::BASE64_STANDARD}; use base64::{prelude::BASE64_STANDARD, Engine as _};
use clap::ValueEnum;
use clap::builder::PossibleValue; use clap::builder::PossibleValue;
use clap::ValueEnum;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum::{Display, EnumString, VariantArray}; use strum::{Display, EnumString, VariantArray};
use tokio::io::AsyncReadExt as _; use tokio::io::AsyncReadExt as _;
use crate::{ use crate::{
common::stun::StunInfoCollector, common::stun::StunInfoCollector,
instance::dns_server::DEFAULT_ET_DNS_ZONE,
proto::{ proto::{
acl::Acl, acl::Acl,
common::{CompressionAlgoPb, PortForwardConfigPb, SecureModeConfig, SocketType}, common::{CompressionAlgoPb, PortForwardConfigPb, SecureModeConfig, SocketType},
@@ -570,26 +569,22 @@ impl ConfigLoader for TomlConfigLoader {
} }
fn get_hostname(&self) -> String { fn get_hostname(&self) -> String {
let hostname = self.config.lock().unwrap().hostname.clone(); let hostname = self
.config
match hostname { .lock()
Some(hostname) => { .unwrap()
let hostname = hostname .hostname
.chars() .clone()
.map(|h| {
h.chars()
.filter(|c| !c.is_control()) .filter(|c| !c.is_control())
.take(32) .take(32)
.collect::<String>(); .collect::<String>()
})
.filter(|h| !h.is_empty());
if !hostname.is_empty() { self.set_hostname(hostname.clone());
self.set_hostname(Some(hostname.clone())); hostname.unwrap_or_else(|| hostname::get().unwrap_or_default().to_string_lossy().into_owned())
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>) { fn set_hostname(&self, name: Option<String>) {
+1 -2
View File
@@ -6,7 +6,6 @@ use crate::dns::utils::addr::NameServerAddrGroup;
use crate::dns::utils::parse; use crate::dns::utils::parse;
use crate::proto::dns::GetExportConfigResponse; use crate::proto::dns::GetExportConfigResponse;
use derivative::Derivative; use derivative::Derivative;
use gethostname::gethostname;
use hickory_proto::rr::{LowerName, Name}; use hickory_proto::rr::{LowerName, Name};
use hickory_proto::xfer::Protocol; use hickory_proto::xfer::Protocol;
use serde::{Deserialize, Deserializer, Serialize}; use serde::{Deserialize, Deserializer, Serialize};
@@ -52,7 +51,7 @@ impl DnsConfig {
impl DnsConfig { impl DnsConfig {
pub fn get_name(&self) -> LowerName { pub fn get_name(&self) -> LowerName {
if self.name.is_empty() { if self.name.is_empty() {
parse(gethostname().to_string_lossy().as_ref()) parse(hostname::get().unwrap_or_default().to_string_lossy().as_ref())
} else { } else {
self.name.clone() self.name.clone()
} }
+1 -4
View File
@@ -225,10 +225,7 @@ pub async fn run_web_client(
let mut flags = global_ctx.get_flags(); let mut flags = global_ctx.get_flags();
flags.bind_device = false; flags.bind_device = false;
global_ctx.set_flags(flags); global_ctx.set_flags(flags);
let hostname = match hostname { let hostname = hostname.unwrap_or_else(|| hostname::get().unwrap_or_default().to_string_lossy().into_owned());
None => gethostname::gethostname().to_string_lossy().to_string(),
Some(hostname) => hostname,
};
Ok(WebClient::new( Ok(WebClient::new(
create_connector_by_url(c_url.as_str(), &global_ctx, IpVersion::Both).await?, create_connector_by_url(c_url.as_str(), &global_ctx, IpVersion::Both).await?,
token.to_string(), token.to_string(),