From 9838e1b6129096e0afae5843a5c2ee8d32004728 Mon Sep 17 00:00:00 2001 From: Valmo Date: Mon, 29 Jun 2026 06:12:21 -0300 Subject: [PATCH] Changed default CIDR --- Dockerfile | 2 +- docker-compose.yml | 2 +- src/config.rs | 20 +++++++++---------- src/main.rs | 49 ++++++++++++++++++++++++++++++++-------------- 4 files changed, 46 insertions(+), 27 deletions(-) diff --git a/Dockerfile b/Dockerfile index a48642f..bc0f7ea 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM rust:1.78-bookworm AS builder +FROM rust:stable-bookworm AS builder WORKDIR /app COPY Cargo.toml Cargo.lock ./ diff --git a/docker-compose.yml b/docker-compose.yml index d994fea..c5299fd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,7 +19,7 @@ services: command: ["coordinator"] environment: DATABASE_URL: postgres://camfinder:camfinder@postgres:5432/camfinder - SCAN_CIDRS: 192.168.15.0/24 + SCAN_CIDR_FILE: /app/cidrs.txt SCAN_PORT: 554 SCAN_TIMEOUT_MS: 1200 SCAN_CONCURRENCY: 16 diff --git a/src/config.rs b/src/config.rs index 652cb97..7e8f690 100644 --- a/src/config.rs +++ b/src/config.rs @@ -200,16 +200,6 @@ fn load_cidrs(file: Option<&Path>, inline: Option<&str>) -> Result> return Err(anyhow!("arquivo de CIDRs vazio: {}", file.display())); } - if let Ok(value) = env::var("SCAN_CIDRS") { - let value = value.trim(); - if !value.is_empty() { - let cidrs = parse_cidr_list(value); - if !cidrs.is_empty() { - return Ok(cidrs); - } - } - } - if let Ok(path) = env::var("SCAN_CIDR_FILE") { let path = path.trim(); if !path.is_empty() { @@ -223,6 +213,16 @@ fn load_cidrs(file: Option<&Path>, inline: Option<&str>) -> Result> } } + if let Ok(value) = env::var("SCAN_CIDRS") { + let value = value.trim(); + if !value.is_empty() { + let cidrs = parse_cidr_list(value); + if !cidrs.is_empty() { + return Ok(cidrs); + } + } + } + if let Ok(value) = env::var("SCAN_CIDR") { let value = value.trim(); if !value.is_empty() { diff --git a/src/main.rs b/src/main.rs index bdc8cd3..961c2e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,10 +21,10 @@ async fn main() -> Result<()> { dotenvy::dotenv().ok(); let config = Config::from_env_and_args()?; - + // Valida todos os CIDRs, filtrando os não roteáveis let valid_cidrs = validate_and_filter_cidrs(&config.cidrs)?; - + // Atualiza a config com apenas CIDRs válidos e roteáveis let config = Config { cidrs: valid_cidrs, @@ -84,7 +84,10 @@ fn validate_and_filter_cidrs(cidrs: &[String]) -> Result> { } if skipped_count > 0 { - eprintln!("ℹ️ Total de {} CIDR(s) privados/não roteáveis ignorados", skipped_count); + eprintln!( + "ℹ️ Total de {} CIDR(s) privados/não roteáveis ignorados", + skipped_count + ); } if valid_cidrs.is_empty() { @@ -111,9 +114,11 @@ fn validate_cidr(cidr: &str) -> Result { let prefix: u8 = prefix .parse() .map_err(|_| anyhow::anyhow!("máscara de sub-rede inválida: {prefix}"))?; - + if prefix > 32 { - return Err(anyhow::anyhow!("máscara de sub-rede deve ser <= 32: {prefix}")); + return Err(anyhow::anyhow!( + "máscara de sub-rede deve ser <= 32: {prefix}" + )); } let ip: std::net::Ipv4Addr = network @@ -124,7 +129,7 @@ fn validate_cidr(cidr: &str) -> Result { // Para /24 ou menores, verificamos o primeiro IP // Para ranges maiores, verificamos se a faixa toda é privada let start = u32::from(ip) & cidr_mask(prefix); - + if is_routable_ip(start) { return Ok(CidrType::Routable); } @@ -249,17 +254,31 @@ mod tests { #[test] fn test_routable_ips() { - assert!(is_routable_ip(u32::from(std::net::Ipv4Addr::new(1, 2, 3, 4)))); - assert!(is_routable_ip(u32::from(std::net::Ipv4Addr::new(8, 8, 8, 8)))); - assert!(is_routable_ip(u32::from(std::net::Ipv4Addr::new(203, 0, 114, 1)))); + assert!(is_routable_ip(u32::from(std::net::Ipv4Addr::new( + 1, 2, 3, 4 + )))); + assert!(is_routable_ip(u32::from(std::net::Ipv4Addr::new( + 8, 8, 8, 8 + )))); + assert!(is_routable_ip(u32::from(std::net::Ipv4Addr::new( + 203, 0, 114, 1 + )))); } #[test] fn test_private_ips() { - assert!(!is_routable_ip(u32::from(std::net::Ipv4Addr::new(10, 0, 0, 1)))); - assert!(!is_routable_ip(u32::from(std::net::Ipv4Addr::new(172, 16, 0, 1)))); - assert!(!is_routable_ip(u32::from(std::net::Ipv4Addr::new(192, 168, 1, 1)))); - assert!(!is_routable_ip(u32::from(std::net::Ipv4Addr::new(127, 0, 0, 1)))); + assert!(!is_routable_ip(u32::from(std::net::Ipv4Addr::new( + 10, 0, 0, 1 + )))); + assert!(!is_routable_ip(u32::from(std::net::Ipv4Addr::new( + 172, 16, 0, 1 + )))); + assert!(!is_routable_ip(u32::from(std::net::Ipv4Addr::new( + 192, 168, 1, 1 + )))); + assert!(!is_routable_ip(u32::from(std::net::Ipv4Addr::new( + 127, 0, 0, 1 + )))); } #[test] @@ -268,9 +287,9 @@ mod tests { assert_eq!(validate_cidr("8.8.8.8/32").unwrap(), CidrType::Routable); assert_eq!(validate_cidr("192.168.1.0/24").unwrap(), CidrType::Private); assert_eq!(validate_cidr("10.0.0.0/8").unwrap(), CidrType::Private); - + assert!(validate_cidr("invalid").is_err()); assert!(validate_cidr("1.2.3.4/33").is_err()); assert!(validate_cidr("999.999.999.999/24").is_err()); } -} \ No newline at end of file +}