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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
use std::ffi::OsStr;
use std::fmt;
use std::fs::{read_to_string, OpenOptions};
use std::io::{prelude::*, ErrorKind};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{Duration, Instant};

use crossbeam_channel::Sender;
use lazy_static::lazy_static;
use regex::bytes::Regex;
use serde_derive::Deserialize;

use crate::blocks::{Block, ConfigBlock, Update};
use crate::config::SharedConfig;
use crate::de::deserialize_duration;
use crate::errors::*;
use crate::formatting::value::Value;
use crate::formatting::FormatTemplate;
use crate::input::{I3BarEvent, MouseButton};
use crate::scheduler::Task;
use crate::util::{escape_pango_text, format_vec_to_bar_graph};
use crate::widgets::{text::TextWidget, I3BarWidget, Spacing};

lazy_static! {
    static ref DEFAULT_DEV_REGEX: Regex = Regex::new("default.*dev (\\w*).*").unwrap();
    static ref WHITESPACE_REGEX: Regex = Regex::new("\\s+").unwrap();
    static ref ETHTOOL_SPEED_REGEX: Regex = Regex::new("Speed: (\\d+\\w\\w/s)").unwrap();
    static ref IW_SSID_REGEX: Regex = Regex::new("SSID: (.*)").unwrap();
    static ref WPA_SSID_REGEX: Regex = Regex::new("^ssid=(.*)").unwrap();
    static ref IWCTL_SSID_REGEX: Regex = Regex::new("Connected network\\s+([[:alnum:]]+)").unwrap();
    static ref IW_BITRATE_REGEX: Regex =
        Regex::new("tx bitrate: (\\d+(?:\\.?\\d+) [[:alpha:]]+/s)").unwrap();
    static ref IW_SIGNAL_REGEX: Regex = Regex::new("signal: (-?\\d+) dBm").unwrap();
}

#[derive(Debug)]
pub struct NetworkDevice {
    device: String,
    device_path: PathBuf,
    wireless: bool,
    tun: bool,
    wg: bool,
    ppp: bool,
}

impl NetworkDevice {
    /// Use the network device `device`. Raises an error if a directory for that
    /// device is not found.
    pub fn from_device(device: String) -> Self {
        let device_path = Path::new("/sys/class/net").join(device.clone());

        // I don't believe that this should ever change, so set it now:
        let wireless = device_path.join("wireless").exists();
        let tun = device_path.join("tun_flags").exists()
            || device.starts_with("tun")
            || device.starts_with("tap");

        let uevent_path = device_path.join("uevent");
        let uevent_content = read_to_string(&uevent_path);

        let wg = match &uevent_content {
            Ok(s) => s.contains("wireguard"),
            Err(_e) => false,
        };
        let ppp = match &uevent_content {
            Ok(s) => s.contains("ppp"),
            Err(_e) => false,
        };

        NetworkDevice {
            device,
            device_path,
            wireless,
            tun,
            wg,
            ppp,
        }
    }

    pub fn device(&self) -> String {
        self.device.clone()
    }

    /// Grab the name of the 'default' device.
    /// A default device is usually selected by the network manager
    /// and will change when the status of devices change.
    pub fn default_device() -> Option<String> {
        String::from_utf8(
            Command::new("ip")
                .args(&["route", "show", "default"])
                .output()
                .ok()
                .and_then(|o| {
                    let mut captures = DEFAULT_DEV_REGEX.captures_iter(&o.stdout);
                    if let Some(cap) = captures.next() {
                        cap.get(1).map(|x| x.as_bytes().to_vec())
                    } else {
                        None
                    }
                })?,
        )
        .ok()
    }

    /// Check whether the device exists.
    pub fn exists(&self) -> Result<bool> {
        Ok(self.device_path.exists())
    }

    /// Check whether this network device is in the `up` state. Note that a
    /// device that is not `up` is not necessarily `down`.
    pub fn is_up(&self) -> Result<bool> {
        let operstate_file = self.device_path.join("operstate");
        if !operstate_file.exists() {
            // It seems more reasonable to treat these as inactive networks as
            // opposed to erroring out the entire block.
            Ok(false)
        } else if self.tun || self.wg || self.ppp {
            Ok(true)
        } else {
            let operstate = read_file(&operstate_file)?;
            let carrier_file = self.device_path.join("carrier");
            if !carrier_file.exists() {
                Ok(operstate == "up")
            } else if operstate == "up" {
                Ok(true)
            } else {
                let carrier = read_file(&carrier_file);
                match carrier {
                    Ok(carrier) => Ok(carrier == "1"),
                    Err(_e) => Ok(operstate == "up"),
                }
            }
        }
    }

    /// Query the device for the current `tx_bytes` statistic.
    pub fn tx_bytes(&self) -> Result<u64> {
        read_file(&self.device_path.join("statistics/tx_bytes"))?
            .parse::<u64>()
            .block_error("net", "Failed to parse tx_bytes")
    }

    /// Query the device for the current `rx_bytes` statistic.
    pub fn rx_bytes(&self) -> Result<u64> {
        read_file(&self.device_path.join("statistics/rx_bytes"))?
            .parse::<u64>()
            .block_error("net", "Failed to parse rx_bytes")
    }

    /// Checks whether this device is wireless.
    pub fn is_wireless(&self) -> bool {
        self.wireless
    }

    /// Checks whether this device is vpn network.
    pub fn is_vpn(&self) -> bool {
        self.tun || self.wg || self.ppp
    }

    /// Queries the wireless SSID of this device, if it is connected to one.
    pub fn ssid(&self) -> Result<Option<String>> {
        if self.is_up()? && self.wireless {
            get_ssid(self)
        } else {
            Ok(None)
        }
    }

    fn absolute_signal_strength(&self) -> Result<Option<i32>> {
        if !self.is_up()? || !self.wireless {
            return Ok(None);
        }

        let iw_output = Command::new("iw")
            .args(&["dev", &self.device, "link"])
            .output()
            .block_error("net", "Failed to execute signal strength query.")?
            .stdout;

        if let Some(raw) = IW_SIGNAL_REGEX
            .captures_iter(&iw_output)
            .next()
            .and_then(|x| x.get(1))
        {
            String::from_utf8(raw.as_bytes().to_vec())
                .block_error("net", "Non-UTF8 signal strength")
                .and_then(|s| {
                    s.parse::<i32>()
                        .block_error("net", "Non numerical signal strength.")
                })
                .map(Some)
        } else {
            Ok(None)
        }
    }

    fn relative_signal_strength(&self) -> Result<Option<u32>> {
        let xbm = if let Some(xbm) = self.absolute_signal_strength()? {
            xbm as f64
        } else {
            return Ok(None);
        };

        // Code inspired by https://github.com/NetworkManager/NetworkManager/blob/master/src/platform/wifi/nm-wifi-utils-nl80211.c
        const NOISE_FLOOR_DBM: f64 = -90.;
        const SIGNAL_MAX_DBM: f64 = -20.;

        let xbm = if xbm < NOISE_FLOOR_DBM {
            NOISE_FLOOR_DBM
        } else if xbm > SIGNAL_MAX_DBM {
            SIGNAL_MAX_DBM
        } else {
            xbm
        };

        let result = 100. - 70. * ((SIGNAL_MAX_DBM - xbm) / (SIGNAL_MAX_DBM - NOISE_FLOOR_DBM));
        let result = result as u32;
        Ok(Some(result))
    }

    /// Queries the inet IP of this device (using `ip`).
    pub fn ip_addr(&self) -> Result<Option<String>> {
        if !self.is_up()? {
            return Ok(None);
        }
        let output = Command::new("ip")
            .args(&["-json", "-family", "inet", "address", "show", &self.device])
            .output()
            .block_error("net", "Failed to execute IP address query.")
            .and_then(|raw_output| {
                String::from_utf8(raw_output.stdout)
                    .block_error("net", "Response contained non-UTF8 characters.")
            })?;

        let ip_devs: Vec<IpDev> =
            serde_json::from_str(&output).block_error("net", "Failed to parse JSON response")?;

        if ip_devs.is_empty() {
            return Ok(Some("".to_string()));
        }

        let ip = ip_devs
            .iter()
            .filter(|dev| dev.addr_info.is_some())
            .flat_map(|dev| &dev.addr_info)
            .flatten()
            .filter_map(|addr| addr.local.clone())
            .next();

        Ok(match ip {
            Some(addr) => Some(addr),
            _ => Some("".to_string()),
        })
    }

    /// Queries the inet IPv6 of this device (using `ip`).
    pub fn ipv6_addr(&self) -> Result<Option<String>> {
        if !self.is_up()? {
            return Ok(None);
        }
        let output = Command::new("ip")
            .args(&["-json", "-family", "inet6", "address", "show", &self.device])
            .output()
            .block_error("net", "Failed to execute IP address query.")
            .and_then(|raw_output| {
                String::from_utf8(raw_output.stdout)
                    .block_error("net", "Response contained non-UTF8 characters.")
            })?;

        let ip_devs: Vec<IpDev> =
            serde_json::from_str(&output).block_error("net", "Failed to parse JSON response")?;

        if ip_devs.is_empty() {
            return Ok(Some("".to_string()));
        }

        let ip = ip_devs
            .iter()
            .filter(|dev| dev.addr_info.is_some())
            .flat_map(|dev| &dev.addr_info)
            .flatten()
            .filter_map(|addr| addr.local.clone())
            .next();

        Ok(match ip {
            Some(addr) => Some(addr),
            _ => Some("".to_string()),
        })
    }

    /// Queries the bitrate of this device
    pub fn bitrate(&self) -> Result<Option<String>> {
        let up = self.is_up()?;
        // Doesn't really make sense to crash the bar here
        if !up {
            return Ok(None);
        }
        if self.wireless {
            let bitrate_output = Command::new("iw")
                .args(&["dev", &self.device, "link"])
                .output()
                .block_error("net", "Failed to execute bitrate query with iw.")?
                .stdout;

            if let Some(rate) = IW_BITRATE_REGEX
                .captures_iter(&bitrate_output)
                .next()
                .and_then(|x| x.get(1))
            {
                String::from_utf8(rate.as_bytes().to_vec())
                    .block_error("net", "Non-UTF8 bitrate")
                    .map(Some)
            } else {
                Ok(None)
            }
        } else {
            let output = Command::new("ethtool")
                .arg(&self.device)
                .output()
                .block_error("net", "Failed to execute bitrate query with ethtool")?
                .stdout;
            if let Some(rate) = ETHTOOL_SPEED_REGEX.captures_iter(&output).next() {
                let rate = rate
                    .get(1)
                    .block_error("net", "Invalid ethtool output: no speed")?;
                String::from_utf8(rate.as_bytes().to_vec())
                    .block_error("net", "Non-UTF8 bitrate")
                    .map(Some)
            } else {
                Ok(None)
            }
        }
    }
}

pub struct Net {
    id: usize,
    format: FormatTemplate,
    format_alt: Option<FormatTemplate>,
    output: TextWidget,
    ssid: Option<String>,
    signal_strength: u32,
    ip_addr: Option<String>,
    ipv6_addr: Option<String>,
    bitrate: Option<String>,
    speed_up: f64,
    speed_down: f64,
    graph_tx: String,
    graph_rx: String,
    update_interval: Duration,
    device: NetworkDevice,
    auto_device: bool,
    tx_buff: Vec<f64>,
    rx_buff: Vec<f64>,
    tx_bytes: u64,
    rx_bytes: u64,
    active: bool,
    exists: bool,
    hide_inactive: bool,
    hide_missing: bool,
    last_update: Instant,
    shared_config: SharedConfig,
}

#[derive(Copy, Clone, Debug, Deserialize)]
pub enum Unit {
    B,
    K,
    M,
    G,
    T,
}

impl Default for Unit {
    fn default() -> Self {
        Unit::K
    }
}

impl fmt::Display for Unit {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

#[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields, default)]
pub struct NetConfig {
    /// Update interval in seconds
    #[serde(deserialize_with = "deserialize_duration")]
    pub interval: Duration,

    pub format: String,

    pub format_alt: Option<String>,

    /// Which interface in /sys/class/net/ to read from.
    pub device: Option<String>,

    /// Whether to hide networks that are down/inactive completely.
    pub hide_inactive: bool,

    /// Whether to hide networks that are missing.
    pub hide_missing: bool,
}

impl Default for NetConfig {
    fn default() -> Self {
        Self {
            interval: Duration::from_secs(1),
            format: "{speed_up;K} {speed_down;K}".to_string(),
            format_alt: None,
            device: None,
            hide_inactive: false,
            hide_missing: false,
        }
    }
}

impl ConfigBlock for Net {
    type Config = NetConfig;

    fn new(
        id: usize,
        block_config: Self::Config,
        shared_config: SharedConfig,
        _tx_update_request: Sender<Task>,
    ) -> Result<Self> {
        let default_device = match NetworkDevice::default_device() {
            Some(ref s) if !s.is_empty() => s.to_string(),
            _ => "lo".to_string(),
        };
        let device = match block_config.device.clone() {
            Some(d) => NetworkDevice::from_device(d),
            _ => NetworkDevice::from_device(default_device),
        };
        let init_rx_bytes = device.rx_bytes().unwrap_or(0);
        let init_tx_bytes = device.tx_bytes().unwrap_or(0);
        let wireless = device.is_wireless();
        let vpn = device.is_vpn();

        let format_alt = if let Some(f) = block_config.format_alt {
            Some(
                FormatTemplate::from_string(&f)
                    .block_error("net", "Invalid format_alt specified")?,
            )
        } else {
            None
        };

        Ok(Net {
            id,
            update_interval: block_config.interval,
            format: FormatTemplate::from_string(&block_config.format)?,
            format_alt,
            output: TextWidget::new(id, 0, shared_config.clone())
                .with_icon(if wireless {
                    "net_wireless"
                } else if vpn {
                    "net_vpn"
                } else if device.device == "lo" {
                    "net_loopback"
                } else {
                    "net_wired"
                })?
                .with_text("")
                .with_spacing(Spacing::Inline),
            ssid: None,
            signal_strength: 0,
            // TODO: a better way to deal with this?
            bitrate: if block_config.format.contains("bitrate") {
                Some("".to_string())
            } else {
                None
            },
            ip_addr: if block_config.format.contains("ip") {
                Some("".to_string())
            } else {
                None
            },
            ipv6_addr: if block_config.format.contains("ipv6") {
                Some("".to_string())
            } else {
                None
            },
            speed_up: 0.0,
            speed_down: 0.0,
            graph_tx: String::new(),
            graph_rx: String::new(),
            device,
            auto_device: block_config.device.is_none(),
            rx_buff: vec![0.; 10],
            tx_buff: vec![0.; 10],
            rx_bytes: init_rx_bytes,
            tx_bytes: init_tx_bytes,
            active: true,
            exists: true,
            hide_inactive: block_config.hide_inactive,
            hide_missing: block_config.hide_missing,
            last_update: Instant::now() - Duration::from_secs(30),
            shared_config,
        })
    }
}

fn read_file(path: &Path) -> Result<String> {
    let mut f = OpenOptions::new().read(true).open(path).block_error(
        "net",
        &format!("failed to open file {}", path.to_string_lossy()),
    )?;
    let mut content = String::new();
    f.read_to_string(&mut content)
        .block_error("net", &format!("failed to read {}", path.to_string_lossy()))?;
    // Removes trailing newline
    content.pop();
    Ok(content)
}

impl Net {
    fn update_bitrate(&mut self) -> Result<()> {
        if let Some(ref mut bitrate_string) = self.bitrate {
            let bitrate = self.device.bitrate()?;
            if let Some(b) = bitrate {
                *bitrate_string = b;
            }
        }
        Ok(())
    }

    fn update_ssid(&mut self) -> Result<()> {
        if let Some(s) = self.device.ssid()? {
            // SSID names can contain chars that need escaping
            self.ssid = Some(escape_pango_text(s));
        } else {
            self.ssid = None;
        }
        Ok(())
    }

    fn update_signal_strength(&mut self) -> Result<()> {
        if self.device.wireless {
            self.signal_strength = self.device.relative_signal_strength()?.unwrap_or(0);
        }

        Ok(())
    }

    fn update_ip_addr(&mut self) -> Result<()> {
        if let Some(ref mut ip_addr_string) = self.ip_addr {
            let ip_addr = self.device.ip_addr()?;
            if let Some(ip) = ip_addr {
                *ip_addr_string = ip;
            }
        }
        if let Some(ref mut ipv6_addr_string) = self.ipv6_addr {
            let ipv6_addr = self.device.ipv6_addr()?;
            if let Some(ip) = ipv6_addr {
                *ipv6_addr_string = ip;
            }
        }
        Ok(())
    }

    fn update_tx_rx(&mut self) -> Result<()> {
        // TODO: consider using `as_nanos`
        let update_interval = (self.update_interval.as_secs() as f64)
            + (self.update_interval.subsec_nanos() as f64 / 1_000_000_000.0);

        // Update the throughput/graph widgets if they are enabled
        let current_tx = self.device.tx_bytes()?;
        let diff = current_tx.saturating_sub(self.tx_bytes);
        let tx_bytes = (diff as f64 / update_interval) as u64;
        self.tx_bytes = current_tx;

        self.speed_up = tx_bytes as f64;

        self.tx_buff.remove(0);
        self.tx_buff.push(tx_bytes as f64);
        self.graph_tx = format_vec_to_bar_graph(&self.tx_buff, None, None);

        let current_rx = self.device.rx_bytes()?;
        let diff = current_rx.saturating_sub(self.rx_bytes);
        let rx_bytes = (diff as f64 / update_interval) as u64;
        self.rx_bytes = current_rx;

        self.speed_down = rx_bytes as f64;

        self.rx_buff.remove(0);
        self.rx_buff.push(rx_bytes as f64);
        self.graph_rx = format_vec_to_bar_graph(&self.rx_buff, None, None);

        Ok(())
    }
}

impl Block for Net {
    fn update(&mut self) -> Result<Option<Update>> {
        // Update device
        if self.auto_device {
            let dev = match NetworkDevice::default_device() {
                Some(ref s) if !s.is_empty() => s.to_string(),
                _ => "lo".to_string(),
            };

            if self.device.device() != dev {
                self.device = NetworkDevice::from_device(dev);
                self.output.set_icon(if self.device.is_wireless() {
                    "net_wireless"
                } else if self.device.is_vpn() {
                    "net_vpn"
                } else if self.device.device == "lo" {
                    "net_loopback"
                } else {
                    "net_wired"
                })?;
            }
        }

        // skip updating if device is not up.
        self.exists = self.device.exists()?;
        self.active = self.exists && self.device.is_up()?;
        if !self.active {
            self.output.set_text("×".to_string());
            return Ok(Some(self.update_interval.into()));
        }

        // Update SSID and IP address every 30s and the bitrate every 10s
        let now = Instant::now();
        if now.duration_since(self.last_update).as_secs() % 10 == 0 {
            self.update_bitrate()?;
        }

        let waiting_for_ip = match self.ip_addr.as_deref() {
            None => false,
            Some("") => true,
            Some(_) => false,
        };

        let waiting_for_ipv6 = match self.ipv6_addr.as_deref() {
            None => false,
            Some("") => true,
            Some(_) => false,
        };

        if (now.duration_since(self.last_update).as_secs() > 30)
            || waiting_for_ip
            || waiting_for_ipv6
        {
            self.update_ssid()?;
            self.update_signal_strength()?;
            self.update_ip_addr()?;
            self.last_update = now;
        }

        self.update_tx_rx()?;

        let empty_string = "".to_string();
        let na_string = "N/A".to_string();

        let values = map!(
            "ssid" => Value::from_string(self.ssid.clone().unwrap_or(na_string)),
            "signal_strength" => Value::from_integer(self.signal_strength as i64).percents(),
            "bitrate" => Value::from_string(self.bitrate.clone().unwrap_or_else(|| empty_string.clone())), // TODO: not a String?
            "ip" => Value::from_string(self.ip_addr.clone().unwrap_or_else(|| empty_string.clone())),
            "ipv6" => Value::from_string(self.ipv6_addr.clone().unwrap_or(empty_string)),
            "speed_up" => Value::from_float(self.speed_up).bytes().icon(self.shared_config.get_icon("net_up")?),
            "speed_down" => Value::from_float(self.speed_down).bytes().icon(self.shared_config.get_icon("net_down")?),
            "graph_up" => Value::from_string(self.graph_tx.clone()),
            "graph_down" => Value::from_string(self.graph_rx.clone()),
        );

        self.output.set_text(self.format.render(&values)?);

        Ok(Some(self.update_interval.into()))
    }

    fn view(&self) -> Vec<&dyn I3BarWidget> {
        if (!self.active && self.hide_inactive) || (!self.exists && self.hide_missing) {
            vec![]
        } else {
            vec![&self.output]
        }
    }

    fn click(&mut self, event: &I3BarEvent) -> Result<()> {
        if event.button == MouseButton::Left {
            if let Some(ref mut format) = self.format_alt {
                std::mem::swap(format, &mut self.format);
            }
            self.update()?;
        }
        Ok(())
    }

    fn id(&self) -> usize {
        self.id
    }
}

#[derive(Deserialize)]
struct IpDev {
    addr_info: Option<Vec<IpAddrInfo>>,
}

#[derive(Deserialize)]
struct IpAddrInfo {
    local: Option<String>,
}

fn get_ssid(dev: &NetworkDevice) -> Result<Option<String>> {
    if let Some(res) = get_iw_ssid(dev)? {
        return Ok(Some(res));
    }

    if let Some(res) = get_wpa_ssid(dev)? {
        return Ok(Some(res));
    }

    if let Some(res) = get_nmcli_ssid(dev)? {
        return Ok(Some(res));
    }

    if let Some(res) = get_iwctl_ssid(dev)? {
        return Ok(Some(res));
    }

    Ok(None)
}

#[inline]
/// Attempt to get the SSID the given device is connected to from iw.
/// Returns Err if:
///     - `iw` is not a valid command
///     - failed to spawn an `iw` command
///     - `iw` failed to produce a valid UTF-8 SSID
/// Returns Ok(None) if `iw` failed to produce a SSID.
fn get_iw_ssid(dev: &NetworkDevice) -> Result<Option<String>> {
    let raw = exec_ssid_cmd("iw", &["dev", &dev.device, "link"])?;

    if raw.is_none() {
        return Ok(None);
    }

    let raw = raw.unwrap();
    let result = raw
        .stdout
        .split(|c| *c == b'\n')
        .filter_map(|x| IW_SSID_REGEX.captures_iter(x).next())
        .filter_map(|x| x.get(1))
        .next();

    maybe_ssid_convert(result.map(|x| x.as_bytes()))
}

#[inline]
/// Attempt to get the SSID the given device is connected to from wpa_cli.
/// Returns Err if:
///     - `wpa_cli` is not a valid command
///     - failed to spawn a `wpa_cli` command
///     - `wpa_cli` failed to produce a valid UTF-8 SSID
/// Returns Ok(None) if `wpa_cli` failed to produce a SSID.
fn get_wpa_ssid(dev: &NetworkDevice) -> Result<Option<String>> {
    let raw = exec_ssid_cmd("wpa_cli", &["status", "-i", &dev.device])?;

    if raw.is_none() {
        return Ok(None);
    }

    let raw = raw.unwrap();
    let result = raw
        .stdout
        .split(|c| *c == b'\n')
        .filter_map(|x| WPA_SSID_REGEX.captures_iter(x).next())
        .filter_map(|x| x.get(1))
        .next();

    maybe_ssid_convert(result.map(|x| x.as_bytes()))
}

#[inline]
/// Attempt to get the SSID the given device is connected to from nmcli.
/// Returns Err if:
///     - `nmcli` is not a valid command
///     - failed to spawn a `nmcli` command
///     - `nmcli` failed to produce a valid UTF-8 SSID
/// Returns Ok(None) if `nmcli` failed to produce a SSID.
fn get_nmcli_ssid(dev: &NetworkDevice) -> Result<Option<String>> {
    let raw = exec_ssid_cmd(
        "nmcli",
        &["-g", "general.connection", "device", "show", &dev.device],
    )?;

    if raw.is_none() {
        return Ok(None);
    }

    let raw = raw.unwrap();
    let result = raw.stdout.split(|c| *c == b'\n').next();

    maybe_ssid_convert(result)
}

#[inline]
/// Attempt to get the SSID the given device is connected to from iwctl.
/// Returns Err if
///     - `iwctl` is not a valid command
///     - failed to spawn a `iwctl` command
///     - `iwctl` failed to produce a valid UTF-8 SSID
/// Returns Ok(None) if `iwctl` failed to produce a SSID.
fn get_iwctl_ssid(dev: &NetworkDevice) -> Result<Option<String>> {
    let raw = exec_ssid_cmd("iwctl", &["station", &dev.device, "show"])?;

    if raw.is_none() {
        return Ok(None);
    }

    let raw = raw.unwrap();
    let result = raw
        .stdout
        .split(|c| *c == b'\n')
        .filter_map(|x| IWCTL_SSID_REGEX.captures_iter(x).next())
        .filter_map(|x| x.get(1))
        .next();

    maybe_ssid_convert(result.map(|x| x.as_bytes()))
}

#[inline]
fn exec_ssid_cmd<S, I, L>(cmd: S, args: I) -> Result<Option<std::process::Output>>
where
    S: AsRef<std::ffi::OsStr>,
    I: IntoIterator<Item = L>,
    L: AsRef<OsStr>,
{
    let raw = Command::new(&cmd).args(args).output();

    if let Err(ref err) = raw {
        if err.kind() == ErrorKind::NotFound {
            return Ok(None);
        }
    }

    raw.map(Some).block_error(
        "net",
        &format!(
            "Failed to execute SSID query using {}",
            cmd.as_ref().to_string_lossy()
        ),
    )
}

#[inline]
fn maybe_ssid_convert(raw: Option<&[u8]>) -> Result<Option<String>> {
    if let Some(raw_ssid) = raw {
        String::from_utf8(decode_escaped_unicode(raw_ssid))
            .block_error("net", "Non-UTF8 SSID")
            .map(Some)
    } else {
        Ok(None)
    }
}

fn decode_escaped_unicode(raw: &[u8]) -> Vec<u8> {
    let mut result: Vec<u8> = Vec::new();

    let mut idx = 0;
    while idx < raw.len() {
        if raw[idx] == b'\\' {
            idx += 2; // skip "\x"

            let hex = std::str::from_utf8(&raw[idx..idx + 2]).unwrap();
            result.extend(Some(u8::from_str_radix(hex, 16).unwrap()));
            idx += 2;
        } else {
            result.extend(Some(&raw[idx]));
            idx += 1;
        }
    }

    result
}

#[cfg(test)]
mod tests {
    use crate::blocks::net::maybe_ssid_convert;

    #[test]
    fn test_ssid_decode_escaped_unicode() {
        assert_eq!(
            maybe_ssid_convert(Some(r"\xc4\x85\xc5\xbeuolas".as_bytes())).unwrap(),
            Some("ąžuolas".to_string())
        );
    }

    #[test]
    fn test_ssid_decode_escaped_emoji() {
        assert_eq!(
            maybe_ssid_convert(Some(r"\xf0\x9f\x8c\xb3oak".as_bytes())).unwrap(),
            Some("🌳oak".to_string())
        );
    }

    #[test]
    fn test_ssid_decode_legit_backslash() {
        assert_eq!(
            maybe_ssid_convert(Some(r"\x5cx backslash".as_bytes())).unwrap(),
            Some(r"\x backslash".to_string())
        );
    }

    #[test]
    fn test_ssid_decode_surrounded_by_spaces() {
        assert_eq!(
            maybe_ssid_convert(Some(r"\x20surrounded by spaces\x20".as_bytes())).unwrap(),
            Some(r" surrounded by spaces ".to_string())
        );
    }
}