trafic info into uniffi

This commit is contained in:
2026-03-27 16:33:57 +07:00
parent f4c6f2efdd
commit f8ca7d05be
3 changed files with 44 additions and 3 deletions
+21 -1
View File
@@ -8,7 +8,10 @@ pub use crate::tun::{routing, tun::Tun};
use crate::{ use crate::{
net::engine::{EngineBuilder, EngineConfig}, 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 netrunner_logger::{error, info};
use std::sync::{Arc, OnceLock}; 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 // SESSION
// ========================================== // ==========================================
@@ -159,4 +170,13 @@ impl SessionManager {
proxy_ip: remote_proxy_ip, 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),
}
}
} }
+10 -1
View File
@@ -1,8 +1,17 @@
namespace netrunner_client {}; namespace netrunner_client {};
dictionary VpnTrafficStats {
u64 rx_bytes;
u64 tx_bytes;
u64 rx_packets;
u64 tx_packets;
};
interface SessionManager { interface SessionManager {
constructor(); 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 { interface Session {
+13 -1
View File
@@ -4,7 +4,7 @@ use std::{
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
sync::{ sync::{
Arc, LazyLock, Mutex, Arc, LazyLock, Mutex,
atomic::{AtomicBool, Ordering}, atomic::{AtomicBool, AtomicU64, Ordering},
}, },
time::Instant as StdInstant, time::Instant as StdInstant,
}; };
@@ -20,6 +20,11 @@ use tokio::sync::mpsc;
const TOKEN_BUFFER_LIST_MAX_SIZE: usize = 64; const TOKEN_BUFFER_LIST_MAX_SIZE: usize = 64;
static TOKEN_BUFFER_LIST: LazyLock<Mutex<Vec<BytesMut>>> = LazyLock::new(|| Mutex::new(Vec::new())); 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)] #[derive(Debug, Clone, Copy)]
pub struct TrafficStats { pub struct TrafficStats {
pub rx_bytes: u64, pub rx_bytes: u64,
@@ -190,9 +195,13 @@ impl Device for VirtTunDevice {
self.check_and_log_stats(); self.check_and_log_stats();
if let Ok(buffer) = self.rx_queue.try_recv() { if let Ok(buffer) = self.rx_queue.try_recv() {
let len = buffer.len() as u64;
self.rx_bytes += buffer.len() as u64; self.rx_bytes += buffer.len() as u64;
self.rx_packets += 1; self.rx_packets += 1;
GLOBAL_RX_BYTES.fetch_add(len, Ordering::Relaxed);
GLOBAL_RX_PACKETS.fetch_add(1, Ordering::Relaxed);
let rx = Self::RxToken { let rx = Self::RxToken {
buffer, buffer,
phantom_device: PhantomData, phantom_device: PhantomData,
@@ -244,6 +253,9 @@ impl phy::TxToken for VirtTxToken<'_> {
self.0.tx_bytes += len as u64; self.0.tx_bytes += len as u64;
self.0.tx_packets += 1; 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); let _ = self.0.tx_queue.send(buffer);
result result