mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2024-12-21 14:37:49 +00:00
Merge pull request #2231 from balena-os/mdns-improvements
Try MDNS lookup only if regular DNS lookup fails
This commit is contained in:
commit
48d288b715
97
src/app.ts
97
src/app.ts
@ -26,26 +26,8 @@ interface DnsLookupOpts {
|
|||||||
family: number;
|
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
|
* NOTE: This is required since Alpine/musl-based images
|
||||||
* do not support mDNS name resolution in the musl libs.
|
* 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
|
* and on openBalena setups, this extra code will perform
|
||||||
* the lookup instead of passing it to the built-in
|
* the lookup instead of passing it to the built-in
|
||||||
* function.
|
* function.
|
||||||
================================================== */
|
* ==================================================
|
||||||
if (name && name.endsWith('.local')) {
|
*/
|
||||||
|
async function mdnsLookup(
|
||||||
|
name: string,
|
||||||
|
opts: Partial<DnsLookupOpts> | number | null | undefined,
|
||||||
|
cb: DnsLookupCallback,
|
||||||
|
) {
|
||||||
// determine which resolvers to use...
|
// determine which resolvers to use...
|
||||||
const getResolvers = () => {
|
const getResolvers = () => {
|
||||||
// logic to determine the families based on Node docs.
|
// logic to determine the families based on Node docs.
|
||||||
@ -75,23 +62,20 @@ interface DnsLookupOpts {
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
// map them and return...
|
// map them and return...
|
||||||
return families.map((family) => {
|
return families.map(async (family) => {
|
||||||
return mdnsResolver
|
let address = '';
|
||||||
.resolve(name, family === 6 ? 'AAAA' : 'A')
|
try {
|
||||||
.catch(() => {
|
address = await mdnsResolver.resolve(name, family === 6 ? 'AAAA' : 'A');
|
||||||
return '';
|
} catch {
|
||||||
})
|
/* noop */
|
||||||
.then((address) => {
|
}
|
||||||
return {
|
|
||||||
address,
|
return { address, family };
|
||||||
family,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// resolve the addresses...
|
// resolve the addresses...
|
||||||
return Promise.all(getResolvers()).then((results) => {
|
const results = await Promise.all(getResolvers());
|
||||||
// remove any that didn't resolve...
|
// remove any that didn't resolve...
|
||||||
let allAddresses = results.filter((result) => result.address !== '');
|
let allAddresses = results.filter((result) => result.address !== '');
|
||||||
|
|
||||||
@ -112,12 +96,47 @@ interface DnsLookupOpts {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// only a single address was requested...
|
// only a single address was requested...
|
||||||
const [{ address, family }] = allAddresses;
|
const [{ address: addr, family: fmly }] = allAddresses;
|
||||||
return cb(null, address, family);
|
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);
|
||||||
|
},
|
||||||
|
);
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user