Bug 1792088 - [remote] Prefer IPv4 over IPv6 for resolved IP addresses of localhost. r=webdriver-reviewers,jgraham

Differential Revision: https://phabricator.services.mozilla.com/D158563
This commit is contained in:
Henrik Skupin 2022-10-05 14:54:50 +00:00
Родитель b7554c3c36
Коммит 00fc04265e
1 изменённых файлов: 25 добавлений и 10 удалений

Просмотреть файл

@ -199,16 +199,29 @@ class RemoteAgentParentProcess {
return; return;
} }
// Try to resolve localhost to an IPv4 or IPv6 address so that the // Try to resolve localhost to an IPv4 and / or IPv6 address so that the
// Server can be started on a given IP. This doesn't force httpd.js to // server can be started on a given IP. Only fallback to use localhost if
// have to use the dual stack support. Only fallback to keep using // the hostname cannot be resolved.
// localhost if the hostname cannot be resolved. //
// Note: This doesn't force httpd.js to use the dual stack support.
let isIPv4Host = false;
try { try {
const addresses = await this.#resolveHostname(DEFAULT_HOST); const addresses = await this.#resolveHostname(DEFAULT_HOST);
if (addresses.length) { lazy.logger.trace(
this.#host = addresses[0]; `Available local IP addresses: ${addresses.join(", ")}`
);
// Prefer IPv4 over IPv6 addresses.
const addressesIPv4 = addresses.filter(value => !value.includes(":"));
isIPv4Host = !!addressesIPv4.length;
if (isIPv4Host) {
this.#host = addressesIPv4[0];
} else {
this.#host = addresses.length ? addresses[0] : DEFAULT_HOST;
} }
} catch (e) { } catch (e) {
this.#host = DEFAULT_HOST;
lazy.logger.debug( lazy.logger.debug(
`Failed to resolve hostname "localhost" to IP address: ${e.message}` `Failed to resolve hostname "localhost" to IP address: ${e.message}`
); );
@ -220,14 +233,15 @@ class RemoteAgentParentProcess {
} }
try { try {
// Bug 1783938: httpd.js refuses connections when started on a IPv4
// address. As workaround start on localhost and add another identity
// for that IP address.
this.#server = new lazy.HttpServer(); this.#server = new lazy.HttpServer();
const host = this.#host === "127.0.0.1" ? DEFAULT_HOST : this.#host; const host = isIPv4Host ? DEFAULT_HOST : this.#host;
this.server._start(port, host); this.server._start(port, host);
this.#port = this.server._port; this.#port = this.server._port;
if (this.#host == "127.0.0.1") { if (isIPv4Host) {
// Bug 1783938: httpd.js refuses connections when started on 127.0.0.1.
// As workaround add another identity for that IP address.
this.server.identity.add("http", this.#host, this.#port); this.server.identity.add("http", this.#host, this.#port);
} }
@ -273,6 +287,7 @@ class RemoteAgentParentProcess {
addresses.push(addr); addresses.push(addr);
} }
} }
resolve(addresses); resolve(addresses);
} }
}, },