extern crate pnet;
use pnet::datalink;
use pnet::ipnetwork;
use pnet::datalink::NetworkInterface;
use std::net::Ipv4Addr;
use std::net::Ipv6Addr;
use dns_lookup::lookup_host;
pub fn get_ac_net_card_info() -> Vec<NetworkInterface>{
let interfaces = datalink::interfaces();
return interfaces;
}
pub fn get_ac_net_card_name() -> String{
let mut r = "0".to_string();
let interfaces = datalink::interfaces();
for interface in interfaces {
let ip_v4:Vec<Ipv4Addr> = interface.ips.iter().map(|ip| match ip {
ipnetwork::IpNetwork::V4(ref ipv4) => Ok(ipv4.ip()),
_ => Err(""),
}).filter_map(Result::ok).collect();
#[cfg(unix)]
if !ip_v4.is_empty() && !interface.is_loopback() && interface.is_running() && interface.is_up() {
r= interface.name;
}
}
return r.to_string();
}
pub fn get_ipv4() -> String{
let mut r = "0".to_string();
let interfaces = datalink::interfaces();
for interface in interfaces {
let ip_v4:Vec<Ipv4Addr> = interface.ips.iter().map(|ip| match ip {
ipnetwork::IpNetwork::V4(ref ipv4) => Ok(ipv4.ip()),
_ => Err(""),
}).filter_map(Result::ok).collect();
#[cfg(unix)]
if !ip_v4.is_empty() && !interface.is_loopback() && interface.is_running() && interface.is_up() {
r= ip_v4[0].to_string();
}
}
return r.to_string();
}
pub fn get_ipv6() -> String{
let mut r = "0".to_string();
let interfaces = datalink::interfaces();
for interface in interfaces {
let ip_v6:Vec<Ipv6Addr> = interface.ips.iter().map(|ip| match ip {
ipnetwork::IpNetwork::V6(ref ipv6) => Ok(ipv6.ip()),
_ => Err(""),
}).filter_map(Result::ok).collect();
#[cfg(unix)]
if !ip_v6.is_empty() && !interface.is_loopback() && interface.is_running() && interface.is_up() {
r= ip_v6[0].to_string();
}
}
return r.to_string();
}
pub fn get_ac_mac() -> String{
let mut r = "0".to_string();
let interfaces = datalink::interfaces();
for interface in interfaces {
let ip_v4:Vec<Ipv4Addr> = interface.ips.iter().map(|ip| match ip {
ipnetwork::IpNetwork::V4(ref ipv4) => Ok(ipv4.ip()),
_ => Err(""),
}).filter_map(Result::ok).collect();
#[cfg(unix)]
if !ip_v4.is_empty() && !interface.is_loopback() && interface.is_running() && interface.is_up() {
r= interface.mac.expect("REASON").to_string();
}
}
return r.to_string();
}
pub fn get_ww_ipv4(uri:&str) -> String {
let response = reqwest::blocking::get(uri).expect("Failed to send request");
if let Ok(body) = response.text() {
return body;
} else {
eprintln!("Failed to read the response body.");
return "".to_string();
}
}
pub fn get_domain_ip(hostname:&str) ->Vec<std::net::IpAddr> {
let ips: Vec<std::net::IpAddr> = lookup_host(hostname).unwrap();
return ips;
}