Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure systemd-resolved has mDNS enabled #982

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 50 additions & 16 deletions src/lib/Advertiser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,18 +547,23 @@ export class ResolvedAdvertiser extends EventEmitter implements Advertiser {

debug(`Starting to advertise '${this.accessoryInfo.displayName}' using systemd-resolved backend!`);

this.path = await ResolvedAdvertiser.resolvedInvoke(this.bus, "RegisterService", {
body: [
this.accessoryInfo.displayName, // name
this.accessoryInfo.displayName, // name_template
"_hap._tcp", // type
this.port, // service_port
0, // service_priority
0, // service_weight
[this.createTxt()], // txt_datas
],
signature: "sssqqqaa{say}",
});
try {
this.path = await ResolvedAdvertiser.managerInvoke(this.bus, "RegisterService", {
body: [
this.accessoryInfo.displayName, // name
this.accessoryInfo.displayName, // name_template
"_hap._tcp", // type
this.port, // service_port
0, // service_priority
0, // service_weight
[this.createTxt()], // txt_datas
],
signature: "sssqqqaa{say}",
});
} catch (error) {
error.message = `Possible permissions issue. Check the wiki for details. ${error.message}`;
elyscape marked this conversation as resolved.
Show resolved Hide resolved
throw error;
}
}

public async updateAdvertisement(silent?: boolean): Promise<void> {
Expand All @@ -580,7 +585,7 @@ export class ResolvedAdvertiser extends EventEmitter implements Advertiser {

if (this.path) {
try {
await ResolvedAdvertiser.resolvedInvoke(this.bus, "UnregisterService", {
await ResolvedAdvertiser.managerInvoke(this.bus, "UnregisterService", {
body: [this.path],
signature: "o",
});
Expand Down Expand Up @@ -616,7 +621,7 @@ export class ResolvedAdvertiser extends EventEmitter implements Advertiser {

try {
// Ensure that systemd-resolved is accessible.
await this.resolvedInvoke(bus, "ResolveHostname", {
await this.managerInvoke(bus, "ResolveHostname", {
body: [0, "127.0.0.1", 0, 0],
signature: "isit",
});
Expand All @@ -626,21 +631,50 @@ export class ResolvedAdvertiser extends EventEmitter implements Advertiser {
return false;
}

try {
const mdnsStatus = await this.resolvedInvoke(
bus,
"org.freedesktop.DBus.Properties",
"Get",
{
body: ["org.freedesktop.resolve1.Manager", "MulticastDNS"],
signature: "ss",
},
);

if (mdnsStatus[0].type !== "s") {
elyscape marked this conversation as resolved.
Show resolved Hide resolved
throw new Error("Invalid type for MulticastDNS");
}

if (mdnsStatus[1][0] !== "yes" ) {
elyscape marked this conversation as resolved.
Show resolved Hide resolved
debug("systemd-resolved/DBus classified unavailable because MulticastDNS is not enabled!");
return false;
}
} catch (error) {
debug("systemd-resolved/DBus classified unavailable due to failure checking system status: " + error);
return false;
}

return true;
} finally {
bus.connection.stream.destroy();
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private static resolvedInvoke(bus: MessageBus, member: string, others?: any): Promise<any> {
private static resolvedInvoke(bus: MessageBus, dbusInterface: string, member: string, others?: any): Promise<any> {
return dbusInvoke(
bus,
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager",
dbusInterface,
member,
others,
);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private static managerInvoke(bus: MessageBus, member: string, others?: any): Promise<any> {
return this.resolvedInvoke(bus, "org.freedesktop.resolve1.Manager", member, others);
}
}