WIP add http rest interface

This commit is contained in:
AJ ONeal 2018-01-10 08:14:05 +00:00
parent abaed056e5
commit 94cf2aa4bc
3 changed files with 81 additions and 23 deletions

View File

@ -34,6 +34,7 @@ cli.parse({
//, 'serve': [ 's', 'path to json file with array of responses to issue for given queries', 'string' ] //, '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' ] //, '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' ] //, '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) { 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 = {}; var dnsd = {};
dnsd.onMessage = function (nb, cb) { dnsd.onMessage = function (nb, cb) {
var byteOffset = nb._dnsByteOffset || nb.byteOffset; var byteOffset = nb._dnsByteOffset || nb.byteOffset;
@ -383,9 +386,8 @@ cli.main(function (args, cli) {
sendResponse(resp); sendResponse(resp);
} }
var engine;
try { try {
engine = require('../lib/store.json.js').create({ filepath: path.resolve(cli.input) }); engine = engine || require('../lib/store.json.js').create(engineOpts);
} catch(e) { } catch(e) {
respondWithResults(e); respondWithResults(e);
return; return;
@ -409,6 +411,15 @@ cli.main(function (args, cli) {
if (cli.tcp /* TODO v1.3 !cli.notcp */) { if (cli.tcp /* TODO v1.3 !cli.notcp */) {
require('../lib/tcpd.js').create(cli, dnsd); 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(''); console.log('');
if (!cli.nocmd) { if (!cli.nocmd) {

31
lib/httpd.js Normal file
View File

@ -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();
};

View File

@ -7,31 +7,47 @@ module.exports.create = function (opts) {
var db = require(opts.filepath); var db = require(opts.filepath);
engine.primaryNameservers = db.primaryNameservers; engine.primaryNameservers = db.primaryNameservers;
engine.getSoas = function (query, cb) { engine.zones = {
var myDomains = db.domains.filter(function (d) { all: function (query, cb) {
return -1 !== query.names.indexOf(d.id.toLowerCase()); process.nextTick(function () {
}); cb(null, db.domains.slice(0));
process.nextTick(function () { });
cb(null, myDomains); }
}); , 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) { engine.records = {
var myRecords = db.records.slice(0).filter(function (r) { 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) { if ('string' !== typeof r.name) {
return false; return false;
} }
// TODO use IN in masterquest (or implement OR) // TODO use IN in masterquest (or implement OR)
// Only return single-level wildcard? // Only return single-level wildcard?
if (query.name === r.name || ('*.' + query.name.split('.').slice(1).join('.')) === r.name) { if (query.name === r.name || ('*.' + query.name.split('.').slice(1).join('.')) === r.name) {
return true; return true;
} }
}); });
process.nextTick(function () { process.nextTick(function () {
cb(null, myRecords); cb(null, myRecords);
}); });
}
}; };
engine.getSoas = engine.zones.get;
engine.getRecords = engine.records.get;
return engine; return engine;
}; };