nomilo/src/dns/mod.rs

92 lines
3 KiB
Rust
Raw Normal View History

2024-12-15 20:21:03 +00:00
pub mod dns_driver;
use std::sync::Arc;
use async_trait::async_trait;
2024-12-22 21:36:26 +00:00
use crate::ressouces::record;
2024-12-15 20:21:03 +00:00
pub type BoxedZoneDriver = Arc<dyn ZoneDriver>;
2024-12-22 21:36:26 +00:00
pub type BoxedRecordDriver = Arc<dyn RecordDriver>;
2024-12-15 20:21:03 +00:00
pub enum DnsDriverError {
ConnectionError { reason: Box<dyn std::error::Error> },
OperationError { reason: Box<dyn std::error::Error> },
ServerError { rcode: String, name: String, qtype: String },
ZoneNotFound { name: String },
}
#[async_trait]
pub trait ZoneDriver: Send + Sync {
// get_zones
// add_zone
// delete_zone
async fn zone_exists(&self, zone: &str) -> Result<(), DnsDriverError>;
}
#[async_trait]
pub trait RecordDriver: Send + Sync {
async fn get_records(&self, zone: &str) -> Result<Vec<record::Record>, DnsDriverError>;
async fn add_records(&self, zone: &str, new_records: &[record::DnsRecordImpl]) -> Result<(), DnsDriverError>;
//async fn update_records(&mut self, zone: dns::Name, class: dns::DNSClass, old_records: Vec<dns::Record>, new_records: Vec<dns::Record>) -> ConnectorResult<()>;
//async fn delete_records(&mut self, zone: dns::Name, class: dns::DNSClass, records: Vec<dns::Record>) -> ConnectorResult<()>;
}
/*
2022-03-04 12:08:03 +00:00
pub mod client;
2022-03-05 12:19:31 +00:00
pub mod dns_connector;
pub mod connector;
2022-03-04 12:08:03 +00:00
2022-03-04 16:17:15 +00:00
// Reexport trust dns types for convenience
pub use trust_dns_client::rr::rdata::{
DNSSECRData, caa, sshfp, mx, null, soa, srv, txt
};
pub use trust_dns_client::rr::{
RData, DNSClass, Record
};
pub use trust_dns_proto::rr::Name;
2022-04-27 17:42:25 +00:00
pub use trust_dns_proto::rr::dnssec::rdata::tsig::TsigAlgorithm;
// Reexport module types
2022-04-22 18:43:24 +00:00
pub use connector::{RecordConnector, ZoneConnector, ConnectorError};
2022-03-05 12:19:31 +00:00
pub use dns_connector::{DnsConnectorClient, DnsConnectorError};
2022-04-22 22:57:32 +00:00
pub use client::DnsClient;
2022-04-27 17:42:25 +00:00
use rocket::{Request, State, http::Status, request::{FromRequest, Outcome}};
use rocket::outcome::try_outcome;
use crate::config::Config;
2022-04-27 19:17:39 +00:00
2022-04-27 17:42:25 +00:00
#[rocket::async_trait]
impl<'r> FromRequest<'r> for Box<dyn RecordConnector> {
type Error = ();
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
let config = try_outcome!(request.guard::<&State<Config>>().await);
match DnsClient::from_config(&config.dns).await {
Err(e) => {
error!("Failed to connect to DNS server: {}", e);
Outcome::Failure((Status::InternalServerError, ()))
},
Ok(c) => Outcome::Success(Box::new(DnsConnectorClient::new(c)))
}
}
}
#[rocket::async_trait]
impl<'r> FromRequest<'r> for Box<dyn ZoneConnector> {
type Error = ();
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
let config = try_outcome!(request.guard::<&State<Config>>().await);
match DnsClient::from_config(&config.dns).await {
Err(e) => {
error!("Failed to connect to DNS server: {}", e);
Outcome::Failure((Status::InternalServerError, ()))
},
Ok(c) => Outcome::Success(Box::new(DnsConnectorClient::new(c)))
}
}
}
2024-12-15 20:21:03 +00:00
*/