fix for unresolvable ip's

This commit is contained in:
Peter Dwyer
2022-07-05 16:27:52 +01:00
parent e4330960c8
commit 6ec5ca42f0

View File

@@ -1,6 +1,6 @@
use std::net::{SocketAddr, IpAddr}; use std::net::{SocketAddr, IpAddr};
use std::net::TcpStream; use std::net::TcpStream;
use std::{thread, time}; use std::{process, thread};
use std::fmt::{Debug}; use std::fmt::{Debug};
use std::time::Duration; use std::time::Duration;
use chrono::Local; use chrono::Local;
@@ -44,9 +44,13 @@ struct Args {
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
let target_result = get_target_ip(&args.host, &args.v4, &args.v6); let target_result = get_target_ip(&args.host, &args.v4, &args.v6);
if target_result.is_err() {
println!("Error: {}", target_result.unwrap_err());
process::exit(0);
}
let target_ip: IpAddr = target_result.unwrap(); let target_ip: IpAddr = target_result.unwrap();
let delay = chrono::Duration::milliseconds(1000); let delay = chrono::Duration::milliseconds(1000);
let timeout = time::Duration::from_millis(args.timeout.into()); let timeout = Duration::from_millis(args.timeout.into());
if args.repeat { if args.repeat {
loop { loop {
let start = Local::now(); let start = Local::now();
@@ -54,7 +58,7 @@ fn main() {
let end = Local::now(); let end = Local::now();
let delta = end - start; let delta = end - start;
if delta < delay { if delta < delay {
thread::sleep((delay-delta).to_std().unwrap()); thread::sleep((delay - delta).to_std().unwrap());
} }
} }
} else { } else {
@@ -74,17 +78,15 @@ fn get_target_ip(s: &str, v4: &bool, v6: &bool) -> Result<IpAddr, String> {
} else if *v6 && ip.is_ipv6() { } else if *v6 && ip.is_ipv6() {
return Ok(ip as IpAddr); return Ok(ip as IpAddr);
} }
} }
Err(format!( Err(format!(
"No resolved IPs", "Failed to Lookup IP",
)) ))
} else { } else {
Err(format!( Err(format!(
"Target wrong", "Unable to resolve Address",
)) ))
} }
} }
fn ping(host: &IpAddr, port: &u16, timeout: &Duration) -> Result<(), ()> { fn ping(host: &IpAddr, port: &u16, timeout: &Duration) -> Result<(), ()> {
@@ -96,5 +98,4 @@ fn ping(host: &IpAddr, port: &u16, timeout: &Duration) -> Result<(), ()> {
println!("{} - {} - Connection Failed", Local::now().format("%Y-%m-%d %H:%M:%S"), host); println!("{} - {} - Connection Failed", Local::now().format("%Y-%m-%d %H:%M:%S"), host);
Err(()) Err(())
} }
} }