trafic info into uniffi
This commit is contained in:
+21
-1
@@ -8,7 +8,10 @@ pub use crate::tun::{routing, tun::Tun};
|
||||
|
||||
use crate::{
|
||||
net::engine::{EngineBuilder, EngineConfig},
|
||||
tun::routing::reset_platform_routing,
|
||||
tun::{
|
||||
device::{GLOBAL_RX_BYTES, GLOBAL_RX_PACKETS, GLOBAL_TX_BYTES, GLOBAL_TX_PACKETS},
|
||||
routing::reset_platform_routing,
|
||||
},
|
||||
};
|
||||
use netrunner_logger::{error, info};
|
||||
use std::sync::{Arc, OnceLock};
|
||||
@@ -26,6 +29,14 @@ fn get_runtime() -> &'static Runtime {
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(uniffi::Record)]
|
||||
pub struct VpnTrafficStats {
|
||||
pub rx_bytes: u64,
|
||||
pub tx_bytes: u64,
|
||||
pub rx_packets: u64,
|
||||
pub tx_packets: u64,
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// SESSION
|
||||
// ==========================================
|
||||
@@ -159,4 +170,13 @@ impl SessionManager {
|
||||
proxy_ip: remote_proxy_ip,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_traffic_stats(&self) -> VpnTrafficStats {
|
||||
VpnTrafficStats {
|
||||
rx_bytes: GLOBAL_RX_BYTES.load(std::sync::atomic::Ordering::Relaxed),
|
||||
tx_bytes: GLOBAL_TX_BYTES.load(std::sync::atomic::Ordering::Relaxed),
|
||||
rx_packets: GLOBAL_RX_PACKETS.load(std::sync::atomic::Ordering::Relaxed),
|
||||
tx_packets: GLOBAL_TX_PACKETS.load(std::sync::atomic::Ordering::Relaxed),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,17 @@
|
||||
namespace netrunner_client {};
|
||||
|
||||
dictionary VpnTrafficStats {
|
||||
u64 rx_bytes;
|
||||
u64 tx_bytes;
|
||||
u64 rx_packets;
|
||||
u64 tx_packets;
|
||||
};
|
||||
|
||||
interface SessionManager {
|
||||
constructor();
|
||||
Session spawn_session(string remote_address, optional i32 tun_fd, string cache_path);
|
||||
Session spawn_session(string remote_address, i32? tun_fd, string cache_path);
|
||||
|
||||
VpnTrafficStats get_traffic_stats();
|
||||
};
|
||||
|
||||
interface Session {
|
||||
|
||||
@@ -4,7 +4,7 @@ use std::{
|
||||
ops::{Deref, DerefMut},
|
||||
sync::{
|
||||
Arc, LazyLock, Mutex,
|
||||
atomic::{AtomicBool, Ordering},
|
||||
atomic::{AtomicBool, AtomicU64, Ordering},
|
||||
},
|
||||
time::Instant as StdInstant,
|
||||
};
|
||||
@@ -20,6 +20,11 @@ use tokio::sync::mpsc;
|
||||
const TOKEN_BUFFER_LIST_MAX_SIZE: usize = 64;
|
||||
static TOKEN_BUFFER_LIST: LazyLock<Mutex<Vec<BytesMut>>> = LazyLock::new(|| Mutex::new(Vec::new()));
|
||||
|
||||
pub static GLOBAL_RX_BYTES: AtomicU64 = AtomicU64::new(0);
|
||||
pub static GLOBAL_TX_BYTES: AtomicU64 = AtomicU64::new(0);
|
||||
pub static GLOBAL_RX_PACKETS: AtomicU64 = AtomicU64::new(0);
|
||||
pub static GLOBAL_TX_PACKETS: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct TrafficStats {
|
||||
pub rx_bytes: u64,
|
||||
@@ -190,9 +195,13 @@ impl Device for VirtTunDevice {
|
||||
self.check_and_log_stats();
|
||||
|
||||
if let Ok(buffer) = self.rx_queue.try_recv() {
|
||||
let len = buffer.len() as u64;
|
||||
self.rx_bytes += buffer.len() as u64;
|
||||
self.rx_packets += 1;
|
||||
|
||||
GLOBAL_RX_BYTES.fetch_add(len, Ordering::Relaxed);
|
||||
GLOBAL_RX_PACKETS.fetch_add(1, Ordering::Relaxed);
|
||||
|
||||
let rx = Self::RxToken {
|
||||
buffer,
|
||||
phantom_device: PhantomData,
|
||||
@@ -244,6 +253,9 @@ impl phy::TxToken for VirtTxToken<'_> {
|
||||
self.0.tx_bytes += len as u64;
|
||||
self.0.tx_packets += 1;
|
||||
|
||||
GLOBAL_TX_BYTES.fetch_add(len as u64, Ordering::Relaxed);
|
||||
GLOBAL_TX_PACKETS.fetch_add(1, Ordering::Relaxed);
|
||||
|
||||
let _ = self.0.tx_queue.send(buffer);
|
||||
|
||||
result
|
||||
|
||||
Reference in New Issue
Block a user