From 65caaca352477d24e1c70ef5f1150f6aed915b04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Severin=20Alexander=20B=C3=BChler?= <8782386+SeverinAlexB@users.noreply.github.com> Date: Mon, 16 Dec 2024 16:33:41 +0200 Subject: [PATCH] feat: use $ttl as a variable in the zonefile (#48) --- .gitignore | 3 +++ .vscode/launch.json | 57 ----------------------------------------- cli/sample/pkarr.zone | 3 ++- cli/src/cli.rs | 4 +-- cli/src/pkarr_packet.rs | 9 ++++++- cli/src/simple_zone.rs | 27 +++++++++---------- 6 files changed, 28 insertions(+), 75 deletions(-) delete mode 100644 .vscode/launch.json diff --git a/.gitignore b/.gitignore index 0434217..e95d99f 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,6 @@ target/ # Mac pollution .DS_STORE + + +.vscode diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 15fd12d..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,57 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "type": "lldb", - "request": "launch", - "name": "Dns Server", - "cargo": { - "args": [ - "build", - "--bin=pkdns", - "--package=pkdns" - ], - "filter": { - "name": "pkdns", - "kind": "bin" - } - }, - "args": [ - "--ip-rate-limit-dht", "1", - "--verbose", - ], - "cwd": "${workspaceFolder}", - "env": { - // "RUST_LOG": "debug" - // "RUST_LOG": "pkdns=debug,any_dns=debug,pkarr=debug,mainline=debug" - } - }, - { - "type": "lldb", - "request": "launch", - "name": "CLI Publish", - "cargo": { - "args": [ - "build", - "--bin=pkdns-cli", - "--package=pkdns-cli" - ], - "filter": { - "name": "pkdns-cli", - "kind": "bin" - } - }, - "args": [ - "publish", "./cli/sample/seed.txt", "./cli/sample/pkarr.zone", "--once" - ], - "cwd": "${workspaceFolder}", - "env": { - // "RUST_LOG": "debug" - // "RUST_LOG": "pkdns=debug,any_dns=debug,pkarr=debug,mainline=debug" - } - }, - ] -} \ No newline at end of file diff --git a/cli/sample/pkarr.zone b/cli/sample/pkarr.zone index 4bf8b7e..aabf24b 100644 --- a/cli/sample/pkarr.zone +++ b/cli/sample/pkarr.zone @@ -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. diff --git a/cli/src/cli.rs b/cli/src/cli.rs index 75fbb71..9c0b1db 100644 --- a/cli/src/cli.rs +++ b/cli/src/cli.rs @@ -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"), ) @@ -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") diff --git a/cli/src/pkarr_packet.rs b/cli/src/pkarr_packet.rs index 996d6e4..484f98f 100644 --- a/cli/src/pkarr_packet.rs +++ b/cli/src/pkarr_packet.rs @@ -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(); }; @@ -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) => { @@ -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) } } \ No newline at end of file diff --git a/cli/src/simple_zone.rs b/cli/src/simple_zone.rs index 1eb0d89..e3e0e14 100644 --- a/cli/src/simple_zone.rs +++ b/cli/src/simple_zone.rs @@ -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 } @@ -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())?; @@ -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()? @@ -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()? @@ -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()? @@ -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()? @@ -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()? @@ -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()? @@ -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