Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show more information when domain name is short #720

Merged
merged 4 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 42 additions & 10 deletions src/utils/formatted_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 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}"),
}
}
} else {
r_dns
}
Expand Down Expand Up @@ -232,4 +235,33 @@ 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("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("..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");
}
}