//! The compiled plan: what one device needs (docs/FORMATS.md, "The plan"). Field order is the wire order. use crate::id::RoadId; use crate::ids::{AppId, DeviceKey, Hostname}; use crate::net::{Addr, Net}; use crate::ports::Proto; use serde::{Deserialize, Serialize}; pub const FORMAT: u32 = 1; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Plan { pub format: u32, pub device: DeviceKey, pub name: String, pub address: Addr, /// The services this device hosts on an address of their own: it answers on these too (sorted by host). pub services: Vec, /// This device is a classic WireGuard peer: not post-quantum. pub classic: bool, /// The earliest `until` of the roads this plan was built from, UTC: compile again by then. pub expires: Option, pub peers: Vec, pub firewall: Firewall, pub dns: Vec, pub exit: Option, pub kill_switch: Option, pub direct: Vec, pub custom: Vec, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Peer { pub key: DeviceKey, pub name: String, pub address: Addr, /// A classic WireGuard peer: **not post-quantum**. pub classic: bool, pub allowed_ips: Vec, /// A path hint (a relay or an exit) when every road to this peer names the same one. pub via: Option, pub roads: Vec, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Firewall { /// Always `deny`: only the rules below let traffic in or through. pub default: Action, pub rules: Vec, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] pub enum Action { Deny, } #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] pub enum Direction { /// To this device's own address. In, /// Through this device: to a subnet it routes, or to the internet as an exit. Forward, } /// Where a rule lets traffic go. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum Dst { Net(Net), /// Every address except the private ranges (`net::NOT_INTERNET`). Internet, } impl Serialize for Dst { fn serialize(&self, s: S) -> Result { match self { Dst::Net(n) => n.serialize(s), Dst::Internet => s.serialize_str("internet"), } } } impl<'de> Deserialize<'de> for Dst { fn deserialize>(d: D) -> Result { let s = String::deserialize(d)?; if s == "internet" { return Ok(Dst::Internet); } Net::parse_canonical(&s) .map(Dst::Net) .map_err(|_| serde::de::Error::custom(format!("`{s}` is not a network or `internet`"))) } } #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Rule { pub direction: Direction, pub src: Net, pub dst: Dst, pub proto: Proto, pub ports: Option<[u16; 2]>, pub roads: Vec, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct ServiceAddress { pub host: Hostname, pub address: Addr, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Dns { pub host: Hostname, pub address: Addr, pub roads: Vec, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Exit { pub through: DeviceKey, pub roads: Vec, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct KillSwitch { /// `None` when kill-switch roads disagree: nothing leaves but the mesh (fails closed). pub through: Option, pub roads: Vec, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Direct { /// `None`: all traffic to `subnet`; `Some`: only that app's. pub app: Option, pub subnet: Net, /// The nets inside `subnet` that this plan sends through the tunnel: they stay in it (QA F-02). A /// platform that cannot keep them in (an app-level bypass, say) must not apply this entry at all. pub except: Vec, pub roads: Vec, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Custom { pub road: RoadId, /// For a classic device (or a road that says so): not post-quantum. pub classic: bool, /// The raw block, byte for byte. **Never checked by the compiler.** pub raw: String, } impl Plan { /// Every allowed IP of every peer, with the peer. pub fn allowed_ips(&self) -> impl Iterator { self.peers .iter() .flat_map(|p| p.allowed_ips.iter().map(move |n| (&p.key, n))) } /// Pretty JSON, two spaces, fields in wire order, a final newline: the same input, the same bytes. pub fn to_json(&self) -> String { let mut s = serde_json::to_string_pretty(self).expect("a plan always serialises"); s.push('\n'); s } }