zc2 0.0.30

P2P compute broker with credit-based billing, WAL, and broker mesh support
//! The summary's `mesh` field (mesh-routes ยง5): the route this Mac reaches
//! the mesh by, and whether peers can reach its broker. There's no live round
//! trip in v1.

use crate::agent::summary::MeshStatus;
use crate::vpn::Route;

/// The summary's `mesh`. `reachable` is true on the host route while the
/// broker runs. On the proxy route it also needs a relayed broker port
/// (9000..=9010) and a sidecar that carries the relay.
pub fn mesh_status(route: &Route, broker_running: bool, broker_port: u16) -> MeshStatus {
    match route {
        Route::Host { ip, interface } => MeshStatus {
            route: route.kind().label().to_string(),
            address: Some(ip.clone()),
            interface: Some(interface.clone()),
            reachable: broker_running,
        },
        Route::Proxy { mesh_ip, relay, .. } => MeshStatus {
            route: route.kind().label().to_string(),
            address: Some(mesh_ip.clone()),
            interface: Some(crate::vpn::docker::SIDECAR.to_string()),
            reachable: broker_running && *relay && crate::vpn::relay_covers(broker_port),
        },
        Route::None => MeshStatus::default(),
    }
}

#[cfg(test)]
mod tests {
    use super::mesh_status;
    use crate::agent::summary::MeshStatus;
    use crate::vpn::{fixtures, Route};

    fn host() -> Route {
        fixtures::host_route()
    }

    fn proxy(relay: bool) -> Route {
        match fixtures::proxy_route() {
            Route::Proxy {
                mesh_ip,
                connect_proxy,
                ..
            } => Route::Proxy {
                mesh_ip,
                connect_proxy,
                relay,
            },
            other => panic!("fixtures::proxy_route() must be a Route::Proxy, got {other:?}"),
        }
    }

    #[test]
    fn the_host_route_is_reachable_while_the_broker_runs() {
        assert_eq!(
            mesh_status(&host(), true, 54321),
            MeshStatus {
                route: "host".into(),
                address: Some("10.13.13.7".into()),
                interface: Some("utun4".into()),
                reachable: true
            }
        );
        assert!(!mesh_status(&host(), false, 9000).reachable);
    }

    #[test]
    fn the_proxy_route_is_reachable_only_on_a_relayed_port_with_the_relay() {
        let s = mesh_status(&proxy(true), true, 9000);
        assert_eq!(
            (
                s.route.as_str(),
                s.address.as_deref(),
                s.interface.as_deref()
            ),
            ("proxy", Some("10.13.13.7"), Some("zakuro-wg"))
        );
        assert!(s.reachable);
        assert!(mesh_status(&proxy(true), true, 9010).reachable);
        assert!(
            !mesh_status(&proxy(true), true, 54321).reachable,
            "outside 9000..=9010"
        );
        assert!(
            !mesh_status(&proxy(false), true, 9000).reachable,
            "a sidecar from before the relay"
        );
        assert!(
            !mesh_status(&proxy(true), false, 9000).reachable,
            "no broker"
        );
    }

    #[test]
    fn no_route_is_none_and_unreachable() {
        assert_eq!(mesh_status(&Route::None, true, 9000), MeshStatus::default());
        assert_eq!(MeshStatus::default().route, "none");
    }
}