server/ (state, ingestion, API, CSV exports, embedded assets), scanner, feeds_cmd, config_cmd and commands. Code moved as is; no behavior change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
238 lines
6.9 KiB
Rust
238 lines
6.9 KiB
Rust
mod auth;
|
|
mod commands;
|
|
mod config_cmd;
|
|
mod feeds_cmd;
|
|
mod scanner;
|
|
mod server;
|
|
|
|
use std::fs;
|
|
use std::path::{Path, PathBuf};
|
|
use std::process::ExitCode;
|
|
|
|
use chrono::{DateTime, Utc};
|
|
use clap::{Parser, Subcommand};
|
|
use commands::{run_changes, run_doctor, run_export};
|
|
use config_cmd::run_config_check;
|
|
use feeds_cmd::{run_feeds_import, run_feeds_sync};
|
|
use scanner::{run_scanner, scan_once};
|
|
use server::run_serve;
|
|
use xexeu_core::config::{ValidationReport, XexeuConfig};
|
|
|
|
#[derive(Parser)]
|
|
#[command(
|
|
name = "xexeu",
|
|
about = "Xexéu — continuous attack-surface and vulnerability auditing",
|
|
version
|
|
)]
|
|
struct Cli {
|
|
#[command(subcommand)]
|
|
command: Command,
|
|
}
|
|
|
|
#[derive(Subcommand)]
|
|
enum Command {
|
|
/// Run the API server, batch ingestion, diffs and the embedded dashboard.
|
|
Serve {
|
|
#[arg(long, default_value = "0.0.0.0:8788")]
|
|
bind: String,
|
|
#[arg(long, default_value = "config/xexeu.toml")]
|
|
config: PathBuf,
|
|
#[arg(long, default_value = "data/sweeps")]
|
|
sweeps: PathBuf,
|
|
#[arg(long, default_value = "data/xexeu")]
|
|
data_dir: PathBuf,
|
|
/// Token required by the API routes that change state. Without it, a
|
|
/// random token is generated at startup and printed once.
|
|
#[arg(long, env = "XEXEU_ADMIN_TOKEN", hide_env_values = true)]
|
|
admin_token: Option<String>,
|
|
},
|
|
/// Run the continuous scheduler and scan engine, writing sweep batches.
|
|
Scanner {
|
|
#[arg(long, default_value = "config/xexeu.toml")]
|
|
config: PathBuf,
|
|
#[arg(long, default_value = "data/sweeps")]
|
|
out: PathBuf,
|
|
#[arg(long, default_value = "data/xexeu")]
|
|
data_dir: PathBuf,
|
|
},
|
|
/// Run a single sweep for diagnostics.
|
|
Scan {
|
|
#[arg(long)]
|
|
group: Option<String>,
|
|
#[arg(long)]
|
|
once: bool,
|
|
#[arg(long, default_value = "config/xexeu.toml")]
|
|
config: PathBuf,
|
|
#[arg(long, default_value = "data/sweeps")]
|
|
out: PathBuf,
|
|
#[arg(long, default_value = "data/xexeu")]
|
|
data_dir: PathBuf,
|
|
},
|
|
/// Inspect the configuration.
|
|
Config {
|
|
#[command(subcommand)]
|
|
action: ConfigAction,
|
|
},
|
|
/// Synchronize or import vulnerability feeds.
|
|
Feeds {
|
|
#[command(subcommand)]
|
|
action: FeedsAction,
|
|
},
|
|
/// Query change events.
|
|
Changes {
|
|
#[arg(long, default_value = "24h")]
|
|
since: String,
|
|
#[arg(long, default_value = "data/xexeu")]
|
|
data_dir: PathBuf,
|
|
},
|
|
/// Export the current state.
|
|
Export {
|
|
#[arg(long, default_value = "json")]
|
|
format: String,
|
|
#[arg(long, default_value = "data/xexeu")]
|
|
data_dir: PathBuf,
|
|
#[arg(long)]
|
|
out: Option<PathBuf>,
|
|
},
|
|
/// Check the runtime environment.
|
|
Doctor {
|
|
#[arg(long)]
|
|
json: bool,
|
|
#[arg(long, default_value = "config/xexeu.toml")]
|
|
config: PathBuf,
|
|
#[arg(long, default_value = "data/xexeu")]
|
|
data_dir: PathBuf,
|
|
},
|
|
}
|
|
|
|
#[derive(Subcommand)]
|
|
enum ConfigAction {
|
|
/// Validate the configuration and print the effective cadence per group.
|
|
Check {
|
|
#[arg(long, default_value = "config/xexeu.toml")]
|
|
config: PathBuf,
|
|
},
|
|
}
|
|
|
|
#[derive(Subcommand)]
|
|
enum FeedsAction {
|
|
/// Download NVD and CISA KEV feeds incrementally.
|
|
Sync {
|
|
#[arg(long, default_value = "config/xexeu.toml")]
|
|
config: PathBuf,
|
|
#[arg(long, default_value = "data/xexeu")]
|
|
data_dir: PathBuf,
|
|
},
|
|
/// Import feed files copied manually for isolated networks.
|
|
Import {
|
|
directory: Option<PathBuf>,
|
|
#[arg(long)]
|
|
nvd: Option<PathBuf>,
|
|
#[arg(long)]
|
|
kev: Option<PathBuf>,
|
|
#[arg(long, default_value = "config/xexeu.toml")]
|
|
config: PathBuf,
|
|
#[arg(long, default_value = "data/xexeu")]
|
|
data_dir: PathBuf,
|
|
},
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> ExitCode {
|
|
let cli = Cli::parse();
|
|
match cli.command {
|
|
Command::Scan {
|
|
group,
|
|
once,
|
|
config,
|
|
out,
|
|
data_dir,
|
|
} => match scan_once(group, once, &config, &out, &data_dir).await {
|
|
Ok(()) => ExitCode::SUCCESS,
|
|
Err(message) => fail(&message),
|
|
},
|
|
Command::Scanner {
|
|
config,
|
|
out,
|
|
data_dir,
|
|
} => match run_scanner(&config, &out, &data_dir).await {
|
|
Ok(()) => ExitCode::SUCCESS,
|
|
Err(message) => fail(&message),
|
|
},
|
|
Command::Serve {
|
|
bind,
|
|
config,
|
|
sweeps,
|
|
data_dir,
|
|
admin_token,
|
|
} => match run_serve(&bind, &config, &sweeps, &data_dir, admin_token.as_deref()).await {
|
|
Ok(()) => ExitCode::SUCCESS,
|
|
Err(message) => fail(&message),
|
|
},
|
|
Command::Changes { since, data_dir } => run_changes(&since, &data_dir),
|
|
Command::Feeds { action } => match action {
|
|
FeedsAction::Import {
|
|
directory,
|
|
nvd,
|
|
kev,
|
|
config: _,
|
|
data_dir,
|
|
} => run_feeds_import(directory, nvd, kev, &data_dir),
|
|
FeedsAction::Sync {
|
|
config: _,
|
|
data_dir,
|
|
} => run_feeds_sync(&data_dir).await,
|
|
},
|
|
Command::Config { action } => match action {
|
|
ConfigAction::Check { config } => run_config_check(&config),
|
|
},
|
|
Command::Doctor {
|
|
json,
|
|
config,
|
|
data_dir,
|
|
} => run_doctor(json, &config, &data_dir),
|
|
Command::Export {
|
|
format,
|
|
data_dir,
|
|
out,
|
|
} => run_export(&format, &data_dir, out.as_deref()),
|
|
}
|
|
}
|
|
|
|
fn fail(message: &str) -> ExitCode {
|
|
eprintln!("{message}");
|
|
ExitCode::FAILURE
|
|
}
|
|
|
|
fn validated_config(path: &Path) -> Result<(XexeuConfig, ValidationReport), String> {
|
|
let raw = fs::read_to_string(path)
|
|
.map_err(|error| format!("não foi possível ler {}: {error}", path.display()))?;
|
|
XexeuConfig::load_validated(&raw)
|
|
.map_err(|error| format!("configuração inválida em {}: {error}", path.display()))
|
|
}
|
|
|
|
fn parse_since(raw: &str) -> Option<DateTime<Utc>> {
|
|
let raw = raw.trim();
|
|
if raw.is_empty() {
|
|
return None;
|
|
}
|
|
if raw.len() > 1 {
|
|
let (num_str, unit) = raw.split_at(raw.len() - 1);
|
|
if let Ok(number) = num_str.parse::<i64>() {
|
|
let duration = match unit {
|
|
"s" => Some(chrono::Duration::seconds(number)),
|
|
"m" => Some(chrono::Duration::minutes(number)),
|
|
"h" => Some(chrono::Duration::hours(number)),
|
|
"d" => Some(chrono::Duration::days(number)),
|
|
_ => None,
|
|
};
|
|
if let Some(dur) = duration {
|
|
return Some(Utc::now() - dur);
|
|
}
|
|
}
|
|
}
|
|
DateTime::parse_from_rfc3339(raw)
|
|
.ok()
|
|
.map(|date_time| date_time.with_timezone(&Utc))
|
|
}
|