2020-05-02 10:39:09 +02:00
|
|
|
const express = require("express");
|
2023-04-04 20:44:32 +02:00
|
|
|
const Log = require("logger");
|
2026-02-08 16:18:56 +01:00
|
|
|
const { replaceSecretPlaceholder } = require("#server_functions");
|
2019-12-31 19:31:13 +00:00
|
|
|
|
2026-06-10 19:38:24 +02:00
|
|
|
/**
|
|
|
|
|
* Determine which secrets a module is allowed to restore. A module may only
|
|
|
|
|
* restore the `**SECRET_***` placeholders that appear in its own config — the
|
|
|
|
|
* exact inverse of how the config is redacted before it is sent to the browser.
|
|
|
|
|
* @param {string} moduleName - Name of the module.
|
|
|
|
|
* @returns {Set<string>} The secret names the module may restore.
|
|
|
|
|
*/
|
|
|
|
|
function getAllowedSecrets (moduleName) {
|
|
|
|
|
const modules = global.configRedacted?.modules || [];
|
|
|
|
|
const moduleConfig = modules.find((m) => m.module === moduleName);
|
|
|
|
|
const allowed = new Set();
|
|
|
|
|
if (moduleConfig) {
|
|
|
|
|
// Stringify the config to easily find all expected **SECRET_*** placeholders
|
|
|
|
|
for (const [, secretName] of JSON.stringify(moduleConfig).matchAll(/\*\*(SECRET_[^*]+)\*\*/g)) {
|
|
|
|
|
allowed.add(secretName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return allowed;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 19:45:47 +02:00
|
|
|
class NodeHelper {
|
2024-01-01 15:38:08 +01:00
|
|
|
init () {
|
2025-10-22 22:50:31 +02:00
|
|
|
Log.log("Initializing new module helper ...");
|
2026-05-08 19:45:47 +02:00
|
|
|
}
|
2019-12-31 19:31:13 +00:00
|
|
|
|
2024-01-01 15:38:08 +01:00
|
|
|
loaded () {
|
2025-10-22 22:50:31 +02:00
|
|
|
Log.log(`Module helper loaded: ${this.name}`);
|
2026-05-08 19:45:47 +02:00
|
|
|
}
|
2019-12-31 19:31:13 +00:00
|
|
|
|
2024-01-01 15:38:08 +01:00
|
|
|
start () {
|
2025-10-22 22:50:31 +02:00
|
|
|
Log.log(`Starting module helper: ${this.name}`);
|
2026-05-08 19:45:47 +02:00
|
|
|
}
|
2019-12-31 19:31:13 +00:00
|
|
|
|
2021-10-18 14:36:31 +02:00
|
|
|
/**
|
2022-01-26 23:47:51 +01:00
|
|
|
* Called when the MagicMirror² server receives a `SIGINT`
|
2019-12-31 19:31:13 +00:00
|
|
|
* Close any open connections, stop any sub-processes and
|
|
|
|
|
* gracefully exit the module.
|
|
|
|
|
*/
|
2024-01-01 15:38:08 +01:00
|
|
|
stop () {
|
2025-10-22 22:50:31 +02:00
|
|
|
Log.log(`Stopping module helper: ${this.name}`);
|
2026-05-08 19:45:47 +02:00
|
|
|
}
|
2019-12-31 19:31:13 +00:00
|
|
|
|
2021-10-18 14:36:31 +02:00
|
|
|
/**
|
2019-12-31 19:31:13 +00:00
|
|
|
* This method is called when a socket notification arrives.
|
2021-10-18 14:36:31 +02:00
|
|
|
* @param {string} notification The identifier of the notification.
|
2026-02-06 00:21:35 +01:00
|
|
|
* @param {object} payload The payload of the notification.
|
2019-12-31 19:31:13 +00:00
|
|
|
*/
|
2024-01-01 15:38:08 +01:00
|
|
|
socketNotificationReceived (notification, payload) {
|
2025-10-22 22:50:31 +02:00
|
|
|
Log.log(`${this.name} received a socket notification: ${notification} - Payload: ${payload}`);
|
2026-05-08 19:45:47 +02:00
|
|
|
}
|
2019-12-31 19:31:13 +00:00
|
|
|
|
2021-10-18 14:36:31 +02:00
|
|
|
/**
|
2019-12-31 19:31:13 +00:00
|
|
|
* Set the module name.
|
2021-10-18 14:36:31 +02:00
|
|
|
* @param {string} name Module name.
|
2019-12-31 19:31:13 +00:00
|
|
|
*/
|
2024-01-01 15:38:08 +01:00
|
|
|
setName (name) {
|
2019-12-31 19:31:13 +00:00
|
|
|
this.name = name;
|
2026-05-08 19:45:47 +02:00
|
|
|
}
|
2019-12-31 19:31:13 +00:00
|
|
|
|
2021-10-18 14:36:31 +02:00
|
|
|
/**
|
2019-12-31 19:31:13 +00:00
|
|
|
* Set the module path.
|
2021-10-18 14:36:31 +02:00
|
|
|
* @param {string} path Module path.
|
2019-12-31 19:31:13 +00:00
|
|
|
*/
|
2024-01-01 15:38:08 +01:00
|
|
|
setPath (path) {
|
2019-12-31 19:31:13 +00:00
|
|
|
this.path = path;
|
2026-05-08 19:45:47 +02:00
|
|
|
}
|
2019-12-31 19:31:13 +00:00
|
|
|
|
2024-08-12 22:52:43 +02:00
|
|
|
/*
|
|
|
|
|
* sendSocketNotification(notification, payload)
|
2019-12-31 19:31:13 +00:00
|
|
|
* Send a socket notification to the node helper.
|
|
|
|
|
*
|
|
|
|
|
* argument notification string - The identifier of the notification.
|
|
|
|
|
* argument payload mixed - The payload of the notification.
|
|
|
|
|
*/
|
2024-01-01 15:38:08 +01:00
|
|
|
sendSocketNotification (notification, payload) {
|
2019-12-31 19:31:13 +00:00
|
|
|
this.io.of(this.name).emit(notification, payload);
|
2026-05-08 19:45:47 +02:00
|
|
|
}
|
2019-12-31 19:31:13 +00:00
|
|
|
|
2024-08-12 22:52:43 +02:00
|
|
|
/*
|
|
|
|
|
* setExpressApp(app)
|
2019-12-31 19:31:13 +00:00
|
|
|
* Sets the express app object for this module.
|
|
|
|
|
* This allows you to host files from the created webserver.
|
|
|
|
|
*
|
|
|
|
|
* argument app Express app - The Express app object.
|
|
|
|
|
*/
|
2024-01-01 15:38:08 +01:00
|
|
|
setExpressApp (app) {
|
2019-12-31 19:31:13 +00:00
|
|
|
this.expressApp = app;
|
|
|
|
|
|
2021-01-05 18:44:36 +01:00
|
|
|
app.use(`/${this.name}`, express.static(`${this.path}/public`));
|
2026-05-08 19:45:47 +02:00
|
|
|
}
|
2019-12-31 19:31:13 +00:00
|
|
|
|
2024-08-12 22:52:43 +02:00
|
|
|
/*
|
|
|
|
|
* setSocketIO(io)
|
2019-12-31 19:31:13 +00:00
|
|
|
* Sets the socket io object for this module.
|
|
|
|
|
* Binds message receiver.
|
|
|
|
|
*
|
|
|
|
|
* argument io Socket.io - The Socket io object.
|
|
|
|
|
*/
|
2024-01-01 15:38:08 +01:00
|
|
|
setSocketIO (io) {
|
2021-01-05 18:44:36 +01:00
|
|
|
this.io = io;
|
2019-12-31 19:31:13 +00:00
|
|
|
|
2025-10-22 22:50:31 +02:00
|
|
|
Log.log(`Connecting socket for: ${this.name}`);
|
2021-01-05 18:44:36 +01:00
|
|
|
|
|
|
|
|
io.of(this.name).on("connection", (socket) => {
|
2019-12-31 19:31:13 +00:00
|
|
|
// register catch all.
|
2024-08-18 03:25:01 -04:00
|
|
|
socket.onAny((notification, payload) => {
|
2026-04-07 21:15:25 +02:00
|
|
|
if (config?.hideConfigSecrets && payload && typeof payload === "object") {
|
2026-02-06 00:21:35 +01:00
|
|
|
try {
|
2026-06-10 19:38:24 +02:00
|
|
|
// Calculate exactly which secrets this module is allowed to receive
|
|
|
|
|
const allowedSecrets = getAllowedSecrets(this.name);
|
|
|
|
|
// Expand only these safe, module-specific secrets in the payload
|
|
|
|
|
const payloadStr = replaceSecretPlaceholder(JSON.stringify(payload), allowedSecrets);
|
2026-02-06 00:21:35 +01:00
|
|
|
this.socketNotificationReceived(notification, JSON.parse(payloadStr));
|
|
|
|
|
} catch (e) {
|
|
|
|
|
Log.error("Error substituting variables in payload: ", e);
|
|
|
|
|
this.socketNotificationReceived(notification, payload);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.socketNotificationReceived(notification, payload);
|
|
|
|
|
}
|
2019-12-31 19:31:13 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-05-08 19:45:47 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check the status of a fetch response.
|
|
|
|
|
* @param {Response} response The fetch response.
|
|
|
|
|
* @returns {Response} The fetch response if ok.
|
|
|
|
|
*/
|
|
|
|
|
static checkFetchStatus (response) {
|
|
|
|
|
// response.status >= 200 && response.status < 300
|
|
|
|
|
if (response.ok) {
|
|
|
|
|
return response;
|
|
|
|
|
} else {
|
|
|
|
|
throw Error(response.statusText);
|
|
|
|
|
}
|
2021-04-24 09:14:08 +02:00
|
|
|
}
|
2026-05-08 19:45:47 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Look at the specified error and return an appropriate error type, that
|
|
|
|
|
* can be translated to a detailed error message
|
|
|
|
|
* @param {Error} error the error from fetching something
|
|
|
|
|
* @returns {string} the string of the detailed error message in the translations
|
|
|
|
|
*/
|
|
|
|
|
static checkFetchError (error) {
|
|
|
|
|
let error_type = "MODULE_ERROR_UNSPECIFIED";
|
|
|
|
|
if (error.code === "EAI_AGAIN") {
|
|
|
|
|
error_type = "MODULE_ERROR_NO_CONNECTION";
|
|
|
|
|
} else {
|
|
|
|
|
const message = typeof error.message === "string" ? error.message.toLowerCase() : "";
|
|
|
|
|
if (message.includes("unauthorized") || message.includes("http 401") || message.includes("http 403")) {
|
|
|
|
|
error_type = "MODULE_ERROR_UNAUTHORIZED";
|
|
|
|
|
}
|
2025-11-14 20:14:23 +01:00
|
|
|
}
|
2026-05-08 19:45:47 +02:00
|
|
|
return error_type;
|
2021-05-02 10:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-08 19:45:47 +02:00
|
|
|
/**
|
|
|
|
|
* Create a new NodeHelper subclass with the given module definition.
|
|
|
|
|
* @param {object} moduleDefinition Methods and properties for the helper.
|
|
|
|
|
* @returns {typeof NodeHelper} A new subclass of NodeHelper.
|
|
|
|
|
*/
|
|
|
|
|
static create (moduleDefinition) {
|
|
|
|
|
return class extends NodeHelper {
|
|
|
|
|
constructor () {
|
|
|
|
|
super();
|
|
|
|
|
Object.assign(this, moduleDefinition);
|
|
|
|
|
this.init();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-31 19:31:13 +00:00
|
|
|
|
2021-01-05 18:44:36 +01:00
|
|
|
module.exports = NodeHelper;
|