domotz-remote-pawn
Version:
Domotz Agent
63 lines (60 loc) • 2.29 kB
JavaScript
/** This file is part of Domotz Agent.
* Copyright (C) 2024 Domotz Ltd
*
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
**/
var ACCEPTED_COMMANDS = [
'domotz_curl',
'domotz_dig',
'domotz_nmap',
'domotz_node',
'domotz_nping',
'domotz_easyrsa',
'domotz_network_utils',
'domotz_npm',
'domotz_openvpn',
'domotz_sshpass',
'domotz_openssl',
'domotz_speed_test',
'domotz_ndt7',
'domotz_mtr'
]
module.exports.post = function (resourceLocator, onException, onSuccess) {
return function (req, res) {
var cb = function (response) {
onSuccess(res,response);
}
var message = {
correlationId: 'BIN_EXECUTOR',
payload: req.body.payload,
};
var myConsole = resourceLocator.log.decorateLogs();
try {
if (ACCEPTED_COMMANDS.indexOf(message.payload.command) === -1) {
var error = {
message: "The " + message.payload.command + " command is not accepted. The accepted commands are: " + JSON.stringify(ACCEPTED_COMMANDS),
status: 400
};
onException(res, error);
}
myConsole.debug('execute - ' + message.payload.command + ': ' + ' payload is ' + JSON.stringify(message.payload));
var customTimeout = message.payload.timeout;
require('../../../../commands/domotz_proc_executor')
.execute(resourceLocator, message.payload.command + ' ' + message.payload.arguments, customTimeout, cb);
} catch (e) {
myConsole.error(JSON.stringify(e));
onException(e);
}
};
};