Add error checking

This commit is contained in:
0xf8 2023-04-16 13:26:33 -04:00
parent 7d91220e99
commit af2561bc30
Signed by: 0xf8
GPG Key ID: 446580D758689584

View File

@ -30,8 +30,8 @@ async fn main() {
// Length check
if targets.len() == 0 {
eprintln!("Fatal: No targets given.");
std::process::exit(1);
eprintln!("{}: No targets given, will default to your IP", "Warning".yellow());
targets.push("".to_string());
}
let client = reqwest::Client::new();
@ -39,15 +39,42 @@ async fn main() {
for (i, target) in targets.into_iter().enumerate() {
if i > 0 { print!("\n") };
let req = client
.get(format!("http://ip-api.com/json/{target}?fields=18601977"))
let req = match client
.get(format!("http://ip-api.com/json/{target}?fields=18610169"))
.send()
.await
.unwrap();
.await {
Ok(r) => r,
Err(e) => {
eprintln!("{}: {e:?}", "Error".red());
continue;
}
};
let body = req.text().await.unwrap();
let data = json::parse(&body.to_owned()).unwrap();
let data = match json::parse(&body.to_owned()) {
Ok(j) => j,
Err(e) => {
eprintln!("{}: {e:?}", "Fatal".red());
continue;
}
};
if data["status"] == "fail" {
let message = data["message"].as_str().unwrap();
let msg = match message {
"reserved range" => "Queried reserved IP",
"private range" => "Queried private IP",
"invalid query" => "Invalid IP",
"SSL unavailable for this endpoint, order a key at https://members.ip-api.com/" => "Unable to use HTTPS endpoint",
_ => { message }
};
eprintln!("{}: {msg}", "Error".red());
continue;
}
let target = if target.is_empty() { data["query"].as_str().unwrap().to_string() } else { target };
println!("Target {}", target.bright_white().bold());
if !data["continent"].is_empty() { println!("Continent {}", data["continent"].as_str().unwrap().yellow()) }
if !data["country"].is_empty() { println!("Country {}", data["country"].as_str().unwrap().yellow()) }