2017-09-18 23:40:11 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var dnsjs = require('dns-suite');
|
|
|
|
var crypto = require('crypto');
|
|
|
|
var dgram = require('dgram');
|
|
|
|
|
|
|
|
function request(queryAb, opts) {
|
|
|
|
var handlers = {};
|
2017-09-19 00:06:27 +00:00
|
|
|
var nameservers;
|
|
|
|
var nameserver = opts.nameserver;
|
|
|
|
var index;
|
|
|
|
var udpType;
|
|
|
|
var receivedMessage;
|
|
|
|
if (!nameserver) {
|
|
|
|
nameservers = require('dns').getServers();
|
|
|
|
index = crypto.randomBytes(2).readUInt16BE(0) % nameservers.length;
|
|
|
|
nameserver = nameservers[index];
|
|
|
|
}
|
|
|
|
udpType = /:/.test(nameserver) ? 'udp6' : 'udp4';
|
2017-09-18 23:40:11 +00:00
|
|
|
var server = dgram.createSocket({
|
2017-09-19 00:06:27 +00:00
|
|
|
type: udpType
|
2017-09-18 23:40:11 +00:00
|
|
|
, reuseAddr: true
|
|
|
|
});
|
2017-09-19 00:06:27 +00:00
|
|
|
server.nameserver = nameserver;
|
2017-09-18 23:40:11 +00:00
|
|
|
|
|
|
|
handlers.onError = function (err) {
|
2017-09-20 18:50:32 +00:00
|
|
|
if (opts.onError) { opts.onError(err); } else { throw err; }
|
2017-09-18 23:40:11 +00:00
|
|
|
server.close();
|
|
|
|
};
|
|
|
|
handlers.onMessage = function (bin) {
|
2017-09-19 00:06:27 +00:00
|
|
|
receivedMessage = true;
|
2017-09-18 23:40:11 +00:00
|
|
|
if (!opts.mdns) {
|
2017-09-19 00:06:27 +00:00
|
|
|
clearTimeout(server._timeoutToken);
|
2017-09-18 23:40:11 +00:00
|
|
|
server.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.onMessage) { opts.onMessage(bin); }
|
|
|
|
};
|
|
|
|
handlers.onListening = function () {
|
|
|
|
/*jshint validthis:true*/
|
|
|
|
var server = this;
|
|
|
|
|
2017-09-19 00:06:27 +00:00
|
|
|
if (opts.mdns || '224.0.0.251' === server.nameserver) {
|
2017-09-18 23:40:11 +00:00
|
|
|
server.setBroadcast(true);
|
2017-09-19 00:06:27 +00:00
|
|
|
server.addMembership(server.nameserver || '224.0.0.251');
|
2017-09-18 23:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.onListening) { opts.onListening.apply(server); }
|
|
|
|
|
|
|
|
server.send(Buffer.from(queryAb), opts.port, server.nameserver, function () {
|
|
|
|
if (opts.onSent) { opts.onSent({ port: opts.port, nameserver: server.nameserver }); }
|
|
|
|
});
|
|
|
|
};
|
2017-09-20 18:50:32 +00:00
|
|
|
handlers.onClose = function () {
|
|
|
|
if (opts.onClose) { opts.onClose(); }
|
|
|
|
};
|
2017-09-18 23:40:11 +00:00
|
|
|
|
|
|
|
server.on('error', handlers.onError);
|
|
|
|
server.on('message', handlers.onMessage);
|
|
|
|
server.on('listening', handlers.onListening);
|
2017-09-20 18:50:32 +00:00
|
|
|
server.on('close', handlers.onClose);
|
2017-09-18 23:40:11 +00:00
|
|
|
|
|
|
|
// 0 dns request
|
|
|
|
// 53 dns server
|
|
|
|
// 5353 mdns
|
|
|
|
if (opts.mdns) {
|
|
|
|
server.bind(opts.port /*5353*/);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
server.bind(0);
|
|
|
|
}
|
2017-09-19 00:06:27 +00:00
|
|
|
var ms = opts.timeout || (5 * 1000);
|
|
|
|
server._timeoutToken = setTimeout(function () {
|
|
|
|
if (!receivedMessage && opts.onTimeout) { opts.onTimeout({ timeout: ms }); }
|
|
|
|
server.close();
|
|
|
|
}, ms);
|
2017-09-18 23:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function requestJson(query, opts) {
|
|
|
|
var queryAb = dnsjs.DNSPacket.write(query);
|
|
|
|
var options = {
|
|
|
|
onError: opts.onError
|
|
|
|
, onMessage: function (nb) {
|
|
|
|
var packet = dnsjs.DNSPacket.parse(nb.buffer.slice(nb.byteOffset, nb.byteOffset + nb.byteLength));
|
|
|
|
|
|
|
|
opts.onMessage(packet);
|
|
|
|
}
|
|
|
|
, onListening: opts.onListening
|
|
|
|
, onSent: opts.onSent
|
|
|
|
, mdns: opts.mdns
|
|
|
|
, nameserver: opts.nameserver
|
|
|
|
, port: opts.port
|
|
|
|
, timeout: opts.timeout
|
|
|
|
};
|
|
|
|
|
|
|
|
return request(queryAb, options);
|
|
|
|
}
|
|
|
|
|
2017-09-20 18:50:32 +00:00
|
|
|
module.exports.resolve = request;
|
2017-09-18 23:40:11 +00:00
|
|
|
module.exports.request = request;
|
|
|
|
module.exports.requestJson = requestJson;
|