From 94cf2aa4bc8193563eaadcfe5d966f54ce271ec6 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 10 Jan 2018 08:14:05 +0000 Subject: [PATCH] WIP add http rest interface --- bin/digd.js | 15 ++++++++++-- lib/httpd.js | 31 +++++++++++++++++++++++++ lib/store.json.js | 58 ++++++++++++++++++++++++++++++----------------- 3 files changed, 81 insertions(+), 23 deletions(-) create mode 100644 lib/httpd.js diff --git a/bin/digd.js b/bin/digd.js index 21f0bfb..cce2ea8 100755 --- a/bin/digd.js +++ b/bin/digd.js @@ -34,6 +34,7 @@ cli.parse({ //, 'serve': [ 's', 'path to json file with array of responses to issue for given queries', 'string' ] //, 'type': [ 't', 'type (defaults to ANY for dns and PTR for mdns)', 'string' ] //, 'query': [ 'q', 'a superfluous explicit option to set the query as a command line flag' ] +, 'http': [ false, 'enable http on the specified port', 'int' ] }); cli.main(function (args, cli) { @@ -95,6 +96,8 @@ cli.main(function (args, cli) { } } + var engine; + var engineOpts = { filepath: path.resolve(cli.input) }; var dnsd = {}; dnsd.onMessage = function (nb, cb) { var byteOffset = nb._dnsByteOffset || nb.byteOffset; @@ -383,9 +386,8 @@ cli.main(function (args, cli) { sendResponse(resp); } - var engine; try { - engine = require('../lib/store.json.js').create({ filepath: path.resolve(cli.input) }); + engine = engine || require('../lib/store.json.js').create(engineOpts); } catch(e) { respondWithResults(e); return; @@ -409,6 +411,15 @@ cli.main(function (args, cli) { if (cli.tcp /* TODO v1.3 !cli.notcp */) { require('../lib/tcpd.js').create(cli, dnsd); } + if (cli.http) { + try { + engine = engine || require('../lib/store.json.js').create(engineOpts); + } catch(e) { + respondWithResults(e); + return; + } + require('../lib/httpd.js').create(cli, engine, dnsd); + } console.log(''); if (!cli.nocmd) { diff --git a/lib/httpd.js b/lib/httpd.js new file mode 100644 index 0000000..3fb0c12 --- /dev/null +++ b/lib/httpd.js @@ -0,0 +1,31 @@ +'use strict'; + +module.exports.create = function (cli, engine, dnsd) { + + function runHttp() { + var path = require('path'); + var express = require('express'); + var app = express(); + var httpServer = require('http').createServer(app); + + app.use('/', express.static(path.join(__dirname, 'public'))); + app.get('/api/peers', function (req, res) { + res.send({ nameservers: [] }); + }); + app.get('/api/zones', function (req, res) { + engine.zones.all(function (zones) { + res.send({ zones: zones }); + }); + }); + app.get('/api/records/:zone', function (req, res) { + engine.records.all(function (records) { + res.send({ records: records }); + }); + }); + + httpServer.listen(cli.http); + } + + runHttp(); + +}; diff --git a/lib/store.json.js b/lib/store.json.js index 55e766c..8e14003 100644 --- a/lib/store.json.js +++ b/lib/store.json.js @@ -7,31 +7,47 @@ module.exports.create = function (opts) { var db = require(opts.filepath); engine.primaryNameservers = db.primaryNameservers; - engine.getSoas = function (query, cb) { - var myDomains = db.domains.filter(function (d) { - return -1 !== query.names.indexOf(d.id.toLowerCase()); - }); - process.nextTick(function () { - cb(null, myDomains); - }); + engine.zones = { + all: function (query, cb) { + process.nextTick(function () { + cb(null, db.domains.slice(0)); + }); + } + , get: function (query, cb) { + var myDomains = db.domains.filter(function (d) { + return -1 !== query.names.indexOf(d.id.toLowerCase()); + }); + process.nextTick(function () { + cb(null, myDomains); + }); + } }; - engine.getRecords = function (query, cb) { - var myRecords = db.records.slice(0).filter(function (r) { + engine.records = { + all: function (query, cb) { + process.nextTick(function () { + cb(null, db.records.slice(0)); + }); + } + , get: function (query, cb) { + var myRecords = db.records.slice(0).filter(function (r) { - if ('string' !== typeof r.name) { - return false; - } + if ('string' !== typeof r.name) { + return false; + } - // TODO use IN in masterquest (or implement OR) - // Only return single-level wildcard? - if (query.name === r.name || ('*.' + query.name.split('.').slice(1).join('.')) === r.name) { - return true; - } - }); - process.nextTick(function () { - cb(null, myRecords); - }); + // TODO use IN in masterquest (or implement OR) + // Only return single-level wildcard? + if (query.name === r.name || ('*.' + query.name.split('.').slice(1).join('.')) === r.name) { + return true; + } + }); + process.nextTick(function () { + cb(null, myRecords); + }); + } }; + engine.getSoas = engine.zones.get; + engine.getRecords = engine.records.get; return engine; };