From ebe8ff3f8f59d76f9ad8c22f8b1dbb5db28b5be6 Mon Sep 17 00:00:00 2001 From: qpxdesign Date: Thu, 16 May 2024 22:04:44 -0400 Subject: [PATCH] added pipe support (fixed #3) --- Cargo.lock | 47 +++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 3 ++- src/main.rs | 34 ++++++++++++++++++++++++-------- src/structs/Args.rs | 2 +- 4 files changed, 74 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 43209cd..062ab4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -189,6 +189,17 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi 0.1.19", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -556,6 +567,15 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hermit-abi" version = "0.3.6" @@ -681,8 +701,9 @@ dependencies = [ [[package]] name = "ngxav" -version = "0.6.0" +version = "0.6.1" dependencies = [ + "atty", "chrono", "clap", "fancy-regex", @@ -711,7 +732,7 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.6", "libc", ] @@ -1186,6 +1207,28 @@ version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d91413b1c31d7539ba5ef2451af3f0b833a005eb27a631cec32bc0635a8602b" +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-core" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index 0140d07..c1ce30e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ workspace = { members = [ "tests","tests/obfuscate_access_log_ips"] } [package] name = "ngxav" -version = "0.6.0" +version = "0.6.1" edition = "2021" license = "MIT" description = "Search through NGINX logs with advanced filters and support for displaying analytics about your selected log entries" @@ -9,6 +9,7 @@ readme = "README.md" repository = "https://github.com/qpxdesign/ngxav-rs/" [dependencies] +atty = "0.2.14" chrono = "0.4.33" clap = { version = "4.4.18", features = ["derive"] } fancy-regex = "0.13.0" diff --git a/src/main.rs b/src/main.rs index 53fc317..0f4d4bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use crate::sort_by_date::sort_by_date; +use atty::Stream; use clap::Parser; use rayon::prelude::*; use std::collections::HashMap; @@ -29,6 +30,14 @@ fn read_line_by_line(filename: impl AsRef) -> io::Result Vec { + let stdin = io::stdin(); + return stdin + .lock() + .lines() + .map(|line| line.expect("Failed to read line")) + .collect(); +} fn main() { let args: crate::structs::Args::ArgParser = ArgParser::parse(); if args.thread_count.is_some() { @@ -37,10 +46,12 @@ fn main() { .build_global() .unwrap(); } - let file_md = metadata(args.file.clone()).unwrap(); - if !args.conserve_memory.is_none() && args.conserve_memory.unwrap() == true && file_md.is_file() + if args.file.is_some() + && !args.conserve_memory.is_none() + && args.conserve_memory.unwrap() == true + && metadata(args.file.clone().unwrap()).unwrap().is_file() { - if let Ok(lines) = read_line_by_line(args.file) { + if let Ok(lines) = read_line_by_line(args.file.unwrap()) { let mut occurrences: HashMap = HashMap::new(); for line in lines.flatten() { let ip: String = line.clone().split(" ").collect::>()[0].to_string(); @@ -59,15 +70,22 @@ fn main() { return; } let mut lines = Vec::new(); - if file_md.is_dir() { + let stdin = load_stdin(); + if args.file.is_none() && stdin.len() == 0 { + panic!("error must either pipe-in log data or provide file with -f") + } + if args.file.is_some() && metadata(args.file.clone().unwrap()).unwrap().is_dir() { if args.conserve_memory.is_some() && args.conserve_memory.unwrap() == true { - utils::read_folder_conserve_memory::read_folder_conserve_memory(args.file, args.unique); + utils::read_folder_conserve_memory::read_folder_conserve_memory( + args.file.unwrap(), + args.unique, + ); return; - } else { - lines = utils::read_folder::read_folder(args.file); } + } else if args.file.is_none() && stdin.len() > 0 { + lines = stdin.clone(); } else { - lines = lines_from_file(args.file).expect("should read"); + lines = lines_from_file(args.file.unwrap()).expect("should read"); } let range = sort_by_date(&lines, &args.last, &args.start_date, &args.end_date); let mut kel: Vec = lines[range.0..range.1] diff --git a/src/structs/Args.rs b/src/structs/Args.rs index 66690be..a5dfdb3 100644 --- a/src/structs/Args.rs +++ b/src/structs/Args.rs @@ -5,7 +5,7 @@ use clap::Parser; #[command(version, about, long_about = None)] pub struct ArgParser { #[arg(short = 'f', long = "file")] - pub file: String, + pub file: Option, #[arg(short = 's', long = "search")] pub search: Option,