-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_helper.js
More file actions
196 lines (170 loc) · 7.38 KB
/
Copy pathnode_helper.js
File metadata and controls
196 lines (170 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* Node Helper for MMM-GetShellScript
*/
const NodeHelper = require("node_helper");
const { execFile } = require("child_process");
const path = require("path");
const fs = require("fs");
module.exports = NodeHelper.create({
start: function() {
console.log("Starting node helper for: " + this.name);
this.scriptConfigs = {};
this.registeredRoutes = new Set();
this.lastExecuted = {};
this.logs = [];
this.logsPath = path.join(__dirname, "logs.json");
this.loadLogs();
},
loadLogs: function() {
try {
if (fs.existsSync(this.logsPath)) {
const data = fs.readFileSync(this.logsPath, "utf8");
this.logs = JSON.parse(data);
}
} catch (err) {
console.error("MMM-GetShellScript: Failed to load logs:", err);
this.logs = [];
}
},
saveLogs: function() {
try {
fs.writeFileSync(this.logsPath, JSON.stringify(this.logs), "utf8");
} catch (err) {
console.error("MMM-GetShellScript: Failed to save logs:", err);
}
},
socketNotificationReceived: function(notification, payload) {
if (notification === "SETUP_ENDPOINT") {
this.config = payload;
this.setupRoutes();
const maxEntries = this.config.maxLogEntries || 10;
this.sendSocketNotification("LOGS_LOADED", this.logs.slice(0, maxEntries));
}
},
setupRoutes: function() {
let scriptsToSetup = [];
if (this.config.scripts && this.config.scripts.length > 0) {
scriptsToSetup = this.config.scripts.map(script => ({
route: script.route,
scriptPath: script.scriptPath,
authToken: script.authToken || this.config.authToken,
requireAuth: script.requireAuth !== undefined ? script.requireAuth : this.config.requireAuth,
cooldownSeconds: script.cooldownSeconds !== undefined ? script.cooldownSeconds : (this.config.cooldownSeconds || 0),
scriptTimeout: script.scriptTimeout || this.config.scriptTimeout || 30000
}));
} else {
scriptsToSetup = [{
route: this.config.route,
scriptPath: this.config.scriptPath,
authToken: this.config.authToken,
requireAuth: this.config.requireAuth,
cooldownSeconds: this.config.cooldownSeconds || 0,
scriptTimeout: this.config.scriptTimeout || 30000
}];
}
scriptsToSetup.forEach(scriptConfig => {
// Always update the stored config so token/path changes take effect on reconnect
this.scriptConfigs[scriptConfig.route] = scriptConfig;
// Only register the Express route handler once
if (this.registeredRoutes.has(scriptConfig.route)) {
return;
}
this.registeredRoutes.add(scriptConfig.route);
const scriptPath = path.resolve(global.root_path + "/" + scriptConfig.scriptPath);
const scriptsDir = path.dirname(scriptPath);
if (!fs.existsSync(scriptsDir)) {
fs.mkdirSync(scriptsDir, { recursive: true });
console.log(`MMM-GetShellScript: Created scripts directory at ${scriptsDir}`);
}
// Make executable once at setup, not on every request
if (fs.existsSync(scriptPath)) {
try {
fs.chmodSync(scriptPath, "755");
} catch (err) {
console.error(`MMM-GetShellScript: Error making script executable: ${err}`);
}
}
this.expressApp.get(scriptConfig.route, (req, res) => {
this.handleRequest(req, res, scriptConfig.route);
});
console.log(`MMM-GetShellScript: Registered route ${scriptConfig.route} -> ${scriptConfig.scriptPath}`);
});
},
handleRequest: function(req, res, route) {
const scriptConfig = this.scriptConfigs[route];
if (!scriptConfig) {
return res.status(500).send("Internal configuration error");
}
if (scriptConfig.requireAuth) {
const token = req.query.token;
if (token !== scriptConfig.authToken) {
console.log(`MMM-GetShellScript: Auth failed for ${route}`);
return res.status(401).send("Authentication failed");
}
}
if (scriptConfig.cooldownSeconds > 0) {
const now = Date.now();
const last = this.lastExecuted[route] || 0;
if (now - last < scriptConfig.cooldownSeconds * 1000) {
const remaining = Math.ceil((scriptConfig.cooldownSeconds * 1000 - (now - last)) / 1000);
return res.status(429).send(`Cooldown active, try again in ${remaining}s`);
}
}
this.lastExecuted[route] = Date.now();
const params = {};
Object.keys(req.query).forEach(key => {
if (key !== "token") params[key] = req.query[key];
});
this.executeScript(scriptConfig, params, (success, output, error) => {
if (success) {
res.send("Script executed successfully: " + output);
} else {
res.status(500).send("Script execution failed: " + error);
}
const logEntry = {
time: new Date().toLocaleTimeString(),
route: route,
success: success
};
const maxEntries = (this.config && this.config.maxLogEntries) || 10;
this.logs.unshift(logEntry);
if (this.logs.length > maxEntries) {
this.logs = this.logs.slice(0, maxEntries);
}
this.saveLogs();
this.sendSocketNotification("SCRIPT_EXECUTED", {
route: route,
success: success,
output: output,
error: error
});
});
},
executeScript: function(scriptConfig, params, callback) {
const scriptPath = path.resolve(global.root_path + "/" + scriptConfig.scriptPath);
if (!fs.existsSync(scriptPath)) {
console.error(`MMM-GetShellScript: Script not found: ${scriptPath}`);
return callback(false, null, "Script not found");
}
// Build args array — avoids shell interpretation of param values entirely
const args = [];
Object.keys(params).forEach(key => {
// Reject keys that aren't safe identifiers to avoid mangling the arg format
if (/^[a-zA-Z0-9_-]+$/.test(key)) {
args.push(`--${key}=${params[key]}`);
}
});
console.log(`MMM-GetShellScript: Executing ${scriptPath} with args:`, args);
execFile(scriptPath, args, { timeout: scriptConfig.scriptTimeout }, (error, stdout, stderr) => {
if (error) {
const isTimeout = error.killed || error.code === null;
const msg = isTimeout ? "Script timed out" : (stderr || error.toString());
console.error(`MMM-GetShellScript: Execution error: ${msg}`);
callback(false, stdout, msg);
} else {
console.log("MMM-GetShellScript: Script executed successfully");
callback(true, stdout, stderr);
}
});
}
});