Skip to content

Commit

Permalink
feat(ports): Option to provide port values [closes #60]
Browse files Browse the repository at this point in the history
  • Loading branch information
pwnwriter committed Jan 9, 2024
1 parent 4788272 commit 89707cf
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 16 deletions.
5 changes: 5 additions & 0 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ pub struct Cli {
#[arg(long, default_value = "100")]
pub timeout: u64,

/// Provide ports of the url
#[arg(long, num_args(0..=1000), required=false)]
// TODO: write a custom type that supports (..) and has `FromStr` impl
pub ports: Option<String>,

/// Read urls from the standard input
#[arg(long)]
pub stdin: bool,
Expand Down
27 changes: 23 additions & 4 deletions src/cli/exec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::args::{Cli, Input};
use super::screenshot::{take_screenshot, take_screenshot_in_bulk};
use super::screenshot::take_screenshot_in_bulk;
use anyhow::Context;
use chromiumoxide::{
browser::{Browser, BrowserConfig},
Expand All @@ -24,6 +24,7 @@ pub async fn run(
verbose,
fullpage,
screenshot_type,
ports,
}: Cli,
) -> anyhow::Result<()> {
let browser = Path::new(&binary_path);
Expand Down Expand Up @@ -69,7 +70,7 @@ pub async fn run(

if stdin {
env::set_current_dir(dump_dir)?;
let urls = super::hxn_helper::read_urls_from_stdin()?;
let urls = super::hxn_helper::read_urls_from_stdin(ports)?;
take_screenshot_in_bulk(
&browser,
urls,
Expand All @@ -83,7 +84,7 @@ pub async fn run(
} else {
match (url, file_path) {
(None, Some(file_path)) => {
let urls = super::hxn_helper::read_urls_from_file(file_path)?;
let urls = super::hxn_helper::read_urls_from_file(file_path, ports)?;
env::set_current_dir(dump_dir)?;
take_screenshot_in_bulk(
&browser,
Expand All @@ -97,8 +98,26 @@ pub async fn run(
.await?;
}
(Some(url), None) => {
let urls = if let Some(ports) = ports {
let ports_vec: Vec<&str> = ports.split(',').collect();
ports_vec
.iter()
.map(|port| format!("{}:{}", url, port))
.collect()
} else {
vec![url.to_string()]
};
env::set_current_dir(dump_dir)?;
take_screenshot(&browser, url, timeout, verbose, fullpage, screenshot_type).await?;
take_screenshot_in_bulk(
&browser,
urls,
tabs,
timeout,
verbose,
fullpage,
screenshot_type,
)
.await?;
}
_ => unreachable!(),
}
Expand Down
95 changes: 83 additions & 12 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,94 @@ pub mod exec;
pub mod screenshot;

pub mod hxn_helper {
use std::{io, path::Path};
use anyhow::{Context, Result};
use std::{
io::{self, BufRead},
path::Path,
};

/// Reads user's input from stdin line by line.
/// Combines URLs with provided ports, generating a new vector of formatted URLs.
///
/// If `ports` are provided, each URL from the `urls` vector will be combined with each port,
/// creating a new vector of formatted URLs. If `ports` are not provided, the function returns
/// the original `urls` vector.
///
/// # Arguments
///
/// * `urls` - A vector containing URLs to be combined with ports if provided.
/// * `ports` - An optional string containing ports separated by commas.
///
/// # Returns
///
/// A vector of strings containing formatted URLs combined with ports or the original URLs.
///
pub fn combine_urls_with_ports(urls: Vec<String>, ports: Option<String>) -> Vec<String> {
if let Some(ports) = ports {
let vector_of_strings: Vec<&str> = ports.split(',').collect();
urls.iter()
.flat_map(|url| {
vector_of_strings
.iter()
.map(move |port| format!("{}:{}", url, port))
})
.collect()
} else {
urls
}
}

/// Reads user's input URLs from stdin line by line and optionally combines them with ports.
///
/// This function reads URLs entered by the user from standard input, processing them line by
/// line. If `ports` are provided, it combines each URL with the specified ports. Returns a
/// vector of processed URLs.
///
/// # Arguments
///
/// * `ports` - An optional string containing ports separated by commas.
///
/// # Returns
///
/// A result containing a vector of processed URLs or an error if stdin reading fails.
///
#[inline]
pub fn read_urls_from_stdin() -> anyhow::Result<Vec<String>> {
Ok(io::read_to_string(io::stdin().lock())?
pub fn read_urls_from_stdin(ports: Option<String>) -> anyhow::Result<Vec<String>> {
let urls: Vec<String> = io::stdin()
.lock()
.lines()
.map(|url| url.trim().to_owned())
.collect())
.map(|line| line.unwrap().trim().to_owned())
.collect::<Vec<String>>();

let combined_urls = combine_urls_with_ports(urls, ports);
Ok(combined_urls)
}

/// Reads URLs from a file.
/// Reads URLs from a file and optionally combines them with provided ports.
///
/// This function reads URLs from a file specified by `file_path`, processing each line as a URL.
/// If `ports` are provided, it combines each URL with the specified ports. Returns a vector of
/// processed URLs.
///
/// # Arguments
///
/// * `file_path` - A path to the file containing URLs to be processed.
/// * `ports` - An optional string containing ports separated by commas.
///
/// # Returns
///
/// A result containing a vector of processed URLs or an error if file reading fails.
///
#[inline]
pub fn read_urls_from_file<T: AsRef<Path>>(file_path: T) -> anyhow::Result<Vec<String>> {
Ok(std::fs::read_to_string(file_path)?
.lines()
.map(|url| url.trim().to_owned())
.collect())
pub fn read_urls_from_file<T: AsRef<Path>>(
file_path: T,
ports: Option<String>,
) -> Result<Vec<String>> {
let urls = std::fs::read_to_string(&file_path)
.with_context(|| format!("Failed to read file: {:?}", file_path.as_ref()))?;

let urls_vec: Vec<String> = urls.lines().map(|url| url.to_string()).collect();

let combined_urls = combine_urls_with_ports(urls_vec, ports);
Ok(combined_urls)
}
}

0 comments on commit 89707cf

Please sign in to comment.