From 6b13d77fc228edf8d9948a7c99698d124b547efd Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Sun, 16 Feb 2025 16:39:16 +0100 Subject: [PATCH 1/4] modify get_domain_from_r_dns to fix #696 --- src/utils/formatted_strings.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/utils/formatted_strings.rs b/src/utils/formatted_strings.rs index dde3c686f..3f83f1d8e 100644 --- a/src/utils/formatted_strings.rs +++ b/src/utils/formatted_strings.rs @@ -107,16 +107,19 @@ pub fn get_domain_from_r_dns(r_dns: String) -> String { r_dns } else { let parts: Vec<&str> = r_dns.split('.').collect(); - if parts.len() >= 2 { - parts - .get(parts.len() - 2..) - .unwrap_or(&parts) - .iter() - .fold(Vec::new(), |mut vec, part| { - vec.push((*part).to_string()); - vec - }) - .join(".") + let len = parts.len(); + if len >= 2 { + let last = parts.get(len - 1).unwrap_or(&""); + let second_last = parts.get(len - 2).unwrap_or(&""); + if last.len() > 3 || second_last.len() > 3 { + format!("{second_last}.{last}") + } else { + let third_last_opt = parts.get(len - 3); + match third_last_opt { + Some(third_last) => format!("{third_last}.{second_last}.{last}"), + None => format!("{second_last}.{last}"), + } + } } else { r_dns } From 14e77da6384b4a13574a3bc9d8c50503ad80be2c Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Sun, 16 Feb 2025 17:02:34 +0100 Subject: [PATCH 2/4] add test_get_domain_from_r_dns --- src/utils/formatted_strings.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/utils/formatted_strings.rs b/src/utils/formatted_strings.rs index 3f83f1d8e..ea5f7c20f 100644 --- a/src/utils/formatted_strings.rs +++ b/src/utils/formatted_strings.rs @@ -114,7 +114,7 @@ pub fn get_domain_from_r_dns(r_dns: String) -> String { if last.len() > 3 || second_last.len() > 3 { format!("{second_last}.{last}") } else { - let third_last_opt = parts.get(len - 3); + let third_last_opt = len.checked_sub(3).and_then(|i| parts.get(i)); match third_last_opt { Some(third_last) => format!("{third_last}.{second_last}.{last}"), None => format!("{second_last}.{last}"), @@ -235,4 +235,22 @@ mod tests { assert!(file_path.is_absolute()); assert_eq!(file_path.file_name().unwrap(), "logs.txt"); } + + #[test] + fn test_get_domain_from_r_dns() { + let f = |s: &str| get_domain_from_r_dns(s.to_string()); + assert_eq!(f(""), ""); + assert_eq!(f("8.8.8.8"), "8.8.8.8"); + assert_eq!(f("a.b.c.d"), "b.c.d"); + assert_eq!(f("ciao.xyz"), "ciao.xyz"); + assert_eq!(f("bye.ciao.xyz"), "ciao.xyz"); + assert_eq!(f("ciao.bye.xyz"), "ciao.bye.xyz"); + assert_eq!(f("hola.ciao.bye.xyz"), "ciao.bye.xyz"); + assert_eq!(f(".bye.xyz"), ".bye.xyz"); + assert_eq!(f("bye.xyz"), "bye.xyz"); + assert_eq!(f("ciao.."), "ciao.."); + assert_eq!(f("ciao..."), ".."); + assert_eq!(f("ciao..xyz"), "ciao..xyz"); + assert_eq!(f("no_dots_in_this"), "no_dots_in_this"); + } } From daf3d4ac22e0a5460d117a5ddcd74f324df623fe Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Sun, 16 Feb 2025 17:10:29 +0100 Subject: [PATCH 3/4] update CHANGELOG --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6900c37d..9d7f71873 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,9 +8,10 @@ All Sniffnet releases with the relative changes are documented in this file. - Updated some of the existing translations to v1.3: - Portuguese ([#690](https://github.com/GyulyVGC/sniffnet/pull/690)) - Ukrainian ([#692](https://github.com/GyulyVGC/sniffnet/pull/692)) +- Show more information when domain name is short ([#720](https://github.com/GyulyVGC/sniffnet/pull/720) — fixes [#696](https://github.com/GyulyVGC/sniffnet/issues/696)) - Added new themes _OLED (Night)_ and _OLED (Day)_ based on palettes optimized for OLED displays and users with visual impairments ([#708](https://github.com/GyulyVGC/sniffnet/pull/708)) -- Fix _crates.io_ package for Windows ([#718](https://github.com/GyulyVGC/sniffnet/pull/718) — fixes [#681](https://github.com/GyulyVGC/sniffnet/issues/681)) - Add icon to window title bar ([#719](https://github.com/GyulyVGC/sniffnet/pull/719) — fixes [#715](https://github.com/GyulyVGC/sniffnet/issues/715)) +- Fix _crates.io_ package for Windows ([#718](https://github.com/GyulyVGC/sniffnet/pull/718) — fixes [#681](https://github.com/GyulyVGC/sniffnet/issues/681)) - Remove pre-uninstall script on Linux (fixes [#644](https://github.com/GyulyVGC/sniffnet/issues/644)) ## [1.3.2] - 2025-01-06 From 5ea8858c301e23589b178378c7668ad042683f2e Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Sun, 16 Feb 2025 17:22:03 +0100 Subject: [PATCH 4/4] modify test_get_domain_from_r_dns --- src/utils/formatted_strings.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/utils/formatted_strings.rs b/src/utils/formatted_strings.rs index ea5f7c20f..88c6bf932 100644 --- a/src/utils/formatted_strings.rs +++ b/src/utils/formatted_strings.rs @@ -248,9 +248,20 @@ mod tests { assert_eq!(f("hola.ciao.bye.xyz"), "ciao.bye.xyz"); assert_eq!(f(".bye.xyz"), ".bye.xyz"); assert_eq!(f("bye.xyz"), "bye.xyz"); + assert_eq!(f("hola.ciao.b"), "ciao.b"); + assert_eq!(f("hola.b.ciao"), "b.ciao"); + assert_eq!(f("ciao."), "ciao."); assert_eq!(f("ciao.."), "ciao.."); + assert_eq!(f(".ciao."), "ciao."); + assert_eq!(f("ciao.bye."), "ciao.bye."); assert_eq!(f("ciao..."), ".."); - assert_eq!(f("ciao..xyz"), "ciao..xyz"); + assert_eq!(f("..bye"), "..bye"); + assert_eq!(f("ciao..bye"), "ciao..bye"); + assert_eq!(f("..ciao"), ".ciao"); + assert_eq!(f("bye..ciao"), ".ciao"); + assert_eq!(f("."), "."); + assert_eq!(f(".."), ".."); + assert_eq!(f("..."), ".."); assert_eq!(f("no_dots_in_this"), "no_dots_in_this"); } }