Add external IP display for OpenVPN connections
All checks were successful
Build and Release / build-and-release (push) Successful in 1m20s

Display VPN external IP as sub-service under openvpn-vpn-connection.
Query external IP through openvpn-namespace using curl ifconfig.me.

Version: v0.1.232
This commit is contained in:
Christoffer Martinsson 2025-12-01 13:49:54 +01:00
parent a7b69b8ae7
commit 885e19f7fd
5 changed files with 49 additions and 6 deletions

6
Cargo.lock generated
View File

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

View File

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

View File

@ -159,6 +159,19 @@ impl SystemdCollector {
}
}
if service_name.contains("openvpn-vpn-connection") && status_info.active_state == "active" {
if let Some(external_ip) = self.get_vpn_external_ip() {
let metrics = Vec::new();
sub_services.push(SubServiceData {
name: format!("IP: {}", external_ip),
service_status: Status::Ok,
metrics,
service_type: "vpn_route".to_string(),
});
}
}
// Create complete service data
let service_data = ServiceData {
name: service_name.clone(),
@ -836,6 +849,36 @@ impl SystemdCollector {
_ => value, // Assume bytes if no unit
}
}
/// Get VPN external IP by querying through the openvpn-namespace
fn get_vpn_external_ip(&self) -> Option<String> {
let output = Command::new("timeout")
.args(&[
"5",
"ip",
"netns",
"exec",
"openvpn-namespace",
"curl",
"-s",
"--max-time",
"4",
"https://ifconfig.me"
])
.output()
.ok()?;
if output.status.success() {
let ip = String::from_utf8_lossy(&output.stdout).trim().to_string();
if !ip.is_empty() && ip.contains('.') {
debug!("VPN external IP: {}", ip);
return Some(ip);
}
}
debug!("Failed to get VPN external IP");
None
}
}
#[async_trait]

View File

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

View File

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