forked from coolaj86/goldilocks.js
changed UDP servers to reuseAddr
This commit is contained in:
parent
f569391cd9
commit
cad8dd686e
|
@ -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"));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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] = {
|
||||
|
|
Loading…
Reference in New Issue