Skip to content

Commit 906d4e3

Browse files
committedOct 5, 2022
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
1 parent 558ad90 commit 906d4e3

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed
 

‎remote/components/RemoteAgent.sys.mjs

+25-10
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,29 @@ class RemoteAgentParentProcess {
199199
return;
200200
}
201201

202-
// Try to resolve localhost to an IPv4 or IPv6 address so that the
203-
// Server can be started on a given IP. This doesn't force httpd.js to
204-
// have to use the dual stack support. Only fallback to keep using
205-
// localhost if the hostname cannot be resolved.
202+
// Try to resolve localhost to an IPv4 and / or IPv6 address so that the
203+
// server can be started on a given IP. Only fallback to use localhost if
204+
// the hostname cannot be resolved.
205+
//
206+
// Note: This doesn't force httpd.js to use the dual stack support.
207+
let isIPv4Host = false;
206208
try {
207209
const addresses = await this.#resolveHostname(DEFAULT_HOST);
208-
if (addresses.length) {
209-
this.#host = addresses[0];
210+
lazy.logger.trace(
211+
`Available local IP addresses: ${addresses.join(", ")}`
212+
);
213+
214+
// Prefer IPv4 over IPv6 addresses.
215+
const addressesIPv4 = addresses.filter(value => !value.includes(":"));
216+
isIPv4Host = !!addressesIPv4.length;
217+
if (isIPv4Host) {
218+
this.#host = addressesIPv4[0];
219+
} else {
220+
this.#host = addresses.length ? addresses[0] : DEFAULT_HOST;
210221
}
211222
} catch (e) {
223+
this.#host = DEFAULT_HOST;
224+
212225
lazy.logger.debug(
213226
`Failed to resolve hostname "localhost" to IP address: ${e.message}`
214227
);
@@ -220,14 +233,15 @@ class RemoteAgentParentProcess {
220233
}
221234

222235
try {
236+
// Bug 1783938: httpd.js refuses connections when started on a IPv4
237+
// address. As workaround start on localhost and add another identity
238+
// for that IP address.
223239
this.#server = new lazy.HttpServer();
224-
const host = this.#host === "127.0.0.1" ? DEFAULT_HOST : this.#host;
240+
const host = isIPv4Host ? DEFAULT_HOST : this.#host;
225241
this.server._start(port, host);
226242
this.#port = this.server._port;
227243

228-
if (this.#host == "127.0.0.1") {
229-
// Bug 1783938: httpd.js refuses connections when started on 127.0.0.1.
230-
// As workaround add another identity for that IP address.
244+
if (isIPv4Host) {
231245
this.server.identity.add("http", this.#host, this.#port);
232246
}
233247

@@ -273,6 +287,7 @@ class RemoteAgentParentProcess {
273287
addresses.push(addr);
274288
}
275289
}
290+
276291
resolve(addresses);
277292
}
278293
},

0 commit comments

Comments
 (0)
Failed to load comments.