-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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 3 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 |
---|---|---|
|
@@ -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 host = isIP(val[0]) === 6 ? `[${val[0]}]` : val[0]; | ||
return `${host}:${val[1]}`; | ||
}); | ||
} | ||
|
||
|
||
|
@@ -311,26 +317,29 @@ function setServers(servers) { | |
const orig = cares.getServers(); | ||
const newSet = []; | ||
const IPv6RE = /\[(.*)\]/; | ||
const addrSplitRE = /:\d+$/; | ||
const addrSplitRE = /(^.+?)(?::(\d+))?$/; | ||
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. another possible way to handle this that may provide more robust and consistent handling of the address and port would be to leverage the new const URL = require('url').URL;
const u = new URL(`dns://${serv}`); If perf is a concern around creating multiple const url = new URL(`dns://${serv}`);
// use it
url.host = nextServ;
// etc 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'm +1 since this is a "cold-spot" 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. But how about something like 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. three options:
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 think keeping RegEx would be fine because URL does some extra operation in semantics, though URL's performance may be better than RegEx. 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. Not sure I follow? Just ignore any additional input. The host and the port are the only bits we care about. If you do not think it is appropriate to ignore the rest, then throw if values for the other parts are provided. 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 think the issue is that this is our only validity check. Now |
||
|
||
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 port = parseInt(serv.replace(addrSplitRE, '$2')) || 53; | ||
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}`); | ||
}); | ||
|
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.
Need to remove
[]
for this line.Ref: #13795
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.