Compare commits

...

1 Commits

Author SHA1 Message Date
8da4522d85 Fix Tailscale peer detection by parsing text output
All checks were successful
Build and Release / build-and-release (push) Successful in 1m13s
Replace JSON parsing with simpler text output parsing from tailscale
status command. The text format clearly shows hostname and connection
method (direct/relay/idle) making detection more reliable.

Fixes issues with incorrect hostname (localhost instead of actual name)
and incorrect connection method detection (showing relay when actually
using direct connection).
2025-12-09 10:34:55 +01:00
5 changed files with 46 additions and 48 deletions

6
Cargo.lock generated
View File

@@ -279,7 +279,7 @@ checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d"
[[package]] [[package]]
name = "cm-dashboard" name = "cm-dashboard"
version = "0.1.262" version = "0.1.263"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"chrono", "chrono",
@@ -301,7 +301,7 @@ dependencies = [
[[package]] [[package]]
name = "cm-dashboard-agent" name = "cm-dashboard-agent"
version = "0.1.262" version = "0.1.263"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",
@@ -325,7 +325,7 @@ dependencies = [
[[package]] [[package]]
name = "cm-dashboard-shared" name = "cm-dashboard-shared"
version = "0.1.262" version = "0.1.263"
dependencies = [ dependencies = [
"chrono", "chrono",
"serde", "serde",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "cm-dashboard-agent" name = "cm-dashboard-agent"
version = "0.1.263" version = "0.1.264"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@@ -941,54 +941,52 @@ impl SystemdCollector {
/// Returns a list of (device_name, connection_method) tuples /// Returns a list of (device_name, connection_method) tuples
fn get_tailscale_peers(&self) -> Vec<(String, String)> { fn get_tailscale_peers(&self) -> Vec<(String, String)> {
match Command::new("timeout") match Command::new("timeout")
.args(["2", "tailscale", "status", "--json"]) .args(["2", "tailscale", "status"])
.output() .output()
{ {
Ok(output) if output.status.success() => { Ok(output) if output.status.success() => {
let json_str = String::from_utf8_lossy(&output.stdout); let status_output = String::from_utf8_lossy(&output.stdout);
let mut peers = Vec::new(); let mut peers = Vec::new();
if let Ok(json_data) = serde_json::from_str::<serde_json::Value>(&json_str) { // Parse tailscale status output
// Look for the self peer (current node) in the peer list // Format: IP hostname user os status
if let Some(peer_map) = json_data["Peer"].as_object() { // Example: 100.110.98.3 wslbox cm@ linux active; direct 192.168.30.227:53757
// Iterate through all peers for line in status_output.lines() {
for (_peer_id, peer_data) in peer_map { let parts: Vec<&str> = line.split_whitespace().collect();
// Only include active/online peers if parts.len() < 5 {
if !peer_data["Online"].as_bool().unwrap_or(false) { continue; // Skip invalid lines
continue;
} }
// Get peer hostname or DNS name // parts[0] = IP
let peer_name = peer_data["HostName"] // parts[1] = hostname
.as_str() // parts[2] = user
.or_else(|| peer_data["DNSName"].as_str()) // parts[3] = OS
.unwrap_or("unknown") // parts[4+] = status (e.g., "active;", "direct", "192.168.30.227:53757" or "idle;" or "offline")
.trim_end_matches('.')
.to_string();
// Determine connection method let hostname = parts[1];
let connection_method = if peer_data["Active"].as_bool().unwrap_or(false) { let status_parts = &parts[4..];
// Check if using relay
let relay_node = peer_data["Relay"].as_str().unwrap_or(""); // Determine connection method from status
if !relay_node.is_empty() { let connection_method = if status_parts.is_empty() {
"relay" continue; // Skip if no status
} else if let Some(cur_addr) = peer_data["CurAddr"].as_str() { } else {
// Check if using direct connection let status_str = status_parts.join(" ");
if !cur_addr.is_empty() { if status_str.contains("offline") {
continue; // Skip offline peers
} else if status_str.contains("direct") {
"direct" "direct"
} else { } else if status_str.contains("relay") {
"unknown" "relay"
} } else if status_str.contains("idle") {
} else {
"unknown"
}
} else {
"idle" "idle"
} else if status_str.contains("active") {
"active"
} else {
continue; // Skip unknown status
}
}; };
peers.push((peer_name, connection_method.to_string())); peers.push((hostname.to_string(), connection_method.to_string()));
}
}
} }
peers peers

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "cm-dashboard" name = "cm-dashboard"
version = "0.1.263" version = "0.1.264"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "cm-dashboard-shared" name = "cm-dashboard-shared"
version = "0.1.263" version = "0.1.264"
edition = "2021" edition = "2021"
[dependencies] [dependencies]