Skip to content

Instantly share code, notes, and snippets.

@nuxlli
Created August 9, 2014 17:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nuxlli/7d82ab7ac3e0072c5faf to your computer and use it in GitHub Desktop.
Save nuxlli/7d82ab7ac3e0072c5faf to your computer and use it in GitHub Desktop.
Adding alias in `.local` domain, using avahi
/*
* Require: lodash, dbus-native
* Referencies:
* http://www.avahi.org/wiki/Examples/PythonPublishAlias
* https://github.com/airtonix/avahi-aliases
* http://code.metager.de/source/xref/freedesktop/avahi/avahi-python/avahi/__init__.py
*/
var _ = require('lodash');
var dbus = require('dbus-native');
var toASCII = require('punycode').toASCII;
var bus = dbus.systemBus();
var namespace = 'org.freedesktop.Avahi'
var Avahi = {
DBUS_NAME: namespace,
DBUS_PATH_SERVER: '/',
DBUS_INTERFACE_SERVER: namespace + '.Server',
DBUS_INTERFACE_ENTRY_GROUP: namespace + '.EntryGroup',
IF_UNSPEC: -1,
PROTO_UNSPEC: -1,
}
var Settings = {
TTL: 60,
CLASS_IN: 0x01,
TYPE_CNAME: 0x05,
}
function encode(name) {
return _.map(name.split('.'), function(p) {
return toASCII(p);
}).join('.');
}
function encode_rdata(rdata) {
return _.map(rdata.split('.'), function(p) {
p = toASCII(p);
return String.fromCharCode(p.length) + p
}).join('') + '\0';
}
function string_to_byte_array(data) {
return _.reduce(data.split(''), function(data, p) {
data.push(p.charCodeAt(0) & 0xFF);
return data;
}, []);
}
function publish(cname) {
var service = bus.getService(Avahi.DBUS_NAME);
service.getInterface(Avahi.DBUS_PATH_SERVER, Avahi.DBUS_INTERFACE_SERVER, function(err, server) {
if (err) { throw new Error("Error in getInterface: " + err);}
server.EntryGroupNew(function(err, entry_group) {
if (err) { throw new Error("Error in EntryGroupNew: " + err);}
service.getObject(entry_group, function(err, obj) {
if (err) { throw new Error("Error in getObject: " + err);}
var group = obj.as(Avahi.DBUS_INTERFACE_ENTRY_GROUP);
server.GetHostNameFqdn(function(err, rdata) {
if (err) { throw new Error("Error in GetHostNameFqdn: " + err);}
// Encode data to send
cname = encode(cname);
rdata = encode_rdata(rdata);
rdata = string_to_byte_array(rdata);
// Register a alias
console.log("adding %s", cname);
group.AddRecord(Avahi.IF_UNSPEC, Avahi.PROTO_UNSPEC, 0, cname, Settings.CLASS_IN, Settings.TYPE_CNAME, Settings.TTL, rdata, function(err, result) {
if (err) { throw new Error("Error in AddRecord: " + err);}
group.Commit(function(err, result) {
if (err) { throw new Error("Error in Commit: " + err);}
});
});
});
});
});
});
}
publish('azk.local');
publish('node-example.azk.local');
@tizzo
Copy link

tizzo commented Jan 5, 2015

This code is working perfectly for me running in an Ubuntu 12.04 vagrant box on my Mountain Lion machine but the cnames do not resolve on a new Yosemite machine. Other than OS X variant I think the setups and configurations are identical.

Worth noting, the main Avahi hostname works on both systems but the aliases don't work on Yosemite.

Any idea as to what would cause the aliases to fail? Anyone do any testing on Yosemite?

[Edit: I originally exhaustedly put Mavericks instead of yosemite, I also created a related issue on a python project]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment