2023-06-20 08:45:01 +02:00
|
|
|
import mdns = require('mdns-js');
|
|
|
|
const cp = require('child_process');
|
|
|
|
const os = require('os');
|
|
|
|
|
|
|
|
export class DiscoveryService {
|
2023-12-30 10:55:30 +01:00
|
|
|
private serviceTcp: any;
|
|
|
|
private serviceTls: any;
|
|
|
|
private serviceWs: any;
|
|
|
|
private serviceWss: 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":
|
|
|
|
const prettyname = cp.execSync("hostnamectl --pretty").toString().trim();
|
|
|
|
return prettyname === "" ? os.hostname() : prettyname;
|
|
|
|
default:
|
|
|
|
return os.hostname();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
2023-12-30 10:55:30 +01:00
|
|
|
if (this.serviceTcp || this.serviceTls || this.serviceWs || this.serviceWss) {
|
2023-06-20 08:45:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const name = `FCast-${DiscoveryService.getComputerName()}`;
|
|
|
|
console.log("Discovery service started.", name);
|
|
|
|
|
2023-12-30 10:55:30 +01:00
|
|
|
this.serviceTcp = mdns.createAdvertisement(mdns.tcp('_fcast'), 46899, { name: name });
|
|
|
|
this.serviceTcp.start();
|
|
|
|
this.serviceTls = mdns.createAdvertisement(mdns.tcp('_fcast-tls'), 46897, { name: name });
|
|
|
|
this.serviceTls.start();
|
|
|
|
this.serviceWs = mdns.createAdvertisement(mdns.tcp('_fcast-ws'), 46898, { name: name });
|
|
|
|
this.serviceWs.start();
|
|
|
|
this.serviceWss = mdns.createAdvertisement(mdns.tcp('_fcast-wss'), 46896, { name: name });
|
|
|
|
this.serviceWss.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.serviceTls) {
|
|
|
|
this.serviceTls.stop();
|
|
|
|
this.serviceTls = null;
|
2023-06-20 08:45:01 +02:00
|
|
|
}
|
|
|
|
|
2023-12-30 10:55:30 +01:00
|
|
|
if (this.serviceWs) {
|
|
|
|
this.serviceWs.stop();
|
|
|
|
this.serviceWs = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.serviceWss) {
|
|
|
|
this.serviceWss.stop();
|
|
|
|
this.serviceWss = null;
|
|
|
|
}
|
2023-06-20 08:45:01 +02:00
|
|
|
}
|
|
|
|
}
|