Skip to content

Commit

Permalink
sync Tue Dec 12 10:26:30 AM CET 2023
Browse files Browse the repository at this point in the history
  • Loading branch information
nbari committed Dec 12, 2023
1 parent 391f5af commit bf0578d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/killswitch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::net::IpAddr;
pub fn default() -> Result<String> {
if let Ok(interface) = default_net::get_default_interface() {
println!("Default network interface");
println!("");
println!(" Name: {}", interface.name);

let ipv4 = interface.ipv4.iter().map(|ip| ip).collect::<Vec<_>>();
Expand Down
31 changes: 24 additions & 7 deletions src/killswitch/whoami.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
use anyhow::{anyhow, Result};

const MYIP_URLS: [&str; 3] = [
"https://myip.country/ip",
"http://trackip.net/ip",
"https://checkip.amazonaws.com",
];

pub fn whoami() -> Result<String> {
let client = reqwest::blocking::Client::builder()
.user_agent("killswitch")
.build()?;

let resp = client.get("https://myip.country/ip").send()?;

if resp.status().is_success() {
let body = resp.text()?;
Ok(body)
} else {
Err(anyhow!("Failed to get public IP: {}", resp.status()))
for url in &MYIP_URLS {
match client.get(*url).send() {
Ok(resp) if resp.status().is_success() => {
let body = resp.text()?;
return Ok(body);
}
Ok(_) => {
// Continue to the next URL if the response is not successful
continue;
}
Err(err) => {
// Log the error or handle it as needed
eprintln!("Error fetching IP from {}: {}", url, err);
continue;
}
}
}

Err(anyhow!("Failed to get public IP from all URLs"))
}

0 comments on commit bf0578d

Please sign in to comment.