use std::path::Path;
use anyhow::Result;
use openmoat_hosts::Host;
use super::Shown;
use crate::home::Home;
use crate::install::{HookState, HostConfig, Recorded};
use crate::integrity::Lock;
use crate::report::{Mark, Row};
use crate::sandbox::{Plan, install as host_sandbox};
pub(super) fn hooks(shown: &mut Shown, home: &Home, binary: &Path) -> Result<bool> {
let recorded = Recorded::load(home).unwrap_or_default();
let mut healthy = true;
crate::install::host_version::prefetch_present(home);
for host in Host::ALL {
let config = HostConfig::for_host(home, host)?;
let name = host.display_name();
let advisories = if config.host_present() {
crate::install::host_version::advisories(host)
} else {
Vec::new()
};
let unsupported = config
.host_present()
.then(|| crate::install::host_version::unsupported(host))
.flatten();
let state = config.state(binary);
let installed = state == HookState::Installed && unsupported.is_none();
let problem = match (state, unsupported) {
(_, Some(why)) => Some(Row::new(
Mark::Broken,
name,
format!("not protected: {why}"),
)),
(HookState::Installed, None) => None,
(HookState::Missing, None) if recorded.skipped(host) || !config.host_present() => None,
(HookState::Missing, None) => {
Some(Row::new(Mark::Broken, name, "hook missing").fix("openmoat init"))
}
(HookState::Stale { command }, None) => Some(Row::new(
Mark::Broken,
name,
crate::install::stale_hint(&command),
)),
(
HookState::Unreadable(error)
| HookState::Unapproved(error)
| HookState::FailsOpen(error)
| HookState::TurnedOff(error),
None,
) => Some(Row::new(Mark::Broken, name, error)),
};
if let Some(row) = problem {
healthy = false;
shown.attention.push(row);
}
for advisory in advisories {
shown.needs(Mark::Attention, name, advisory);
}
for (fine, line) in crate::install::host_version::bundled_lines(host) {
if fine {
shown.agents.push(Row::new(Mark::Info, name, line));
} else {
shown.needs(Mark::Attention, name, line);
}
}
if installed && let Some(warning) = config.trust_warning() {
let row = Row::new(Mark::Attention, name, warning);
let row = match crate::install::trust_note::step(host) {
Some(step) => row.fix(step),
None => row,
};
shown.attention.push(row);
}
if let Some(loaded) = installed
.then(|| crate::heartbeat::check(home, &config))
.flatten()
{
let (mark, text) = crate::heartbeat::line(name, &loaded);
match mark {
crate::heartbeat::Mark::Note => shown.agents.push(Row::new(Mark::Info, name, text)),
crate::heartbeat::Mark::Warning => shown.needs(Mark::Attention, name, text),
}
}
}
for (host, found) in crate::install::detected()? {
let text = format!("found at {}; {}", found.display(), host.spec().gaps);
shown
.agents
.push(Row::new(Mark::Attention, host.display_name(), text));
}
Ok(healthy)
}
pub(super) fn sandboxes(shown: &mut Shown, home: &Home, plan: &Plan, binary: &Path) -> bool {
let lock = Lock::load(&home.lock_path()).ok();
let mut healthy = true;
let mut with_sandbox = Vec::new();
for host in host_sandbox::hosts()
.filter(|h| HostConfig::for_host(home, *h).is_ok_and(|c| c.set_up(binary)))
{
let name = host.display_name();
let problems = host_sandbox::problems(home, host, plan, lock.as_ref());
if let (Ok(Some(_)), Some(_)) = (&problems, host_sandbox::unavailable(host)) {
continue;
}
if !matches!(problems, Ok(None)) {
with_sandbox.push(host);
}
match problems {
Ok(None) => {}
Ok(Some(problems)) if problems.is_empty() => {}
Ok(Some(problems)) => {
healthy = false;
for problem in problems {
shown.needs(Mark::Broken, name, format!("sandbox: {problem}"));
}
}
Err(error) => {
healthy = false;
shown.needs(Mark::Broken, name, format!("sandbox: {error:#}"));
}
}
}
if let Some(port) = plan.proxy_port.filter(|_| !with_sandbox.is_empty()) {
let (ok, text) = crate::commands::proxy::listening_line(port);
proxy_row(shown, ok, "openmoat proxy", text);
let lines =
crate::commands::proxy::session_lines(&with_sandbox, plan.serve_without_credential);
for (ok, label, text) in lines {
proxy_row(shown, ok, label, text);
}
}
for (ok, text) in crate::commands::proxy::service_lines(home) {
let (label, rest) = match text.strip_prefix("service file") {
Some(rest) => ("Service file", rest),
None => ("Service", text.strip_prefix("service").unwrap_or(&text)),
};
proxy_row(shown, ok, label, rest.trim().to_owned());
healthy &= ok;
}
healthy
}
fn proxy_row(shown: &mut Shown, ok: bool, label: &str, text: String) {
if ok {
shown.state(Mark::Ok, label, text);
} else {
shown.needs(Mark::Attention, label, text);
}
}