remove port scanning for an available port #3036

This commit is contained in:
zadam 2022-08-03 00:19:29 +02:00
parent f161488c13
commit 8588ed5eec
6 changed files with 17 additions and 41 deletions

View file

@ -65,7 +65,6 @@
"node-abi": "3.22.0", "node-abi": "3.22.0",
"normalize-strings": "1.1.1", "normalize-strings": "1.1.1",
"open": "8.4.0", "open": "8.4.0",
"portscanner": "2.2.0",
"rand-token": "1.0.1", "rand-token": "1.0.1",
"react": "17.0.2", "react": "17.0.2",
"react-dom": "17.0.2", "react-dom": "17.0.2",

View file

@ -1,10 +1,3 @@
const config = require('./config'); const config = require('./config');
const env = require('./env');
let environmentHost; module.exports = process.env.TRILIUM_HOST || config['Network']['host'] || '0.0.0.0';
if (process.env.TRILIUM_HOST) {
environmentHost = process.env.TRILIUM_HOST;
}
module.exports = Promise.resolve(environmentHost || config['Network']['host'] || '0.0.0.0');

View file

@ -1,28 +1,15 @@
const config = require('./config'); const config = require('./config');
const utils = require('./utils'); const utils = require('./utils');
const env = require('./env'); const env = require('./env');
const portscanner = require('portscanner');
let environmentPort;
if (process.env.TRILIUM_PORT) { if (process.env.TRILIUM_PORT) {
environmentPort = parseInt(process.env.TRILIUM_PORT); module.exports = parseInt(process.env.TRILIUM_PORT);
return;
} }
if (utils.isElectron()) { if (utils.isElectron()) {
module.exports = new Promise((resolve, reject) => { module.exports = env.isDev() ? 37740 : 37840;
const startingPort = environmentPort || (env.isDev() ? 37740 : 37840);
portscanner.findAPortNotInUse(startingPort, startingPort + 10, '127.0.0.1', function(error, port) {
if (error) {
reject(error);
} }
else { else {
resolve(port); module.exports = config['Network']['port'] || '3000';
}
})
});
}
else {
module.exports = Promise.resolve(environmentPort || config['Network']['port'] || '3000');
} }

View file

@ -33,7 +33,7 @@ function isDbInitialized() {
async function initDbConnection() { async function initDbConnection() {
if (!isDbInitialized()) { if (!isDbInitialized()) {
log.info(`DB not initialized, please visit setup page` + 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; return;
} }

View file

@ -35,7 +35,7 @@ async function createExtraWindow(notePath, hoistedNoteId = 'root') {
}); });
win.setMenuBarVisibility(false); 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); configureWebContents(win.webContents, spellcheckEnabled);
} }
@ -77,7 +77,7 @@ async function createMainWindow(app) {
mainWindowState.manage(mainWindow); mainWindowState.manage(mainWindow);
mainWindow.setMenuBarVisibility(false); 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); mainWindow.on('closed', () => mainWindow = null);
configureWebContents(mainWindow.webContents, spellcheckEnabled); configureWebContents(mainWindow.webContents, spellcheckEnabled);
@ -143,7 +143,7 @@ async function createSetupWindow() {
}); });
setupWindow.setMenuBarVisibility(false); 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); setupWindow.on('closed', () => setupWindow = null);
} }

17
src/www
View file

@ -53,11 +53,8 @@ async function startTrilium() {
*/ */
require("electron").app.requestSingleInstanceLock(); require("electron").app.requestSingleInstanceLock();
const usedPort = await port; app.set('port', port);
const usedHost = await host; app.set('host', host);
app.set('port', usedPort);
app.set('host', usedHost);
// Check from config whether to trust reverse proxies to supply user IPs, hostnames and protocols // Check from config whether to trust reverse proxies to supply user IPs, hostnames and protocols
if (config['Network']['trustedReverseProxy']) { if (config['Network']['trustedReverseProxy']) {
@ -83,12 +80,12 @@ async function startTrilium() {
httpServer = https.createServer(options, app); 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 { else {
httpServer = http.createServer(app); 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)); log.info(JSON.stringify(appInfo, null, 2));
@ -101,7 +98,7 @@ async function startTrilium() {
*/ */
httpServer.keepAliveTimeout = 120000 * 5; httpServer.keepAliveTimeout = 120000 * 5;
httpServer.listen(usedPort, usedHost); httpServer.listen(port, host);
httpServer.on('error', error => { httpServer.on('error', error => {
if (error.syscall !== 'listen') { if (error.syscall !== 'listen') {
throw error; throw error;
@ -110,12 +107,12 @@ async function startTrilium() {
// handle specific listen errors with friendly messages // handle specific listen errors with friendly messages
switch (error.code) { switch (error.code) {
case 'EACCES': case 'EACCES':
console.error(`Port ${usedPort} requires elevated privileges`); console.error(`Port ${port} requires elevated privileges`);
process.exit(1); process.exit(1);
break; break;
case 'EADDRINUSE': case 'EADDRINUSE':
console.error(`Port ${usedPort} is already in use`); console.error(`Port ${port} is already in use`);
process.exit(1); process.exit(1);
break; break;