This commit is contained in:
Christoffer Martinsson 2025-10-12 16:08:44 +02:00
parent bd6c14c8c1
commit d33b24318a

View File

@ -622,70 +622,42 @@ impl ServiceCollector {
} }
async fn get_nginx_sites(&self) -> Option<Vec<String>> { async fn get_nginx_sites(&self) -> Option<Vec<String>> {
// Check enabled sites in sites-enabled directory // For NixOS and other systems, get the actual running nginx config
let sites_enabled_dir = "/etc/nginx/sites-enabled"; let output = Command::new("nginx")
.args(["-T"])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.await
.ok()?;
let mut entries = match fs::read_dir(sites_enabled_dir).await { if !output.status.success() {
Ok(entries) => entries, return None;
Err(_) => return None, }
};
let config = String::from_utf8_lossy(&output.stdout);
let mut sites = Vec::new(); let mut sites = Vec::new();
while let Ok(Some(entry)) = entries.next_entry().await { for line in config.lines() {
let path = entry.path();
// Skip if it's not a file or is a symlink to default
if !path.is_file() {
continue;
}
let filename = path.file_name()?.to_string_lossy();
// Skip default site unless it's the only one
if filename == "default" {
continue;
}
// Try to extract server names from the config file
if let Ok(config_content) = fs::read_to_string(&path).await {
let mut server_names = Vec::new();
for line in config_content.lines() {
let trimmed = line.trim(); let trimmed = line.trim();
if trimmed.starts_with("server_name") { if trimmed.starts_with("server_name") {
// Extract server names from "server_name example.com www.example.com;" // Extract server names from "server_name example.com www.example.com;"
if let Some(names_part) = trimmed.strip_prefix("server_name") { if let Some(names_part) = trimmed.strip_prefix("server_name") {
let names_clean = names_part.trim().trim_end_matches(';'); let names_clean = names_part.trim().trim_end_matches(';');
for name in names_clean.split_whitespace() { for name in names_clean.split_whitespace() {
if name != "_" && !name.is_empty() { // Skip default catch-all server names
server_names.push(name.to_string()); if name != "_" && !name.is_empty() && name.contains('.') && !name.starts_with('$') {
break; // Only take the first valid server name sites.push(name.to_string());
} }
} }
} }
} }
} }
if !server_names.is_empty() { // Remove duplicates and limit to reasonable number
sites.push(server_names[0].clone()); sites.sort();
} else { sites.dedup();
// Fallback to filename if no server_name found sites.truncate(15); // Show max 15 sites to avoid overwhelming the UI
sites.push(filename.to_string());
}
} else {
// Fallback to filename if can't read config
sites.push(filename.to_string());
}
}
// If no sites found, check for default
if sites.is_empty() {
let default_path = format!("{}/default", sites_enabled_dir);
if fs::metadata(&default_path).await.is_ok() {
sites.push("default".to_string());
}
}
if sites.is_empty() { if sites.is_empty() {
None None