From 4c5786f9e3516f4e5f2ffcee60b7e791aaa721fd Mon Sep 17 00:00:00 2001 From: Funtimes909 Date: Sat, 22 Feb 2025 13:43:15 +1300 Subject: [PATCH] Polishing work, more logging, add warnings and errors for invalid config options --- src/colors.rs | 1 - src/config.rs | 19 +++++++++++++++++++ src/database.rs | 2 +- src/main.rs | 39 +++++++++++++++++++++++---------------- 4 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/colors.rs b/src/colors.rs index e5f2ea8..f363ed1 100644 --- a/src/colors.rs +++ b/src/colors.rs @@ -1,5 +1,4 @@ pub const GREEN: &str = "\x1b[0;32m"; pub const YELLOW: &str = "\x1b[0;33m"; -pub const ORANGE: &str = "\x1b[0;36m"; pub const RED: &str = "\x1b[0;31m"; pub const RESET: &str = "\x1b[0m"; \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index 8e0617c..2d7b32f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,7 @@ use serde::Deserialize; use std::fs::File; use std::io::{ErrorKind, Read}; +use crate::colors::{RED, RESET}; #[derive(Deserialize)] pub struct Config { @@ -36,6 +37,24 @@ impl Default for Rescanner { } } +impl Rescanner { + pub fn total_ports(&self) -> u16 { + let start = self.port_range_start; + let end = self.port_range_end; + + if start > end { + println!("{RED}port_range_start cannot be greater than port_range_end!{RESET}"); + std::process::exit(1); + } + + if end - start == 0 { + return 1; + } + + end - start + } +} + pub fn load_config(path: String) -> Config { let mut file = match File::open(&path) { Ok(file) => file, diff --git a/src/database.rs b/src/database.rs index 5d7124d..df58946 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,7 +1,7 @@ +use crate::colors::{RED, RESET}; use crate::response::Server; use sqlx::{postgres::PgQueryResult, Connection, Error, Executor, PgPool, Pool, Postgres, Row}; use std::time::{SystemTime, UNIX_EPOCH}; -use crate::colors::{RED, RESET}; pub async fn connect(database_url: &str) -> Pool { match PgPool::connect(&database_url).await { diff --git a/src/main.rs b/src/main.rs index 0d1b8ca..4f8edcd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,13 +4,12 @@ mod ping; mod response; mod colors; -use crate::database::{connect, fetch_servers, update_server}; -use crate::ping::ping_server; -use crate::response::parse_response; -use crate::colors::{GREEN, RED, RESET, YELLOW}; +use colors::{GREEN, RED, RESET, YELLOW}; +use database::{connect, fetch_servers, update_server}; +use ping::ping_server; +use response::parse_response; use config::{load_config, Config}; -use tokio::spawn; -use indicatif::{ProgressBar, ProgressIterator, ProgressStyle}; +use indicatif::{ProgressIterator, ProgressStyle}; #[tokio::main] async fn main() { @@ -27,7 +26,23 @@ async fn main() { config.database.port, config.database.table); + let port_start = config.rescanner.port_range_start; + let port_end = config.rescanner.port_range_end; + let total_ports = config.rescanner.total_ports(); + + if total_ports > 24 { + println!("{RED}[WARN] Large amount of ports! Scans will take exponentially longer for each port to scan!{RESET}"); + } + + if !config.rescanner.repeat { + println!("{YELLOW}[WARN] Repeat is not enabled in config file! Will only scan once!{RESET}"); + } + let pool = connect(database_url.as_str()).await; + println!("{GREEN}[INFO] Scanning port range {} - {} ({} port(s) per host){RESET}", + &port_start, + &port_end, + total_ports); loop { // Query servers from database @@ -39,14 +54,8 @@ async fn main() { Err(_) => continue }; - let style = ProgressStyle::default_bar().progress_chars("##-"); - // Loop over every result - for address in (&servers).iter().progress_with_style(style) { - let port_start = config.rescanner.port_range_start; - let port_end = config.rescanner.port_range_end; - - + for address in (&servers).iter().progress_with_style(ProgressStyle::default_bar().progress_chars("##-")) { for port in port_start..=port_end { // Ping server match ping_server(&address, port).await { @@ -61,14 +70,12 @@ async fn main() { } Err(_) => continue } - } } - println!("Finished pinging all servers"); + println!("{GREEN}[INFO] Finished pinging all servers{RESET}"); if !config.rescanner.repeat { - println!("{YELLOW}[INFO] Repeat is not enabled in config file! Exiting...{RESET}"); std::process::exit(0); } }