Skip to content

Commit

Permalink
bugfix & enums
Browse files Browse the repository at this point in the history
- better handle unwrap for interfaces (#6)
- added enums file and moved packets enums & structs into it
- prepared for better visualization and categorization of packets
  • Loading branch information
Chleba committed Mar 10, 2024
1 parent 8349b8a commit cd1f1f3
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 51 deletions.
18 changes: 8 additions & 10 deletions src/action.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
use std::{fmt, net::Ipv4Addr};
use chrono::{Local, DateTime};
use pnet::util::MacAddr;
use chrono::{DateTime, Local};
use pnet::datalink::NetworkInterface;
use pnet::util::MacAddr;
use ratatui::text::Line;
use serde::{
de::{self, Deserializer, Visitor},
Deserialize, Serialize,
};
use std::{fmt, net::Ipv4Addr};

use crate::components::{
packetdump::ArpPacketData,
wifi_scan::WifiInfo,
packetdump::PacketTypeEnum,
use crate::{
components::{packetdump::ArpPacketData, wifi_scan::WifiInfo},
enums::PacketTypeEnum,
mode::Mode,
};
use crate::mode::Mode;

// #[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[derive(Debug, Clone, PartialEq)]
pub enum Action {
Tick,
Expand Down Expand Up @@ -56,7 +54,7 @@ impl<'de> Deserialize<'de> for Action {

impl<'de> Visitor<'de> for ActionVisitor {
type Value = Action;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a valid string representation of Action")
}
Expand Down
63 changes: 28 additions & 35 deletions src/components/packetdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,27 @@ use super::{Component, Frame};
use crate::{
action::Action,
config::{Config, KeyBindings},
enums::{
ARPPacketInfo, ICMPPacketInfo, PacketTypeEnum, PacketsInfoTypesEnum, TCPPacketInfo,
UDPPacketInfo,
},
utils::MaxSizeVec,
};
use regex::Regex;
use strum::{Display, EnumCount, EnumIter, FromRepr, IntoEnumIterator};

// enum

#[derive(Default, Clone, Copy, Display, FromRepr, EnumIter, EnumCount, PartialEq, Debug)]
pub enum PacketTypeEnum {
#[default]
#[strum(to_string = "All")]
All,
#[strum(to_string = "ARP")]
Arp,
#[strum(to_string = "TCP")]
Tcp,
#[strum(to_string = "UDP")]
Udp,
#[strum(to_string = "ICMP")]
Icmp,
}

impl PacketTypeEnum {
fn previous(&self) -> Self {
let current_index: usize = *self as usize;
let previous_index = current_index.saturating_sub(1);
Self::from_repr(previous_index).unwrap_or(*self)
}

fn next(&self) -> Self {
let current_index = *self as usize;
let next_index = current_index.saturating_add(1);
Self::from_repr(next_index).unwrap_or(*self)
}
}
use strum::{EnumCount, IntoEnumIterator};

// impl PacketTypeEnum {
// pub fn previous(&self) -> Self {
// let current_index: usize = *self as usize;
// let previous_index = current_index.saturating_sub(1);
// Self::from_repr(previous_index).unwrap_or(*self)
// }

// pub fn next(&self) -> Self {
// let current_index = *self as usize;
// let next_index = current_index.saturating_add(1);
// Self::from_repr(next_index).unwrap_or(*self)
// }
// }

#[derive(Debug, Clone, PartialEq)]
pub struct ArpPacketData {
Expand Down Expand Up @@ -412,15 +398,22 @@ impl PacketDump {

fn set_scrollbar_height(&mut self) {
let logs_len = self.get_array_by_packet_type(self.packet_type).len();
self.scrollbar_state = self.scrollbar_state.content_length(logs_len - 1);
if logs_len > 0 {
self.scrollbar_state = self.scrollbar_state.content_length(logs_len - 1);
}
}

fn previous_in_table(&mut self) {
let index = match self.table_state.selected() {
Some(index) => {
let logs = self.get_array_by_packet_type(self.packet_type);
let logs_len = logs.len();
if index == 0 {
logs.len() - 1
if logs_len > 0 {
logs.len() - 1
} else {
0
}
} else {
index - 1
}
Expand Down
12 changes: 6 additions & 6 deletions src/components/wifi_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,20 @@ impl WifiInterface {
.get("Interface")
.unwrap_or(&"")
.parse::<String>()
.unwrap(),
ssid: hash.get("ssid").unwrap_or(&"").parse::<String>().unwrap(),
ifindex: hash.get("ifindex").unwrap_or(&"").parse::<u8>().unwrap(),
mac: hash.get("addr").unwrap_or(&"").parse::<String>().unwrap(),
.unwrap_or(String::from("")),
ssid: hash.get("ssid").unwrap_or(&"").parse::<String>().unwrap_or(String::from("")),
ifindex: hash.get("ifindex").unwrap_or(&"").parse::<u8>().unwrap_or(0),
mac: hash.get("addr").unwrap_or(&"").parse::<String>().unwrap_or(String::from("")),
channel: hash
.get("channel")
.unwrap_or(&"")
.parse::<String>()
.unwrap(),
.unwrap_or(String::from("")),
txpower: hash
.get("txpower")
.unwrap_or(&"")
.parse::<String>()
.unwrap(),
.unwrap_or(String::from("")),
}
}

Expand Down
74 changes: 74 additions & 0 deletions src/enums.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use pnet::packet::icmp::IcmpType;
use strum::{Display, EnumCount, EnumIter, FromRepr};

pub struct UDPPacketInfo {
interface_name: String,
source: String,
source_port: String,
destination: String,
destination_port: String,
length: String,
}

pub struct TCPPacketInfo {
interface_name: String,
source: String,
source_port: String,
destination: String,
destination_port: String,
length: String,
}

pub struct ARPPacketInfo {
interface_name: String,
source_mac: String,
source_ip: String,
destination_mac: String,
destination_ip: String,
operation: String,
}

pub struct ICMPPacketInfo {
interface_name: String,
source: String,
destination: String,
operation: String,
icmp_type: IcmpType,
}

pub enum PacketsInfoTypesEnum {
Arp(ARPPacketInfo),
Tcp(TCPPacketInfo),
Udp(UDPPacketInfo),
Icmp(ICMPPacketInfo),
}

#[derive(Default, Clone, Copy, Display, FromRepr, EnumIter, EnumCount, PartialEq, Debug)]
pub enum PacketTypeEnum {
#[default]
#[strum(to_string = "All")]
All,
#[strum(to_string = "ARP")]
Arp,
#[strum(to_string = "TCP")]
Tcp,
#[strum(to_string = "UDP")]
Udp,
#[strum(to_string = "ICMP")]
Icmp,
}

impl PacketTypeEnum {
pub fn previous(&self) -> Self {
let current_index: usize = *self as usize;
let previous_index = current_index.saturating_sub(1);
Self::from_repr(previous_index).unwrap_or(*self)
}

pub fn next(&self) -> Self {
let current_index = *self as usize;
let next_index = current_index.saturating_add(1);
Self::from_repr(next_index).unwrap_or(*self)
}
}

1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod config;
pub mod mode;
pub mod tui;
pub mod utils;
pub mod enums;

use clap::Parser;
use cli::Cli;
Expand Down

0 comments on commit cd1f1f3

Please sign in to comment.