rings_node/dns_relay/
descriptor.rs1use std::borrow::Borrow;
18
19use rings_core::delegation::DelegateeKey;
20use rings_core::dht::Did;
21use rings_core::domain_tag;
22use rings_core::ecc::VerificationPublicKey;
23use rings_core::error::Error;
24use rings_core::error::Result;
25use rings_core::message::DomainTag;
26use rings_core::message::MessageSigner;
27use rings_core::message::MessageVerification;
28use rings_dns_relay::Announcement;
29use rings_dns_relay::AnnouncementError;
30use rings_dns_relay::ProxyProtocol;
31use rings_dns_relay::SubName;
32use rings_dns_relay::TxtValue;
33use serde::Deserialize;
34use serde::Serialize;
35
36use crate::descriptor::latest_valid_by_did;
37use crate::descriptor::sign_descriptor_body;
38use crate::descriptor::RegistryElement;
39use crate::descriptor::SignedDescriptor;
40use crate::descriptor::SignedDescriptorBody;
41
42const SERVICE_DESCRIPTOR_DOMAIN_TAG: DomainTag = domain_tag!("rings-node:service-descriptor");
44
45pub fn service_topic(did: Did) -> String {
50 format!("dns_relay:{did}")
51}
52
53pub(crate) fn announcement_of(
55 ports: impl IntoIterator<Item = ServedPort>,
56 txt: &[ServedTxt],
57) -> std::result::Result<Announcement, AnnouncementError> {
58 let ports = ports
59 .into_iter()
60 .map(|served| (served.port, served.proxy_protocol));
61 let txt = txt
62 .iter()
63 .map(|served| {
64 let values = served
65 .values
66 .iter()
67 .map(|value| TxtValue::new(value.as_str()))
68 .collect::<std::result::Result<Vec<_>, _>>()?;
69 SubName::new(served.name.as_str()).map(|name| (name, values))
70 })
71 .collect::<std::result::Result<Vec<_>, _>>()?;
72 Announcement::new(ports, txt)
73}
74
75#[derive(Clone, Copy, Debug, Deserialize, Serialize, Eq, PartialEq)]
77pub struct ServedPort {
78 pub port: u16,
80 pub proxy_protocol: ProxyProtocol,
82}
83
84#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
86pub struct ServedTxt {
87 pub name: String,
89 pub values: Vec<String>,
91}
92
93#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
95pub struct ServiceDescriptorBody {
96 pub did: Did,
98 pub public_key: VerificationPublicKey,
100 pub network_id: u32,
102 pub ports: Vec<ServedPort>,
104 pub txt: Vec<ServedTxt>,
106 pub fresh_for_ms: u64,
109 pub expires_at_ms: u128,
111}
112
113impl ServiceDescriptorBody {
114 fn signing_data(&self) -> Result<Vec<u8>> {
116 rings_codec::serialize(self).map_err(Error::CodecSerialize)
117 }
118}
119
120impl SignedDescriptorBody for ServiceDescriptorBody {
121 type Descriptor = ServiceDescriptor;
122
123 const DOMAIN_TAG: DomainTag = SERVICE_DESCRIPTOR_DOMAIN_TAG;
124
125 fn body_did(&self) -> Did {
126 self.did
127 }
128
129 fn body_public_key(&self) -> &VerificationPublicKey {
130 &self.public_key
131 }
132
133 fn body_network_id(&self) -> u32 {
134 self.network_id
135 }
136
137 fn body_signing_data(&self) -> Result<Vec<u8>> {
138 self.signing_data()
139 }
140
141 fn into_signed_descriptor(self, signature: MessageVerification) -> Self::Descriptor {
142 ServiceDescriptor {
143 body: self,
144 signature,
145 heartbeat_at_ms: 0,
146 }
147 }
148}
149
150#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
152pub struct ServiceDescriptor {
153 pub body: ServiceDescriptorBody,
155 pub signature: MessageVerification,
157 #[serde(skip)]
161 pub heartbeat_at_ms: u128,
162}
163
164impl ServiceDescriptor {
165 pub fn new_signed(
167 body: ServiceDescriptorBody,
168 signer: MessageSigner<impl Borrow<DelegateeKey>>,
169 ) -> Result<Self> {
170 sign_descriptor_body(
171 body,
172 signer,
173 "service descriptor DID/public key/session mismatch",
174 )
175 }
176
177 pub fn announcement(&self) -> std::result::Result<Announcement, AnnouncementError> {
179 announcement_of(self.body.ports.iter().copied(), &self.body.txt)
180 }
181
182 pub fn latest_valid_for(
185 descriptors: impl IntoIterator<Item = Self>,
186 did: Did,
187 now_ms: u128,
188 network_id: u32,
189 ) -> Option<Self> {
190 latest_valid_by_did(
191 descriptors
192 .into_iter()
193 .filter(|descriptor| descriptor.body.did == did),
194 now_ms,
195 network_id,
196 false,
197 )
198 .into_iter()
199 .next()
200 }
201}
202
203impl SignedDescriptor for ServiceDescriptor {
204 type Body = ServiceDescriptorBody;
205
206 fn descriptor_did(&self) -> Did {
207 self.body.did
208 }
209
210 fn descriptor_public_key(&self) -> &VerificationPublicKey {
211 &self.body.public_key
212 }
213
214 fn descriptor_network_id(&self) -> u32 {
215 self.body.network_id
216 }
217
218 fn descriptor_signature(&self) -> &MessageVerification {
219 &self.signature
220 }
221
222 fn descriptor_heartbeat_at_ms(&self) -> u128 {
223 self.heartbeat_at_ms
224 }
225
226 fn descriptor_expires_at_ms(&self) -> u128 {
227 self.body.expires_at_ms
228 }
229
230 fn descriptor_fresh_for_ms(&self) -> u64 {
231 self.body.fresh_for_ms
232 }
233
234 fn descriptor_signing_data(&self) -> Result<Vec<u8>> {
235 self.body.signing_data()
236 }
237
238 fn heard_at(self, heartbeat_at_ms: u128) -> Self {
239 Self {
240 heartbeat_at_ms,
241 ..self
242 }
243 }
244}
245
246impl RegistryElement for ServiceDescriptor {}