use std::borrow::Borrow;
use rings_core::delegation::DelegateeKey;
use rings_core::dht::Did;
use rings_core::domain_tag;
use rings_core::ecc::VerificationPublicKey;
use rings_core::error::Error;
use rings_core::error::Result;
use rings_core::message::DomainTag;
use rings_core::message::MessageSigner;
use rings_core::message::MessageVerification;
use rings_dns_relay::Announcement;
use rings_dns_relay::AnnouncementError;
use rings_dns_relay::ProxyProtocol;
use rings_dns_relay::SubName;
use rings_dns_relay::TxtValue;
use serde::Deserialize;
use serde::Serialize;
use crate::descriptor::latest_valid_by_did;
use crate::descriptor::sign_descriptor_body;
use crate::descriptor::RegistryElement;
use crate::descriptor::SignedDescriptor;
use crate::descriptor::SignedDescriptorBody;
const SERVICE_DESCRIPTOR_DOMAIN_TAG: DomainTag = domain_tag!("rings-node:service-descriptor");
pub fn service_topic(did: Did) -> String {
format!("dns_relay:{did}")
}
pub(crate) fn announcement_of(
ports: impl IntoIterator<Item = ServedPort>,
txt: &[ServedTxt],
) -> std::result::Result<Announcement, AnnouncementError> {
let ports = ports
.into_iter()
.map(|served| (served.port, served.proxy_protocol));
let txt = txt
.iter()
.map(|served| {
let values = served
.values
.iter()
.map(|value| TxtValue::new(value.as_str()))
.collect::<std::result::Result<Vec<_>, _>>()?;
SubName::new(served.name.as_str()).map(|name| (name, values))
})
.collect::<std::result::Result<Vec<_>, _>>()?;
Announcement::new(ports, txt)
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct ServedPort {
pub port: u16,
pub proxy_protocol: ProxyProtocol,
}
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct ServedTxt {
pub name: String,
pub values: Vec<String>,
}
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct ServiceDescriptorBody {
pub did: Did,
pub public_key: VerificationPublicKey,
pub network_id: u32,
pub ports: Vec<ServedPort>,
pub txt: Vec<ServedTxt>,
pub fresh_for_ms: u64,
pub expires_at_ms: u128,
}
impl ServiceDescriptorBody {
fn signing_data(&self) -> Result<Vec<u8>> {
rings_codec::serialize(self).map_err(Error::CodecSerialize)
}
}
impl SignedDescriptorBody for ServiceDescriptorBody {
type Descriptor = ServiceDescriptor;
const DOMAIN_TAG: DomainTag = SERVICE_DESCRIPTOR_DOMAIN_TAG;
fn body_did(&self) -> Did {
self.did
}
fn body_public_key(&self) -> &VerificationPublicKey {
&self.public_key
}
fn body_network_id(&self) -> u32 {
self.network_id
}
fn body_signing_data(&self) -> Result<Vec<u8>> {
self.signing_data()
}
fn into_signed_descriptor(self, signature: MessageVerification) -> Self::Descriptor {
ServiceDescriptor {
body: self,
signature,
heartbeat_at_ms: 0,
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct ServiceDescriptor {
pub body: ServiceDescriptorBody,
pub signature: MessageVerification,
#[serde(skip)]
pub heartbeat_at_ms: u128,
}
impl ServiceDescriptor {
pub fn new_signed(
body: ServiceDescriptorBody,
signer: MessageSigner<impl Borrow<DelegateeKey>>,
) -> Result<Self> {
sign_descriptor_body(
body,
signer,
"service descriptor DID/public key/session mismatch",
)
}
pub fn announcement(&self) -> std::result::Result<Announcement, AnnouncementError> {
announcement_of(self.body.ports.iter().copied(), &self.body.txt)
}
pub fn latest_valid_for(
descriptors: impl IntoIterator<Item = Self>,
did: Did,
now_ms: u128,
network_id: u32,
) -> Option<Self> {
latest_valid_by_did(
descriptors
.into_iter()
.filter(|descriptor| descriptor.body.did == did),
now_ms,
network_id,
false,
)
.into_iter()
.next()
}
}
impl SignedDescriptor for ServiceDescriptor {
type Body = ServiceDescriptorBody;
fn descriptor_did(&self) -> Did {
self.body.did
}
fn descriptor_public_key(&self) -> &VerificationPublicKey {
&self.body.public_key
}
fn descriptor_network_id(&self) -> u32 {
self.body.network_id
}
fn descriptor_signature(&self) -> &MessageVerification {
&self.signature
}
fn descriptor_heartbeat_at_ms(&self) -> u128 {
self.heartbeat_at_ms
}
fn descriptor_expires_at_ms(&self) -> u128 {
self.body.expires_at_ms
}
fn descriptor_fresh_for_ms(&self) -> u64 {
self.body.fresh_for_ms
}
fn descriptor_signing_data(&self) -> Result<Vec<u8>> {
self.body.signing_data()
}
fn heard_at(self, heartbeat_at_ms: u128) -> Self {
Self {
heartbeat_at_ms,
..self
}
}
}
impl RegistryElement for ServiceDescriptor {}