denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
97.78k stars 5.38k forks source link

Support reverse DNS API #14432

Open cmorten opened 2 years ago

cmorten commented 2 years ago

Feature request to support address to name translation ("reverse DNS"), a.k.a. wrapper around https://man7.org/linux/man-pages/man3/getnameinfo.3.html (e.g. using https://docs.rs/dns-lookup/latest/dns_lookup/#getnameinfo) or equivalent, so that users of Deno can translate an IP and port into a hostname and service.

E.g.

interface NameInfoRecord {
  hostname: string;
  service: string;
}

function Deno.resolveNameInfo(ipAddr: string, port: number): Promise<NameInfoRecord>;

API shape and names just as example - suspect we can devise better alternatives! Answers on a postcard 🙃

const { hostname, service } = await Deno.resolveNameInfo('127.0.0.1', 22);

console.log(hostname, service); // "localhost" "ssh"

This would provide a sibling to the existing Deno.resolveDns() API which offers DNS resolution currently.

This would increase parity with existing Node APIs:

And is required to support the dns and dns/promises modules in the Deno std library compat layer.


Alternatives considered

1) PTR + service lookup

Could use PTR records with existing Deno.resolveDns() but more limited in capability (don’t get the port to service mapping without additional work) and requires the user or any wrapper to handle IPv4 vs IPv6 address construction (incl. hex for IPv6) - overheads / features that could be provided by core if use getnameinfo.

E.g.

const ipv4Results = await Deno.resolveDns("4.4.8.8.in-addr.arpa", "PTR");

console.log(ipv4Results); // [ "dns.google." ]

To then get the service information could write a wrapper in TS to either interrogate the services db direct (e.g. parse /etc/services), or spawn execution of getservbyport.

2) Overload Deno.resolveDns()

Introduce some faux record type, or just an overloaded interface where ip + port args internally mapped to getnameinfo like behaviour.

Complicates an otherwise stable and clear interface so leaning away from this kind of idea.

cmorten commented 2 years ago

Looks like trust-dns support reverse lookup as a dedicated API (REF: https://docs.rs/trust-dns-resolver/latest/trust_dns_resolver/struct.AsyncResolver.html#method.reverse_lookup) but it is just a wrapper around PTR record lookup which is limited as discussed above (no port to service mapping).

cmorten commented 2 years ago

Example implementation in https://github.com/denoland/deno/pull/14634

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.