Skip to content

Commit

Permalink
feat: use $ttl as a variable in the zonefile (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
SeverinAlexB authored Dec 16, 2024
1 parent 18629cc commit 65caaca
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 75 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ target/

# Mac pollution
.DS_STORE


.vscode
57 changes: 0 additions & 57 deletions .vscode/launch.json

This file was deleted.

3 changes: 2 additions & 1 deletion cli/sample/pkarr.zone
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@ IN NS dns1.example.com.
$TTL 300
@ IN NS dns1.example.com.

@ IN MX 10 mail.example.com.
@ IN MX 20 mail2.example.com.
Expand Down
4 changes: 1 addition & 3 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ pub async fn run_cli() {
.about("Publish pkarr dns records.")
.arg(
clap::Arg::new("seed")
.required(true)
.help("File path to the pkarr seed file.")
.default_value("./seed.txt"),
)
.arg(
clap::Arg::new("zonefile")
.required(true)
.help("File path to the dns zone file.")
.default_value("./pkarr.zone"),
)
Expand All @@ -29,7 +27,7 @@ pub async fn run_cli() {
.required(false)
.num_args(0)
.help("File path to the dns records csv file."),
),
)
)
.subcommand(
clap::Command::new("resolve")
Expand Down
9 changes: 8 additions & 1 deletion cli/src/pkarr_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl fmt::Display for PkarrPacket {
};
let records = self.to_records();
write!(f, "Packet {}\n", records.get(0).unwrap().pubkey()).unwrap();
write!(f, "{0: <20} {1: <7} {2: <6} {3: <25}{4:}", "Name", "TTL", "Type", "Data", "\n").unwrap();
for record in self.to_records() {
write!(f, "{record}\n").unwrap();
};
Expand Down Expand Up @@ -116,6 +117,11 @@ impl PkarrRecord {
}
}

pub fn ttl(&self) -> u32 {
let rr = self.get_resource_record();
rr.ttl
}

pub fn data_as_strings(&self) -> (&str, String) {
let (record_type, data) = match self.get_resource_record().rdata {
RData::A(a) => {
Expand Down Expand Up @@ -157,7 +163,8 @@ impl PkarrRecord {
impl fmt::Display for PkarrRecord {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let name = self.name();
let ttl = self.ttl();
let data = self.data_as_strings();
write!(f, "{0: <20} {1: <6} {2: <10}", name, data.0, data.1)
write!(f, "{0: <20} {1: <7} {2: <6} {3: <25}", name, ttl, data.0, data.1)
}
}
27 changes: 14 additions & 13 deletions cli/src/simple_zone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ impl SimpleZone {
*/
fn generate_soa(pubkey: &str) -> String {
let formatted = format!("$ORIGIN {pubkey}.
$TTL 86400
$TTL 300
@ IN SOA 127.0.0.1. hostmaster.example.com. (
2001062501 ; serial
21600 ; refresh after 6 hours
3600 ; retry after 1 hour
604800 ; expire after 1 week
86400 ) ; minimum TTL of 1 day
300 ) ; minimum TTL of 1 day
");
formatted
}
Expand Down Expand Up @@ -80,6 +80,7 @@ $TTL 86400
let packet = match entry {
Entry::Include { path, origin } => continue,
Entry::Record(val) => {
let ttl = val.ttl().as_secs();
let (name, data) = val.clone().into_owner_and_data();
let simple_name_str = name.to_string();
let simple_name = Name::try_from(simple_name_str.as_str())?;
Expand All @@ -90,7 +91,7 @@ $TTL 86400
address: val.addr().into()
}
);
let rr = ResourceRecord::new(simple_name, pkarr::dns::CLASS::IN, 60*60, rdata);
let rr = ResourceRecord::new(simple_name, pkarr::dns::CLASS::IN, ttl, rdata);
let mut packet = pkarr::dns::Packet::new_reply(0);
packet.answers.push(rr);
packet.build_bytes_vec_compressed()?
Expand All @@ -101,7 +102,7 @@ $TTL 86400
address: val.addr().into()
}
);
let rr = ResourceRecord::new(simple_name, pkarr::dns::CLASS::IN, 60*60, rdata);
let rr = ResourceRecord::new(simple_name, pkarr::dns::CLASS::IN, ttl, rdata);
let mut packet = pkarr::dns::Packet::new_reply(0);
packet.answers.push(rr);
packet.build_bytes_vec_compressed()?
Expand All @@ -112,7 +113,7 @@ $TTL 86400
pkarr::dns::rdata::NS(Name::try_from(ns_name.as_str())?)
);

let rr = ResourceRecord::new(simple_name, pkarr::dns::CLASS::IN, 60*60, rdata);
let rr = ResourceRecord::new(simple_name, pkarr::dns::CLASS::IN, ttl, rdata);
let mut packet = pkarr::dns::Packet::new_reply(0);
packet.answers.push(rr);
packet.build_bytes_vec_compressed()?
Expand All @@ -126,7 +127,7 @@ $TTL 86400
txt
);

let rr = ResourceRecord::new(simple_name, pkarr::dns::CLASS::IN, 60*60, rdata);
let rr = ResourceRecord::new(simple_name, pkarr::dns::CLASS::IN, ttl, rdata);
let mut packet = pkarr::dns::Packet::new_reply(0);
packet.answers.push(rr);
packet.build_bytes_vec_compressed()?
Expand All @@ -142,7 +143,7 @@ $TTL 86400
mx
);

let rr = ResourceRecord::new(simple_name, pkarr::dns::CLASS::IN, 60*60, rdata);
let rr = ResourceRecord::new(simple_name, pkarr::dns::CLASS::IN, ttl, rdata);
let mut packet = pkarr::dns::Packet::new_reply(0);
packet.answers.push(rr);
packet.build_bytes_vec_compressed()?
Expand All @@ -153,7 +154,7 @@ $TTL 86400
let rdata: pkarr::dns::rdata::RData = pkarr::dns::rdata::RData::CNAME(
pkarr::dns::rdata::CNAME(value)
);
let rr = ResourceRecord::new(simple_name, pkarr::dns::CLASS::IN, 60*60, rdata);
let rr = ResourceRecord::new(simple_name, pkarr::dns::CLASS::IN, ttl, rdata);
let mut packet = pkarr::dns::Packet::new_reply(0);
packet.answers.push(rr);
packet.build_bytes_vec_compressed()?
Expand Down Expand Up @@ -189,16 +190,16 @@ mod tests {

fn simplified_zone() -> String {
String::from(
"
@ IN NS dns1.example.com.
@ IN NS dns2.example.com.
"
@ IN NS dns1.example.com.
@ 400 IN NS dns2.example.com.
@ IN MX 10 mail.example.com.
@ 301 IN MX 10 mail.example.com.
@ IN MX 20 mail2.example.com.
@ IN A 127.0.0.1
test IN A 127.0.0.1
test IN A 127.0.0.1
dns1 IN A 10.0.1.1
Expand Down

0 comments on commit 65caaca

Please sign in to comment.