Skip to content

Commit e6c72ec

Browse files
committed
fix: wip
1 parent 879ecec commit e6c72ec

File tree

3 files changed

+72
-20
lines changed

3 files changed

+72
-20
lines changed

src/config/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use anyhow::Result;
22
use serde::Deserialize;
3-
use std::collections::HashMap;
43
use std::fs::File;
54
use std::io::Read;
65
use tracing::{debug, info, info_span};
76

87
pub use listener::{Listener, ProxyProtocol};
8+
pub use records::PreconfiguredRecords;
99

1010
mod listener;
1111
mod records;
@@ -35,7 +35,7 @@ pub struct Config {
3535
pub general: General,
3636
pub api: Api,
3737
#[serde(default)]
38-
pub records: HashMap<String, Vec<Vec<String>>>,
38+
pub records: PreconfiguredRecords,
3939
}
4040

4141
const DEFAULT_CONFIG_PATH: &str = "config.toml";

src/config/records.rs

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ use serde::de::{DeserializeSeed, Error as DeError, MapAccess, SeqAccess, Visitor
22
use serde::{Deserialize, Deserializer};
33
use std::collections::HashMap;
44
use std::fmt::Formatter;
5+
use std::ops::Deref;
56
use std::str::FromStr;
67
use std::sync::Arc;
7-
use trust_dns_server::proto::rr::{Name, RecordSet, RecordType};
8+
use trust_dns_server::proto::rr::{Name, RecordSet, RecordType, RData};
89

9-
#[derive(Default)]
10-
struct PreconfiguredRecords(HashMap<Name, HashMap<&str, Arc<RecordSet>>>);
10+
#[derive(Debug, Default)]
11+
pub struct PreconfiguredRecords(HashMap<Name, HashMap<RecordType, Arc<RecordSet>>>);
12+
13+
impl Deref for PreconfiguredRecords {
14+
type Target = HashMap<Name, HashMap<RecordType, Arc<RecordSet>>>;
15+
16+
fn deref(&self) -> &Self::Target {
17+
&self.0
18+
}
19+
}
1120

1221
impl<'de> Deserialize<'de> for PreconfiguredRecords {
1322
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
@@ -50,40 +59,85 @@ impl<'de> Deserialize<'de> for PreconfiguredRecords {
5059
struct RecordDataSeed(Name);
5160

5261
impl<'de> DeserializeSeed<'de> for RecordDataSeed {
53-
type Value = (RecordType, Arc<RecordSet>);
62+
type Value = (Name, HashMap<RecordType, Arc<RecordSet>>);
5463

5564
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
5665
where
5766
D: Deserializer<'de>,
5867
{
5968
struct RecordDataVisitor(Name);
6069
impl<'de> Visitor<'de> for RecordDataVisitor {
61-
type Value = (RecordType, Arc<RecordSet>);
70+
type Value = (Name, HashMap<RecordType, Arc<RecordSet>>);
6271

6372
fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
6473
formatter.write_str("RecordData")
6574
}
6675

76+
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
77+
where
78+
A: MapAccess<'de>,
79+
{
80+
let name = self.0;
81+
let mut res = HashMap::new();
82+
while let Some(record_type) = map.next_key::<&str>()? {
83+
let record_type = match record_type {
84+
"TXT" => RecordType::TXT,
85+
"A" => RecordType::A,
86+
"CNAME" => RecordType::CNAME,
87+
_ => return Err(DeError::custom("Could not find RecordType")),
88+
};
89+
90+
let record_set = map.next_value_seed(RecordSeed(name.clone(), record_type))?;
91+
92+
res.insert(record_type, record_set);
93+
}
94+
95+
Ok((name, res))
96+
}
97+
}
98+
99+
deserializer.deserialize_map(RecordDataVisitor(self.0))
100+
}
101+
}
102+
103+
struct RecordSeed(Name, RecordType);
104+
105+
impl<'de> DeserializeSeed<'de> for RecordSeed {
106+
type Value = Arc<RecordSet>;
107+
108+
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
109+
where
110+
D: Deserializer<'de>,
111+
{
112+
struct RecordVisitor(Name, RecordType);
113+
impl<'de> Visitor<'de> for RecordVisitor {
114+
type Value = Arc<RecordSet>;
115+
116+
fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
117+
formatter.write_str("Record")
118+
}
119+
67120
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
68121
where
69122
A: SeqAccess<'de>,
70123
{
71-
let record_type = match seq.next_element::<&str>()? {
72-
Some(record_type) => record_type,
73-
None => return Err(DeError::custom("Could not find RecordType")),
74-
};
75-
76124
let ttl = match seq.next_element::<u32>()? {
77125
Some(ttl) => ttl,
78126
None => return Err(DeError::custom("Could not find TTL")),
79127
};
80128

81-
let record_set = Arc::new(RecordSet::with_ttl(self.0, record_type, ttl));
129+
// todo: finish this code
130+
let mut record_set = RecordSet::with_ttl(self.0, self.1, ttl);
131+
while let Some(data) = seq.next_element::<&str>()? {
132+
match self.1 {
133+
_ => return Err(DeError::custom("Invalid key"))
134+
}
135+
}
82136

83-
Ok((record_type, record_set))
137+
Ok(Arc::new(record_set))
84138
}
85139
}
86140

87-
deserializer.deserialize_seq(RecordDataVisitor(self.0))
141+
deserializer.deserialize_seq(RecordVisitor(self.0, self.1))
88142
}
89143
}

src/dns/authority.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use anyhow::{anyhow, Result};
22
use futures_util::TryFutureExt;
33
use sqlx::PgPool;
4-
use std::collections::HashMap;
54
use std::net::IpAddr::V4;
65
use std::str::FromStr;
76
use std::sync::Arc;
@@ -18,30 +17,29 @@ use trust_dns_server::proto::rr::rdata::{SOA, TXT};
1817
use trust_dns_server::proto::rr::record_data::RData;
1918
use trust_dns_server::proto::rr::{Record, RecordSet, RecordType};
2019

21-
use super::parse::parse;
2220
use crate::cert::CertFacade;
2321
use crate::domain::{Domain, DomainFacade};
2422
use crate::util::error;
23+
use crate::config::PreconfiguredRecords;
2524

2625
pub struct DatabaseAuthority(Arc<DatabaseAuthorityInner>);
2726

2827
struct DatabaseAuthorityInner {
2928
lower: LowerName,
3029
pool: PgPool,
31-
records: HashMap<Name, HashMap<RecordType, Arc<RecordSet>>>,
30+
records: PreconfiguredRecords,
3231
supported_algorithms: SupportedAlgorithms,
3332
}
3433

3534
impl DatabaseAuthority {
3635
pub fn new(
3736
pool: PgPool,
3837
name: &str,
39-
records: HashMap<String, Vec<Vec<String>>>,
38+
records: PreconfiguredRecords,
4039
) -> Box<DatabaseAuthority> {
4140
// todo: remove unwrap
4241
let lower = LowerName::from(Name::from_str(name).unwrap());
4342
// todo: remove unwrap
44-
let records = parse(records).unwrap();
4543

4644
let inner = DatabaseAuthorityInner {
4745
lower,

0 commit comments

Comments
 (0)