Merge pull request #1448 from balena-io/support-mdns-api-urls

bug: Resolve mDNS API URLs
This commit is contained in:
bulldozer-balena[bot] 2020-08-27 11:44:08 +00:00 committed by GitHub
commit 9d86b81a1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 152 additions and 10 deletions

View File

@ -33,6 +33,9 @@ export DBUS_SYSTEM_BUS_ADDRESS="unix:path=/mnt/root/run/dbus/system_bus_socket"
# Include self-signed CAs, should they exist
if [ -n "${BALENA_ROOT_CA}" ]; then
if [ ! -e '/etc/ssl/certs/balenaRootCA.pem' ]; then
echo "${BALENA_ROOT_CA}" > /etc/ssl/certs/balenaRootCA.pem
# Include the balenaRootCA in the system store for services like Docker
mkdir -p /usr/local/share/ca-certificates
echo "${BALENA_ROOT_CA}" > /usr/local/share/ca-certificates/balenaRootCA.crt
update-ca-certificates

54
package-lock.json generated
View File

@ -1311,7 +1311,7 @@
},
"util": {
"version": "0.10.3",
"resolved": "http://registry.npmjs.org/util/-/util-0.10.3.tgz",
"resolved": "https://registry.npmjs.org/util/-/util-0.10.3.tgz",
"integrity": "sha1-evsa/lCAUkZInj23/g7TeTNqwPk=",
"dev": true,
"requires": {
@ -3195,6 +3195,15 @@
"path-type": "^4.0.0"
}
},
"dns-packet": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/dns-packet/-/dns-packet-4.2.0.tgz",
"integrity": "sha512-bn1AKpfkFbm0MIioOMHZ5qJzl2uypdBwI4nYNsqvhjsegBhcKJUlCrMPWLx6JEezRjxZmxhtIz/FkBEur2l8Cw==",
"requires": {
"ip": "^1.1.5",
"safe-buffer": "^5.1.1"
}
},
"docker-delta": {
"version": "2.2.11",
"resolved": "https://registry.npmjs.org/docker-delta/-/docker-delta-2.2.11.tgz",
@ -3361,7 +3370,7 @@
},
"stream-combiner": {
"version": "0.2.2",
"resolved": "http://registry.npmjs.org/stream-combiner/-/stream-combiner-0.2.2.tgz",
"resolved": "https://registry.npmjs.org/stream-combiner/-/stream-combiner-0.2.2.tgz",
"integrity": "sha1-rsjLrBd7Vrb0+kec7YwZEs7lKFg=",
"dev": true,
"requires": {
@ -3707,7 +3716,7 @@
},
"event-stream": {
"version": "3.3.4",
"resolved": "http://registry.npmjs.org/event-stream/-/event-stream-3.3.4.tgz",
"resolved": "https://registry.npmjs.org/event-stream/-/event-stream-3.3.4.tgz",
"integrity": "sha1-SrTJoPWlTbkzi0w02Gv86PSzVXE=",
"dev": true,
"requires": {
@ -5118,6 +5127,11 @@
"integrity": "sha512-igASwWWkDY757OutNcM6zTtdJf/eTZYkoe2ymsX2qpm5bKZLo74FJYjsCtMQOEdY7dRHLLEulCyFQwdN69GBCg==",
"dev": true
},
"ip": {
"version": "1.1.5",
"resolved": "https://registry.npmjs.org/ip/-/ip-1.1.5.tgz",
"integrity": "sha1-vd7XARQpCCjAoDnnLvJfWq7ENUo="
},
"ipaddr.js": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz",
@ -6190,7 +6204,7 @@
},
"map-stream": {
"version": "0.1.0",
"resolved": "http://registry.npmjs.org/map-stream/-/map-stream-0.1.0.tgz",
"resolved": "https://registry.npmjs.org/map-stream/-/map-stream-0.1.0.tgz",
"integrity": "sha1-5WqpTEyAVaFkBKBnS3jyFffI4ZQ=",
"dev": true
},
@ -6214,6 +6228,14 @@
"safe-buffer": "^5.1.2"
}
},
"mdns-resolver": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/mdns-resolver/-/mdns-resolver-1.0.0.tgz",
"integrity": "sha512-lKE0JNVlXEpMOfqkRk/aHjMC6jaozB8SlZKoeNKh1kRRnZ3GNbWx/aywJUhofYJTfnDjq7Ac9H6smqdEWlj6ng==",
"requires": {
"multicast-dns": "^7.2.2"
}
},
"media-typer": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
@ -6371,7 +6393,7 @@
},
"minimist": {
"version": "0.0.8",
"resolved": "http://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=",
"dev": true
},
@ -6522,7 +6544,7 @@
"dependencies": {
"commander": {
"version": "2.15.1",
"resolved": "http://registry.npmjs.org/commander/-/commander-2.15.1.tgz",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.15.1.tgz",
"integrity": "sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag==",
"dev": true
},
@ -6601,6 +6623,15 @@
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=",
"dev": true
},
"multicast-dns": {
"version": "7.2.2",
"resolved": "https://registry.npmjs.org/multicast-dns/-/multicast-dns-7.2.2.tgz",
"integrity": "sha512-XqSMeO8EWV/nOXOpPV8ztIpNweVfE1dSpz6SQvDPp71HD74lMXjt4m/mWB1YBMG0kHtOodxRWc5WOb/UNN1A5g==",
"requires": {
"dns-packet": "^4.0.0",
"thunky": "^1.0.2"
}
},
"mz": {
"version": "2.7.0",
"resolved": "https://registry.npmjs.org/mz/-/mz-2.7.0.tgz",
@ -7674,7 +7705,7 @@
},
"readable-stream": {
"version": "2.3.6",
"resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz",
"integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==",
"requires": {
"core-util-is": "~1.0.0",
@ -8553,7 +8584,7 @@
},
"split": {
"version": "0.3.3",
"resolved": "http://registry.npmjs.org/split/-/split-0.3.3.tgz",
"resolved": "https://registry.npmjs.org/split/-/split-0.3.3.tgz",
"integrity": "sha1-zQ7qXmOiEd//frDwkcQTPi0N0o8=",
"dev": true,
"requires": {
@ -8661,7 +8692,7 @@
},
"stream-combiner": {
"version": "0.0.4",
"resolved": "http://registry.npmjs.org/stream-combiner/-/stream-combiner-0.0.4.tgz",
"resolved": "https://registry.npmjs.org/stream-combiner/-/stream-combiner-0.0.4.tgz",
"integrity": "sha1-TV5DPBhSYd3mI8o/RMWGvPXErRQ=",
"dev": true,
"requires": {
@ -9041,6 +9072,11 @@
"xtend": "~4.0.1"
}
},
"thunky": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/thunky/-/thunky-1.1.0.tgz",
"integrity": "sha512-eHY7nBftgThBqOyHGVN+l8gF0BucP09fMo0oO/Lb0w1OF80dJv+lDVpXG60WMQvkcxAkNybKsrEIE3ZtKGmPrA=="
},
"tildify": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/tildify/-/tildify-2.0.0.tgz",

View File

@ -28,6 +28,7 @@
"private": true,
"dependencies": {
"dbus": "^1.0.7",
"mdns-resolver": "^1.0.0",
"semver": "^7.3.2",
"sqlite3": "^4.1.1"
},

View File

@ -1,3 +1,24 @@
import { NOTFOUND } from 'dns';
import * as mdnsResolver from 'mdns-resolver';
class DnsLookupError extends Error {
public constructor(public code: string = NOTFOUND) {
super();
}
}
interface DnsLookupCallback {
(err: any): void;
(err: undefined | null, address: string, family: number): void;
(err: undefined | null, addresses: string[]): void;
}
interface DnsLookupOpts {
verbatim: boolean;
all: boolean;
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
@ -7,10 +28,91 @@
// https://github.com/nodejs/node/pull/17793
const dns = require('dns');
const { lookup } = dns;
dns.lookup = (name: string, opts: any, cb: (err?: Error) => void) => {
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.
*
* In order to support .local mDNS names in development
* 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')) {
// determine which resolvers to use...
const getResolvers = () => {
// logic to determine the families based on Node docs.
// See: https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback
const families = (() => {
// strictly defined...
if (opts === 4 || opts === 6) {
return [opts as number];
}
// opts is passed, not a number and the `family` parameter is passed...
if (opts && typeof opts !== 'number' && opts.family) {
return [opts.family];
}
// default to resolve both v4 and v6...
return [4, 6];
})();
// map them and return...
return families.map((family) => {
return mdnsResolver
.resolve(name, family === 6 ? 'AAAA' : 'A')
.catch(() => {
return '';
})
.then((addr) => {
return {
addr,
family,
};
});
});
};
// resolve the addresses...
return Promise.all(getResolvers()).then((results) => {
// remove any that didn't resolve...
let allAddresses = results.filter((result) => result.addr !== '');
// unless the results should be returned verbatim, sort them so v4 comes first...
if (opts && typeof opts !== 'number' && !opts.verbatim) {
allAddresses = allAddresses.sort((a, b) => a.family - b.family);
}
// see if we resolved anything...
if (allAddresses.length === 0) {
// nothing! return a suitable error...
return cb(new DnsLookupError());
}
// all the addresses were requested...
if (opts && typeof opts !== 'number' && opts.all) {
return cb(
null,
allAddresses.map((r) => r.addr),
);
}
// only a single address was requested...
const [{ addr, family }] = allAddresses;
return cb(null, addr, family);
});
}
return lookup(name, Object.assign({ verbatim: true }, opts), cb);
};
})();