rings_node/dns_relay/
config.rs1use std::collections::BTreeMap;
23use std::net::SocketAddr;
24use std::time::Duration;
25
26use rings_dns_relay::ProxyProtocol;
27use serde::Deserialize;
28use serde::Serialize;
29
30use super::descriptor::ServedTxt;
31use super::registration::ServiceOffer;
32use crate::error::Error;
33use crate::error::Result;
34use crate::registration::validate_schedule;
35use crate::registration::TimingKeys;
36
37const DEFAULT_SERVICES_HEARTBEAT_INTERVAL_SECS: u64 = 30;
39const DEFAULT_SERVICES_TTL_SECS: u64 = 90;
41
42#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
44#[serde(deny_unknown_fields)]
45pub struct ServedPortConfig {
46 pub port: u16,
48 pub target: SocketAddr,
50 #[serde(default)]
52 pub proxy_protocol: ProxyProtocol,
53}
54
55#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
57#[serde(deny_unknown_fields)]
58pub struct ServedTxtConfig {
59 #[serde(default)]
61 pub name: String,
62 pub values: Vec<String>,
64}
65
66#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
68#[serde(deny_unknown_fields)]
69pub struct ServicesConfig {
70 #[serde(default)]
72 pub ports: Vec<ServedPortConfig>,
73 #[serde(default)]
75 pub txt: Vec<ServedTxtConfig>,
76 #[serde(default = "default_services_heartbeat_interval_secs")]
78 pub heartbeat_interval_secs: u64,
79 #[serde(default = "default_services_ttl_secs")]
81 pub ttl_secs: u64,
82}
83
84impl ServicesConfig {
85 pub fn offer(&self) -> Result<ServiceOffer> {
90 let heartbeat_interval = Duration::from_secs(self.heartbeat_interval_secs);
91 let ttl = Duration::from_secs(self.ttl_secs);
92 let keys = TimingKeys {
93 heartbeat_interval: "services.heartbeat_interval_secs",
94 ttl: "services.ttl_secs",
95 enabled_by: None,
96 };
97 validate_schedule(&keys, heartbeat_interval, ttl)?;
98 let mut ports = BTreeMap::new();
99 for served in &self.ports {
100 if ports.insert(served.port, served.proxy_protocol).is_some() {
101 return Err(Error::InvalidConfig(format!(
102 "services: port {} is served twice",
103 served.port
104 )));
105 }
106 }
107 let txt = self
108 .txt
109 .iter()
110 .map(|served| ServedTxt {
111 name: served.name.clone(),
112 values: served.values.clone(),
113 })
114 .collect();
115 ServiceOffer::new(ports, txt, heartbeat_interval, ttl)
116 .map_err(|error| Error::InvalidConfig(format!("services: {error}")))
117 }
118
119 pub fn targets(&self) -> impl Iterator<Item = (u16, SocketAddr)> + '_ {
121 self.ports.iter().map(|served| (served.port, served.target))
122 }
123}
124
125const fn default_services_heartbeat_interval_secs() -> u64 {
127 DEFAULT_SERVICES_HEARTBEAT_INTERVAL_SECS
128}
129
130const fn default_services_ttl_secs() -> u64 {
132 DEFAULT_SERVICES_TTL_SECS
133}