implemented UDP forwarding for DNS

This commit is contained in:
tigerbot 2017-05-03 13:55:16 -06:00
parent f4de15b14f
commit 0a7e70517f
3 changed files with 115 additions and 15 deletions

View File

@ -61,6 +61,9 @@ function readConfigAndRun(args) {
);
}
}
if (!config.dns) {
config.dns = { proxy: { port: 3053 } };
}
if (!config.tcp) {
config.tcp = {};
}
@ -116,24 +119,63 @@ function readConfigAndRun(args) {
config.addresses = addresses;
config.device = { hostname: 'daplien-pod' };
var PromiseA = require('bluebird');
var tcpProm, dnsProm;
if (config.tcp.ports) {
run(config);
return;
tcpProm = PromiseA.resolve();
} else {
tcpProm = new PromiseA(function (resolve, reject) {
require('../lib/check-ports').checkTcpPorts(function (failed, bound) {
config.tcp.ports = Object.keys(bound);
if (config.tcp.ports.length) {
resolve();
} else {
reject(failed);
}
});
});
}
require('../lib/check-ports.js').checkPorts(config, function (failed, bound) {
config.tcp.ports = Object.keys(bound);
if (config.dns.bind) {
dnsProm = PromiseA.resolve();
} else {
dnsProm = new PromiseA(function (resolve) {
require('../lib/check-ports').checkUdpPorts(function (failed, bound) {
var ports = Object.keys(bound);
if (!config.tcp.ports.length) {
if (ports.length === 0) {
// I don't think we want to prevent the rest of the app from running in
// this case like we do for TCP, do don't call reject.
console.warn('could not bind to the desired ports for DNS');
Object.keys(failed).forEach(function (key) {
console.log('[error bind]', key, failed[key].code);
});
}
else if (ports.length === 1) {
config.dns.bind = parseInt(ports[0], 10);
}
else {
config.dns.bind = ports.map(function (numStr) {
return parseInt(numStr, 10);
});
}
resolve();
});
});
}
PromiseA.all([tcpProm, dnsProm])
.then(function () {
run(config);
})
.catch(function (failed) {
console.warn("could not bind to the desired ports");
Object.keys(failed).forEach(function (key) {
console.log('[error bind]', key, failed[key].code);
});
return;
}
run(config);
});
});
}
function readEnv(args) {

View File

@ -11,7 +11,18 @@ function bindTcpAndRelease(port, cb) {
});
}
function checkPorts(config, cb) {
function bindUdpAndRelease(port, cb) {
var socket = require('dgram').createSocket('udp4');
socket.on('error', function (e) {
cb(e);
});
socket.bind(port, function () {
socket.close();
cb();
});
}
function checkTcpPorts(cb) {
var bound = {};
var failed = {};
@ -32,7 +43,6 @@ function checkPorts(config, cb) {
}
if (bound['80'] && bound['443']) {
//config.tcp.ports = [ 80, 443 ];
cb(null, bound);
return;
}
@ -52,4 +62,34 @@ function checkPorts(config, cb) {
});
}
module.exports.checkPorts = checkPorts;
function checkUdpPorts(cb) {
var bound = {};
var failed = {};
bindUdpAndRelease(53, function (e) {
if (e) {
failed[53] = e;
} else {
bound[53] = true;
}
if (bound[53]) {
cb(null, bound);
return;
}
console.warn("default DNS port 53 not available, trying 8053");
bindUdpAndRelease(8053, function (e) {
if (e) {
failed[8053] = e;
} else {
bound[8053] = true;
}
cb(null, bound);
});
});
}
module.exports.checkTcpPorts = checkTcpPorts;
module.exports.checkUdpPorts = checkUdpPorts;

View File

@ -236,6 +236,12 @@ module.exports.create = function (deps, config) {
});
}
function dnsListener(msg) {
var dgram = require('dgram');
var socket = dgram.createSocket('udp4');
socket.send(msg, config.dns.proxy.port, config.dns.proxy.address || '127.0.0.1');
}
function approveDomains(opts, certs, cb) {
// This is where you check your database and associated
// email addresses with domains and agreements and such
@ -448,7 +454,19 @@ module.exports.create = function (deps, config) {
});
});
PromiseA.all(config.tcp.ports.map(function (port) {
var listenPromises = config.tcp.ports.map(function (port) {
return listeners.tcp.add(port, netHandler);
}));
});
if (config.dns.bind) {
if (Array.isArray(config.dns.bind)) {
listenPromises = listenPromises.concat(config.dns.bind.map(function (port) {
return listeners.udp.add(port, dnsListener);
}));
} else {
listenPromises.push(listeners.udp.add(config.dns.bind, dnsListener));
}
}
return PromiseA.all(listenPromises);
};