Changed default CIDR
This commit is contained in:
+1
-1
@@ -1,4 +1,4 @@
|
||||
FROM rust:1.78-bookworm AS builder
|
||||
FROM rust:stable-bookworm AS builder
|
||||
WORKDIR /app
|
||||
|
||||
COPY Cargo.toml Cargo.lock ./
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+10
-10
@@ -200,16 +200,6 @@ fn load_cidrs(file: Option<&Path>, inline: Option<&str>) -> Result<Vec<String>>
|
||||
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<Vec<String>>
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
|
||||
+34
-15
@@ -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<Vec<String>> {
|
||||
}
|
||||
|
||||
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<CidrType> {
|
||||
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<CidrType> {
|
||||
// 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user