1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 16:19:12 -05:00

feat(ext/net): support NAPTR records in Deno.resolveDns() API (#14613)

This commit is contained in:
Craig Morten 2022-05-15 16:42:02 +01:00 committed by GitHub
parent 45ee727412
commit c9e9265c3e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 14 deletions

View file

@ -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<MXRecord[]>;
export function resolveDns(
query: string,
recordType: "NAPTR",
options?: ResolveDnsOptions,
): Promise<NAPTRRecord[]>;
export function resolveDns(
query: string,
recordType: "SOA",
@ -3049,5 +3066,12 @@ declare namespace Deno {
query: string,
recordType: RecordType,
options?: ResolveDnsOptions,
): Promise<string[] | MXRecord[] | SOARecord[] | SRVRecord[] | string[][]>;
): Promise<
| string[]
| MXRecord[]
| NAPTRRecord[]
| SOARecord[]
| SRVRecord[]
| string[][]
>;
}

View file

@ -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([
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));

View file

@ -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

View file

@ -22,3 +22,5 @@ alias CNAME cname
1.2.3.4.IN-ADDR.ARPA. PTR www
PTR alias
_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.

View file

@ -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,
<Box<[u8]>>::default(),
<Box<[u8]>>::default(),
<Box<[u8]>>::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);