Skip to content

Commit

Permalink
sync Mon Dec 11 23:47:12 CET 2023
Browse files Browse the repository at this point in the history
  • Loading branch information
nbari committed Dec 11, 2023
1 parent 5dc9640 commit 6ac8e7c
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions src/killswitch/mod.rs
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())
}

0 comments on commit 6ac8e7c

Please sign in to comment.