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
Show file tree
Hide file tree
Changes from all 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
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
],
"dependencies": {
"@homebridge/ciao": "^1.1.5",
"@homebridge/dbus-native": "^0.4.2",
"@homebridge/dbus-native": "^0.5.0",
"bonjour-hap": "~3.6.3",
"debug": "^4.3.4",
"fast-srp-hap": "2.0.4",
Expand Down
103 changes: 83 additions & 20 deletions src/lib/Advertiser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/// <reference path="../../@types/bonjour-hap.d.ts" />
import ciao, { CiaoService, MDNSServerOptions, Responder, ServiceEvent, ServiceTxt, ServiceType } from "@homebridge/ciao";
import { InterfaceName, IPAddress } from "@homebridge/ciao/lib/NetworkManager";
import dbus, { DBusInterface, MessageBus } from "@homebridge/dbus-native";
import dbus, { DBusInterface, InvokeError, MessageBus } from "@homebridge/dbus-native";
import assert from "assert";
import bonjour, { BonjourHAP, BonjourHAPService, MulticastOptions } from "bonjour-hap";
import crypto from "crypto";
Expand Down Expand Up @@ -278,6 +278,25 @@ function messageBusConnectionResult(bus: MessageBus): Promise<void> {
});
}

export class DBusInvokeError extends Error {
readonly errorName: string;

constructor(errorObject: InvokeError) {
super();

Object.setPrototypeOf(this, DBusInvokeError.prototype);

this.name = "DBusInvokeError";

this.errorName = errorObject.name;

if (Array.isArray(errorObject.message) && errorObject.message.length === 1) {
this.message = errorObject.message[0];
} else {
this.message = errorObject.message.toString();
}
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function dbusInvoke( bus: MessageBus, destination: string, path: string, dbusInterface: string, member: string, others?: any): Promise<any> {
Expand All @@ -290,10 +309,9 @@ function dbusInvoke( bus: MessageBus, destination: string, path: string, dbusInt
...(others || {}),
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
bus.invoke(command, (err: any, result: any) => {
bus.invoke(command, (err, result) => {
if (err) {
reject(new Error(`dbusInvoke error: ${JSON.stringify(err)}`));
reject(new DBusInvokeError(err));
} else {
resolve(result);
}
Expand Down Expand Up @@ -504,6 +522,13 @@ export class AvahiAdvertiser extends EventEmitter implements Advertiser {

type ResolvedServiceTxt = Array<Array<string | Buffer>>;

const RESOLVED_PERMISSIONS_ERRORS = [
"org.freedesktop.DBus.Error.AccessDenied",
"org.freedesktop.DBus.Error.AuthFailed",
"org.freedesktop.DBus.Error.InteractiveAuthorizationRequired",
];


/**
* Advertiser based on the systemd-resolved D-Bus library.
* For docs on the interface, see: https://www.freedesktop.org/software/systemd/man/org.freedesktop.resolve1.html
Expand Down Expand Up @@ -547,18 +572,27 @@ 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) {
if (error instanceof DBusInvokeError) {
if (RESOLVED_PERMISSIONS_ERRORS.includes(error.errorName)) {
error.message = `Permissions issue. See https://homebridge.io/w/mDNS-Options for more info. ${error.message}`;
}
}
throw error;
}
}

public async updateAdvertisement(silent?: boolean): Promise<void> {
Expand All @@ -580,7 +614,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 +650,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 +660,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][0].type !== "s") {
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);
}
}
8 changes: 7 additions & 1 deletion src/types/dbus-native.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ declare module "@homebridge/dbus-native" {

function systemBus(): MessageBus;

export class InvokeError {
name: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
message: any;
}

export class MessageBus {
connection: BusConnection;

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types,@typescript-eslint/no-explicit-any
public invoke(message: any, callback: any): void;
public invoke(message: any, callback: (error: InvokeError | undefined, value: any) => void): void;

public getService(name: string): DBusService;
}
Expand Down