mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-02-05 10:39:39 +00:00
7273656d07
Instead of using the more generic resin-discoverable-services lib which is unmantained and currently has several vulnerabilities and forks for fixing issues (that were later on fixed upstream) we directly talk with mDNS using standard (and currently mantained) bonjour-service. Change-type: patch
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import Bonjour from 'bonjour-service';
|
|
import type { Service } from 'bonjour-service';
|
|
|
|
interface LocalBalenaOsDevice {
|
|
address: string;
|
|
host: string;
|
|
osVariant?: string;
|
|
port: number;
|
|
}
|
|
|
|
const avahiBalenaSshConfig = {
|
|
type: 'ssh',
|
|
name: '_resin-device._sub',
|
|
protocol: 'tcp' as const,
|
|
};
|
|
|
|
const avahiBalenaSshSubtype = 'resin-device';
|
|
|
|
export async function discoverLocalBalenaOsDevices(
|
|
timeout = 4000,
|
|
): Promise<LocalBalenaOsDevice[]> {
|
|
const services = await new Promise<Service[]>((resolve) => {
|
|
const bonjour = new Bonjour({}, async (err: string | Error) => {
|
|
await (await import('../errors')).handleError(err);
|
|
});
|
|
const resinSshServices: Service[] = [];
|
|
const browser = bonjour.find(avahiBalenaSshConfig, (service) =>
|
|
resinSshServices.push(service),
|
|
);
|
|
setTimeout(() => {
|
|
browser.stop();
|
|
bonjour.destroy();
|
|
resolve(resinSshServices);
|
|
}, timeout);
|
|
});
|
|
|
|
return services
|
|
.filter(
|
|
({ subtypes, referer }) =>
|
|
subtypes?.includes(avahiBalenaSshSubtype) && referer != null,
|
|
)
|
|
.map(({ referer, host, port }) => ({
|
|
// We ensure referer is not null on the filter above
|
|
address: referer!.address,
|
|
host,
|
|
port,
|
|
}));
|
|
}
|