Skip to main content

openmoat_proxy/
audit.rs

1//! One record per connection decision.
2//!
3//! As with `moat guard`, the record is part of the decision: a connection that
4//! cannot be recorded is not made. The CLI records into the moat audit log;
5//! this crate stays free of storage.
6
7use openmoat_core::Decision;
8
9/// One connection decision.
10#[derive(Debug, Clone, Copy)]
11pub struct Connection<'a> {
12    /// Request method (`CONNECT`, `GET`, …); `None` when the head was unreadable.
13    pub method: Option<&'a str>,
14    /// Destination host and port; `None` when the head was unreadable.
15    pub destination: Option<(&'a str, u16)>,
16    /// The verdict and the rules behind it.
17    pub decision: &'a Decision,
18    /// Time from accepting the connection to this decision, in microseconds.
19    pub latency_us: u64,
20}
21
22/// Where connection decisions go. An error refuses the connection.
23pub trait Recorder: Send + Sync {
24    /// Record one decision.
25    fn record(&self, connection: &Connection<'_>) -> Result<(), RecordError>;
26
27    /// Told when [`record`](Self::record) failed. The connection is refused
28    /// either way; this is where the failure is reported, somewhere other than
29    /// the audit log that just failed. The default does nothing.
30    fn unrecorded(&self, error: &RecordError) {
31        let _ = error;
32    }
33}
34
35/// A decision could not be recorded.
36#[derive(Debug, thiserror::Error)]
37#[error("audit log unavailable: {0}")]
38pub struct RecordError(pub String);