feat: improve hostname resolution with FQDN and IPv6 support
Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
@@ -54,23 +54,60 @@ impl HostnameMetaPlugin {
|
||||
Err(_) => return "unknown".to_string(),
|
||||
};
|
||||
|
||||
// Try to get the full hostname using DNS lookup
|
||||
// This will work if the hostname can be resolved
|
||||
match dns_lookup::lookup_host(&short_hostname) {
|
||||
Ok(addrs) => {
|
||||
for addr in addrs {
|
||||
if let std::net::IpAddr::V4(ipv4) = addr {
|
||||
match dns_lookup::lookup_addr(&ipv4) {
|
||||
Ok(full_hostname) => return full_hostname,
|
||||
Err(_) => continue,
|
||||
// Try to get the FQDN using the system's hostname resolution
|
||||
// This should give us the full hostname if configured
|
||||
if let Ok(full_hostname) = std::process::Command::new("hostname")
|
||||
.arg("-f")
|
||||
.output()
|
||||
{
|
||||
if full_hostname.status.success() {
|
||||
let full_hostname_str = String::from_utf8_lossy(&full_hostname.stdout).trim().to_string();
|
||||
if !full_hostname_str.is_empty() && full_hostname_str != short_hostname {
|
||||
return full_hostname_str;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: try DNS resolution for both IPv4 and IPv6 addresses
|
||||
// lookup_host should handle both A and AAAA records
|
||||
if let Ok(addrs) = dns_lookup::lookup_host(&short_hostname) {
|
||||
// Try each address (both IPv4 and IPv6)
|
||||
for addr in addrs {
|
||||
// Convert to IpAddr for lookup_addr
|
||||
let ip_addr = match addr {
|
||||
std::net::IpAddr::V4(ipv4) => std::net::IpAddr::V4(ipv4),
|
||||
std::net::IpAddr::V6(ipv6) => std::net::IpAddr::V6(ipv6),
|
||||
};
|
||||
// Perform reverse lookup for each address
|
||||
match dns_lookup::lookup_addr(&ip_addr) {
|
||||
Ok(full_hostname) => {
|
||||
// Only use if it's different from the short hostname and looks like a FQDN
|
||||
if full_hostname != short_hostname && full_hostname.contains('.') {
|
||||
return full_hostname;
|
||||
}
|
||||
}
|
||||
Err(_) => continue,
|
||||
}
|
||||
}
|
||||
|
||||
// If no reverse lookup worked, but we have addresses, try to construct FQDN
|
||||
// from the first address's domain if the short hostname is part of a domain
|
||||
if let Some(first_addr) = addrs.first() {
|
||||
// For local addresses, we might not get a reverse lookup, so try to infer
|
||||
// from the system's domain name
|
||||
if let Ok(domain) = std::process::Command::new("domainname").output() {
|
||||
if domain.status.success() {
|
||||
let domain_str = String::from_utf8_lossy(&domain.stdout).trim().to_string();
|
||||
if !domain_str.is_empty() && domain_str != "(none)" {
|
||||
return format!("{}.{}", short_hostname, domain_str);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Fall back to short hostname if resolution fails
|
||||
short_hostname
|
||||
}
|
||||
Err(_) => short_hostname,
|
||||
}
|
||||
|
||||
// Final fallback: return the short hostname
|
||||
short_hostname
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user