Skip to main content

tauri_plugin_network_manager/
models.rs

1use serde::{Deserialize, Serialize};
2use tauri::{AppHandle, Runtime};
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct NetworkInfo {
6    pub name: String,
7    pub ssid: String,
8    pub connection_type: String,
9    pub icon: String,
10    pub ip_address: String,
11    pub mac_address: String,
12    pub signal_strength: u8,
13    pub security_type: WiFiSecurityType,
14    pub is_connected: bool,
15}
16
17impl Default for NetworkInfo {
18    fn default() -> Self {
19        Self {
20            name: String::from("Unknown"),
21            ssid: String::from("Unknown"),
22            connection_type: String::from("Unknown"),
23            icon: String::from("network-offline-symbolic"), // icono por defecto
24            ip_address: String::from("0.0.0.0"),
25            mac_address: String::from("00:00:00:00:00:00"),
26            signal_strength: 0,
27            security_type: WiFiSecurityType::None,
28            is_connected: false,
29        }
30    }
31}
32
33#[derive(Serialize, Deserialize, Debug, Clone)]
34#[serde(rename_all = "kebab-case")]
35pub enum WiFiSecurityType {
36    None,
37    Wep,
38    WpaPsk,
39    WpaEap,
40    Wpa2Psk,
41    Wpa3Psk,
42}
43
44#[derive(Serialize, Deserialize, Debug, Clone, Default)]
45pub struct WiFiNetwork {
46    pub ssid: String,
47    pub signal_strength: u8,
48    pub icon: String,
49}
50
51#[derive(Serialize, Deserialize, Debug)]
52pub struct WiFiConnectionConfig {
53    pub ssid: String,
54    #[serde(default)]
55    pub password: Option<String>,
56    pub security_type: WiFiSecurityType,
57    #[serde(default)]
58    pub username: Option<String>,
59}
60
61#[derive(Serialize, Deserialize, Debug, Clone)]
62#[serde(rename_all = "kebab-case")]
63pub enum VpnType {
64    OpenVpn,
65    WireGuard,
66    L2tp,
67    Pptp,
68    Sstp,
69    Ikev2,
70    Fortisslvpn,
71    OpenConnect,
72    Generic,
73}
74
75#[derive(Serialize, Deserialize, Debug, Clone)]
76pub struct VpnProfile {
77    pub id: String,
78    pub uuid: String,
79    pub vpn_type: VpnType,
80    pub interface_name: Option<String>,
81    pub autoconnect: bool,
82    pub editable: bool,
83    pub last_error: Option<String>,
84}
85
86#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
87#[serde(rename_all = "kebab-case")]
88pub enum VpnConnectionState {
89    Disconnected,
90    Connecting,
91    Connected,
92    Disconnecting,
93    Failed,
94    Unknown,
95}
96
97#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
98pub struct VpnStatus {
99    pub state: VpnConnectionState,
100    pub active_profile_id: Option<String>,
101    pub active_profile_uuid: Option<String>,
102    pub active_profile_name: Option<String>,
103    pub ip_address: Option<String>,
104    pub gateway: Option<String>,
105    pub since_unix_ms: Option<u64>,
106    /// La interfaz por donde sale el túnel: sdwan0, tailscale0, wg0…
107    pub interface: Option<String>,
108    /// La levantó su propia aplicación y no NetworkManager, así que se puede
109    /// mostrar pero no conectar ni desconectar desde acá: hacerlo pelearía con
110    /// el programa que la administra.
111    pub managed_externally: bool,
112}
113
114#[derive(Serialize, Deserialize, Debug, Clone)]
115pub struct VpnEventPayload {
116    pub status: VpnStatus,
117    pub profile: Option<VpnProfile>,
118    pub reason: Option<String>,
119}
120
121impl Default for VpnStatus {
122    fn default() -> Self {
123        Self {
124            state: VpnConnectionState::Disconnected,
125            active_profile_id: None,
126            active_profile_uuid: None,
127            active_profile_name: None,
128            ip_address: None,
129            gateway: None,
130            since_unix_ms: None,
131            interface: None,
132            managed_externally: false,
133        }
134    }
135}
136
137#[derive(Deserialize, Clone)]
138pub struct VpnCreateConfig {
139    pub id: String,
140    pub vpn_type: VpnType,
141    #[serde(default)]
142    pub autoconnect: Option<bool>,
143    #[serde(default)]
144    pub username: Option<String>,
145    #[serde(default)]
146    pub password: Option<String>,
147    #[serde(default)]
148    pub gateway: Option<String>,
149    #[serde(default)]
150    pub ca_cert_path: Option<String>,
151    #[serde(default)]
152    pub user_cert_path: Option<String>,
153    #[serde(default)]
154    pub private_key_path: Option<String>,
155    #[serde(default)]
156    pub private_key_password: Option<String>,
157    #[serde(default)]
158    pub settings: Option<std::collections::HashMap<String, String>>,
159    #[serde(default)]
160    pub secrets: Option<std::collections::HashMap<String, String>>,
161}
162
163#[derive(Deserialize, Clone)]
164pub struct VpnUpdateConfig {
165    pub uuid: String,
166    #[serde(default)]
167    pub id: Option<String>,
168    #[serde(default)]
169    pub autoconnect: Option<bool>,
170    #[serde(default)]
171    pub username: Option<String>,
172    #[serde(default)]
173    pub password: Option<String>,
174    #[serde(default)]
175    pub gateway: Option<String>,
176    #[serde(default)]
177    pub ca_cert_path: Option<String>,
178    #[serde(default)]
179    pub user_cert_path: Option<String>,
180    #[serde(default)]
181    pub private_key_path: Option<String>,
182    #[serde(default)]
183    pub private_key_password: Option<String>,
184    #[serde(default)]
185    pub settings: Option<std::collections::HashMap<String, String>>,
186    #[serde(default)]
187    pub secrets: Option<std::collections::HashMap<String, String>>,
188}
189
190/// Network statistics for bandwidth monitoring
191#[derive(Serialize, Deserialize, Debug, Clone)]
192pub struct NetworkStats {
193    /// Current download speed in bytes per second
194    pub download_speed: u64,
195    /// Current upload speed in bytes per second
196    pub upload_speed: u64,
197    /// Total bytes downloaded since connection
198    pub total_downloaded: u64,
199    /// Total bytes uploaded since connection
200    pub total_uploaded: u64,
201    /// Connection duration in seconds
202    pub connection_duration: u64,
203    /// Network interface name
204    pub interface: String,
205}
206
207/// Bandwidth data point for historical tracking
208#[derive(Serialize, Deserialize, Debug, Clone)]
209pub struct BandwidthPoint {
210    /// Timestamp in seconds since epoch
211    pub timestamp: u64,
212    /// Download speed in bytes per second
213    pub download_speed: u64,
214    /// Upload speed in bytes per second
215    pub upload_speed: u64,
216}
217
218// Removed duplicate init function
219
220#[derive(Clone, Debug)]
221pub struct VSKNetworkManager<'a, R: Runtime> {
222    pub connection: zbus::blocking::Connection,
223    pub proxy: zbus::blocking::fdo::PropertiesProxy<'a>,
224    pub app: AppHandle<R>,
225}