From 1854fd6431020997c1bd195ad46b8076413052de Mon Sep 17 00:00:00 2001 From: SkuldNorniern Date: Mon, 5 Feb 2024 09:45:25 +0900 Subject: [PATCH] refactor: separate cli function from main --- src/cli.rs | 239 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 240 +--------------------------------------------------- 2 files changed, 241 insertions(+), 238 deletions(-) create mode 100644 src/cli.rs diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..49ecf35 --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,239 @@ +use clap::{Arg, ArgAction, Command}; + +// This function sets up the command line interface for the application using the clap library. +// It defines the available commands and their arguments. +pub fn cli_template() -> Command { + Command::new("fluere") + .version("0.6.2") + .author("Skuld Norniern. ") + .about("Netflow Capture Tool") + .subcommand_required(true) + .subcommand( + Command::new("online") + .about("Capture netflow online") + .arg( + Arg::new("csv") + .help("Title of the exported csv file") + .short('c') + .long("csv") + .default_value("output"), + ) + .arg( + Arg::new("list") + .help("List of network interfaces") + .short('l') + .long("list") + .action(ArgAction::SetTrue), + ) + .arg( + Arg::new("interface") + .help("Select network interface to use [Required]") + .short('i') + .long("interface") + //.required(true), + ) + .arg( + Arg::new("duration") + .help("Set capture duration, in milliseconds (0: infinite)") + .default_value("0") + .short('d') + .long("duration"), + ) + .arg( + Arg::new("timeout") + .help("Set flow timeout, in milliseconds (0: infinite)") + .default_value("600000") + .short('t') + .long("timeout"), + ) + .arg( + Arg::new("useMACaddress") + .help("Set use MAC address on Key value [default: false]") + .short('M') + .long("useMAC") + .action(ArgAction::SetTrue), + ) + .arg( + Arg::new("interval") + .help("Set export interval, in milliseconds") + .default_value("1800000") + .short('I') + .long("interval"), + ) + .arg( + Arg::new("sleep_windows") + .help("Set inverval of thread pause for (only)MS Windows per n packet (need it for stopping random stop on Windows)") + .default_value("10") + .short('s') + .long("sleep"), + ) + .arg( + Arg::new("verbose") + .help("Set verbosity level") + .default_value("1") + .short('v') + .long("verbose"), // 0: quiet, 1: normal,2: extended, 3: verbose + ), + ) + .subcommand( + Command::new("offline") + .about("Convet pcap files to netflow") + .arg( + Arg::new("file") + .help("Name of the input pcap file [Required]") + .short('f') + .long("file") + .required(true), + ) + .arg( + Arg::new("csv") + .help("Title of the exported csv file") + .short('c') + .long("csv") + .default_value("output"), + ) + .arg( + Arg::new("timeout") + .help("Set flow timeout, in milliseconds (0: infinite)") + .default_value("600000") + .short('t') + .long("timeout"), + ) + .arg( + Arg::new("useMACaddress") + .help("Set use MAC address on Key value [default: false]") + .short('M') + .long("useMAC") + .action(ArgAction::SetTrue), + ) + .arg( + Arg::new("verbose") + .help("Set verbosity level") + .default_value("1") + .short('v') + .long("verbose"), // 0: quiet, 1: normal,2: extended, 3: verbose + ), + ) + .subcommand( + Command::new("live") + .about("Capture netflow online with live TUI feedback") + .arg( + Arg::new("csv") + .help("Title of the exported csv file") + .short('c') + .long("csv") + .default_value("output"), + ) + .arg( + Arg::new("list") + .help("List of network interfaces") + .short('l') + .long("list") + .action(ArgAction::SetTrue), + ) + .arg( + Arg::new("interface") + .help("Select network interface to use [Required]") + .short('i') + .long("interface") + //.required(true), + ) + .arg( + Arg::new("duration") + .help("Set capture duration, in milliseconds (0: infinite)") + .default_value("0") + .short('d') + .long("duration"), + ) + .arg( + Arg::new("timeout") + .help("Set flow timeout, in milliseconds (0: infinite)") + .default_value("600000") + .short('t') + .long("timeout"), + ) + .arg( + Arg::new("useMACaddress") + .help("Set use MAC address on Key value [default: false]") + .short('M') + .long("useMAC") + .action(ArgAction::SetTrue), + ) + .arg( + Arg::new("interval") + .help("Set export interval, in milliseconds") + .default_value("1800000") + .short('I') + .long("interval"), + ) + .arg( + Arg::new("sleep_windows") + .help("Set inverval of thread pause for (only)MS Windows per n packet (need it for stopping random stop on Windows)") + .default_value("10") + .short('s') + .long("sleep"), + ) + .arg( + Arg::new("verbose") + .help("Set verbosity level") + .default_value("1") + .short('v') + .long("verbose"), // 0: quiet, 1: normal,2: extended, 3: verbose + ), + ) + .subcommand( + Command::new("pcap") + .about("Collect packet and save to .pcap file") + .arg( + Arg::new("pcap") + .help("Name of the output pcap files title [Required]") + .short('p') + .long("pcap") + //.required(true), + ) + .arg( + Arg::new("interface") + .help("Select network interface to use [Required]") + .short('i') + .long("interface") + //.required(true), + ) + .arg( + Arg::new("duration") + .help("Set capture duration, in milliseconds (0: infinite)") + .default_value("0") + .short('d') + .long("duration"), + ) + .arg( + Arg::new("interval") + .help("Set export interval, in milliseconds") + .default_value("1800000") + .short('I') + .long("interval"), + ) + .arg( + Arg::new("sleep_windows") + .help("Set inverval of thread pause for (only)MS Windows per n packet (need it for stopping random stop on Windows)") + .default_value("10") + .short('s') + .long("sleep"), + ) + .arg( + Arg::new("list") + .help("List of network interfaces") + .short('l') + .long("list") + .action(ArgAction::SetTrue), + ) + .arg( + Arg::new("verbose") + .help("Set verbosity level") + .default_value("1") + .short('v') + .long("verbose"), // 0: quiet, 1: normal,2: extended, 3: verbose + ), + ) +} + + diff --git a/src/main.rs b/src/main.rs index e47dc9d..91e1a05 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,255 +7,19 @@ pub mod net; pub mod plugin; pub mod types; pub mod utils; +pub mod cli; -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. -// It defines the available commands and their arguments. -fn cli() -> Command { - Command::new("fluere") - .version("0.6.2") - .author("Skuld Norniern. ") - .about("Netflow Capture Tool") - .subcommand_required(true) - .subcommand( - Command::new("online") - .about("Capture netflow online") - .arg( - Arg::new("csv") - .help("Title of the exported csv file") - .short('c') - .long("csv") - .default_value("output"), - ) - .arg( - Arg::new("list") - .help("List of network interfaces") - .short('l') - .long("list") - .action(ArgAction::SetTrue), - ) - .arg( - Arg::new("interface") - .help("Select network interface to use [Required]") - .short('i') - .long("interface") - //.required(true), - ) - .arg( - Arg::new("duration") - .help("Set capture duration, in milliseconds (0: infinite)") - .default_value("0") - .short('d') - .long("duration"), - ) - .arg( - Arg::new("timeout") - .help("Set flow timeout, in milliseconds (0: infinite)") - .default_value("600000") - .short('t') - .long("timeout"), - ) - .arg( - Arg::new("useMACaddress") - .help("Set use MAC address on Key value [default: false]") - .short('M') - .long("useMAC") - .action(ArgAction::SetTrue), - ) - .arg( - Arg::new("interval") - .help("Set export interval, in milliseconds") - .default_value("1800000") - .short('I') - .long("interval"), - ) - .arg( - Arg::new("sleep_windows") - .help("Set inverval of thread pause for (only)MS Windows per n packet (need it for stopping random stop on Windows)") - .default_value("10") - .short('s') - .long("sleep"), - ) - .arg( - Arg::new("verbose") - .help("Set verbosity level") - .default_value("1") - .short('v') - .long("verbose"), // 0: quiet, 1: normal,2: extended, 3: verbose - ), - ) - .subcommand( - Command::new("offline") - .about("Convet pcap files to netflow") - .arg( - Arg::new("file") - .help("Name of the input pcap file [Required]") - .short('f') - .long("file") - .required(true), - ) - .arg( - Arg::new("csv") - .help("Title of the exported csv file") - .short('c') - .long("csv") - .default_value("output"), - ) - .arg( - Arg::new("timeout") - .help("Set flow timeout, in milliseconds (0: infinite)") - .default_value("600000") - .short('t') - .long("timeout"), - ) - .arg( - Arg::new("useMACaddress") - .help("Set use MAC address on Key value [default: false]") - .short('M') - .long("useMAC") - .action(ArgAction::SetTrue), - ) - .arg( - Arg::new("verbose") - .help("Set verbosity level") - .default_value("1") - .short('v') - .long("verbose"), // 0: quiet, 1: normal,2: extended, 3: verbose - ), - ) - .subcommand( - Command::new("live") - .about("Capture netflow online with live TUI feedback") - .arg( - Arg::new("csv") - .help("Title of the exported csv file") - .short('c') - .long("csv") - .default_value("output"), - ) - .arg( - Arg::new("list") - .help("List of network interfaces") - .short('l') - .long("list") - .action(ArgAction::SetTrue), - ) - .arg( - Arg::new("interface") - .help("Select network interface to use [Required]") - .short('i') - .long("interface") - //.required(true), - ) - .arg( - Arg::new("duration") - .help("Set capture duration, in milliseconds (0: infinite)") - .default_value("0") - .short('d') - .long("duration"), - ) - .arg( - Arg::new("timeout") - .help("Set flow timeout, in milliseconds (0: infinite)") - .default_value("600000") - .short('t') - .long("timeout"), - ) - .arg( - Arg::new("useMACaddress") - .help("Set use MAC address on Key value [default: false]") - .short('M') - .long("useMAC") - .action(ArgAction::SetTrue), - ) - .arg( - Arg::new("interval") - .help("Set export interval, in milliseconds") - .default_value("1800000") - .short('I') - .long("interval"), - ) - .arg( - Arg::new("sleep_windows") - .help("Set inverval of thread pause for (only)MS Windows per n packet (need it for stopping random stop on Windows)") - .default_value("10") - .short('s') - .long("sleep"), - ) - .arg( - Arg::new("verbose") - .help("Set verbosity level") - .default_value("1") - .short('v') - .long("verbose"), // 0: quiet, 1: normal,2: extended, 3: verbose - ), - ) - .subcommand( - Command::new("pcap") - .about("Collect packet and save to .pcap file") - .arg( - Arg::new("pcap") - .help("Name of the output pcap files title [Required]") - .short('p') - .long("pcap") - //.required(true), - ) - .arg( - Arg::new("interface") - .help("Select network interface to use [Required]") - .short('i') - .long("interface") - //.required(true), - ) - .arg( - Arg::new("duration") - .help("Set capture duration, in milliseconds (0: infinite)") - .default_value("0") - .short('d') - .long("duration"), - ) - .arg( - Arg::new("interval") - .help("Set export interval, in milliseconds") - .default_value("1800000") - .short('I') - .long("interval"), - ) - .arg( - Arg::new("sleep_windows") - .help("Set inverval of thread pause for (only)MS Windows per n packet (need it for stopping random stop on Windows)") - .default_value("10") - .short('s') - .long("sleep"), - ) - .arg( - Arg::new("list") - .help("List of network interfaces") - .short('l') - .long("list") - .action(ArgAction::SetTrue), - ) - .arg( - Arg::new("verbose") - .help("Set verbosity level") - .default_value("1") - .short('v') - .long("verbose"), // 0: quiet, 1: normal,2: extended, 3: verbose - ), - ) -} - // This is the main function of the application. // It gets the command line arguments, parses them, and calls the appropriate functions based on the arguments. #[tokio::main] async fn main() { - let args = cli().get_matches(); + let args = cli::cli_template().get_matches(); let interfaces = datalink::interfaces(); //let _plugins = scan_plugins("plugins"); //println!("Plugins: {:?}", plugins); //match generate_config() {