changed UDP servers to reuseAddr

This commit is contained in:
tigerbot 2017-06-09 12:14:25 -06:00
parent f569391cd9
commit cad8dd686e
3 changed files with 5 additions and 76 deletions

View File

@ -141,7 +141,7 @@ function fillConfig(config, args) {
config.debug = config.debug || args.debug; config.debug = config.debug || args.debug;
if (!config.dns) { 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. // 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. // 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; config.tunnel = args.tunnel || config.tunnel;
var tcpProm, dnsProm; var tcpProm;
if (config.tcp.bind) { if (config.tcp.bind) {
tcpProm = PromiseA.resolve(); tcpProm = PromiseA.resolve();
} else { } else {
@ -220,42 +219,13 @@ function fillConfig(config, args) {
}); });
} }
if (config.dns.bind) { return tcpProm
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])
.then(function () { return config; }) .then(function () { return config; })
.catch(function (failed) { .catch(function (failed) {
Object.keys(failed).forEach(function (key) { Object.keys(failed).forEach(function (key) {
console.log('[error bind]', key, failed[key].code); 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"));
}); });
} }

View File

@ -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) { function checkTcpPorts(cb) {
var bound = {}; var bound = {};
var failed = {}; 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.checkTcpPorts = checkTcpPorts;
module.exports.checkUdpPorts = checkUdpPorts;

View File

@ -111,7 +111,7 @@ module.exports.addUdpListener = function (port, handler) {
} }
var dgram = require('dgram'); var dgram = require('dgram');
var server = dgram.createSocket('udp4'); var server = dgram.createSocket({type: 'udp4', reuseAddr: true});
var resolved = false; var resolved = false;
stat = dgramMap[port] = { stat = dgramMap[port] = {