diff options
author | XadillaX <admin@xcoder.in> | 2017-06-16 20:27:21 +0300 |
---|---|---|
committer | Refael Ackermann <refack@gmail.com> | 2017-06-21 01:42:25 +0300 |
commit | 330349f706a1f21234eee9b9d75d7c1e953ad1f0 (patch) | |
tree | 90206709c3203c7a28a033cd4a32ade61ce7dddb /lib/dns.js | |
parent | 1fcb76e8f2f05fc084337da097a17b06cc7d0c68 (diff) |
dns: make `dns.setServers` support customized port
allow `dns.setServers` parameter to contain port
e.g.
```
dns.setServers([ '103.238.225.181:666' ]);
```
And `dns.getServers` will return IP with port if not the default port.
PR-URL: https://github.com/nodejs/node/pull/13723
Refs: https://github.com/nodejs/node/issues/7903
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Diffstat (limited to 'lib/dns.js')
-rw-r--r-- | lib/dns.js | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/lib/dns.js b/lib/dns.js index e2af27863e7..8f84a079502 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -60,6 +60,7 @@ function errnoException(err, syscall, hostname) { return ex; } +const IANA_DNS_PORT = 53; const digits = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0-15 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16-31 @@ -301,7 +302,13 @@ function resolve(hostname, rrtype, callback) { function getServers() { - return cares.getServers(); + const ret = cares.getServers(); + return ret.map((val) => { + if (!val[1] || val[1] === IANA_DNS_PORT) return val[0]; + + const host = isIP(val[0]) === 6 ? `[${val[0]}]` : val[0]; + return `${host}:${val[1]}`; + }); } @@ -311,26 +318,31 @@ function setServers(servers) { const orig = cares.getServers(); const newSet = []; const IPv6RE = /\[(.*)\]/; - const addrSplitRE = /:\d+$/; + const addrSplitRE = /(^.+?)(?::(\d+))?$/; servers.forEach((serv) => { var ipVersion = isIP(serv); if (ipVersion !== 0) - return newSet.push([ipVersion, serv]); + return newSet.push([ipVersion, serv, IANA_DNS_PORT]); const match = serv.match(IPv6RE); // we have an IPv6 in brackets if (match) { ipVersion = isIP(match[1]); - if (ipVersion !== 0) - return newSet.push([ipVersion, match[1]]); + if (ipVersion !== 0) { + const port = + parseInt(serv.replace(addrSplitRE, '$2')) || + IANA_DNS_PORT; + return newSet.push([ipVersion, match[1], port]); + } } - const s = serv.split(addrSplitRE)[0]; + const [, s, p] = serv.match(addrSplitRE); ipVersion = isIP(s); - if (ipVersion !== 0) - return newSet.push([ipVersion, s]); + if (ipVersion !== 0) { + return newSet.push([ipVersion, s, parseInt(p)]); + } throw new Error(`IP address is not properly formatted: ${serv}`); }); |