///|
/// A validated, compact plan. Hostnames remain unresolved until execution.
#derive(Debug)
pub struct ScanPreview {
hosts : Int64
ports : Array[Int32]
worst_case_attempts : Int64
targets : Array[String]
dns_pending : Array[String]
scope : Json
}
///|
/// Validate numeric scope and resource budgets without DNS or TCP traffic.
///
/// ```mbt check
/// test {
/// let plan = @mscan.TargetPlan::parse(["127.0.0.0/30"])
/// assert_eq(plan.preview([80, 443]).worst_case_attempts, 8)
/// }
/// ```
pub fn TargetPlan::preview(
self : TargetPlan,
ports : ArrayView[Int32],
scope~ : Scope = Scope::loopback(),
retries~ : Int32 = 0,
max_hosts~ : Int32 = 256,
max_attempts~ : Int32 = 65536,
) -> ScanPreview raise InputError {
guard !ports.is_empty() &&
ports.all(p => p > 0 && p <= 65535) &&
retries >= 0 &&
retries <= 10 &&
max_hosts > 0 &&
max_attempts > 0 else {
raise InputError("invalid preview ports or budgets")
}
self.validate_limits(scope, ports.length(), retries, max_hosts, max_attempts)
let mut hosts = self.hosts.length().to_int64()
let targets = []
for range in self.ranges {
hosts += range.last - range.first + 1
targets.push(
"\{@socket.IpAddr::v4(range.first.to_uint())}-\{@socket.IpAddr::v4(range.last.to_uint())}",
)
}
let pending = []
for host in self.hosts {
targets.push(host)
ignore(@socket.IpAddr::parse(host)) catch {
_ => pending.push(host)
}
}
{
hosts,
ports: ports.to_owned(),
worst_case_attempts: hosts *
ports.length().to_int64() *
(retries + 1).to_int64(),
targets,
dns_pending: pending,
scope: scope.to_json(),
}
}
///|
pub fn Scope::to_json(self : Scope) -> Json {
{
"ipv4_ranges": self.ranges.map(r => {
"\{@socket.IpAddr::v4(r.first.to_uint())}-\{@socket.IpAddr::v4(r.last.to_uint())}"
}),
"ipv6_addresses": self.ipv6.iter().map(ip => ip.to_string()).collect(),
}
}
///|
pub fn ScanPreview::to_json(self : ScanPreview) -> Json {
{
"hosts": Json::number(self.hosts.to_double()),
"ports": self.ports,
"worst_case_attempts": Json::number(self.worst_case_attempts.to_double()),
"targets": self.targets,
"dns_pending": self.dns_pending,
"scope": self.scope,
}
}