|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Flags: --enable-network-family-autoselection |
| 4 | + |
| 5 | +const common = require('../common'); |
| 6 | +const { parseDNSPacket, writeDNSPacket } = require('../common/dns'); |
| 7 | + |
| 8 | +const assert = require('assert'); |
| 9 | +const dgram = require('dgram'); |
| 10 | +const { Resolver } = require('dns'); |
| 11 | +const { createConnection, createServer } = require('net'); |
| 12 | + |
| 13 | +// Test that happy eyeballs algorithm can be enable from command line. |
| 14 | + |
| 15 | +let autoSelectFamilyAttemptTimeout = common.platformTimeout(250); |
| 16 | +if (common.isWindows) { |
| 17 | + // Some of the windows machines in the CI need more time to establish connection |
| 18 | + autoSelectFamilyAttemptTimeout = common.platformTimeout(1500); |
| 19 | +} |
| 20 | + |
| 21 | +function _lookup(resolver, hostname, options, cb) { |
| 22 | + resolver.resolve(hostname, 'ANY', (err, replies) => { |
| 23 | + assert.notStrictEqual(options.family, 4); |
| 24 | + |
| 25 | + if (err) { |
| 26 | + return cb(err); |
| 27 | + } |
| 28 | + |
| 29 | + const hosts = replies |
| 30 | + .map((r) => ({ address: r.address, family: r.type === 'AAAA' ? 6 : 4 })) |
| 31 | + .sort((a, b) => b.family - a.family); |
| 32 | + |
| 33 | + if (options.all === true) { |
| 34 | + return cb(null, hosts); |
| 35 | + } |
| 36 | + |
| 37 | + return cb(null, hosts[0].address, hosts[0].family); |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +function createDnsServer(ipv6Addr, ipv4Addr, cb) { |
| 42 | + // Create a DNS server which replies with a AAAA and a A record for the same host |
| 43 | + const socket = dgram.createSocket('udp4'); |
| 44 | + |
| 45 | + socket.on('message', common.mustCall((msg, { address, port }) => { |
| 46 | + const parsed = parseDNSPacket(msg); |
| 47 | + const domain = parsed.questions[0].domain; |
| 48 | + assert.strictEqual(domain, 'example.org'); |
| 49 | + |
| 50 | + socket.send(writeDNSPacket({ |
| 51 | + id: parsed.id, |
| 52 | + questions: parsed.questions, |
| 53 | + answers: [ |
| 54 | + { type: 'AAAA', address: ipv6Addr, ttl: 123, domain: 'example.org' }, |
| 55 | + { type: 'A', address: ipv4Addr, ttl: 123, domain: 'example.org' }, |
| 56 | + ] |
| 57 | + }), port, address); |
| 58 | + })); |
| 59 | + |
| 60 | + socket.bind(0, () => { |
| 61 | + const resolver = new Resolver(); |
| 62 | + resolver.setServers([`127.0.0.1:${socket.address().port}`]); |
| 63 | + |
| 64 | + cb({ dnsServer: socket, lookup: _lookup.bind(null, resolver) }); |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +// Test that IPV4 is reached if IPV6 is not reachable |
| 69 | +{ |
| 70 | + createDnsServer('::1', '127.0.0.1', common.mustCall(function({ dnsServer, lookup }) { |
| 71 | + const ipv4Server = createServer((socket) => { |
| 72 | + socket.on('data', common.mustCall(() => { |
| 73 | + socket.write('response-ipv4'); |
| 74 | + socket.end(); |
| 75 | + })); |
| 76 | + }); |
| 77 | + |
| 78 | + ipv4Server.listen(0, '127.0.0.1', common.mustCall(() => { |
| 79 | + const port = ipv4Server.address().port; |
| 80 | + |
| 81 | + const connection = createConnection({ |
| 82 | + host: 'example.org', |
| 83 | + port: port, |
| 84 | + lookup, |
| 85 | + autoSelectFamilyAttemptTimeout, |
| 86 | + }); |
| 87 | + |
| 88 | + let response = ''; |
| 89 | + connection.setEncoding('utf-8'); |
| 90 | + |
| 91 | + connection.on('ready', common.mustCall(() => { |
| 92 | + assert.deepStrictEqual(connection.autoSelectFamilyAttemptedAddresses, [`::1:${port}`, `127.0.0.1:${port}`]); |
| 93 | + })); |
| 94 | + |
| 95 | + connection.on('data', (chunk) => { |
| 96 | + response += chunk; |
| 97 | + }); |
| 98 | + |
| 99 | + connection.on('end', common.mustCall(() => { |
| 100 | + assert.strictEqual(response, 'response-ipv4'); |
| 101 | + ipv4Server.close(); |
| 102 | + dnsServer.close(); |
| 103 | + })); |
| 104 | + |
| 105 | + connection.write('request'); |
| 106 | + })); |
| 107 | + })); |
| 108 | +} |
0 commit comments