From c9e9265c3eea93cd6006546a70f3552a41718944 Mon Sep 17 00:00:00 2001 From: Craig Morten Date: Sun, 15 May 2022 16:42:02 +0100 Subject: [PATCH] feat(ext/net): support NAPTR records in Deno.resolveDns() API (#14613) --- cli/dts/lib.deno.ns.d.ts | 26 +++++++++++++++- cli/tests/testdata/resolve_dns.ts | 29 ++++++++++-------- cli/tests/testdata/resolve_dns.ts.out | 2 ++ cli/tests/testdata/resolve_dns.zone.in | 4 ++- ext/net/ops.rs | 41 ++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 14 deletions(-) diff --git a/cli/dts/lib.deno.ns.d.ts b/cli/dts/lib.deno.ns.d.ts index 8ed0ab83e7..3777aa56bd 100644 --- a/cli/dts/lib.deno.ns.d.ts +++ b/cli/dts/lib.deno.ns.d.ts @@ -2955,6 +2955,7 @@ declare namespace Deno { | "ANAME" | "CNAME" | "MX" + | "NAPTR" | "NS" | "PTR" | "SOA" @@ -2979,6 +2980,16 @@ declare namespace Deno { exchange: string; } + /** If `resolveDns` is called with "NAPTR" record type specified, it will return an array of this interface. */ + export interface NAPTRRecord { + order: number; + preference: number; + flags: string; + services: string; + regexp: string; + replacement: string; + } + /** If `resolveDns` is called with "SOA" record type specified, it will return an array of this interface. */ export interface SOARecord { mname: string; @@ -3010,6 +3021,12 @@ declare namespace Deno { options?: ResolveDnsOptions, ): Promise; + export function resolveDns( + query: string, + recordType: "NAPTR", + options?: ResolveDnsOptions, + ): Promise; + export function resolveDns( query: string, recordType: "SOA", @@ -3049,5 +3066,12 @@ declare namespace Deno { query: string, recordType: RecordType, options?: ResolveDnsOptions, - ): Promise; + ): Promise< + | string[] + | MXRecord[] + | NAPTRRecord[] + | SOARecord[] + | SRVRecord[] + | string[][] + >; } diff --git a/cli/tests/testdata/resolve_dns.ts b/cli/tests/testdata/resolve_dns.ts index 1e0fa08b88..317828a5e5 100644 --- a/cli/tests/testdata/resolve_dns.ts +++ b/cli/tests/testdata/resolve_dns.ts @@ -1,17 +1,19 @@ const nameServer = { nameServer: { ipAddr: "127.0.0.1", port: 4553 } }; -const [a, aaaa, aname, cname, mx, ns, ptr, soa, srv, txt] = await Promise.all([ - Deno.resolveDns("www.example.com", "A", nameServer), - Deno.resolveDns("www.example.com", "AAAA", nameServer), - Deno.resolveDns("www.example.com", "ANAME", nameServer), - Deno.resolveDns("alias.example.com", "CNAME", nameServer), - Deno.resolveDns("example.com", "MX", nameServer), - Deno.resolveDns("example.com", "NS", nameServer), - Deno.resolveDns("1.2.3.4.IN-ADDR.ARPA.", "PTR", nameServer), - Deno.resolveDns("example.com", "SOA", nameServer), - Deno.resolveDns("_service._tcp.example.com", "SRV", nameServer), - Deno.resolveDns("example.com", "TXT", nameServer), -]); +const [a, aaaa, aname, cname, mx, naptr, ns, ptr, soa, srv, txt] = await Promise + .all([ + Deno.resolveDns("www.example.com", "A", nameServer), + Deno.resolveDns("www.example.com", "AAAA", nameServer), + Deno.resolveDns("www.example.com", "ANAME", nameServer), + Deno.resolveDns("alias.example.com", "CNAME", nameServer), + Deno.resolveDns("example.com", "MX", nameServer), + Deno.resolveDns("example.com", "NAPTR", nameServer), + Deno.resolveDns("example.com", "NS", nameServer), + Deno.resolveDns("1.2.3.4.IN-ADDR.ARPA.", "PTR", nameServer), + Deno.resolveDns("example.com", "SOA", nameServer), + Deno.resolveDns("_service._tcp.example.com", "SRV", nameServer), + Deno.resolveDns("example.com", "TXT", nameServer), + ]); console.log("A"); console.log(JSON.stringify(a)); @@ -28,6 +30,9 @@ console.log(JSON.stringify(cname)); console.log("MX"); console.log(JSON.stringify(mx)); +console.log("NAPTR"); +console.log(JSON.stringify(naptr)); + console.log("NS"); console.log(JSON.stringify(ns)); diff --git a/cli/tests/testdata/resolve_dns.ts.out b/cli/tests/testdata/resolve_dns.ts.out index 2a6de755cf..2f4c87c4c9 100644 --- a/cli/tests/testdata/resolve_dns.ts.out +++ b/cli/tests/testdata/resolve_dns.ts.out @@ -8,6 +8,8 @@ CNAME ["cname.example.com."] MX [{"preference":10,"exchange":"mx1.com."},{"preference":20,"exchange":"mx2.com."}] +NAPTR +[{"order":10,"preference":0,"flags":"s","services":"SIPS+D2T","regexp":"","replacement":"_sips._tcp.example.com."},{"order":10,"preference":0,"flags":"s","services":"RELAY:turn.udp","regexp":"","replacement":"_turn._udp.example.com."}] NS ["ns1.ns.com.","ns2.ns.com.","ns3.ns.com."] PTR diff --git a/cli/tests/testdata/resolve_dns.zone.in b/cli/tests/testdata/resolve_dns.zone.in index 6b611bafae..456a8d8fbf 100644 --- a/cli/tests/testdata/resolve_dns.zone.in +++ b/cli/tests/testdata/resolve_dns.zone.in @@ -21,4 +21,6 @@ alias CNAME cname 1.2.3.4.IN-ADDR.ARPA. PTR www PTR alias -_service._tcp SRV 0 100 1234 srv \ No newline at end of file +_service._tcp SRV 0 100 1234 srv +@ IN NAPTR 10 0 "s" "SIPS+D2T" "" _sips._tcp.example.com. +@ IN NAPTR 10 0 "s" RELAY:turn.udp "" _turn._udp.example.com. \ No newline at end of file diff --git a/ext/net/ops.rs b/ext/net/ops.rs index 83df7b626b..ec3be091a6 100644 --- a/ext/net/ops.rs +++ b/ext/net/ops.rs @@ -579,6 +579,14 @@ pub enum DnsReturnRecord { preference: u16, exchange: String, }, + Naptr { + order: u16, + preference: u16, + flags: String, + services: String, + regexp: String, + replacement: String, + }, Ns(String), Ptr(String), Soa { @@ -740,6 +748,14 @@ fn rdata_to_return_record( preference: mx.preference(), exchange: mx.exchange().to_string(), }), + NAPTR => r.as_naptr().map(|naptr| DnsReturnRecord::Naptr { + order: naptr.order(), + preference: naptr.preference(), + flags: String::from_utf8(naptr.flags().to_vec()).unwrap(), + services: String::from_utf8(naptr.services().to_vec()).unwrap(), + regexp: String::from_utf8(naptr.regexp().to_vec()).unwrap(), + replacement: naptr.replacement().to_string(), + }), NS => r.as_ns().map(ToString::to_string).map(DnsReturnRecord::Ns), PTR => r .as_ptr() @@ -788,6 +804,7 @@ mod tests { use std::net::Ipv6Addr; use std::path::Path; use trust_dns_proto::rr::rdata::mx::MX; + use trust_dns_proto::rr::rdata::naptr::NAPTR; use trust_dns_proto::rr::rdata::srv::SRV; use trust_dns_proto::rr::rdata::txt::TXT; use trust_dns_proto::rr::rdata::SOA; @@ -838,6 +855,30 @@ mod tests { ); } + #[test] + fn rdata_to_return_record_naptr() { + let func = rdata_to_return_record(RecordType::NAPTR); + let rdata = RData::NAPTR(NAPTR::new( + 1, + 2, + >::default(), + >::default(), + >::default(), + Name::new(), + )); + assert_eq!( + func(&rdata), + Some(DnsReturnRecord::Naptr { + order: 1, + preference: 2, + flags: "".to_string(), + services: "".to_string(), + regexp: "".to_string(), + replacement: "".to_string() + }) + ); + } + #[test] fn rdata_to_return_record_ns() { let func = rdata_to_return_record(RecordType::NS);