@@ -199,16 +199,29 @@ class RemoteAgentParentProcess {
199
199
return ;
200
200
}
201
201
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 ;
206
208
try {
207
209
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 ;
210
221
}
211
222
} catch ( e ) {
223
+ this . #host = DEFAULT_HOST ;
224
+
212
225
lazy . logger . debug (
213
226
`Failed to resolve hostname "localhost" to IP address: ${ e . message } `
214
227
) ;
@@ -220,14 +233,15 @@ class RemoteAgentParentProcess {
220
233
}
221
234
222
235
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.
223
239
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;
225
241
this . server . _start ( port , host ) ;
226
242
this . #port = this . server . _port ;
227
243
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 ) {
231
245
this . server . identity . add ( "http" , this . #host, this . #port) ;
232
246
}
233
247
@@ -273,6 +287,7 @@ class RemoteAgentParentProcess {
273
287
addresses . push ( addr ) ;
274
288
}
275
289
}
290
+
276
291
resolve ( addresses ) ;
277
292
}
278
293
} ,
0 commit comments