From bdb926d6b24950ddacee38b0230a3dcc16b52e37 Mon Sep 17 00:00:00 2001 From: Tom <25043847+Douile@users.noreply.github.com> Date: Sun, 19 Nov 2023 19:02:55 +0000 Subject: [PATCH] feat: dns: Use node's built in isIP check instead of regex (#410) Fixes #408 --- lib/DnsResolver.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/DnsResolver.js b/lib/DnsResolver.js index 4e5bb2a..8cadd8f 100644 --- a/lib/DnsResolver.js +++ b/lib/DnsResolver.js @@ -1,4 +1,5 @@ import dns from 'node:dns' +import { isIP } from 'node:net' import punycode from 'punycode/punycode.js' export default class DnsResolver { @@ -9,12 +10,12 @@ export default class DnsResolver { this.logger = logger } - isIp (host) { - return !!host.match(/\d+\.\d+\.\d+\.\d+/) - } - /** - * Response port will only be present if srv record was involved. + * Resolve a host name to its IP, if the given host name is already + * an IP address no request is made. + * + * If a srvRecordPrefix is provided a SRV request will be made and the + * port returned will be included in the output. * @param {string} host * @param {number} ipFamily * @param {string=} srvRecordPrefix @@ -23,7 +24,8 @@ export default class DnsResolver { async resolve (host, ipFamily, srvRecordPrefix) { this.logger.debug('DNS Lookup: ' + host) - if (this.isIp(host)) { + // Check if host is IPv4 or IPv6 + if (isIP(host) === 4 || isIP(host) === 6) { this.logger.debug('Raw IP Address: ' + host) return { address: host } }