From cad8dd686e5062ce9507c617efb90a3b37d9041d Mon Sep 17 00:00:00 2001 From: tigerbot Date: Fri, 9 Jun 2017 12:14:25 -0600 Subject: [PATCH] changed UDP servers to reuseAddr --- bin/goldilocks.js | 38 ++++---------------------------------- lib/check-ports.js | 41 ----------------------------------------- lib/servers.js | 2 +- 3 files changed, 5 insertions(+), 76 deletions(-) diff --git a/bin/goldilocks.js b/bin/goldilocks.js index 173c951..a995608 100755 --- a/bin/goldilocks.js +++ b/bin/goldilocks.js @@ -141,7 +141,7 @@ function fillConfig(config, args) { config.debug = config.debug || args.debug; if (!config.dns) { - config.dns = { modules: [{ name: 'proxy', port: 3053 }] }; + config.dns = { bind: [ 53 ], modules: [{ name: 'proxy', port: 3053 }] }; } // Use Object.assign to add any properties needed but not defined in the mdns config. // It will first copy the defaults into an empty object, then copy any real config over that. @@ -203,8 +203,7 @@ function fillConfig(config, args) { config.tunnel = args.tunnel || config.tunnel; - var tcpProm, dnsProm; - + var tcpProm; if (config.tcp.bind) { tcpProm = PromiseA.resolve(); } else { @@ -220,42 +219,13 @@ function fillConfig(config, args) { }); } - 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 (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, so 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(); - }); - }); - } - - return PromiseA.all([tcpProm, dnsProm]) + return tcpProm .then(function () { return config; }) .catch(function (failed) { Object.keys(failed).forEach(function (key) { console.log('[error bind]', key, failed[key].code); }); - return PromiseA.reject(new Error("could not bind to the desired ports")); + return PromiseA.reject(new Error("could not bind to the default ports")); }); } diff --git a/lib/check-ports.js b/lib/check-ports.js index f6e6a8e..f137d88 100644 --- a/lib/check-ports.js +++ b/lib/check-ports.js @@ -11,17 +11,6 @@ function bindTcpAndRelease(port, 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 = {}; @@ -62,34 +51,4 @@ function checkTcpPorts(cb) { }); } -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(failed, bound); - }); - }); -} - module.exports.checkTcpPorts = checkTcpPorts; -module.exports.checkUdpPorts = checkUdpPorts; diff --git a/lib/servers.js b/lib/servers.js index c4000a5..0338172 100644 --- a/lib/servers.js +++ b/lib/servers.js @@ -111,7 +111,7 @@ module.exports.addUdpListener = function (port, handler) { } var dgram = require('dgram'); - var server = dgram.createSocket('udp4'); + var server = dgram.createSocket({type: 'udp4', reuseAddr: true}); var resolved = false; stat = dgramMap[port] = {