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}
107
108#[derive(Serialize, Deserialize, Debug, Clone)]
109pub struct VpnEventPayload {
110    pub status: VpnStatus,
111    pub profile: Option<VpnProfile>,
112    pub reason: Option<String>,
113}
114
115impl Default for VpnStatus {
116    fn default() -> Self {
117        Self {
118            state: VpnConnectionState::Disconnected,
119            active_profile_id: None,
120            active_profile_uuid: None,
121            active_profile_name: None,
122            ip_address: None,
123            gateway: None,
124            since_unix_ms: None,
125        }
126    }
127}
128
129#[derive(Deserialize, Clone)]
130pub struct VpnCreateConfig {
131    pub id: String,
132    pub vpn_type: VpnType,
133    #[serde(default)]
134    pub autoconnect: Option<bool>,
135    #[serde(default)]
136    pub username: Option<String>,
137    #[serde(default)]
138    pub password: Option<String>,
139    #[serde(default)]
140    pub gateway: Option<String>,
141    #[serde(default)]
142    pub ca_cert_path: Option<String>,
143    #[serde(default)]
144    pub user_cert_path: Option<String>,
145    #[serde(default)]
146    pub private_key_path: Option<String>,
147    #[serde(default)]
148    pub private_key_password: Option<String>,
149    #[serde(default)]
150    pub settings: Option<std::collections::HashMap<String, String>>,
151    #[serde(default)]
152    pub secrets: Option<std::collections::HashMap<String, String>>,
153}
154
155#[derive(Deserialize, Clone)]
156pub struct VpnUpdateConfig {
157    pub uuid: String,
158    #[serde(default)]
159    pub id: Option<String>,
160    #[serde(default)]
161    pub autoconnect: Option<bool>,
162    #[serde(default)]
163    pub username: Option<String>,
164    #[serde(default)]
165    pub password: Option<String>,
166    #[serde(default)]
167    pub gateway: Option<String>,
168    #[serde(default)]
169    pub ca_cert_path: Option<String>,
170    #[serde(default)]
171    pub user_cert_path: Option<String>,
172    #[serde(default)]
173    pub private_key_path: Option<String>,
174    #[serde(default)]
175    pub private_key_password: Option<String>,
176    #[serde(default)]
177    pub settings: Option<std::collections::HashMap<String, String>>,
178    #[serde(default)]
179    pub secrets: Option<std::collections::HashMap<String, String>>,
180}
181
182/// Network statistics for bandwidth monitoring
183#[derive(Serialize, Deserialize, Debug, Clone)]
184pub struct NetworkStats {
185    /// Current download speed in bytes per second
186    pub download_speed: u64,
187    /// Current upload speed in bytes per second
188    pub upload_speed: u64,
189    /// Total bytes downloaded since connection
190    pub total_downloaded: u64,
191    /// Total bytes uploaded since connection
192    pub total_uploaded: u64,
193    /// Connection duration in seconds
194    pub connection_duration: u64,
195    /// Network interface name
196    pub interface: String,
197}
198
199/// Bandwidth data point for historical tracking
200#[derive(Serialize, Deserialize, Debug, Clone)]
201pub struct BandwidthPoint {
202    /// Timestamp in seconds since epoch
203    pub timestamp: u64,
204    /// Download speed in bytes per second
205    pub download_speed: u64,
206    /// Upload speed in bytes per second
207    pub upload_speed: u64,
208}
209
210// Removed duplicate init function
211
212#[derive(Clone, Debug)]
213pub struct VSKNetworkManager<'a, R: Runtime> {
214    pub connection: zbus::blocking::Connection,
215    pub proxy: zbus::blocking::fdo::PropertiesProxy<'a>,
216    pub app: AppHandle<R>,
217}