96 lines
1.7 KiB
JavaScript
96 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
function bindTcpAndRelease(port, cb) {
|
|
var server = require('net').createServer();
|
|
server.on('error', function (e) {
|
|
cb(e);
|
|
});
|
|
server.listen(port, function () {
|
|
server.close();
|
|
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 = {};
|
|
|
|
bindTcpAndRelease(80, function (e) {
|
|
if (e) {
|
|
failed[80] = e;
|
|
//console.log(e.code);
|
|
//console.log(e.message);
|
|
} else {
|
|
bound['80'] = true;
|
|
}
|
|
|
|
bindTcpAndRelease(443, function (e) {
|
|
if (e) {
|
|
failed[443] = e;
|
|
} else {
|
|
bound['443'] = true;
|
|
}
|
|
|
|
if (bound['80'] && bound['443']) {
|
|
cb(null, bound);
|
|
return;
|
|
}
|
|
|
|
console.warn("default ports 80 and 443 are not available, trying 8443");
|
|
|
|
bindTcpAndRelease(8443, function (e) {
|
|
if (e) {
|
|
failed[8443] = e;
|
|
} else {
|
|
bound['8443'] = true;
|
|
}
|
|
|
|
cb(failed, bound);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
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;
|