1
0
Fork 0
mirror of https://gitlab.com/futo-org/fcast.git synced 2025-08-10 17:42:50 +00:00
fcast/receivers/electron/src/DiscoveryService.ts

74 lines
2.3 KiB
TypeScript
Raw Normal View History

import mdns from 'mdns-js';
2024-11-11 12:24:17 -06:00
import * as log4js from "log4js";
2024-11-04 09:17:20 -06:00
const cp = require('child_process');
const os = require('os');
2024-11-11 12:24:17 -06:00
const logger = log4js.getLogger();
2023-06-20 08:45:01 +02:00
export class DiscoveryService {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2023-12-30 10:55:30 +01:00
private serviceTcp: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2023-12-30 10:55:30 +01:00
private serviceWs: any;
2023-06-20 08:45:01 +02:00
private static getComputerName() {
switch (process.platform) {
case "win32":
return process.env.COMPUTERNAME;
case "darwin":
return cp.execSync("scutil --get ComputerName").toString().trim();
case "linux": {
let hostname: string;
// Some distro's don't work with `os.hostname()`, but work with `hostnamectl` and vice versa...
try {
hostname = os.hostname();
}
catch (err) {
2024-11-11 12:24:17 -06:00
logger.warn('Error fetching hostname, trying different method...');
logger.warn(err);
try {
hostname = cp.execSync("hostnamectl hostname").toString().trim();
}
catch (err2) {
2024-11-11 12:24:17 -06:00
logger.warn('Error fetching hostname again, using generic name...');
logger.warn(err2);
hostname = 'linux device';
}
}
return hostname;
}
2023-06-20 08:45:01 +02:00
default:
return os.hostname();
}
}
start() {
2023-12-30 11:28:36 +01:00
if (this.serviceTcp || this.serviceWs) {
2023-06-20 08:45:01 +02:00
return;
}
const name = `FCast-${DiscoveryService.getComputerName()}`;
2024-11-11 12:24:17 -06:00
logger.info("Discovery service started.", name);
2023-06-20 08:45:01 +02:00
2023-12-30 10:55:30 +01:00
this.serviceTcp = mdns.createAdvertisement(mdns.tcp('_fcast'), 46899, { name: name });
this.serviceTcp.start();
this.serviceWs = mdns.createAdvertisement(mdns.tcp('_fcast-ws'), 46898, { name: name });
this.serviceWs.start();
2023-06-20 08:45:01 +02:00
}
stop() {
2023-12-30 10:55:30 +01:00
if (this.serviceTcp) {
this.serviceTcp.stop();
this.serviceTcp = null;
}
if (this.serviceWs) {
this.serviceWs.stop();
this.serviceWs = null;
}
2023-06-20 08:45:01 +02:00
}
}