psnet 1.1.0

A beautiful real-time TUI network monitor for Windows — 9 tabs, GeoIP, device discovery, firewall, topology & more
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
//! System-level security monitoring — GlassWire-style change detection.
//!
//! Monitors: hosts file tampering, proxy setting changes, WiFi evil twin
//! attacks, and application binary integrity. All checks are fault-tolerant
//! and will silently return empty results on errors rather than crashing.

use std::collections::HashMap;
use std::fs;
use std::net::TcpStream;
use std::process::Command;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

// ─── System event type ──────────────────────────────────────────────────────

/// Security-relevant system change detected by the monitor.
#[derive(Clone, Debug)]
pub enum SystemEvent {
    /// The hosts file content was modified.
    HostsFileChanged(String),
    /// Windows proxy settings were changed.
    ProxyChanged(String),
    /// A known SSID appeared with an unexpected BSSID (evil twin indicator).
    EvilTwinDetected(String),
    /// A tracked application's binary hash changed on disk.
    AppBinaryChanged { app: String, detail: String },
    /// Internet connectivity was lost.
    InternetLost(String),
    /// Internet connectivity was restored.
    InternetRestored,
}

// ─── Proxy state snapshot ───────────────────────────────────────────────────

#[derive(Clone, Debug, PartialEq, Eq)]
struct ProxyState {
    enabled: bool,
    server: String,
    auto_config_url: String,
}

impl Default for ProxyState {
    fn default() -> Self {
        Self {
            enabled: false,
            server: String::new(),
            auto_config_url: String::new(),
        }
    }
}

// ─── WiFi network record ────────────────────────────────────────────────────

#[derive(Clone, Debug)]
struct WifiNetwork {
    ssid: String,
    bssid: String,
    encryption: String,
}

// ─── Tracked app record ─────────────────────────────────────────────────────

#[derive(Clone, Debug)]
struct TrackedApp {
    exe_path: String,
    hash: u64,
}

// ─── FNV-1a hash (fast, no dependencies) ────────────────────────────────────

fn fnv1a_hash(data: &[u8]) -> u64 {
    const FNV_OFFSET: u64 = 14695981039346656037;
    const FNV_PRIME: u64 = 1099511628211;

    let mut hash = FNV_OFFSET;
    for &byte in data {
        hash ^= byte as u64;
        hash = hash.wrapping_mul(FNV_PRIME);
    }
    hash
}

// ─── SystemMonitor ──────────────────────────────────────────────────────────

/// Monitors system-level security indicators.
///
/// Tracks hosts file changes, proxy setting mutations, WiFi evil twin
/// attacks, and application binary integrity. Each category runs on its
/// own tick interval to avoid excessive I/O on every frame.
/// Result from background proxy/WiFi check.
enum BgCheckResult {
    Proxy(ProxyState),
    Wifi(Vec<WifiNetwork>),
}

pub struct SystemMonitor {
    /// FNV hash of the last-read hosts file content.
    hosts_hash: Option<u64>,
    /// Last known proxy configuration.
    proxy_state: Option<ProxyState>,
    /// Known SSID → set of observed BSSIDs.
    known_wifi: HashMap<String, Vec<String>>,
    /// Tracked application binaries: process_name → TrackedApp.
    tracked_apps: HashMap<String, TrackedApp>,
    /// Monotonic tick counter.
    tick_count: u64,
    /// Cached internet availability (None = not yet checked)
    internet_available: Option<bool>,
    /// Result from background connectivity check thread
    connectivity_result: Arc<Mutex<Option<bool>>>,
    /// Whether a connectivity check is in progress
    connectivity_checking: Arc<Mutex<bool>>,
    /// Background check results (proxy, WiFi) — avoid blocking main thread
    bg_check_results: Arc<Mutex<Vec<BgCheckResult>>>,
}

impl SystemMonitor {
    pub fn new() -> Self {
        Self {
            hosts_hash: None,
            proxy_state: None,
            known_wifi: HashMap::new(),
            tracked_apps: HashMap::new(),
            tick_count: 0,
            internet_available: None,
            connectivity_result: Arc::new(Mutex::new(None)),
            connectivity_checking: Arc::new(Mutex::new(false)),
            bg_check_results: Arc::new(Mutex::new(Vec::new())),
        }
    }

    // ── Master tick ─────────────────────────────────────────────────────

    /// Run all periodic checks and return any detected events.
    ///
    /// Check intervals (to avoid excessive I/O):
    /// - Hosts file / proxy: every 30 ticks
    /// - WiFi security: every 60 ticks
    /// - App binary hashes: every 120 ticks
    pub fn tick(&mut self) -> Vec<SystemEvent> {
        let mut events = Vec::new();
        let tick = self.tick_count;
        self.tick_count = self.tick_count.wrapping_add(1);

        // Poll background proxy/WiFi results first — drain into local vec to avoid borrow conflict
        let bg_results: Vec<BgCheckResult> = if let Ok(mut results) = self.bg_check_results.lock() {
            results.drain(..).collect()
        } else {
            Vec::new()
        };
        for result in bg_results {
            match result {
                BgCheckResult::Proxy(current) => {
                    if let Some(desc) = self.process_proxy_result(current) {
                        events.push(SystemEvent::ProxyChanged(desc));
                    }
                }
                BgCheckResult::Wifi(networks) => {
                    for warning in self.process_wifi_result(&networks) {
                        events.push(SystemEvent::EvilTwinDetected(warning));
                    }
                }
            }
        }

        // Hosts file — every 30 ticks (fast fs::read, OK on main thread)
        if tick % 30 == 0 {
            if let Some(desc) = self.check_hosts_file() {
                events.push(SystemEvent::HostsFileChanged(desc));
            }
        }

        // Proxy settings — every 30 ticks, on background thread (spawns `reg query`)
        if tick % 30 == 0 {
            let bg = Arc::clone(&self.bg_check_results);
            thread::spawn(move || {
                let state = read_proxy_state();
                if let Ok(mut results) = bg.lock() {
                    results.push(BgCheckResult::Proxy(state));
                }
            });
        }

        // WiFi evil twin — every 60 ticks, on background thread (spawns `netsh wlan`)
        if tick % 60 == 0 {
            let bg = Arc::clone(&self.bg_check_results);
            thread::spawn(move || {
                let networks = scan_wifi_networks();
                if let Ok(mut results) = bg.lock() {
                    results.push(BgCheckResult::Wifi(networks));
                }
            });
        }

        // App binary integrity — every 120 ticks
        if tick % 120 == 0 {
            for (app, detail) in self.check_app_changes() {
                events.push(SystemEvent::AppBinaryChanged { app, detail });
            }
        }

        // Internet connectivity — every 60 ticks
        if tick % 60 == 0 {
            self.start_connectivity_check();
        }
        if let Some(event) = self.poll_connectivity() {
            events.push(event);
        }

        events
    }

    // ── 1. Hosts file monitoring ────────────────────────────────────────

    /// Read the hosts file and compare its hash to the previously stored
    /// value. Returns a description string if the file changed, or `None`
    /// on first run / no change / read error.
    pub fn check_hosts_file(&mut self) -> Option<String> {
        let path = r"C:\Windows\System32\drivers\etc\hosts";
        let content = match fs::read(path) {
            Ok(bytes) => bytes,
            Err(_) => return None, // Can't read — skip silently
        };

        let hash = fnv1a_hash(&content);

        match self.hosts_hash {
            None => {
                // First read — establish baseline, no alert.
                self.hosts_hash = Some(hash);
                None
            }
            Some(prev) if prev == hash => None,
            Some(_) => {
                self.hosts_hash = Some(hash);
                let line_count = content.iter().filter(|&&b| b == b'\n').count();
                Some(format!(
                    "Hosts file modified ({} lines, hash {:016x})",
                    line_count, hash
                ))
            }
        }
    }

    // ── 2. Proxy settings monitoring ────────────────────────────────────

    /// Process a proxy state result (from background thread or direct call).
    fn process_proxy_result(&mut self, current: ProxyState) -> Option<String> {
        match &self.proxy_state {
            None => {
                self.proxy_state = Some(current);
                None
            }
            Some(prev) if *prev == current => None,
            Some(prev) => {
                let mut changes = Vec::new();

                if prev.enabled != current.enabled {
                    changes.push(format!(
                        "proxy {}",
                        if current.enabled { "enabled" } else { "disabled" }
                    ));
                }
                if prev.server != current.server {
                    if current.server.is_empty() {
                        changes.push("proxy server cleared".to_string());
                    } else {
                        changes.push(format!("proxy server → {}", current.server));
                    }
                }
                if prev.auto_config_url != current.auto_config_url {
                    if current.auto_config_url.is_empty() {
                        changes.push("auto-config URL cleared".to_string());
                    } else {
                        changes.push(format!(
                            "auto-config URL → {}",
                            current.auto_config_url
                        ));
                    }
                }

                self.proxy_state = Some(current);

                if changes.is_empty() {
                    None
                } else {
                    Some(format!("Proxy settings changed: {}", changes.join(", ")))
                }
            }
        }
    }

    // ── 3. WiFi / evil twin detection ───────────────────────────────────

    /// Process WiFi scan results (from background thread or direct call).
    fn process_wifi_result(&mut self, networks: &[WifiNetwork]) -> Vec<String> {
        if networks.is_empty() {
            return Vec::new();
        }

        let mut warnings = Vec::new();

        for net in networks {
            if net.ssid.is_empty() {
                continue;
            }

            let known_bssids = self
                .known_wifi
                .entry(net.ssid.clone())
                .or_insert_with(Vec::new);

            if known_bssids.is_empty() {
                // First sighting — learn it.
                known_bssids.push(net.bssid.clone());
            } else if !known_bssids.contains(&net.bssid) {
                // New BSSID for a known SSID — possible evil twin.
                warnings.push(format!(
                    "SSID \"{}\" seen with new BSSID {} (known: {})",
                    net.ssid,
                    net.bssid,
                    known_bssids.join(", ")
                ));
                known_bssids.push(net.bssid.clone());
            }

            // Warn if encryption is missing on a non-open network.
            let enc_lower = net.encryption.to_lowercase();
            if enc_lower.contains("open") || enc_lower.contains("none") {
                warnings.push(format!(
                    "SSID \"{}\" (BSSID {}) has no encryption",
                    net.ssid, net.bssid
                ));
            }
        }

        warnings
    }

    // ── 4. App binary hash tracking ─────────────────────────────────────

    /// Re-hash every tracked application binary and return a list of
    /// `(process_name, description)` for any that changed since last check.
    pub fn check_app_changes(&mut self) -> Vec<(String, String)> {
        let mut changed = Vec::new();

        for (name, tracked) in &mut self.tracked_apps {
            match fs::read(&tracked.exe_path) {
                Ok(bytes) => {
                    let new_hash = fnv1a_hash(&bytes);
                    if new_hash != tracked.hash {
                        changed.push((
                            name.clone(),
                            format!(
                                "Binary changed: {} (hash {:016x} → {:016x})",
                                tracked.exe_path, tracked.hash, new_hash
                            ),
                        ));
                        tracked.hash = new_hash;
                    }
                }
                Err(_) => {
                    // File disappeared or became unreadable.
                    changed.push((
                        name.clone(),
                        format!("Binary no longer readable: {}", tracked.exe_path),
                    ));
                }
            }
        }

        changed
    }

    /// Spawn a background connectivity check (non-blocking).
    fn start_connectivity_check(&self) {
        if let Ok(mut checking) = self.connectivity_checking.lock() {
            if *checking { return; }
            *checking = true;
        } else {
            return;
        }
        let result = Arc::clone(&self.connectivity_result);
        let checking = Arc::clone(&self.connectivity_checking);
        thread::spawn(move || {
            let available = TcpStream::connect_timeout(
                &"8.8.8.8:53".parse().unwrap(),
                Duration::from_secs(3),
            ).is_ok();
            if let Ok(mut r) = result.lock() {
                *r = Some(available);
            }
            if let Ok(mut c) = checking.lock() {
                *c = false;
            }
        });
    }

    /// Poll the background connectivity result and emit a SystemEvent if state changed.
    pub fn poll_connectivity(&mut self) -> Option<SystemEvent> {
        let result = if let Ok(mut r) = self.connectivity_result.lock() {
            r.take()
        } else {
            None
        };
        let available = result?;
        match self.internet_available {
            None => {
                self.internet_available = Some(available);
                None
            }
            Some(prev) if prev == available => None,
            Some(false) => {
                self.internet_available = Some(true);
                Some(SystemEvent::InternetRestored)
            }
            Some(true) => {
                self.internet_available = Some(false);
                Some(SystemEvent::InternetLost("Cannot reach 8.8.8.8:53".to_string()))
            }
        }
    }

}

// ─── Registry / system helpers ──────────────────────────────────────────────

/// Read proxy settings from the Windows registry via `reg query`.
fn read_proxy_state() -> ProxyState {
    let key = r"HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings";

    let output = match Command::new("reg")
        .args(["query", key, "/v", "ProxyEnable"])
        .output()
    {
        Ok(o) => o,
        Err(_) => return ProxyState::default(),
    };

    let text = String::from_utf8_lossy(&output.stdout);
    let enabled = parse_reg_dword(&text).unwrap_or(0) != 0;

    let server = query_reg_string(key, "ProxyServer");
    let auto_config = query_reg_string(key, "AutoConfigURL");

    ProxyState {
        enabled,
        server,
        auto_config_url: auto_config,
    }
}

/// Query a REG_SZ value from the registry.
fn query_reg_string(key: &str, value_name: &str) -> String {
    let output = match Command::new("reg")
        .args(["query", key, "/v", value_name])
        .output()
    {
        Ok(o) if o.status.success() => o,
        _ => return String::new(),
    };

    let text = String::from_utf8_lossy(&output.stdout);
    parse_reg_sz(&text).unwrap_or_default()
}

/// Parse a REG_DWORD value from `reg query` output.
/// Expected line format: `    ProxyEnable    REG_DWORD    0x0`
fn parse_reg_dword(output: &str) -> Option<u32> {
    for line in output.lines() {
        let trimmed = line.trim();
        if trimmed.contains("REG_DWORD") {
            // Last whitespace-delimited token is the value.
            if let Some(val_str) = trimmed.split_whitespace().last() {
                let val_str = val_str.trim_start_matches("0x");
                return u32::from_str_radix(val_str, 16).ok();
            }
        }
    }
    None
}

/// Parse a REG_SZ value from `reg query` output.
/// Expected line format: `    ProxyServer    REG_SZ    http://proxy:8080`
fn parse_reg_sz(output: &str) -> Option<String> {
    for line in output.lines() {
        let trimmed = line.trim();
        if trimmed.contains("REG_SZ") {
            let parts: Vec<&str> = trimmed.splitn(3, "REG_SZ").collect();
            if parts.len() >= 2 {
                return Some(parts[1].trim().to_string());
            }
        }
    }
    None
}

// ─── WiFi scanning ──────────────────────────────────────────────────────────

/// Run `netsh wlan show networks mode=bssid` and parse the output into
/// a list of visible networks with their SSIDs, BSSIDs, and encryption type.
fn scan_wifi_networks() -> Vec<WifiNetwork> {
    let output = match Command::new("netsh")
        .args(["wlan", "show", "networks", "mode=bssid"])
        .output()
    {
        Ok(o) if o.status.success() => o,
        _ => return Vec::new(),
    };

    let text = String::from_utf8_lossy(&output.stdout);
    parse_wifi_output(&text)
}

/// Parse the multi-block output from `netsh wlan show networks mode=bssid`.
///
/// The output looks like:
/// ```text
/// SSID 1 : MyNetwork
///     Network type            : Infrastructure
///     Authentication          : WPA2-Personal
///     Encryption              : CCMP
///     BSSID 1                 : aa:bb:cc:dd:ee:ff
///         Signal              : 85%
///         ...
///     BSSID 2                 : 11:22:33:44:55:66
///         Signal              : 42%
///         ...
/// ```
///
/// Each SSID block can contain multiple BSSIDs. We emit one `WifiNetwork`
/// entry per BSSID.
fn parse_wifi_output(text: &str) -> Vec<WifiNetwork> {
    let mut networks = Vec::new();
    let mut current_ssid = String::new();
    let mut current_encryption = String::new();

    for line in text.lines() {
        let trimmed = line.trim();

        if let Some(rest) = strip_field_prefix(trimmed, "SSID") {
            // Filter out BSSID lines — those start with "BSSID".
            if !trimmed.starts_with("BSSID") {
                current_ssid = rest.to_string();
            }
        }

        if let Some(rest) = strip_field_prefix(trimmed, "Encryption") {
            current_encryption = rest.to_string();
        }

        if let Some(rest) = strip_field_prefix(trimmed, "BSSID") {
            if !current_ssid.is_empty() {
                networks.push(WifiNetwork {
                    ssid: current_ssid.clone(),
                    bssid: rest.to_string(),
                    encryption: current_encryption.clone(),
                });
            }
        }
    }

    networks
}

/// Strip a field prefix like `"SSID 1 :"` or `"Encryption :"` and return
/// the value portion. Handles the `netsh` output format where the field
/// name is followed by an optional index number and a colon.
fn strip_field_prefix<'a>(line: &'a str, field: &str) -> Option<&'a str> {
    if !line.starts_with(field) {
        return None;
    }
    // Find the colon separator.
    if let Some(colon_pos) = line.find(':') {
        let value = line[colon_pos + 1..].trim();
        Some(value)
    } else {
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn fnv1a_produces_consistent_hash() {
        let data = b"hello world";
        let h1 = fnv1a_hash(data);
        let h2 = fnv1a_hash(data);
        assert_eq!(h1, h2);
        assert_ne!(h1, 0);
    }

    #[test]
    fn fnv1a_different_data_different_hash() {
        let h1 = fnv1a_hash(b"hello");
        let h2 = fnv1a_hash(b"world");
        assert_ne!(h1, h2);
    }

    #[test]
    fn parse_reg_dword_hex() {
        let output = "    ProxyEnable    REG_DWORD    0x1\r\n";
        assert_eq!(parse_reg_dword(output), Some(1));
    }

    #[test]
    fn parse_reg_dword_zero() {
        let output = "    ProxyEnable    REG_DWORD    0x0\r\n";
        assert_eq!(parse_reg_dword(output), Some(0));
    }

    #[test]
    fn parse_reg_dword_missing() {
        let output = "ERROR: The system was unable to find the specified registry key.\r\n";
        assert_eq!(parse_reg_dword(output), None);
    }

    #[test]
    fn parse_reg_sz_value() {
        let output = "    ProxyServer    REG_SZ    http://proxy:8080\r\n";
        assert_eq!(
            parse_reg_sz(output),
            Some("http://proxy:8080".to_string())
        );
    }

    #[test]
    fn parse_reg_sz_empty() {
        let output = "    AutoConfigURL    REG_SZ    \r\n";
        assert_eq!(parse_reg_sz(output), Some(String::new()));
    }

    #[test]
    fn parse_wifi_output_multiple_bssids() {
        let text = "\
SSID 1 : HomeNetwork
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP
    BSSID 1                 : aa:bb:cc:dd:ee:ff
        Signal              : 85%
        Radio type          : 802.11ac
        Channel             : 36
    BSSID 2                 : 11:22:33:44:55:66
        Signal              : 42%
        Radio type          : 802.11n
        Channel             : 6

SSID 2 : CoffeeShop
    Network type            : Infrastructure
    Authentication          : Open
    Encryption              : None
    BSSID 1                 : de:ad:be:ef:00:01
        Signal              : 60%
";
        let nets = parse_wifi_output(text);
        assert_eq!(nets.len(), 3);

        assert_eq!(nets[0].ssid, "HomeNetwork");
        assert_eq!(nets[0].bssid, "aa:bb:cc:dd:ee:ff");
        assert_eq!(nets[0].encryption, "CCMP");

        assert_eq!(nets[1].ssid, "HomeNetwork");
        assert_eq!(nets[1].bssid, "11:22:33:44:55:66");

        assert_eq!(nets[2].ssid, "CoffeeShop");
        assert_eq!(nets[2].bssid, "de:ad:be:ef:00:01");
        assert_eq!(nets[2].encryption, "None");
    }

    #[test]
    fn parse_wifi_empty_output() {
        assert!(parse_wifi_output("").is_empty());
        assert!(parse_wifi_output("No wireless interfaces found.\n").is_empty());
    }

    #[test]
    fn strip_field_prefix_basic() {
        assert_eq!(
            strip_field_prefix("SSID 1 : MyNet", "SSID"),
            Some("MyNet")
        );
        assert_eq!(
            strip_field_prefix("Encryption              : CCMP", "Encryption"),
            Some("CCMP")
        );
        assert_eq!(strip_field_prefix("Signal : 85%", "SSID"), None);
    }

    #[test]
    fn system_monitor_new_is_clean() {
        let _mon = SystemMonitor::new();
    }

    #[test]
    fn evil_twin_detection_logic() {
        let mut mon = SystemMonitor::new();

        // Simulate learning a network.
        mon.known_wifi
            .insert("TestNet".to_string(), vec!["aa:bb:cc:dd:ee:ff".to_string()]);

        // Simulate a second scan with same SSID but different BSSID.
        mon.known_wifi
            .entry("TestNet".to_string())
            .and_modify(|bssids| {
                if !bssids.contains(&"11:22:33:44:55:66".to_string()) {
                    bssids.push("11:22:33:44:55:66".to_string());
                }
            });

        let bssids = mon.known_wifi.get("TestNet").unwrap();
        assert_eq!(bssids.len(), 2);
    }

    #[test]
    fn proxy_state_equality() {
        let a = ProxyState {
            enabled: true,
            server: "proxy:8080".to_string(),
            auto_config_url: String::new(),
        };
        let b = ProxyState {
            enabled: true,
            server: "proxy:8080".to_string(),
            auto_config_url: String::new(),
        };
        let c = ProxyState {
            enabled: false,
            server: "proxy:8080".to_string(),
            auto_config_url: String::new(),
        };
        assert_eq!(a, b);
        assert_ne!(a, c);
    }
}