Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
AJ ONeal | 823819c982 | |
AJ ONeal | abaed056e5 | |
AJ ONeal | e2393d965a |
|
@ -27,9 +27,10 @@ module.exports.create = function (cli, dnsd) {
|
||||||
|
|
||||||
// TODO pad two bytes for lengths
|
// TODO pad two bytes for lengths
|
||||||
dnsd.onMessage(nb, function (err, newAb, dbgmsg) {
|
dnsd.onMessage(nb, function (err, newAb, dbgmsg) {
|
||||||
var lenbuf = Buffer.from([ newAb.length >> 8, newAb.length & 255 ]);
|
|
||||||
// TODO XXX generate legit error packet
|
// TODO XXX generate legit error packet
|
||||||
if (err) { console.error("Error", err); c.end(); return; }
|
if (err) { console.error("Error", err); c.end(); return; }
|
||||||
|
|
||||||
|
var lenbuf = Buffer.from([ newAb.length >> 8, newAb.length & 255 ]);
|
||||||
console.log('TCP ' + dbgmsg);
|
console.log('TCP ' + dbgmsg);
|
||||||
|
|
||||||
c.write(lenbuf);
|
c.write(lenbuf);
|
||||||
|
@ -53,10 +54,11 @@ module.exports.create = function (cli, dnsd) {
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
}
|
||||||
console.error("TCP Server Error:");
|
console.error("TCP Server Error:");
|
||||||
console.error(err);
|
console.error(err.stack);
|
||||||
tcpServer.close(function () {
|
tcpServer.close(function () {
|
||||||
setTimeout(runTcp, 1000);
|
setTimeout(runTcp, 1000);
|
||||||
});
|
});
|
||||||
|
//throw new Error(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
tcpServer.listen(cli.port, function () {
|
tcpServer.listen(cli.port, function () {
|
||||||
|
|
104
lib/udpd.js
104
lib/udpd.js
|
@ -1,58 +1,66 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
module.exports.create = function (cli, dnsd) {
|
module.exports.create = function (cli, dnsd) {
|
||||||
var server = require('dgram').createSocket({
|
function runUdp() {
|
||||||
type: cli.udp6 ? 'udp6' : 'udp4'
|
var server = require('dgram').createSocket({
|
||||||
, reuseAddr: true
|
type: cli.udp6 ? 'udp6' : 'udp4'
|
||||||
});
|
, reuseAddr: true
|
||||||
server.bind({
|
});
|
||||||
port: cli.port
|
server.bind({
|
||||||
, address: cli.address
|
port: cli.port
|
||||||
});
|
, address: cli.address
|
||||||
|
|
||||||
var handlers = {};
|
|
||||||
handlers.onError = function (err) {
|
|
||||||
if ('EACCES' === err.code) {
|
|
||||||
console.error("");
|
|
||||||
console.error("EACCES: Couldn't bind to port. You probably need to use sudo, authbind, or setcap.");
|
|
||||||
console.error("");
|
|
||||||
process.exit(123);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
console.error("error:", err.stack);
|
|
||||||
server.close();
|
|
||||||
};
|
|
||||||
|
|
||||||
handlers.onMessage = function (nb, rinfo) {
|
|
||||||
//console.log('[DEBUG] got a UDP message', nb.length);
|
|
||||||
//console.log(nb.toString('hex'));
|
|
||||||
|
|
||||||
dnsd.onMessage(nb, function (err, newAb, dbgmsg) {
|
|
||||||
// TODO send legit error message
|
|
||||||
if (err) { server.send(Buffer.from([0x00])); return; }
|
|
||||||
server.send(newAb, rinfo.port, rinfo.address, function () {
|
|
||||||
console.log(dbgmsg, rinfo.port, rinfo.address);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
|
||||||
|
|
||||||
handlers.onListening = function () {
|
var handlers = {};
|
||||||
/*jshint validthis:true*/
|
handlers.onError = function (err) {
|
||||||
var server = this;
|
if ('EACCES' === err.code) {
|
||||||
|
console.error("");
|
||||||
|
console.error("EACCES: Couldn't bind to port. You probably need to use sudo, authbind, or setcap.");
|
||||||
|
console.error("");
|
||||||
|
process.exit(123);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.error("UDP Server Error:");
|
||||||
|
console.error(err.stack);
|
||||||
|
server.close(function () {
|
||||||
|
setTimeout(runUdp, 1000);
|
||||||
|
});
|
||||||
|
//throw new Error(err);
|
||||||
|
};
|
||||||
|
|
||||||
if (cli.mdns || '224.0.0.251' === cli.nameserver) {
|
handlers.onMessage = function (nb, rinfo) {
|
||||||
server.setBroadcast(true);
|
//console.log('[DEBUG] got a UDP message', nb.length);
|
||||||
server.addMembership(cli.nameserver);
|
//console.log(nb.toString('hex'));
|
||||||
}
|
|
||||||
|
|
||||||
console.log('');
|
dnsd.onMessage(nb, function (err, newAb, dbgmsg) {
|
||||||
console.log('Bound and Listening:');
|
// TODO send legit error message
|
||||||
console.log(server.address().address + '#' + server.address().port + ' (' + server.type + ')');
|
if (err) { server.send(Buffer.from([0x00]), rinfo.port, rinfo.address); return; }
|
||||||
};
|
server.send(newAb, rinfo.port, rinfo.address, function () {
|
||||||
|
console.log('[dnsd.onMessage] ' + dbgmsg, rinfo.port, rinfo.address);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
server.on('error', handlers.onError);
|
handlers.onListening = function () {
|
||||||
server.on('message', handlers.onMessage);
|
/*jshint validthis:true*/
|
||||||
server.on('listening', handlers.onListening);
|
var server = this;
|
||||||
|
|
||||||
return server;
|
if (cli.mdns || '224.0.0.251' === cli.nameserver) {
|
||||||
|
server.setBroadcast(true);
|
||||||
|
server.addMembership(cli.nameserver);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('');
|
||||||
|
console.log('Bound and Listening:');
|
||||||
|
console.log(server.address().address + '#' + server.address().port + ' (' + server.type + ')');
|
||||||
|
};
|
||||||
|
|
||||||
|
server.on('error', handlers.onError);
|
||||||
|
server.on('message', handlers.onMessage);
|
||||||
|
server.on('listening', handlers.onListening);
|
||||||
|
|
||||||
|
return server;
|
||||||
|
}
|
||||||
|
|
||||||
|
return runUdp();
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "digd.js",
|
"name": "digd.js",
|
||||||
"version": "1.2.1",
|
"version": "1.2.2",
|
||||||
"description": "A lightweight DNS / mDNS daemon (server) for creating and capturing DNS and mDNS query and response packets to disk as binary and/or JSON. Options are similar to the Unix dig command.",
|
"description": "A lightweight DNS / mDNS daemon (server) for creating and capturing DNS and mDNS query and response packets to disk as binary and/or JSON. Options are similar to the Unix dig command.",
|
||||||
"main": "bin/digd.js",
|
"main": "bin/digd.js",
|
||||||
"homepage": "https://git.coolaj86.com/coolaj86/digd.js",
|
"homepage": "https://git.coolaj86.com/coolaj86/digd.js",
|
||||||
|
|
Loading…
Reference in New Issue