-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sync Tue Dec 12 10:26:30 AM CET 2023
- Loading branch information
Showing
2 changed files
with
24 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |