Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Chleba authored Oct 21, 2024
2 parents f76a87b + 596753c commit 1ecc0b6
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 133 deletions.
158 changes: 79 additions & 79 deletions src/components/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use pnet::packet::{
use pnet::util::MacAddr;

use core::str;
use ratatui::{prelude::*, widgets::*};
use ratatui::layout::Position;
use ratatui::{prelude::*, widgets::*};
use std::net::{IpAddr, Ipv4Addr};
use std::string;
use std::time::{Duration, Instant};
Expand All @@ -35,12 +35,12 @@ use crate::{
tui::Frame,
utils::{count_ipv4_net_length, get_ips4_from_cidr},
};
use crossterm::event::Event;
use crossterm::event::{KeyCode, KeyEvent};
use mac_oui::Oui;
use rand::random;
use tui_input::backend::crossterm::EventHandler;
use tui_input::Input;
use crossterm::event::Event;

static POOL_SIZE: usize = 32;
static INPUT_SIZE: usize = 30;
Expand Down Expand Up @@ -102,19 +102,20 @@ impl Discovery {

pub fn get_scanned_ips(&self) -> &Vec<ScannedIp> {
&self.scanned_ips
}
}

fn set_cidr(&mut self, cidr_str: String, scan: bool) {
match &cidr_str.parse::<Ipv4Cidr>() {
match cidr_str.parse::<Ipv4Cidr>() {
Ok(ip_cidr) => {
self.cidr = Some(*ip_cidr);
self.cidr = Some(ip_cidr);
if scan {
self.scan();
}
}
Err(e) => {
let tx = self.action_tx.clone().unwrap();
tx.send(Action::CidrError).unwrap();
if let Some(tx) = &self.action_tx {
tx.send(Action::CidrError).unwrap();
}
}
}
}
Expand All @@ -125,63 +126,63 @@ impl Discovery {
}

fn send_arp(&mut self, target_ip: Ipv4Addr) {
let active_interface = match self.active_interface.clone() {
Some(intf) => intf,
None => return, // Or send an error Action
};

if let Some(active_interface) = &self.active_interface {
if let Some(active_interface_mac) = active_interface.mac {
let ipv4 = active_interface.ips.iter().find(|f| f.is_ipv4()).unwrap();
let source_ip: Ipv4Addr = ipv4.ip().to_string().parse().unwrap();

let (mut sender, _) =
match pnet::datalink::channel(&active_interface, Default::default()) {
Ok(Channel::Ethernet(tx, rx)) => (tx, rx),
Ok(_) => {
let tx_action = self.action_tx.clone().unwrap();
tx_action
.send(Action::Error("Unknown or unsopported channel type".into()))
.unwrap();
return;
}
Err(e) => {
let tx_action = self.action_tx.clone().unwrap();
tx_action
.send(Action::Error(format!(
"Unable to create datalink channel: {e}"
)))
.unwrap();
return;
}
};

let mut ethernet_buffer = [0u8; 42];
let mut ethernet_packet = MutableEthernetPacket::new(&mut ethernet_buffer).unwrap();

ethernet_packet.set_destination(MacAddr::broadcast());
// ethernet_packet.set_source(active_interface.mac.unwrap());
ethernet_packet.set_source(active_interface_mac);
ethernet_packet.set_ethertype(EtherTypes::Arp);

let mut arp_buffer = [0u8; 28];
let mut arp_packet = MutableArpPacket::new(&mut arp_buffer).unwrap();

arp_packet.set_hardware_type(ArpHardwareTypes::Ethernet);
arp_packet.set_protocol_type(EtherTypes::Ipv4);
arp_packet.set_hw_addr_len(6);
arp_packet.set_proto_addr_len(4);
arp_packet.set_operation(ArpOperations::Request);
arp_packet.set_sender_hw_addr(active_interface_mac);
arp_packet.set_sender_proto_addr(source_ip);
arp_packet.set_target_hw_addr(MacAddr::zero());
arp_packet.set_target_proto_addr(target_ip);

ethernet_packet.set_payload(arp_packet.packet_mut());

sender
.send_to(ethernet_packet.packet(), None)
.unwrap()
.unwrap();
let ipv4 = active_interface.ips.iter().find(|f| f.is_ipv4()).unwrap();
let source_ip: Ipv4Addr = ipv4.ip().to_string().parse().unwrap();

let (mut sender, _) =
match pnet::datalink::channel(active_interface, Default::default()) {
Ok(Channel::Ethernet(tx, rx)) => (tx, rx),
Ok(_) => {
if let Some(tx_action) = &self.action_tx {
tx_action
.send(Action::Error(
"Unknown or unsupported channel type".into(),
))
.unwrap();
}
return;
}
Err(e) => {
if let Some(tx_action) = &self.action_tx {
tx_action
.send(Action::Error(format!(
"Unable to create datalink channel: {e}"
)))
.unwrap();
}
return;
}
};

let mut ethernet_buffer = [0u8; 42];
let mut ethernet_packet = MutableEthernetPacket::new(&mut ethernet_buffer).unwrap();

ethernet_packet.set_destination(MacAddr::broadcast());
ethernet_packet.set_source(active_interface_mac);
ethernet_packet.set_ethertype(EtherTypes::Arp);

let mut arp_buffer = [0u8; 28];
let mut arp_packet = MutableArpPacket::new(&mut arp_buffer).unwrap();

arp_packet.set_hardware_type(ArpHardwareTypes::Ethernet);
arp_packet.set_protocol_type(EtherTypes::Ipv4);
arp_packet.set_hw_addr_len(6);
arp_packet.set_proto_addr_len(4);
arp_packet.set_operation(ArpOperations::Request);
arp_packet.set_sender_hw_addr(active_interface_mac);
arp_packet.set_sender_proto_addr(source_ip);
arp_packet.set_target_hw_addr(MacAddr::zero());
arp_packet.set_target_proto_addr(target_ip);

ethernet_packet.set_payload(arp_packet.packet_mut());

sender
.send_to(ethernet_packet.packet(), None)
.unwrap()
.unwrap();
}
}
}

Expand All @@ -190,10 +191,9 @@ impl Discovery {

if let Some(cidr) = self.cidr {
self.is_scanning = true;
let tx = self.action_tx.clone().unwrap();
let tx = self.action_tx.as_ref().unwrap().clone();
self.task = tokio::spawn(async move {
let ips = get_ips4_from_cidr(cidr);
let tx = tx.clone();
let chunks: Vec<_> = ips.chunks(POOL_SIZE).collect();
for chunk in chunks {
let tasks: Vec<_> = chunk
Expand Down Expand Up @@ -254,23 +254,23 @@ impl Discovery {
}

fn process_ip(&mut self, ip: &str) {
let tx = self.action_tx.clone().unwrap();
let tx = self.action_tx.as_ref().unwrap();
let ipv4: Ipv4Addr = ip.parse().unwrap();
self.send_arp(ipv4);

if let Some(n) = self.scanned_ips.iter_mut().find(|item| item.ip == ip) {
let hip: IpAddr = ip.parse().unwrap();
let host = lookup_addr(&hip).unwrap_or(String::from(""));
let host = lookup_addr(&hip).unwrap_or_default();
n.hostname = host;
n.ip = ip.to_string();
} else {
let hip: IpAddr = ip.parse().unwrap();
let host = lookup_addr(&hip).unwrap_or(String::from(""));
let host = lookup_addr(&hip).unwrap_or_default();
self.scanned_ips.push(ScannedIp {
ip: ip.to_string(),
mac: String::from(""),
mac: String::new(),
hostname: host,
vendor: String::from(""),
vendor: String::new(),
});

self.scanned_ips.sort_by(|a, b| {
Expand Down Expand Up @@ -340,11 +340,11 @@ impl Discovery {
self.scrollbar_state = self.scrollbar_state.position(index);
}

fn make_table(
scanned_ips: Vec<ScannedIp>,
fn make_table<'a>(
scanned_ips: &'a Vec<ScannedIp>,
cidr: Option<Ipv4Cidr>,
ip_num: i32,
) -> Table<'static> {
) -> Table<'a> {
let header = Row::new(vec!["ip", "mac", "hostname", "vendor"])
.style(Style::default().fg(Color::Yellow))
.top_margin(1)
Expand All @@ -362,9 +362,9 @@ impl Discovery {
format!("{ip:<2}"),
Style::default().fg(Color::Blue),
)),
Cell::from(sip.mac.clone().green()),
Cell::from(sip.hostname.clone()),
Cell::from(sip.vendor.clone().yellow()),
Cell::from(sip.mac.as_str().green()),
Cell::from(sip.hostname.as_str()),
Cell::from(sip.vendor.as_str().yellow()),
]));
}

Expand Down Expand Up @@ -661,8 +661,8 @@ impl Component for Discovery {
table_rect.y += 1;
table_rect.height -= 1;

let table = Self::make_table(self.scanned_ips.clone(), self.cidr, self.ip_num);
f.render_stateful_widget(table, table_rect, &mut self.table_state.clone());
let table = Self::make_table(&self.scanned_ips, self.cidr, self.ip_num);
f.render_stateful_widget(table, table_rect, &mut self.table_state);

// -- SCROLLBAR
let scrollbar = Self::make_scrollbar();
Expand Down Expand Up @@ -698,7 +698,7 @@ impl Component for Discovery {
let scroll = self.input.visual_scroll(INPUT_SIZE - 3);
let mut block = self.make_input(scroll);
if self.is_scanning {
block = block.clone().add_modifier(Modifier::DIM);
block = block.add_modifier(Modifier::DIM);
}
f.render_widget(block, input_rect);

Expand Down
13 changes: 4 additions & 9 deletions src/components/packetdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ impl PacketDump {
let (_, mut receiver) = match pnet::datalink::channel(&interface, Default::default()) {
Ok(Channel::Ethernet(tx, rx)) => (tx, rx),
Ok(_) => {
tx.send(Action::Error("Unknown or unsopported channel type".into()))
tx.send(Action::Error("Unknown or unsupported channel type".into()))
.unwrap();
return;
}
Expand All @@ -424,10 +424,9 @@ impl PacketDump {
)))
.unwrap();
return;
} // Ok(_) => panic!("Unknown channel type"),
// Err(e) => panic!("Error happened {}", e),
}
};
// while !paused.load(Ordering::Relaxed) {

loop {
let mut buf: [u8; 1600] = [0u8; 1600];
let mut fake_ethernet_frame = MutableEthernetPacket::new(&mut buf[..]).unwrap();
Expand Down Expand Up @@ -850,11 +849,7 @@ impl PacketDump {
rows
}

fn make_table(
rows: Vec<Row>,
packet_type: PacketTypeEnum,
dump_paused: bool,
) -> Table {
fn make_table(rows: Vec<Row>, packet_type: PacketTypeEnum, dump_paused: bool) -> Table {
let header = Row::new(vec!["time", "packet log"])
.style(Style::default().fg(Color::Yellow))
.top_margin(1)
Expand Down
34 changes: 18 additions & 16 deletions src/components/sniff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,13 @@ impl Sniffer {
},
);

let a_intfs = self.active_inft_ips.clone();
let a_intfs = &self.active_inft_ips;
let tu = self
.traffic_ips
.iter()
.filter(|item| {
let t_ip = item.ip.to_string();
for i_ip in a_intfs.clone() {
for i_ip in a_intfs {
if i_ip.ip().to_string() == t_ip {
return false;
}
Expand All @@ -265,9 +265,9 @@ impl Sniffer {

let mut tu_ip = String::from("");
let mut tu_name = String::from("");
if tu.is_some() {
tu_ip = tu.unwrap().ip.to_string();
tu_name = format!(" ({})", tu.unwrap().hostname);
if let Some(tu) = tu {
tu_ip = tu.ip.to_string();
tu_name = format!(" ({})", tu.hostname);
}
let top_uploader = Line::from(vec![
"Top uploader: ".into(),
Expand All @@ -289,7 +289,7 @@ impl Sniffer {
.iter()
.filter(|item| {
let t_ip = item.ip.to_string();
for i_ip in a_intfs.clone() {
for i_ip in a_intfs {
if i_ip.ip().to_string() == t_ip {
return false;
}
Expand All @@ -300,9 +300,9 @@ impl Sniffer {

let mut td_ip = String::from("");
let mut td_name = String::from("");
if td.is_some() {
td_ip = td.unwrap().ip.to_string();
td_name = format!(" ({})", tu.unwrap().hostname);
if let Some(td) = td {
td_ip = td.ip.to_string();
td_name = format!(" ({})", td.hostname);
}
let top_downloader = Line::from(vec![
"Top downloader: ".into(),
Expand Down Expand Up @@ -343,12 +343,14 @@ impl Component for Sniffer {
self.tab_changed(tab).unwrap();
}

if let Action::Down = action {
self.scroll_down();
}
if self.active_tab == TabsEnum::Traffic {
if let Action::Down = action {
self.scroll_down();
}

if let Action::Up = action {
self.scroll_up();
if let Action::Up = action {
self.scroll_up();
}
}

if let Action::ActiveInterface(ref interface) = action {
Expand All @@ -357,8 +359,8 @@ impl Component for Sniffer {

if let Action::PacketDump(time, packet, packet_type) = action {
match packet_type {
PacketTypeEnum::Tcp => self.process_packet(packet.clone()),
PacketTypeEnum::Udp => self.process_packet(packet.clone()),
PacketTypeEnum::Tcp => self.process_packet(packet),
PacketTypeEnum::Udp => self.process_packet(packet),
_ => {}
}
}
Expand Down
Loading

0 comments on commit 1ecc0b6

Please sign in to comment.