Skip to main content

rings_node/dns_relay/
mod.rs

1//! The DNS relay on the node (#1049): the services a node announces at `<did>.<zone>`, and the
2//! relay a node may run for its zones.
3//!
4//! The relay itself, its DNS authority, address map and ingress, is the overlay-free crate
5//! `rings_dns_relay`. This module is what ties it to Rings:
6//!
7//! - `descriptor`: the signed service descriptor a node publishes in the DHT, on the topic of its
8//!   own DID, built with the node's signed-descriptor machinery.
9//! - `registration`: the heartbeat that publishes it, one of the node's registration tasks.
10//! - `directory`: the relay's [`Directory`](rings_dns_relay::Directory): an owner's latest valid
11//!   descriptor, read from the DHT.
12//! - `forward`: the relay's [`Forward`](rings_dns_relay::Forward): the TCP relay to the owner's
13//!   node, which dials only the services it registered, a PROXY protocol header first where the
14//!   owner asks for one.
15//! - `config`: the node's `services` section.
16//! - `open`: what `rings run` starts: the served ports, and the relay the node runs.
17//!
18//! ```text
19//!   node d:   services ─▶ register tcp relay services "dns-relay/<p>" ─▶ publish σ(d) every I
20//!   relay:    DNS  label(d).z ─▶ σ(d) from the DHT ─▶ AAAA addr(d), MX, TXT
21//!             TCP  (addr(d), p) ─▶ tcp relay ─▶ d's "dns-relay/<p>" ─▶ d's local target
22//! ```
23
24mod config;
25mod descriptor;
26mod directory;
27mod forward;
28mod open;
29mod registration;
30
31use std::fmt;
32
33pub use config::ServedPortConfig;
34pub use config::ServedTxtConfig;
35pub use config::ServicesConfig;
36pub use descriptor::service_topic;
37pub use descriptor::ServedPort;
38pub use descriptor::ServedTxt;
39pub use descriptor::ServiceDescriptor;
40pub use descriptor::ServiceDescriptorBody;
41pub use directory::DhtDirectory;
42pub use directory::DirectoryError;
43pub use forward::ForwardError;
44pub use forward::RelayForward;
45pub use open::open;
46pub use open::NodeDnsRelay;
47pub use open::OpenError;
48pub use registration::ServiceOffer;
49pub(crate) use registration::ServiceRegistration;
50pub use rings_dns_relay::DnsRelay;
51pub use rings_dns_relay::DnsRelayConfig;
52use rings_dns_relay::Owner;
53
54use crate::prelude::rings_core::dht::Did;
55
56/// A DID as the owner of a relayed name: its label is the DID's canonical text, `0x` and 40
57/// lowercase hex digits.
58///
59/// ```text
60///   label(DidOwner(d)) = d.to_string()
61///   from_label(l) = Some(DidOwner(d))  ⇔  l parses as d ∧ d.to_string() = l
62/// ```
63///
64/// The second law makes the parse canonical: a label without `0x`, or with fewer digits, names
65/// no owner, so a DID has one name per zone.
66#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
67pub struct DidOwner(pub Did);
68
69impl Owner for DidOwner {
70    fn from_label(label: &str) -> Option<Self> {
71        let did = label.parse::<Did>().ok()?;
72        (did.to_string() == label).then_some(Self(did))
73    }
74
75    fn label(&self) -> String {
76        self.0.to_string()
77    }
78}
79
80impl fmt::Display for DidOwner {
81    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82        self.0.fmt(f)
83    }
84}
85
86/// The TCP relay service a node serves `port` under: what the relay's ingress asks the node to
87/// dial for a connection to that port of its address.
88///
89/// Law: injective in `port`, and disjoint from every service name an operator registers that
90/// does not start with `dns-relay/`.
91pub fn relay_service(port: u16) -> String {
92    format!("dns-relay/{port}")
93}
94
95#[cfg(test)]
96mod test_dns_relay;