Polishing work, more logging, add warnings and errors for invalid config options

This commit is contained in:
Funtimes909 committed 2025-02-22 13:43:15 +13:00
1 parent f61556c231
commit 4c5786f9e3
4 files changed
+43 -18

No files matched your search

-1
View File
@@ -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";
+19
View File
@@ -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,
+1 -1
View File
@@ -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<Postgres> {
match PgPool::connect(&database_url).await {
+23 -16
View File
@@ -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);
}
}