small refactor

This commit is contained in:
AJ ONeal 2017-09-25 15:12:58 -06:00
parent 6d0f9b1588
commit 083df5755b
1 changed files with 102 additions and 101 deletions

View File

@ -169,6 +169,106 @@ cli.main(function (args, cli) {
}); });
} }
function recurse() {
if (!query.header.rd) {
console.log("[Could not answer. Sent empty response.]");
sendEmptyResponse(query);
return;
}
if (cli.norecurse) {
console.log("[Could not answer. Sent empty response.]");
sendEmptyResponse(query);
return;
}
// ANY, A, AAAA, CNAME, MX, NAPTR, NS, PTR, SOA, SRV, TXT
newQuery = {
header: {
id: query.header.id // require('crypto').randomBytes(2).readUInt16BE(0)
, qr: 0
, opcode: 0
, aa: query.header.aa ? 1 : 0 // NA? not sure what this would do
, tc: 0 // NA
, rd: 1
, ra: 0 // NA
, rcode: 0 // NA
}
, question: []
, answer: []
, authority: []
, additional: []
};
query.question.forEach(function (q) {
newQuery.question.push({
name: q.name
, type: q.type
, typeName: q.typeName
, class: q.class
, className: q.className
});
function updateCount() {
count -= 1;
if (!count) {
server.send(dnsjs.DNSPacket.write(newQuery), rinfo.port, rinfo.address, function () {
console.log('[DEV] response sent');
});
}
}
var opts = {
onError: function () {
updateCount();
}
, onMessage: function (packet) {
(packet.answer||[]).forEach(function (a) {
// TODO copy each relevant property
console.log('ans', a);
newQuery.answer.push(a);
});
(packet.authority||[]).forEach(function (a) {
// TODO copy each relevant property
console.log('auth', a);
newQuery.authority.push(a);
});
(packet.additional||[]).forEach(function (a) {
// TODO copy each relevant property
console.log('add', a);
newQuery.additional.push(a);
});
updateCount();
}
, onListening: function () {}
, onSent: function (res) {
if (cli.debug) {
console.log('');
console.log('request sent to', res.nameserver);
}
}
, onTimeout: function (res) {
console.log(";; [" + q.name + "] connection timed out; no servers could be reached");
console.log(";; [timed out after " + res.timeout + "ms and 1 tries]");
}
, onClose: function () {
console.log('');
}
, mdns: cli.mdns
, nameserver: cli.nameserver
, port: cli.port
, timeout: cli.timeout
};
//dig.resolve(queryAb, opts);
dig.resolveJson(query, opts);
console.log(';' + q.name + '.', ' ', q.className, q.typeName);
});
}
count = query.question.length; count = query.question.length;
if (!count) { if (!count) {
sendEmptyResponse(query); sendEmptyResponse(query);
@ -176,107 +276,8 @@ cli.main(function (args, cli) {
} }
// TODO get local answer first, if available // TODO get local answer first, if available
recurse();
if (query.header.rd) {
if (cli.norecurse) {
console.log("[Could not answer. Sent empty response.]");
sendEmptyResponse(query);
return;
} else {
// ANY, A, AAAA, CNAME, MX, NAPTR, NS, PTR, SOA, SRV, TXT
newQuery = {
header: {
id: query.header.id // require('crypto').randomBytes(2).readUInt16BE(0)
, qr: 0
, opcode: 0
, aa: query.header.aa ? 1 : 0 // NA? not sure what this would do
, tc: 0 // NA
, rd: 1
, ra: 0 // NA
, rcode: 0 // NA
}
, question: [
/*
{ name: cli.query
, typeName: cli.type
, className: cli.class
}
*/
]
, answer: []
, authority: []
, additional: []
};
query.question.forEach(function (q) {
newQuery.question.push({
name: q.name
, type: q.type
, typeName: q.typeName
, class: q.class
, className: q.className
});
function updateCount() {
count -= 1;
if (!count) {
server.send(dnsjs.DNSPacket.write(newQuery), rinfo.port, rinfo.address, function () {
console.log('[DEV] response sent');
});
}
}
var opts = {
onError: function () {
updateCount();
}
, onMessage: function (packet) {
(packet.answer||[]).forEach(function (a) {
// TODO copy each relevant property
console.log('ans', a);
newQuery.answer.push(a);
});
(packet.authority||[]).forEach(function (a) {
// TODO copy each relevant property
console.log('auth', a);
newQuery.authority.push(a);
});
(packet.additional||[]).forEach(function (a) {
// TODO copy each relevant property
console.log('add', a);
newQuery.additional.push(a);
});
updateCount();
}
, onListening: function () {}
, onSent: function (res) {
if (cli.debug) {
console.log('');
console.log('request sent to', res.nameserver);
}
}
, onTimeout: function (res) {
console.log(";; [" + q.name + "] connection timed out; no servers could be reached");
console.log(";; [timed out after " + res.timeout + "ms and 1 tries]");
}
, onClose: function () {
console.log('');
}
, mdns: cli.mdns
, nameserver: cli.nameserver
, port: cli.port
, timeout: cli.timeout
};
//dig.resolve(queryAb, opts);
dig.resolveJson(query, opts);
console.log(';' + q.name + '.', ' ', q.className, q.typeName);
});
}
}
}; };
handlers.onListening = function () { handlers.onListening = function () {
@ -305,7 +306,7 @@ cli.main(function (args, cli) {
console.log(''); console.log('');
if (!cli.nocmd) { if (!cli.nocmd) {
console.log('; <<>> dig.js ' + 'v0.0.0' + ' <<>> ' + process.argv.slice(2)); console.log('; <<>> digd.js ' + 'v0.0.0' + ' <<>> ' + process.argv.slice(2));
console.log(';; global options: +cmd'); console.log(';; global options: +cmd');
} }