Refactor port checking (#3336)

This commit is contained in:
Aya Morisawa 2018-11-20 11:23:32 +09:00 committed by GitHub
parent 375b2bb284
commit 4a77548672
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -106,6 +106,14 @@ const runningNodejsVersion = process.version.slice(1).split('.').map(x => parseI
const requiredNodejsVersion = [10, 0, 0]; const requiredNodejsVersion = [10, 0, 0];
const satisfyNodejsVersion = !lessThan(runningNodejsVersion, requiredNodejsVersion); const satisfyNodejsVersion = !lessThan(runningNodejsVersion, requiredNodejsVersion);
function isWellKnownPort(port: number): boolean {
return port < 1024;
}
async function isPortAvailable(port: number): Promise<boolean> {
return await portscanner.checkPortStatus(port, '127.0.0.1') === 'closed';
}
async function showMachine() { async function showMachine() {
const logger = new Logger('Machine'); const logger = new Logger('Machine');
logger.info(`Hostname: ${os.hostname()}`); logger.info(`Hostname: ${os.hostname()}`);
@ -172,12 +180,12 @@ async function init(): Promise<Config> {
process.exit(1); process.exit(1);
} }
if (process.platform === 'linux' && !isRoot() && config.port < 1024) { if (process.platform === 'linux' && isWellKnownPort(config.port) && !isRoot()) {
Logger.error('You need root privileges to listen on port below 1024 on Linux'); Logger.error('You need root privileges to listen on well-known port on Linux');
process.exit(1); process.exit(1);
} }
if (await portscanner.checkPortStatus(config.port, '127.0.0.1') === 'open') { if (!await isPortAvailable(config.port)) {
Logger.error(`Port ${config.port} is already in use`); Logger.error(`Port ${config.port} is already in use`);
process.exit(1); process.exit(1);
} }