-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dns: make dns.setServers
support customized port
#13723
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,8 +59,20 @@ the [Implementation considerations section][] for more information. | |
added: v0.11.3 | ||
--> | ||
|
||
Returns an array of IP address strings that are being used for name | ||
resolution. | ||
Returns an array of IP address and port strings that are being used for name | ||
resolution, splited with `:`. If a server uses a default DNS server port, the | ||
port part will be ignored in the result. | ||
|
||
eg. | ||
|
||
```js | ||
[ | ||
'4.4.4.4', | ||
'[2001:4860:4860::8888]', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This still needs to be changed, since currently the output will be without the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. > dns.setServers(['192.168.1.1', '2001:4860:4860::8888'])
undefined
> dns.getServers()
[ '192.168.1.1', '2001:4860:4860::8888' ] |
||
'4.4.4.4:1053', | ||
'[2001:4860:4860::8888]:1053' | ||
] | ||
``` | ||
|
||
## dns.lookup(hostname[, options], callback) | ||
<!-- YAML | ||
|
@@ -484,10 +496,20 @@ added: v0.11.3 | |
--> | ||
- `servers` {string[]} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
Sets the IP addresses of the servers to be used when resolving. The `servers` | ||
argument is an array of IPv4 or IPv6 addresses. | ||
Sets the IP addresses and port of the servers to be used when resolving. The | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
`servers` argument is an array of IPv4 or IPv6 addresses. If the port of a | ||
server is the default DNS server port, this part can be ignored in the string. | ||
|
||
If a port is specified on the address, it will be removed. | ||
eg. | ||
|
||
```js | ||
dns.setServers([ | ||
'4.4.4.4', | ||
'[2001:4860:4860::8888]', | ||
'4.4.4.4:1053', | ||
'[2001:4860:4860::8888]:1053' | ||
]); | ||
``` | ||
|
||
An error will be thrown if an invalid address is provided. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -301,7 +301,13 @@ function resolve(hostname, rrtype, callback) { | |
|
||
|
||
function getServers() { | ||
return cares.getServers(); | ||
const ret = cares.getServers(); | ||
return ret.map((val) => { | ||
if (!val[1] || val[1] === 53) return val[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just noticed this, IMHO we should format IPv6 with return ret.map(([host, port]) => {
if (isIP(host) === 6) host = `[${host}]`;
return (!port || port === 53) host : `${host}:${port}`;
} But that will make this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix this in a future PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you put the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: |
||
|
||
const ipVersion = isIP(val[0]); | ||
return ipVersion === 6 ? `[${val[0]}]:${val[1]}` : `${val[0]}:${val[1]}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personal preference nit: const host= 6 ? `[${val[0]}]` : val[0];
return ${host}:${val[1]}; |
||
}); | ||
} | ||
|
||
|
||
|
@@ -311,26 +317,33 @@ 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, 53]); | ||
|
||
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 portMatch = serv.match(addrSplitRE) || []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. personal style nit: const addrSplitRE = /(^.+?)(?::(\d+))?$/;
const port = parseInt(serv.replace(addrSplitRE, '$2')) || 53;
return newSet.push([ipVersion, match[1], port); |
||
return newSet.push([ipVersion, match[1], parseInt(portMatch[1] || 53)]); | ||
} | ||
} | ||
|
||
const s = serv.split(addrSplitRE)[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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(serv.substr(serv.indexOf(':') + 1)) | ||
]); | ||
} | ||
|
||
throw new Error(`IP address is not properly formatted: ${serv}`); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚲 🎪 Suggestion: