Skip to main content

gent_daemon/config/
transport.rs

1//! Native transport settings. External Iroh relays always require an explicit URL.
2use super::*;
3use std::net::SocketAddr;
4#[derive(Clone, Debug, Default, Serialize, Deserialize)]
5#[serde(rename_all = "snake_case")]
6pub enum Direct {
7    #[default]
8    Auto,
9    Off,
10}
11#[derive(Clone, Debug, Serialize, Deserialize)]
12#[serde(default, deny_unknown_fields)]
13pub struct Transport {
14    pub direct: Direct,
15    pub relays: Vec<String>,
16    /// Permit IP-based QUIC paths. TCP-only peers can still use a cluster helper.
17    pub udp: bool,
18    /// Serve a helper for enrolled peers. Clients can use another node's helper in either mode.
19    pub helper: Direct,
20    pub helper_port: Option<u16>,
21    pub helper_public_address: Option<SocketAddr>,
22}
23impl Default for Transport {
24    fn default() -> Self {
25        Self {
26            direct: Direct::Auto,
27            relays: Vec::new(),
28            udp: true,
29            helper: Direct::Auto,
30            helper_port: None,
31            helper_public_address: None,
32        }
33    }
34}
35impl Config {
36    pub fn transport(&self) -> Result<Transport> {
37        let value: Transport = self
38            .extra
39            .get("transport")
40            .cloned()
41            .map(toml::Value::try_into)
42            .transpose()?
43            .unwrap_or_default();
44        ensure!(value.relays.len() <= 4, "too many direct transport relays");
45        if let Some(address) = value.helper_public_address {
46            ensure!(
47                matches!(address, SocketAddr::V4(addr) if addr.port() != 0
48                    && !addr.ip().is_unspecified() && !addr.ip().is_multicast()
49                    && !addr.ip().is_broadcast()),
50                "expected a usable IPv4 address and port for the helper"
51            );
52        }
53        for relay in &value.relays {
54            let url = reqwest::Url::parse(relay)?;
55            ensure!(
56                url.scheme() == "https"
57                    && url.host_str().is_some()
58                    && url.username().is_empty()
59                    && url.password().is_none()
60                    && url.query().is_none()
61                    && url.fragment().is_none(),
62                "invalid direct transport relay"
63            );
64        }
65        Ok(value)
66    }
67}