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:
Andrew Phillips
2025-08-26 21:26:29 -03:00
parent 81ac8fcfbb
commit 23906d4796

View File

@@ -54,24 +54,61 @@ impl HostnameMetaPlugin {
Err(_) => return "unknown".to_string(), Err(_) => return "unknown".to_string(),
}; };
// Try to get the full hostname using DNS lookup // Try to get the FQDN using the system's hostname resolution
// This will work if the hostname can be resolved // This should give us the full hostname if configured
match dns_lookup::lookup_host(&short_hostname) { if let Ok(full_hostname) = std::process::Command::new("hostname")
Ok(addrs) => { .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 { for addr in addrs {
if let std::net::IpAddr::V4(ipv4) = addr { // Convert to IpAddr for lookup_addr
match dns_lookup::lookup_addr(&ipv4) { let ip_addr = match addr {
Ok(full_hostname) => return full_hostname, 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, 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 }
}
}
}
// Final fallback: return the short hostname
short_hostname short_hostname
} }
Err(_) => short_hostname,
}
}
} }
impl MetaPlugin for HostnameMetaPlugin { impl MetaPlugin for HostnameMetaPlugin {