From 8588ed5eec442d7a51bc2b7b67059a8878d27b64 Mon Sep 17 00:00:00 2001 From: zadam Date: Wed, 3 Aug 2022 00:19:29 +0200 Subject: [PATCH] remove port scanning for an available port #3036 --- package.json | 1 - src/services/host.js | 9 +-------- src/services/port.js | 23 +++++------------------ src/services/sql_init.js | 2 +- src/services/window.js | 6 +++--- src/www | 17 +++++++---------- 6 files changed, 17 insertions(+), 41 deletions(-) diff --git a/package.json b/package.json index 6094f86dc..1dc6ce2c8 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,6 @@ "node-abi": "3.22.0", "normalize-strings": "1.1.1", "open": "8.4.0", - "portscanner": "2.2.0", "rand-token": "1.0.1", "react": "17.0.2", "react-dom": "17.0.2", diff --git a/src/services/host.js b/src/services/host.js index e638931bd..389203e99 100644 --- a/src/services/host.js +++ b/src/services/host.js @@ -1,10 +1,3 @@ const config = require('./config'); -const env = require('./env'); -let environmentHost; - -if (process.env.TRILIUM_HOST) { - environmentHost = process.env.TRILIUM_HOST; -} - -module.exports = Promise.resolve(environmentHost || config['Network']['host'] || '0.0.0.0'); +module.exports = process.env.TRILIUM_HOST || config['Network']['host'] || '0.0.0.0'; diff --git a/src/services/port.js b/src/services/port.js index d6504c2f2..9d231c813 100644 --- a/src/services/port.js +++ b/src/services/port.js @@ -1,28 +1,15 @@ const config = require('./config'); const utils = require('./utils'); const env = require('./env'); -const portscanner = require('portscanner'); - -let environmentPort; if (process.env.TRILIUM_PORT) { - environmentPort = parseInt(process.env.TRILIUM_PORT); + module.exports = parseInt(process.env.TRILIUM_PORT); + return; } if (utils.isElectron()) { - module.exports = new Promise((resolve, reject) => { - const startingPort = environmentPort || (env.isDev() ? 37740 : 37840); - - portscanner.findAPortNotInUse(startingPort, startingPort + 10, '127.0.0.1', function(error, port) { - if (error) { - reject(error); - } - else { - resolve(port); - } - }) - }); + module.exports = env.isDev() ? 37740 : 37840; } else { - module.exports = Promise.resolve(environmentPort || config['Network']['port'] || '3000'); -} \ No newline at end of file + module.exports = config['Network']['port'] || '3000'; +} diff --git a/src/services/sql_init.js b/src/services/sql_init.js index 66bc98fe9..182391ac9 100644 --- a/src/services/sql_init.js +++ b/src/services/sql_init.js @@ -33,7 +33,7 @@ function isDbInitialized() { async function initDbConnection() { if (!isDbInitialized()) { log.info(`DB not initialized, please visit setup page` + - (utils.isElectron() ? '' : ` - http://[your-server-host]:${await port} to see instructions on how to initialize Trilium.`)); + (utils.isElectron() ? '' : ` - http://[your-server-host]:${port} to see instructions on how to initialize Trilium.`)); return; } diff --git a/src/services/window.js b/src/services/window.js index f99f374ff..6ccf16a5d 100644 --- a/src/services/window.js +++ b/src/services/window.js @@ -35,7 +35,7 @@ async function createExtraWindow(notePath, hoistedNoteId = 'root') { }); win.setMenuBarVisibility(false); - win.loadURL('http://127.0.0.1:' + await port + '/?extra=1&extraHoistedNoteId=' + hoistedNoteId + '#' + notePath); + win.loadURL(`http://127.0.0.1:${port}/?extra=1&extraHoistedNoteId=${hoistedNoteId}#${notePath}`); configureWebContents(win.webContents, spellcheckEnabled); } @@ -77,7 +77,7 @@ async function createMainWindow(app) { mainWindowState.manage(mainWindow); mainWindow.setMenuBarVisibility(false); - mainWindow.loadURL('http://127.0.0.1:' + await port); + mainWindow.loadURL('http://127.0.0.1:' + port); mainWindow.on('closed', () => mainWindow = null); configureWebContents(mainWindow.webContents, spellcheckEnabled); @@ -143,7 +143,7 @@ async function createSetupWindow() { }); setupWindow.setMenuBarVisibility(false); - setupWindow.loadURL('http://127.0.0.1:' + await port); + setupWindow.loadURL('http://127.0.0.1:' + port); setupWindow.on('closed', () => setupWindow = null); } diff --git a/src/www b/src/www index 8571f921e..e3a26de4d 100644 --- a/src/www +++ b/src/www @@ -53,11 +53,8 @@ async function startTrilium() { */ require("electron").app.requestSingleInstanceLock(); - const usedPort = await port; - const usedHost = await host; - - app.set('port', usedPort); - app.set('host', usedHost); + app.set('port', port); + app.set('host', host); // Check from config whether to trust reverse proxies to supply user IPs, hostnames and protocols if (config['Network']['trustedReverseProxy']) { @@ -83,12 +80,12 @@ async function startTrilium() { httpServer = https.createServer(options, app); - log.info("App HTTPS server starting up at port " + usedPort); + log.info("App HTTPS server starting up at port " + port); } else { httpServer = http.createServer(app); - log.info("App HTTP server starting up at port " + usedPort); + log.info("App HTTP server starting up at port " + port); } log.info(JSON.stringify(appInfo, null, 2)); @@ -101,7 +98,7 @@ async function startTrilium() { */ httpServer.keepAliveTimeout = 120000 * 5; - httpServer.listen(usedPort, usedHost); + httpServer.listen(port, host); httpServer.on('error', error => { if (error.syscall !== 'listen') { throw error; @@ -110,12 +107,12 @@ async function startTrilium() { // handle specific listen errors with friendly messages switch (error.code) { case 'EACCES': - console.error(`Port ${usedPort} requires elevated privileges`); + console.error(`Port ${port} requires elevated privileges`); process.exit(1); break; case 'EADDRINUSE': - console.error(`Port ${usedPort} is already in use`); + console.error(`Port ${port} is already in use`); process.exit(1); break;