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

add avahi file advertiser #1023

Draft
wants to merge 2 commits into
base: latest
Choose a base branch
from
Draft
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
11 changes: 9 additions & 2 deletions src/lib/Accessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
VoidCallback,
WithUUID,
} from "../types";
import { Advertiser, AdvertiserEvent, AvahiAdvertiser, BonjourHAPAdvertiser, CiaoAdvertiser, ResolvedAdvertiser } from "./Advertiser";
import { Advertiser, AdvertiserEvent, AvahiAdvertiser, BonjourHAPAdvertiser, CiaoAdvertiser, ResolvedAdvertiser, AvahiFileAdvertiser } from "./Advertiser";
// noinspection JSDeprecatedSymbols
import { LegacyCameraSource, LegacyCameraSourceAdapter, StreamController } from "./camera";
import {
Expand Down Expand Up @@ -290,7 +290,7 @@ export interface PublishInfo {
/**
* @group Accessory
*/
export const enum MDNSAdvertiser {
export enum MDNSAdvertiser {
/**
* Use the `@homebridge/ciao` module as advertiser.
*/
Expand All @@ -311,6 +311,10 @@ export const enum MDNSAdvertiser {
* Consequentially, treat this feature as an experimental feature.
*/
RESOLVED = "resolved",
/**
* Use the `avahi-file` module as advertiser.
*/
AVAHI_FILE = "avahi-file",
}

/**
Expand Down Expand Up @@ -1332,6 +1336,9 @@ export class Accessory extends EventEmitter {
case MDNSAdvertiser.RESOLVED:
this._advertiser = new ResolvedAdvertiser(this._accessoryInfo);
break;
case MDNSAdvertiser.AVAHI_FILE:
this._advertiser = new AvahiFileAdvertiser(this._accessoryInfo);
break;
default:
throw new Error("Unsupported advertiser setting: '" + info.advertiser + "'");
}
Expand Down
82 changes: 82 additions & 0 deletions src/lib/Advertiser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import createDebug from "debug";
import { EventEmitter } from "events";
import { AccessoryInfo } from "./model/AccessoryInfo";
import { PromiseTimeout } from "./util/promise-utils";
import fs from "fs";

const debug = createDebug("HAP-NodeJS:Advertiser");

Expand Down Expand Up @@ -200,6 +201,87 @@ export class CiaoAdvertiser extends EventEmitter implements Advertiser {
}
}

/**
* Advertiser proxies information through Avahi to broadcast the presence of an Accessory to the local network.
* Useful in situations where network isolation of homebridge is wanted (eg. running in a docker container without host networking)
*/
export class AvahiFileAdvertiser extends EventEmitter implements Advertiser {
private dir = "/homebridge/";
private port?: number;

private readonly accessoryInfo: AccessoryInfo;
private readonly setupHash: string;

constructor(accessoryInfo: AccessoryInfo) {
super();
this.accessoryInfo = accessoryInfo;
this.setupHash = CiaoAdvertiser.computeSetupHash(accessoryInfo);

console.log(`Preparing Advertiser for '${this.accessoryInfo.displayName}' using avahi-file backend!`);
}

private get filePath(): string {
return this.dir + this.accessoryInfo.displayName.replace(/ /g, "-") + ".service";
}

private publish(): Promise<void> {
if (this.port == null) {
throw new Error("Tried starting avahi-file advertisement without initializing port!");
}
return this.writeXMLTo(this.filePath);
}

private async writeXMLTo(path: string): Promise<void> {
await fs.promises.writeFile(path, AvahiFileAdvertiser.serialize(
this.accessoryInfo.displayName,
"_hap._tcp",
this.port!,
CiaoAdvertiser.createTxt(this.accessoryInfo, this.setupHash),
));
}

private static serialize(name: string, type: string, port: number, txt: ServiceTxt): string {
return (
`<service-group>
<name>${name}</name>
<service>
<type>${type}</type>
<port>${port}</port>
` + Object.entries(txt)
.map(([k, v]) => ` <txt-record>${k}=${v}</txt-record>`)
.join("\n") + `
</service>
</service-group>
`);
}

public initPort(port: number): void {
this.port = port;
this.publish();
}

public startAdvertising(): Promise<void> {
return this.publish();
}

public updateAdvertisement(silent?: boolean): void {
debug(`Updating avahi-file advertisement (silent: ${silent})`);
this.publish();
}

public async destroy(): Promise<void> {
this.removeAllListeners();
try {
await fs.promises.unlink(this.filePath);
} catch (err) {
if (err.code === "ENOENT") {
return;
}
throw err;
}
}
}

/**
* Advertiser base on the legacy "bonjour-hap" library.
*
Expand Down
Loading