logging, domain white-listing

This commit is contained in:
2024-05-28 18:27:50 +02:00
parent d51d857b88
commit 6a53286f67
4 changed files with 65 additions and 8 deletions
+49 -7
View File
@@ -1,22 +1,37 @@
const { app, BrowserWindow } = require('electron/main');
const path = require('node:path');
const fs = require('fs');
// const http = require('http');
const log = require('electron-log/main');
//------------------------------------------------------------------------------
// Properties
//------------------------------------------------------------------------------
let config;
let win;
//------------------------------------------------------------------------------
// Function
//------------------------------------------------------------------------------
/**
* Loads a config file from disc.
* The file is expected to be in the application path and called “config.json”.
*/
function loadConfig() {
try {
const data = fs.readFileSync(path.join(__dirname, "config.json"), { encoding: 'utf8' });
config = JSON.parse(data);
} catch (err) {
console.log(err);
console.error(err);
}
}
/**
* Creates the browser window
*/
async function createWindow() {
win = new BrowserWindow({
width: 1920,
@@ -35,6 +50,8 @@ async function createWindow() {
win = null;
});
win.webContents.addListener("will-navigate", validateDomain);
win.once("ready-to-show", () => {
win.setKiosk(true);
win.show();
@@ -51,20 +68,45 @@ async function createWindow() {
// path: '/'
// };
// let req = http.request(options, function (r) {
// console.log(r.headers);
// log.info(r.headers);
// });
// req.end();
win.loadURL(config.url);
} catch (err) {
console.log(err);
log.error(err);
} finally {
}
}
/**
* 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
*/
function validateDomain(event) {
let url = new URL(event.url);
if ('allowedDomains' in config && !config.allowedDomains.includes(url.hostname)) {
event.preventDefault();
log.info("Navigation to " + event.url + " prevented");
}
}
//------------------------------------------------------------------------------
// Init electon app
//------------------------------------------------------------------------------
app.whenReady().then(() => {
loadConfig();
log.initialize();
if ('logPath' in config) {
log.transports.file.resolvePathFn = () => config.logPath;
}
createWindow();
app.on('activate', () => {
@@ -76,4 +118,4 @@ app.whenReady().then(() => {
app.on('window-all-closed', () => {
app.quit()
});
});