//! EasyTier networking SDK: application sockets by default, opt-in macOS system VPN. mod quality; mod system_probe; use anyhow::{Result, ensure}; use meshport_easytier::Engine; pub use meshport_easytier::parse_config as validate_config; pub use meshport_netstack::{TcpListener, TcpStream, UdpSocket}; use std::{ net::SocketAddr, time::{Duration, Instant}, }; pub struct Network { engine: Engine, } impl Network { #[cfg(target_os = "android")] pub fn attach_tun_fd(&self, fd: i32) -> Result<()> { self.engine.attach_tun_fd(fd) } /// Running means the engine is started, not that a remote peer is reachable. pub async fn start(toml: &str) -> Result { Ok(Self { engine: Engine::start(toml).await?, }) } pub async fn stop(&self) { self.engine.stop().await; } /// Removes this network's secret from diagnostic text before export. pub fn redact_diagnostic(&self, text: &str) -> String { self.engine.redact_diagnostic(text) } pub async fn snapshot(&self) -> serde_json::Value { self.engine.snapshot().await } pub async fn connect_tcp(&self, target: SocketAddr, timeout: Duration) -> Result { self.require_application_mode()?; ensure!( target.is_ipv4(), "IPv6 data plane is not supported in this prototype" ); Ok(self .engine .instance .data_plane_tcp_connect(target, timeout) .await?) } pub async fn listen_tcp(&self, port: u16, timeout: Duration) -> Result { self.require_application_mode()?; Ok(self .engine .instance .data_plane_tcp_bind(port, timeout) .await?) } pub async fn bind_udp(&self, port: u16, timeout: Duration) -> Result { self.require_application_mode()?; Ok(self .engine .instance .data_plane_udp_bind(port, timeout) .await?) } fn require_application_mode(&self) -> Result<()> { ensure!( self.engine.mode == meshport_easytier::RuntimeMode::Application, "use ordinary OS sockets in system_vpn mode" ); Ok(()) } pub async fn probe( &self, target: SocketAddr, udp: bool, echo: bool, timeout: Duration, ) -> Result { if self.engine.mode == meshport_easytier::RuntimeMode::SystemVpn { return system_probe::probe(target, udp, echo, timeout).await; } ensure!(!udp || echo, "UDP tests require a cooperating echo server"); let started = Instant::now(); let payload = b"MeshPort echo verification v1"; let bytes = if udp { let socket = self.bind_udp(0, timeout).await?; meshport_netstack::udp_echo(&socket, target, payload, timeout) .await? .len() } else { let mut stream = self.connect_tcp(target, timeout).await?; if echo { meshport_netstack::tcp_echo(&mut stream, payload, timeout) .await? .len() } else { 0 } }; Ok( serde_json::json!({"protocol": if udp { "UDP" } else { "TCP" }, "target": target.to_string(), "elapsed_ms": started.elapsed().as_secs_f64()*1000.0, "verified_bytes": bytes, "kind": if echo { "echo_roundtrip" } else { "tcp_connect" }}), ) } }