fix: fast VPN status — read config file instead of slow nvpn CLI
nvpn status command hangs for seconds (connects to relays), causing the Network page to never finish loading. Read tunnel_ip from the local config file instead (instant). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
37b6b376b2
commit
fac5f117a9
@ -202,47 +202,34 @@ pub async fn get_status() -> VpnStatus {
|
|||||||
|
|
||||||
/// Check if NostrVPN system service is running and get its status.
|
/// Check if NostrVPN system service is running and get its status.
|
||||||
async fn get_nostr_vpn_status() -> Result<VpnStatus> {
|
async fn get_nostr_vpn_status() -> Result<VpnStatus> {
|
||||||
// Check if nostr-vpn service is active or activating
|
// Fast check: is the service unit enabled/active?
|
||||||
let output = tokio::process::Command::new("systemctl")
|
let svc_state = tokio::process::Command::new("systemctl")
|
||||||
.args(["is-active", "nostr-vpn"])
|
.args(["is-active", "nostr-vpn"])
|
||||||
.output()
|
.output()
|
||||||
.await
|
.await
|
||||||
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
|
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
let active = output == "active" || output == "activating";
|
if svc_state != "active" && svc_state != "activating" {
|
||||||
if !active {
|
anyhow::bail!("nostr-vpn service not running");
|
||||||
anyhow::bail!("nostr-vpn service not active");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to get status from nvpn CLI
|
// Quick IP check: read from config file (fast, no subprocess)
|
||||||
let output = tokio::process::Command::new("nvpn")
|
let ip = tokio::fs::read_to_string("/var/lib/archipelago/nostr-vpn/.config/nvpn/config.json")
|
||||||
.arg("status")
|
.await
|
||||||
.output()
|
.ok()
|
||||||
.await;
|
.and_then(|s| {
|
||||||
|
serde_json::from_str::<serde_json::Value>(&s).ok()
|
||||||
let (peers, ip) = match output {
|
})
|
||||||
Ok(o) if o.status.success() => {
|
.and_then(|v| v.get("tunnel_ip").and_then(|t| t.as_str()).map(|s| s.to_string()));
|
||||||
let stdout = String::from_utf8_lossy(&o.stdout);
|
|
||||||
let peers = stdout.lines()
|
|
||||||
.filter(|l| l.contains("peer") || l.contains("connected"))
|
|
||||||
.count() as u32;
|
|
||||||
let ip = stdout.lines()
|
|
||||||
.find(|l| l.contains("address") || l.contains("ip"))
|
|
||||||
.and_then(|l| l.split_whitespace().last())
|
|
||||||
.map(|s| s.to_string());
|
|
||||||
(peers, ip)
|
|
||||||
}
|
|
||||||
_ => (0, None),
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(VpnStatus {
|
Ok(VpnStatus {
|
||||||
connected: true,
|
connected: svc_state == "active",
|
||||||
provider: Some("nostr-vpn".to_string()),
|
provider: Some("nostr-vpn".to_string()),
|
||||||
interface: Some("nvpn0".to_string()),
|
interface: Some("nvpn0".to_string()),
|
||||||
ip_address: ip,
|
ip_address: ip,
|
||||||
hostname: None,
|
hostname: None,
|
||||||
peers_connected: peers,
|
peers_connected: 0,
|
||||||
bytes_in: 0,
|
bytes_in: 0,
|
||||||
bytes_out: 0,
|
bytes_out: 0,
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user