From 3c38fe48fef80208f57fee80c23ab66b4eb8310a Mon Sep 17 00:00:00 2001 From: cetteup Date: Tue, 13 Dec 2022 10:46:43 +0100 Subject: [PATCH] feat: Allow direct control of IP family to be returned by DNS lookup implements #306 --- bin/gamedig.js | 2 +- lib/DnsResolver.js | 7 ++++--- lib/QueryRunner.js | 5 +++-- protocols/core.js | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/bin/gamedig.js b/bin/gamedig.js index 4351b37..e85e3f2 100755 --- a/bin/gamedig.js +++ b/bin/gamedig.js @@ -5,7 +5,7 @@ const Minimist = require('minimist'), const argv = Minimist(process.argv.slice(2), { boolean: ['pretty','debug','givenPortOnly','requestRules'], - string: ['guildId','listenUdpPort'] + string: ['guildId','listenUdpPort','ipFamily'] }); const debug = argv.debug; diff --git a/lib/DnsResolver.js b/lib/DnsResolver.js index 33aa041..5d9e0a6 100644 --- a/lib/DnsResolver.js +++ b/lib/DnsResolver.js @@ -20,10 +20,11 @@ class DnsResolver { /** * Response port will only be present if srv record was involved. * @param {string} host + * @param {number} ipFamily * @param {string=} srvRecordPrefix * @returns {Promise<{address:string, port:number=}>} */ - async resolve(host, srvRecordPrefix) { + async resolve(host, ipFamily, srvRecordPrefix) { this.logger.debug("DNS Lookup: " + host); if(this.isIp(host)) { @@ -52,7 +53,7 @@ class DnsResolver { } return { port: srvPort, - ...await this.resolve(srvHost, srvRecordPrefix) + ...await this.resolve(srvHost, ipFamily, srvRecordPrefix) }; } this.logger.debug("No SRV Record"); @@ -62,7 +63,7 @@ class DnsResolver { } this.logger.debug("Standard Resolve: " + host); - const dnsResult = await dnsLookupAsync(host); + const dnsResult = await dnsLookupAsync(host, ipFamily); // For some reason, this sometimes returns a string address rather than an object. // I haven't been able to reproduce, but it's been reported on the issue tracker. let address; diff --git a/lib/QueryRunner.js b/lib/QueryRunner.js index 47244b7..3abb953 100644 --- a/lib/QueryRunner.js +++ b/lib/QueryRunner.js @@ -5,7 +5,8 @@ const GameResolver = require('./GameResolver'), const defaultOptions = { socketTimeout: 2000, attemptTimeout: 10000, - maxAttempts: 1 + maxAttempts: 1, + ipFamily: 0 }; class QueryRunner { @@ -19,7 +20,7 @@ class QueryRunner { async run(userOptions) { for (const key of Object.keys(userOptions)) { const value = userOptions[key]; - if (['port'].includes(key)) { + if (['port', 'ipFamily'].includes(key)) { userOptions[key] = parseInt(value); } } diff --git a/protocols/core.js b/protocols/core.js index 4885928..fe846d8 100644 --- a/protocols/core.js +++ b/protocols/core.js @@ -69,7 +69,7 @@ class Core extends EventEmitter { async runOnce() { const options = this.options; if (('host' in options) && !('address' in options)) { - const resolved = await this.dnsResolver.resolve(options.host, this.srvRecord); + const resolved = await this.dnsResolver.resolve(options.host, options.ipFamily, this.srvRecord); options.address = resolved.address; if (resolved.port) options.port = resolved.port; }