From 10fc80c2b75516aa59e95e9ac5d89d3117982ea5 Mon Sep 17 00:00:00 2001 From: tigerbot Date: Fri, 7 Jul 2017 17:53:12 -0600 Subject: [PATCH] switched the mdns ID to be human readable --- lib/mdns.js | 30 +----------------------------- lib/storage.js | 24 ++++++++++++++++++++++++ package.json | 1 + 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/lib/mdns.js b/lib/mdns.js index 2c6ef0c..d41f25a 100644 --- a/lib/mdns.js +++ b/lib/mdns.js @@ -1,36 +1,8 @@ 'use strict'; var PromiseA = require('bluebird'); -var fs = PromiseA.promisifyAll(require('fs')); -var path = require('path'); -var idFilename = path.join(__dirname, '..', 'var', 'mdns-id'); var queryName = '_cloud._tcp.local'; -var randomId = { - get: function () { - return fs.readFileAsync(idFilename) - .catch(function (err) { - if (err.code !== 'ENOENT') { - return PromiseA.reject(err); - } - var id = require('crypto').randomBytes(5).toString('hex'); - return randomId.set(id); - }); - } - -, set: function (value) { - return fs.mkdirAsync(path.dirname(idFilename)).catch(function (err) { - if (err.code !== 'EEXIST') { - console.error('failed to mkdir', path.dirname(idFilename), err.toString()); - } - }).then(function () { - return fs.writeFileAsync(idFilename, value).then(function () { - return value; - }); - }); - } -}; - function createResponse(name, ownerIds, packet, ttl, mainPort) { var rpacket = { header: { @@ -148,7 +120,7 @@ module.exports.start = function (deps, config, mainPort) { } var proms = [ - randomId.get() + deps.storage.mdnsId.get() , deps.storage.owners.all().then(function (owners) { // The ID is the sha256 hash of the PPID, which shouldn't be reversible and therefore // should be safe to expose without needing authentication. diff --git a/lib/storage.js b/lib/storage.js index 071aa12..4651f2d 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -5,6 +5,7 @@ var path = require('path'); var fs = PromiseA.promisifyAll(require('fs')); module.exports.create = function (deps, conf) { + var hrIds = require('human-readable-ids').humanReadableIds; var scmp = require('scmp'); var storageDir = path.join(__dirname, '..', 'var'); @@ -73,8 +74,31 @@ module.exports.create = function (deps, conf) { } }; + var mdnsId = { + _filename: 'mdns-id' + , get: function () { + var self = this; + return read("mdns-id").then(function (result) { + if (typeof result !== 'string') { + throw new Error('mDNS ID not present'); + } + return result; + }).catch(function () { + return self.set(hrIds.random()); + }); + } + + , set: function (value) { + var self = this; + return write(self._filename, value).then(function () { + return self.get(); + }); + } + }; + return { owners: owners , config: config + , mdnsId: mdnsId }; }; diff --git a/package.json b/package.json index 2fe2407..4a5e5ba 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "finalhandler": "^0.4.0", "greenlock": "git+https://git.daplie.com/Daplie/node-greenlock.git#master", "http-proxy": "^1.16.2", + "human-readable-ids": "git+https://git.daplie.com/Daplie/human-readable-ids-js#master", "ipaddr.js": "git+https://github.com/whitequark/ipaddr.js.git#v1.3.0", "ipify": "^1.1.0", "js-yaml": "^3.8.3",