From b3a23282956623396a4384f8a175ba553da64db9 Mon Sep 17 00:00:00 2001 From: jkauerl Date: Tue, 14 May 2024 07:07:05 -0400 Subject: [PATCH 1/8] doc: added documentation of nameserver --- src/blocks/net.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/blocks/net.rs b/src/blocks/net.rs index c9fb1de6ab..d4cd4b6b51 100644 --- a/src/blocks/net.rs +++ b/src/blocks/net.rs @@ -31,6 +31,7 @@ //! `bitrate` | WiFi connection bitrate | Number | Bits per second //! `ip` | IPv4 address of the iface | Text | - //! `ipv6` | IPv6 address of the iface | Text | - +//! `nameserver` | Nameserver | Text | - //! //! # Example //! From 7663fa5a632cf1c8d70548f57f969cc7e258d1f1 Mon Sep 17 00:00:00 2001 From: jkauerl Date: Mon, 27 May 2024 23:48:26 +0200 Subject: [PATCH 2/8] Feat: added nameserver to device struct --- src/netlink.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/netlink.rs b/src/netlink.rs index 05abc83eb1..65cf16c0c9 100644 --- a/src/netlink.rs +++ b/src/netlink.rs @@ -9,7 +9,7 @@ use regex::Regex; use libc::c_uchar; -use std::net::{Ipv4Addr, Ipv6Addr}; +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use std::ops; use std::path::Path; @@ -27,6 +27,7 @@ pub struct NetDevice { pub ipv6: Option, pub icon: &'static str, pub tun_wg_ppp: bool, + pub nameservers: Vec, } #[derive(Debug, Default)] From 09da2bc18c6664797fc7ae74323ed4a348a1aaf0 Mon Sep 17 00:00:00 2001 From: jkauerl Date: Tue, 28 May 2024 00:01:24 +0200 Subject: [PATCH 3/8] Feat: implementation for reading all the nameservers --- src/netlink.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/netlink.rs b/src/netlink.rs index 65cf16c0c9..d96adae1d2 100644 --- a/src/netlink.rs +++ b/src/netlink.rs @@ -429,6 +429,21 @@ async fn ipv6(sock: &mut NlSocket, ifa_index: i32) -> Result> { .map(Ipv6Addr::from)) } +async fn read_nameservers() -> Result> { + let file = util::read_file("/etc/resolv.conf").await?; + let mut nameservers = Vec::new(); + + for line in file.lines() { + if let Some(ip) = line.strip_prefix("nameserver ") { + if let Ok(ip) = ip.parse() { + nameservers.push(ip); + } + } + } + + Ok(nameservers) +} + // Source: https://www.kernel.org/doc/Documentation/networking/operstates.txt #[derive(Debug, PartialEq, Eq)] pub enum Operstate { From 468cc51ac3e9149f7448aa79fa5c1efca656215c Mon Sep 17 00:00:00 2001 From: jkauerl Date: Tue, 28 May 2024 11:40:28 +0200 Subject: [PATCH 4/8] Feat: finished implementation of reading nameservers --- src/blocks/net.rs | 1 + src/netlink.rs | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/blocks/net.rs b/src/blocks/net.rs index d4cd4b6b51..d7dea95ab7 100644 --- a/src/blocks/net.rs +++ b/src/blocks/net.rs @@ -160,6 +160,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> { [if let Some(v) = device.frequency()] "frequency" => Value::hertz(v), [if let Some(v) = device.bitrate()] "bitrate" => Value::bits(v), [if let Some(v) = device.signal()] "signal_strength" => Value::percents(v), + [if let Some(v) = device.nameserver] "nameserver" => Value::text(v.to_string()), "device" => Value::text(device.iface.name), }); diff --git a/src/netlink.rs b/src/netlink.rs index d96adae1d2..3ea66cce02 100644 --- a/src/netlink.rs +++ b/src/netlink.rs @@ -27,7 +27,7 @@ pub struct NetDevice { pub ipv6: Option, pub icon: &'static str, pub tun_wg_ppp: bool, - pub nameservers: Vec, + pub nameserver: Option, } #[derive(Debug, Default)] @@ -68,6 +68,10 @@ impl NetDevice { let wifi_info = WifiInfo::new(iface.index).await?; let ip = ipv4(&mut sock, iface.index).await?; let ipv6 = ipv6(&mut sock, iface.index).await?; + let nameserver = read_nameservers() + .await + .map(|ns| ns.get(0).map(|ip| ip.to_string())) + .error("Failed to read nameservers")?; // TODO: use netlink for the these too // I don't believe that this should ever change, so set it now: @@ -98,6 +102,7 @@ impl NetDevice { ipv6, icon, tun_wg_ppp: tun | wg | ppp, + nameserver, })) } @@ -430,7 +435,9 @@ async fn ipv6(sock: &mut NlSocket, ifa_index: i32) -> Result> { } async fn read_nameservers() -> Result> { - let file = util::read_file("/etc/resolv.conf").await?; + let file = util::read_file("/etc/resolv.conf") + .await + .error("Failed to read /etc/resolv.conf")?; let mut nameservers = Vec::new(); for line in file.lines() { From 45696dddb1c88e8abcc41d58d167f154b8ef6219 Mon Sep 17 00:00:00 2001 From: jkauerl Date: Sun, 2 Jun 2024 23:23:02 +0200 Subject: [PATCH 5/8] Feat: added trim method to remove trailling whitespaces and tabs --- src/netlink.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/netlink.rs b/src/netlink.rs index 3ea66cce02..ca80892ffc 100644 --- a/src/netlink.rs +++ b/src/netlink.rs @@ -442,6 +442,7 @@ async fn read_nameservers() -> Result> { for line in file.lines() { if let Some(ip) = line.strip_prefix("nameserver ") { + let ip = ip.trim(); if let Ok(ip) = ip.parse() { nameservers.push(ip); } From 710f55852f30bad61450c9e8f0b6eaa3722394f3 Mon Sep 17 00:00:00 2001 From: jkauerl Date: Tue, 11 Jun 2024 13:37:00 +0200 Subject: [PATCH 6/8] Feat: added resolv to cspell checker --- cspell.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cspell.yaml b/cspell.yaml index 4aa7648a1a..dda275469c 100644 --- a/cspell.yaml +++ b/cspell.yaml @@ -1,4 +1,4 @@ -version: '0.2' +version: "0.2" language: en-US, en-GB allowCompoundWords: true ignorePaths: @@ -124,6 +124,7 @@ words: - recip - repr - reqwest + - resolv - retval - rofi - rofication From 8f3a6d4dbde4a92fd46a76dc200cc29490968f86 Mon Sep 17 00:00:00 2001 From: jkauerl Date: Tue, 11 Jun 2024 13:50:07 +0200 Subject: [PATCH 7/8] Feat: added suggestion to only remove whitespaces --- src/netlink.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/netlink.rs b/src/netlink.rs index ca80892ffc..878daee8ec 100644 --- a/src/netlink.rs +++ b/src/netlink.rs @@ -70,7 +70,7 @@ impl NetDevice { let ipv6 = ipv6(&mut sock, iface.index).await?; let nameserver = read_nameservers() .await - .map(|ns| ns.get(0).map(|ip| ip.to_string())) + .map(|ns| ns.first().map(|ip| ip.to_string())) .error("Failed to read nameservers")?; // TODO: use netlink for the these too @@ -441,10 +441,10 @@ async fn read_nameservers() -> Result> { let mut nameservers = Vec::new(); for line in file.lines() { - if let Some(ip) = line.strip_prefix("nameserver ") { - let ip = ip.trim(); - if let Ok(ip) = ip.parse() { - nameservers.push(ip); + let mut line_parts = line.split_whitespace(); + if line_parts.next() == Some("nameserver") { + if let Some(ip) = line_parts.next() { + nameservers.push(ip.parse().error("Unable to parse ip")?); } } } From d0f5de44524f46af8c017473fabe2cde3b3bbcfa Mon Sep 17 00:00:00 2001 From: jkauerl Date: Tue, 11 Jun 2024 13:54:49 +0200 Subject: [PATCH 8/8] Feat: now stores a vector of ip address instead of only one --- src/blocks/net.rs | 9 ++++++++- src/netlink.rs | 7 +++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/blocks/net.rs b/src/blocks/net.rs index d7dea95ab7..3b41882b88 100644 --- a/src/blocks/net.rs +++ b/src/blocks/net.rs @@ -62,6 +62,7 @@ use super::prelude::*; use crate::netlink::NetDevice; use crate::util; +use itertools::Itertools; use regex::Regex; use std::time::Instant; @@ -160,7 +161,13 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> { [if let Some(v) = device.frequency()] "frequency" => Value::hertz(v), [if let Some(v) = device.bitrate()] "bitrate" => Value::bits(v), [if let Some(v) = device.signal()] "signal_strength" => Value::percents(v), - [if let Some(v) = device.nameserver] "nameserver" => Value::text(v.to_string()), + [if !device.nameservers.is_empty()] "nameserver" => Value::text( + device + .nameservers + .into_iter() + .map(|s| s.to_string()) + .join(" "), + ), "device" => Value::text(device.iface.name), }); diff --git a/src/netlink.rs b/src/netlink.rs index 878daee8ec..b8557dbff6 100644 --- a/src/netlink.rs +++ b/src/netlink.rs @@ -27,7 +27,7 @@ pub struct NetDevice { pub ipv6: Option, pub icon: &'static str, pub tun_wg_ppp: bool, - pub nameserver: Option, + pub nameservers: Vec, } #[derive(Debug, Default)] @@ -68,9 +68,8 @@ impl NetDevice { let wifi_info = WifiInfo::new(iface.index).await?; let ip = ipv4(&mut sock, iface.index).await?; let ipv6 = ipv6(&mut sock, iface.index).await?; - let nameserver = read_nameservers() + let nameservers = read_nameservers() .await - .map(|ns| ns.first().map(|ip| ip.to_string())) .error("Failed to read nameservers")?; // TODO: use netlink for the these too @@ -102,7 +101,7 @@ impl NetDevice { ipv6, icon, tun_wg_ppp: tun | wg | ppp, - nameserver, + nameservers, })) }