Merge pull request #2231 from balena-os/mdns-improvements

Try MDNS lookup only if regular DNS lookup fails
This commit is contained in:
flowzone-app[bot] 2024-01-08 13:24:53 +00:00 committed by GitHub
commit 48d288b715
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,26 +26,8 @@ interface DnsLookupOpts {
family: number;
}
// This was originally wrapped in a do block in
// coffeescript, and it's not clear now why that was the
// case, so I'm going to maintain that behaviour
(() => {
// Make NodeJS RFC 3484 compliant for properly handling IPv6
// See: https://github.com/nodejs/node/pull/14731
// https://github.com/nodejs/node/pull/17793
const dns = require('dns');
const { lookup } = dns;
dns.lookup = (
name: string,
opts: Partial<DnsLookupOpts> | number | null | undefined,
cb: DnsLookupCallback,
) => {
if (typeof cb !== 'function') {
return lookup(name, { verbatim: true }, opts);
}
/* =====================================================
/*
* =====================================================
* NOTE: This is required since Alpine/musl-based images
* do not support mDNS name resolution in the musl libs.
*
@ -53,8 +35,13 @@ interface DnsLookupOpts {
* and on openBalena setups, this extra code will perform
* the lookup instead of passing it to the built-in
* function.
================================================== */
if (name && name.endsWith('.local')) {
* ==================================================
*/
async function mdnsLookup(
name: string,
opts: Partial<DnsLookupOpts> | number | null | undefined,
cb: DnsLookupCallback,
) {
// determine which resolvers to use...
const getResolvers = () => {
// logic to determine the families based on Node docs.
@ -75,23 +62,20 @@ interface DnsLookupOpts {
})();
// map them and return...
return families.map((family) => {
return mdnsResolver
.resolve(name, family === 6 ? 'AAAA' : 'A')
.catch(() => {
return '';
})
.then((address) => {
return {
address,
family,
};
});
return families.map(async (family) => {
let address = '';
try {
address = await mdnsResolver.resolve(name, family === 6 ? 'AAAA' : 'A');
} catch {
/* noop */
}
return { address, family };
});
};
// resolve the addresses...
return Promise.all(getResolvers()).then((results) => {
const results = await Promise.all(getResolvers());
// remove any that didn't resolve...
let allAddresses = results.filter((result) => result.address !== '');
@ -112,12 +96,47 @@ interface DnsLookupOpts {
}
// only a single address was requested...
const [{ address, family }] = allAddresses;
return cb(null, address, family);
});
const [{ address: addr, family: fmly }] = allAddresses;
return cb(null, addr, fmly);
}
return lookup(name, Object.assign({ verbatim: true }, opts), cb);
// This was originally wrapped in a do block in
// coffeescript, and it's not clear now why that was the
// case, so I'm going to maintain that behaviour
(() => {
// Make NodeJS RFC 3484 compliant for properly handling IPv6
// See: https://github.com/nodejs/node/pull/14731
// https://github.com/nodejs/node/pull/17793
const dns = require('dns');
const { lookup } = dns;
dns.lookup = (
name: string,
opts: Partial<DnsLookupOpts> | number | null | undefined,
cb: DnsLookupCallback,
) => {
if (typeof cb !== 'function') {
return lookup(name, { verbatim: true }, opts);
}
// Try a regular dns lookup first
return lookup(
name,
Object.assign({ verbatim: true }, opts),
(error: any, address: string, family: number) => {
if (error == null) {
return cb(null, address, family);
}
// If the regular lookup fails, we perform a mdns lookup if the
// name ends with .local
if (name && name.endsWith('.local')) {
return mdnsLookup(name, opts, cb);
}
return cb(error);
},
);
};
})();