Skip to content

Commit

Permalink
feat: migrate capture device handling to capture.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
SkuldNorniern committed Jan 8, 2024
1 parent 9ed0479 commit 1e82fd3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
16 changes: 9 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub mod utils;
use clap::{Arg, ArgAction, Command};
use pnet::datalink;

use crate::net::list_devices;

use std::process::exit;

// This function sets up the command line interface for the application using the clap library.
Expand Down Expand Up @@ -266,11 +268,11 @@ async fn main() {
println!("Online mode");
utils::get_local_ip();
if args.get_flag("list") {
println!("List of interfaces");
for iface in interfaces {
println!("[{}]: {}", iface.index, iface.name);
let interfaces = list_devices().unwrap();
println!("Found {} devices", interfaces.len());
for (i, interface) in interfaces.iter().enumerate() {
println!("[{}]: {}", i, interface.name);
}

exit(0);
}
let use_mac = args.get_flag("useMACaddress");
Expand Down Expand Up @@ -332,11 +334,11 @@ async fn main() {
Some(("live", args)) => {
println!("Live mode");
if args.get_flag("list") {
println!("List of interfaces");
let interfaces = list_devices().unwrap();
println!("Found {} devices", interfaces.len());
for (i, interface) in interfaces.iter().enumerate() {
println!("[{}]: {}", i, interface.name);
}

}
exit(0);
}
let use_mac = args.get_flag("useMACaddress");
Expand Down
19 changes: 7 additions & 12 deletions src/net/live_fluereflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use fluere_config::Config;
use fluere_plugin::PluginManager;
use fluereflow::FluereRecord;

use pcap::Capture;
use ratatui::{
backend::CrosstermBackend,
layout::{Constraint, Direction, Layout},
Expand All @@ -25,13 +24,13 @@ use tokio::sync::Mutex;
use tokio::task;
use tokio::time::sleep;

use super::interface::get_interface;

use crate::{
net::{
flows::update_flow,
parser::{microseconds_to_timestamp, parse_fluereflow, parse_keys, parse_microseconds},
types::TcpFlags,
CaptureDevice,
find_device,
},
types::{Args, UDFlowKey},
utils::{cur_time_file, fluere_exporter},
Expand Down Expand Up @@ -84,15 +83,10 @@ pub async fn online_packet_capture(arg: Args) {
.load_plugins(&config)
.await
.expect("Failed to load plugins");
let interface = get_interface(interface_name.as_str());
let mut cap = Capture::from_device(interface)
.unwrap()
.promisc(true)
.timeout(60000)
//.buffer_size(100000000)
.immediate_mode(true)
.open()
.unwrap();

let interface = find_device(interface_name.as_str()).unwrap();
let cap_device = CaptureDevice::new(interface.clone()).unwrap();
let mut cap = cap_device.capture;

let file_dir = "./output";
match fs::create_dir_all(<&str>::clone(&file_dir)) {
Expand Down Expand Up @@ -178,6 +172,7 @@ pub async fn online_packet_capture(arg: Args) {
});

tokio::spawn(listen_for_exit_keys());

loop {
match cap.next_packet() {
Err(_) => {
Expand Down
5 changes: 5 additions & 0 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod errors;
//mod fluereflow;
mod flows;
mod interface;
mod capture;
pub mod live_fluereflow;
mod offline_fluereflows;
pub mod online_fluereflow;
Expand All @@ -14,4 +15,8 @@ pub use interface::list_interface_names;
pub use interface::list_interfaces;
pub use offline_fluereflows::fluereflow_fileparse;
pub use packet_pcap::pcap_capture;
pub use capture::CaptureDevice;
pub use capture::CaptureError;
pub use capture::find_device;
pub use capture::list_devices;
//pub use types::FluereRecord;
25 changes: 13 additions & 12 deletions src/net/online_fluereflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
// The data is then exported to a CSV file.
extern crate csv;

use pcap::Capture;

use fluere_config::Config;
use fluere_plugin::PluginManager;
use fluereflow::FluereRecord;

use tokio::task;
use tokio::time::sleep;

use super::interface::get_interface;

use crate::{
net::{
flows::update_flow,
parser::{parse_fluereflow, parse_keys, parse_microseconds},
types::{Key, TcpFlags},
CaptureDevice,
find_device,
},
types::{Args, UDFlowKey},
utils::{cur_time_file, fluere_exporter},
Expand Down Expand Up @@ -51,14 +49,17 @@ pub async fn packet_capture(arg: Args) {
.await
.expect("Failed to load plugins");

let interface = get_interface(interface_name.as_str());
let mut cap = Capture::from_device(interface)
.unwrap()
.promisc(true)
//.buffer_size(100000000)
.immediate_mode(true)
.open()
.unwrap();
let interface = find_device(interface_name.as_str()).unwrap();
let cap_device = CaptureDevice::new(interface.clone()).unwrap();
let mut cap = cap_device.capture;
// let mut cp_device
// let mut cap = Capture::from_device(interface)
// .unwrap()
// .promisc(true)
// //.buffer_size(100000000)
// .immediate_mode(true)
// .open()
// .unwrap();

let file_dir = "./output";
match fs::create_dir_all(<&str>::clone(&file_dir)) {
Expand Down

0 comments on commit 1e82fd3

Please sign in to comment.