2026-10-20 10:50:40 +02:00
|
|
|
|
const { app, BrowserWindow, nativeTheme, Menu, MenuItem, ipcMain } = require('electron/main');
|
2024-05-28 16:43:25 +02:00
|
|
|
|
const path = require('node:path');
|
2026-09-15 15:00:24 +02:00
|
|
|
|
const { fileURLToPath } = require('node:url');
|
2024-05-28 16:43:25 +02:00
|
|
|
|
const fs = require('fs');
|
2024-05-28 18:27:50 +02:00
|
|
|
|
const log = require('electron-log/main');
|
2026-09-15 15:00:24 +02:00
|
|
|
|
const { default: isReachable } = require('is-reachable');
|
|
|
|
|
|
const { setTimeout: delay } = require('node:timers/promises');
|
2024-06-04 14:58:52 +02:00
|
|
|
|
const config = require('./js/config.js');
|
|
|
|
|
|
const { Config } = require('./js/config.js');
|
2026-10-20 10:50:40 +02:00
|
|
|
|
const { isAuthorizedWatchdogEvent: isWatchdogEventAuthorized, registerWatchdogHandler } = require('./js/watchdog.js');
|
2024-05-28 18:27:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
// Properties
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2024-05-28 14:25:32 +02:00
|
|
|
|
|
2024-05-28 16:43:25 +02:00
|
|
|
|
let win;
|
2024-05-31 10:57:15 +02:00
|
|
|
|
let menu;
|
2026-09-15 15:00:24 +02:00
|
|
|
|
let navigationController;
|
|
|
|
|
|
let navigationRequest = 0;
|
|
|
|
|
|
let restartScheduled = false;
|
|
|
|
|
|
let rendererRecoveryScheduled = false;
|
|
|
|
|
|
let restartTimer;
|
|
|
|
|
|
|
|
|
|
|
|
const restartArgument = '--tooloop-restart-count=';
|
|
|
|
|
|
const restartCount = Number(
|
|
|
|
|
|
process.argv.find(argument => argument.startsWith(restartArgument))?.slice(restartArgument.length) || 0
|
|
|
|
|
|
);
|
|
|
|
|
|
const maxAutomaticRestarts = 3;
|
2024-05-28 14:25:32 +02:00
|
|
|
|
|
2024-06-04 14:58:52 +02:00
|
|
|
|
const isMac = process.platform === 'darwin';
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-05-28 18:27:50 +02:00
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
// Function
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Creates the browser window
|
|
|
|
|
|
*/
|
2024-06-03 12:16:11 +02:00
|
|
|
|
function createMainWindow() {
|
2024-05-28 16:43:25 +02:00
|
|
|
|
win = new BrowserWindow({
|
2024-05-28 14:25:32 +02:00
|
|
|
|
width: 1920,
|
|
|
|
|
|
height: 1080,
|
2024-05-28 16:43:25 +02:00
|
|
|
|
backgroundColor: '#000000',
|
2024-05-29 10:09:11 +02:00
|
|
|
|
icon: 'images/icon-512.png',
|
2024-05-29 22:29:17 +02:00
|
|
|
|
autoHideMenuBar: true,
|
2024-06-04 08:09:59 +02:00
|
|
|
|
kiosk: true,
|
2024-11-27 10:46:46 +01:00
|
|
|
|
alwaysOnTop: true,
|
2024-05-28 14:25:32 +02:00
|
|
|
|
webPreferences: {
|
2024-06-03 16:32:05 +02:00
|
|
|
|
preload: path.join(__dirname, 'js/preload.js'),
|
2024-05-28 14:25:32 +02:00
|
|
|
|
webSecurity: false,
|
2024-05-29 22:29:17 +02:00
|
|
|
|
disableDialogs: true
|
2024-05-28 14:25:32 +02:00
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2024-05-29 22:29:17 +02:00
|
|
|
|
nativeTheme.themeSource = 'dark';
|
|
|
|
|
|
|
2024-05-29 21:38:55 +02:00
|
|
|
|
// register event callbacks
|
|
|
|
|
|
win.on("closed", function () { win = null; });
|
|
|
|
|
|
win.webContents.on("will-frame-navigate", (event) => validateDomain(event));
|
|
|
|
|
|
win.webContents.setWindowOpenHandler(({ url }) => {
|
|
|
|
|
|
// we need to manually validate the url as `loadURL`
|
|
|
|
|
|
// doesn’t trigger the `will-navigate` event
|
|
|
|
|
|
let event = new Event("DummyNavigation");
|
|
|
|
|
|
event.url = url;
|
|
|
|
|
|
if (validateDomain(event)) {
|
2024-06-05 09:24:51 +02:00
|
|
|
|
win.loadURL(config.data.url);
|
2024-05-29 21:38:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
return { action: 'deny' };
|
2024-05-28 14:25:32 +02:00
|
|
|
|
});
|
2026-10-19 11:50:38 +02:00
|
|
|
|
win.webContents.on("did-fail-load", (event, errorCode, errorDescription, validatedURL, isMainFrame) => {
|
|
|
|
|
|
log.error(`Failed to load URL: ${validatedURL} with error: ${errorDescription}`);
|
|
|
|
|
|
if (errorCode !== -3 && config.data?.url) {
|
2026-09-15 15:00:24 +02:00
|
|
|
|
loadOfflinePage();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
win.webContents.on("render-process-gone", (_event, details) => {
|
|
|
|
|
|
rendererRecoveryScheduled = false;
|
|
|
|
|
|
log.error('Renderer process gone: ' + JSON.stringify(details));
|
|
|
|
|
|
loadOfflinePage();
|
|
|
|
|
|
});
|
|
|
|
|
|
win.webContents.on("unresponsive", () => {
|
|
|
|
|
|
log.error('Renderer became unresponsive');
|
|
|
|
|
|
if (rendererRecoveryScheduled || win.isDestroyed()) return;
|
|
|
|
|
|
|
|
|
|
|
|
rendererRecoveryScheduled = true;
|
|
|
|
|
|
try {
|
|
|
|
|
|
win.webContents.forcefullyCrashRenderer();
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
rendererRecoveryScheduled = false;
|
|
|
|
|
|
log.error('Could not restart renderer: ' + (error?.stack || error));
|
|
|
|
|
|
loadOfflinePage();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
win.webContents.on("responsive", () => {
|
|
|
|
|
|
rendererRecoveryScheduled = false;
|
|
|
|
|
|
log.info('Renderer became responsive');
|
|
|
|
|
|
});
|
2024-06-03 12:16:11 +02:00
|
|
|
|
win.on("page-title-updated", (event) => event.preventDefault());
|
2024-05-28 14:25:32 +02:00
|
|
|
|
|
2024-05-29 22:05:48 +02:00
|
|
|
|
// Load page from config file
|
2024-06-04 14:58:52 +02:00
|
|
|
|
if (config.data && 'url' in config.data && config.data.url) {
|
|
|
|
|
|
loadUrlAsync(config.data.url);
|
2024-05-29 22:05:48 +02:00
|
|
|
|
} else {
|
|
|
|
|
|
try {
|
|
|
|
|
|
// Load file from data folder if available
|
|
|
|
|
|
fs.accessSync('/assets/data/index.html');
|
|
|
|
|
|
win.loadFile('/assets/data/index.html');
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
// Load fallback page
|
2024-06-03 12:16:11 +02:00
|
|
|
|
win.loadFile('./html/onboarding.html');
|
2024-05-29 22:05:48 +02:00
|
|
|
|
}
|
2024-05-28 16:43:25 +02:00
|
|
|
|
}
|
2024-05-31 10:57:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-15 15:00:24 +02:00
|
|
|
|
function loadOfflinePage() {
|
|
|
|
|
|
log.warn('Loading offline page');
|
|
|
|
|
|
if (win && !win.isDestroyed()) {
|
|
|
|
|
|
win.loadFile(path.join(__dirname, 'html/offline.html'));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-29 21:38:55 +02:00
|
|
|
|
|
2024-05-31 10:57:15 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Creates all available keyboard shortcuts
|
|
|
|
|
|
*/
|
|
|
|
|
|
function createMenu() {
|
|
|
|
|
|
menu = new Menu();
|
|
|
|
|
|
|
|
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
|
|
label: app.name,
|
|
|
|
|
|
submenu: [
|
|
|
|
|
|
{
|
|
|
|
|
|
label: 'Toggle Fullscreen',
|
|
|
|
|
|
accelerator: isMac ? 'Cmd+F' : 'Ctrl+F',
|
2024-11-27 10:46:46 +01:00
|
|
|
|
click: () => {
|
|
|
|
|
|
win.setKiosk(!win.kiosk);
|
|
|
|
|
|
}
|
2024-05-31 10:57:15 +02:00
|
|
|
|
},
|
2024-06-03 12:16:11 +02:00
|
|
|
|
{ role: 'reload' },
|
|
|
|
|
|
{ role: 'quit' },
|
|
|
|
|
|
{
|
|
|
|
|
|
label: 'Config',
|
|
|
|
|
|
accelerator: isMac ? 'Cmd+,' : 'Ctrl+,',
|
2024-06-04 14:58:52 +02:00
|
|
|
|
click: () => {
|
|
|
|
|
|
config.showWindow(
|
|
|
|
|
|
win,
|
|
|
|
|
|
(newConfig) => { loadUrlAsync(newConfig.url); }
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2024-06-03 12:16:11 +02:00
|
|
|
|
}
|
2024-05-31 10:57:15 +02:00
|
|
|
|
]
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
Menu.setApplicationMenu(menu);
|
2024-05-28 14:25:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-31 10:57:15 +02:00
|
|
|
|
|
2024-05-28 18:27:50 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Validates the url of a navigatio event against the list of allowed domains in
|
|
|
|
|
|
* the config file. See https://www.electronjs.org/docs/latest/api/web-contents#event-will-frame-navigate
|
|
|
|
|
|
* @param {Event} event
|
2024-05-29 22:05:48 +02:00
|
|
|
|
* @returns `true` if the url is allowed, `false` otherwise
|
2024-05-28 18:27:50 +02:00
|
|
|
|
*/
|
|
|
|
|
|
function validateDomain(event) {
|
2026-10-20 10:50:40 +02:00
|
|
|
|
if (isUrlAllowed(event.url)) return true;
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
log.info("Navigation to " + event.url + " prevented");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function isUrlAllowed(urlString) {
|
2026-09-15 15:00:24 +02:00
|
|
|
|
if (config == undefined || config.data == undefined) return true;
|
2024-05-29 21:38:55 +02:00
|
|
|
|
|
2026-10-20 10:50:40 +02:00
|
|
|
|
let url;
|
|
|
|
|
|
try {
|
|
|
|
|
|
url = new URL(urlString);
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2024-05-29 21:38:55 +02:00
|
|
|
|
|
2024-05-29 22:05:48 +02:00
|
|
|
|
if (['file:', 'file'].includes(url.protocol)) return true;
|
|
|
|
|
|
|
2026-09-15 15:00:24 +02:00
|
|
|
|
if ('whitelist' in config.data &&
|
|
|
|
|
|
Array.isArray(config.data.whitelist) &&
|
|
|
|
|
|
config.data.whitelist.length > 0 &&
|
|
|
|
|
|
!config.data.whitelist.includes(url.hostname)) {
|
2024-05-29 21:38:55 +02:00
|
|
|
|
return false;
|
2024-05-28 18:27:50 +02:00
|
|
|
|
}
|
2024-05-29 21:38:55 +02:00
|
|
|
|
|
|
|
|
|
|
return true;
|
2024-05-28 18:27:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-20 10:50:40 +02:00
|
|
|
|
function isAuthorizedWatchdogEvent(event) {
|
|
|
|
|
|
return isWatchdogEventAuthorized(event, win, isUrlAllowed);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function logWatchdogError(error) {
|
|
|
|
|
|
log.warn('Watchdog reset failed: ' + (error?.message || error));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function registerWatchdogBridge() {
|
|
|
|
|
|
registerWatchdogHandler(ipcMain, {
|
|
|
|
|
|
isAuthorized: isAuthorizedWatchdogEvent,
|
|
|
|
|
|
onError: logWatchdogError
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2024-06-04 14:58:52 +02:00
|
|
|
|
|
2024-05-29 12:36:42 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Tests if the host of the url is reachable.
|
|
|
|
|
|
* Access is tried in a 1 second interval and the url is loaded if successfull.
|
2026-10-20 10:50:40 +02:00
|
|
|
|
* @param {string} testurl
|
2024-05-29 12:36:42 +02:00
|
|
|
|
*/
|
2024-05-31 17:28:43 +02:00
|
|
|
|
async function loadUrlAsync(testurl) {
|
2026-09-15 15:00:24 +02:00
|
|
|
|
const request = ++navigationRequest;
|
|
|
|
|
|
navigationController?.abort();
|
|
|
|
|
|
navigationController = new AbortController();
|
|
|
|
|
|
const { signal } = navigationController;
|
|
|
|
|
|
|
2024-05-29 12:36:42 +02:00
|
|
|
|
try {
|
|
|
|
|
|
let url = new URL(testurl);
|
2024-05-29 22:05:48 +02:00
|
|
|
|
// If it’s an online url, test whether it’s reachable
|
2024-06-03 12:16:11 +02:00
|
|
|
|
if (['https:', 'http:', 'https', 'http'].includes(url.protocol)) {
|
2026-09-15 15:00:24 +02:00
|
|
|
|
let fallbackShown = false;
|
2024-06-03 12:16:11 +02:00
|
|
|
|
|
2026-09-15 15:00:24 +02:00
|
|
|
|
while (!signal.aborted && request === navigationRequest) {
|
|
|
|
|
|
const timeoutSignal = AbortSignal.timeout(3000);
|
|
|
|
|
|
const reachable = await isReachable(url.href, {
|
|
|
|
|
|
signal: AbortSignal.any([signal, timeoutSignal])
|
|
|
|
|
|
});
|
2024-05-31 17:28:43 +02:00
|
|
|
|
|
2026-09-15 15:00:24 +02:00
|
|
|
|
if (signal.aborted || request !== navigationRequest) return;
|
|
|
|
|
|
|
|
|
|
|
|
if (reachable) {
|
|
|
|
|
|
log.info("Successful access to " + url.hostname);
|
|
|
|
|
|
log.info("Loading " + testurl);
|
|
|
|
|
|
if (!win.isDestroyed()) win.loadURL(testurl);
|
|
|
|
|
|
return;
|
2024-05-29 12:36:42 +02:00
|
|
|
|
}
|
2024-06-03 12:16:11 +02:00
|
|
|
|
|
2026-09-15 15:00:24 +02:00
|
|
|
|
log.warn("Could not reach " + url.hostname);
|
|
|
|
|
|
if (!fallbackShown && !win.isDestroyed()) {
|
|
|
|
|
|
loadOfflinePage();
|
|
|
|
|
|
fallbackShown = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
await delay(1000, undefined, { signal });
|
2024-05-31 17:28:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-29 12:36:42 +02:00
|
|
|
|
}
|
2026-09-15 15:00:24 +02:00
|
|
|
|
// Load local files and other protocols immediately
|
|
|
|
|
|
else if (url.protocol === 'file:') {
|
|
|
|
|
|
if (!signal.aborted && request === navigationRequest && !win.isDestroyed()) {
|
|
|
|
|
|
await win.loadFile(fileURLToPath(url), {
|
|
|
|
|
|
search: url.search,
|
|
|
|
|
|
hash: url.hash
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-05-29 12:36:42 +02:00
|
|
|
|
else {
|
2026-09-15 15:00:24 +02:00
|
|
|
|
if (!signal.aborted && request === navigationRequest && !win.isDestroyed()) {
|
|
|
|
|
|
win.loadURL(testurl);
|
|
|
|
|
|
}
|
2024-05-29 12:36:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
2026-09-15 15:00:24 +02:00
|
|
|
|
if (signal.aborted || request !== navigationRequest) return;
|
2024-06-04 14:58:52 +02:00
|
|
|
|
log.error(err.message);
|
|
|
|
|
|
// Load fallback page
|
2026-09-15 15:00:24 +02:00
|
|
|
|
loadOfflinePage();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function resolveLogPath() {
|
|
|
|
|
|
if (config.data != undefined && 'logPath' in config.data) {
|
|
|
|
|
|
if (fs.existsSync(config.data.logPath)) {
|
|
|
|
|
|
return config.data.logPath;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return "tooloop-kiosk-browser.log";
|
|
|
|
|
|
}
|
2024-05-29 12:36:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-28 18:27:50 +02:00
|
|
|
|
|
2026-09-15 15:00:24 +02:00
|
|
|
|
function restartAfterFatalError(error) {
|
|
|
|
|
|
log.error('Fatal main-process error: ' + (error?.stack || error));
|
|
|
|
|
|
if (restartScheduled) return;
|
|
|
|
|
|
restartScheduled = true;
|
|
|
|
|
|
|
|
|
|
|
|
if (restartCount >= maxAutomaticRestarts) {
|
|
|
|
|
|
log.error('Automatic restart limit reached; exiting');
|
|
|
|
|
|
app.exit(1);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const relaunchArgs = process.argv.slice(1)
|
|
|
|
|
|
.filter(argument => !argument.startsWith(restartArgument));
|
|
|
|
|
|
relaunchArgs.push(restartArgument + (restartCount + 1));
|
|
|
|
|
|
log.warn('Restarting application in 5 seconds (attempt ' + (restartCount + 1) + '/' + maxAutomaticRestarts + ')');
|
|
|
|
|
|
restartTimer = setTimeout(() => {
|
|
|
|
|
|
app.relaunch({ args: relaunchArgs });
|
|
|
|
|
|
app.exit(1);
|
|
|
|
|
|
}, 5000);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-05-28 18:27:50 +02:00
|
|
|
|
//------------------------------------------------------------------------------
|
2024-05-29 22:50:18 +02:00
|
|
|
|
// Init electron app
|
2024-05-28 18:27:50 +02:00
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
2024-06-03 12:16:11 +02:00
|
|
|
|
// https://github.com/electron/electron/issues/17972
|
|
|
|
|
|
app.commandLine.appendSwitch('--no-sandbox');
|
2024-06-03 12:24:44 +02:00
|
|
|
|
app.commandLine.appendSwitch('no-sandbox');
|
2024-06-03 12:16:11 +02:00
|
|
|
|
|
2024-05-28 14:25:32 +02:00
|
|
|
|
app.whenReady().then(() => {
|
2024-06-03 20:40:33 +02:00
|
|
|
|
|
2024-06-04 14:58:52 +02:00
|
|
|
|
config.load();
|
2024-05-28 18:27:50 +02:00
|
|
|
|
|
|
|
|
|
|
log.initialize();
|
2026-09-15 15:00:24 +02:00
|
|
|
|
log.transports.file.resolvePathFn = resolveLogPath;
|
2024-05-28 18:27:50 +02:00
|
|
|
|
|
2024-05-31 10:57:15 +02:00
|
|
|
|
log.info('----------------------------------------------');
|
|
|
|
|
|
log.info('Starting Tooloop Kiosk Browser...');
|
2026-09-15 15:00:24 +02:00
|
|
|
|
log.info('Logging to ' + log.transports.file.resolvePathFn());
|
2024-05-31 10:57:15 +02:00
|
|
|
|
|
2026-10-20 10:50:40 +02:00
|
|
|
|
registerWatchdogBridge();
|
2024-05-31 10:57:15 +02:00
|
|
|
|
createMenu();
|
2024-06-03 12:16:11 +02:00
|
|
|
|
createMainWindow();
|
2024-05-28 14:25:32 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2024-05-31 10:57:15 +02:00
|
|
|
|
app.on('activate', () => {
|
|
|
|
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
2024-06-03 12:16:11 +02:00
|
|
|
|
createMainWindow();
|
2024-05-31 10:57:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
})
|
2024-05-29 22:50:18 +02:00
|
|
|
|
|
2024-05-28 14:25:32 +02:00
|
|
|
|
app.on('window-all-closed', () => {
|
2024-05-28 16:43:25 +02:00
|
|
|
|
app.quit()
|
2024-05-28 18:27:50 +02:00
|
|
|
|
});
|
2024-05-29 22:50:18 +02:00
|
|
|
|
|
2024-05-31 10:57:15 +02:00
|
|
|
|
app.on('quit', () => {
|
|
|
|
|
|
log.info('Quit');
|
2024-05-31 17:28:43 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// prevent error dialogs in case of exceptions
|
2026-09-15 15:00:24 +02:00
|
|
|
|
process.on('uncaughtException', restartAfterFatalError);
|
|
|
|
|
|
process.on('unhandledRejection', restartAfterFatalError);
|