Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src-tauri/core/src/connection/active_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub struct InterfaceStats {
impl From<super::apple::Stats> for InterfaceStats {
fn from(stats: super::apple::Stats) -> Self {
Self {
listen_port: 0,
listen_port: stats.listen_port.unwrap_or_default().into(),
tx_bytes: stats.tx_bytes,
rx_bytes: stats.rx_bytes,
last_handshake: (stats.last_handshake != 0).then_some(stats.last_handshake),
Expand Down
27 changes: 27 additions & 0 deletions src-tauri/core/src/connection/apple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ pub struct Stats {
pub tx_bytes: u64,
pub rx_bytes: u64,
pub last_handshake: u64,
pub listen_port: Option<u16>,
pub persistent_keep_alive: Option<u16>,
}

/// Retrieve VPN tunnel statistics from VPNExtension.
Expand Down Expand Up @@ -441,3 +443,28 @@ pub async fn sync_locations_and_tunnels(mtu: Option<u32>) -> Result<(), sqlx::Er

Ok(())
}

#[cfg(test)]
mod tests {
use super::Stats;

#[test]
fn test_stats_deserialize_listen_port_and_keepalive() {
let stats: Stats = serde_json::from_str(
r#"{"txBytes":1,"rxBytes":2,"lastHandshake":3,"locationId":7,
"listenPort":51820,"persistentKeepAlive":25}"#,
)
.unwrap();
assert_eq!(stats.location_id, Some(7));
assert_eq!(stats.listen_port, Some(51820));
assert_eq!(stats.persistent_keep_alive, Some(25));

// VPNExtension builds without these fields still send stats.
let stats: Stats =
serde_json::from_str(r#"{"txBytes":1,"rxBytes":2,"lastHandshake":3,"tunnelId":4}"#)
.unwrap();
assert_eq!(stats.tunnel_id, Some(4));
assert_eq!(stats.listen_port, None);
assert_eq!(stats.persistent_keep_alive, None);
}
}
8 changes: 4 additions & 4 deletions src-tauri/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ pub(crate) async fn stats_handler(id: Id, connection_type: ConnectionType) {
stats.tx_bytes.cast_signed(),
stats.rx_bytes.cast_signed(),
stats.last_handshake.cast_signed(),
0,
None,
stats.listen_port.unwrap_or_default().into(),
stats.persistent_keep_alive,
)
.with_diffs(upload_diff, download_diff);
match location_stats.save(&mut *transaction).await {
Expand All @@ -185,8 +185,8 @@ pub(crate) async fn stats_handler(id: Id, connection_type: ConnectionType) {
stats.rx_bytes.cast_signed(),
stats.last_handshake.cast_signed(),
chrono::Utc::now().naive_utc(),
0,
0,
stats.listen_port.unwrap_or_default().into(),
stats.persistent_keep_alive.unwrap_or_default(),
)
.with_diffs(upload_diff, download_diff);
match tunnel_stats.save(&mut *transaction).await {
Expand Down
10 changes: 9 additions & 1 deletion swift/extension/VPNExtension/Adapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ private struct PathSignature: Equatable {
/// For statistics returned to Rust code.
var locationId: UInt64?
var tunnelId: UInt64?
var persistentKeepAlive: UInt16?

private let notificationCenter = CFNotificationCenterGetDarwinNotifyCenter()

Expand Down Expand Up @@ -109,12 +110,18 @@ private struct PathSignature: Equatable {
func stats() -> Stats? {
syncOnQueue {
guard let stats = tunnel?.stats() else { return nil }
var listenPort: UInt16?
if case .hostPort(_, let port)? = connection?.currentPath?.localEndpoint {
listenPort = port.rawValue
}
return Stats(
txBytes: stats.txBytes,
rxBytes: stats.rxBytes,
lastHandshake: stats.lastHandshake,
locationId: locationId,
tunnelId: tunnelId
tunnelId: tunnelId,
listenPort: listenPort,
persistentKeepAlive: persistentKeepAlive
)
}
}
Expand Down Expand Up @@ -154,6 +161,7 @@ private struct PathSignature: Equatable {
)
locationId = tunnelConfiguration.locationId
tunnelId = tunnelConfiguration.tunnelId
persistentKeepAlive = peer.persistentKeepAlive
state = .running
reconnectAttempts = 0
lastPathSignature = nil
Expand Down
9 changes: 8 additions & 1 deletion swift/plugin/Sources/Defguard/Stats.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ public class Stats: NSObject, Codable {
// One or the other.
var locationId: UInt64?
var tunnelId: UInt64?
var listenPort: UInt16?
var persistentKeepAlive: UInt16?

init(txBytes: UInt64, rxBytes: UInt64, lastHandshake: UInt64, locationId: UInt64?, tunnelId: UInt64?) {
init(
txBytes: UInt64, rxBytes: UInt64, lastHandshake: UInt64, locationId: UInt64?,
tunnelId: UInt64?, listenPort: UInt16?, persistentKeepAlive: UInt16?
) {
self.txBytes = txBytes
self.rxBytes = rxBytes
self.lastHandshake = lastHandshake
self.locationId = locationId
self.tunnelId = tunnelId
self.listenPort = listenPort
self.persistentKeepAlive = persistentKeepAlive
}
}
Loading