-
-
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.
- Loading branch information
Showing
1 changed file
with
39 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,48 @@ | ||
use anyhow::Result; | ||
use pnet::datalink::{self, NetworkInterface}; | ||
use std::net::IpAddr; | ||
|
||
use pnet::datalink; | ||
struct ActiveInterface { | ||
network_interface: NetworkInterface, | ||
ips: Vec<IpAddr>, | ||
} | ||
|
||
pub fn default() -> Result<String> { | ||
let all_interfaces = datalink::interfaces(); | ||
let default_interface = all_interfaces | ||
.iter() | ||
.find(|e| e.is_up() && !e.is_loopback() && !e.ips.is_empty()); | ||
|
||
println!("{:#?}", default_interface); | ||
let mut interface_with_ip: Vec<ActiveInterface> = Vec::new(); | ||
|
||
for interface in all_interfaces { | ||
if interface.is_up() && !interface.is_loopback() && !interface.ips.is_empty() { | ||
// Filter out loopback IPs and create IpNetwork instances | ||
let ips: Vec<IpAddr> = interface | ||
.ips | ||
.iter() | ||
.filter_map(|ip| match ip.ip() { | ||
IpAddr::V4(ipv4) if !ipv4.is_loopback() => Some(IpAddr::V4(ipv4)), | ||
IpAddr::V6(ipv6) if !ipv6.is_loopback() && ipv6.segments()[0] != 0xfe80 => { | ||
Some(IpAddr::V6(ipv6)) | ||
} | ||
_ => None, | ||
}) | ||
.collect(); | ||
|
||
if ips.is_empty() { | ||
continue; | ||
} | ||
|
||
let active_interface = ActiveInterface { | ||
network_interface: interface, | ||
ips, | ||
}; | ||
|
||
interface_with_ip.push(active_interface); | ||
} | ||
} | ||
|
||
for x in interface_with_ip { | ||
println!("{} - ips:{:#?} ", x.network_interface.name, x.ips); | ||
} | ||
|
||
Ok("".to_string()) | ||
} |