UNPKG

domotz-remote-pawn

Version:

Domotz Agent

130 lines (117 loc) 6.38 kB
/** This file is part of Domotz Agent. * Copyright (C) 2016 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 create = function (express, status, configuration, resourceLocator) { var myConsole = resourceLocator.log.decorateLogs(); var router = express.Router(); var apiResources = []; apiResources.push(['activation-token', 'api/activationToken', ['post']]); apiResources.push(['status', 'api/status', ['get']]); apiResources.push(['log/flush', 'api/flush_log', ['get']]); apiResources.push(['restart', 'api/restart', ['post']]); apiResources.push(['random', 'api/random', ['get']]); apiResources.push(['agent', 'api/agent', ['post']]); // 404 is returned by handler if not in restricted mode if (status.isConfigured() || status.isPassive()) { apiResources.push(['agent/' + configuration.id, 'api/coupling', ['get']]); apiResources.push(['agent', 'api/agent', ['get']]); apiResources.push(['net-info', 'api/netinfo', ['get']]); apiResources.push(['mtr', 'api/mtr', ['get']]); apiResources.push(['ndt-server', 'api/ndtServer', ['get']]); apiResources.push(['network-speed?', 'api/networkSpeed', ['get']]); apiResources.push(['connectivity', 'api/connectivity', ['get']]); apiResources.push(['device', 'api/device', ['get']]); apiResources.push(['network-config', 'api/networkConfig', ['post', 'delete', 'get']]); apiResources.push(['vlan-config/:tag?', 'api/vlanConfig', ['get', 'post', 'delete']]); apiResources.push(['ping', 'api/ping', ['post']]); apiResources.push(['accumulator', 'api/accumulator', ['get']]); apiResources.push(['bindings', 'api/bindingStorageDump', ['get']]); if (process.env.DPLATFORM === 'win') { apiResources.push(['process/clean', 'api/process_clean', ['get']]); } if ( status.isPassive() || process.env.ENABLE_HTTP_TEST_API === '1' || resourceLocator.lodash.get(resourceLocator, 'configuration.settings.enable_http_test_api', false) ) { myConsole.warn('Enabling HTTP Test API'); apiResources.push(['event/trigger/:event_name', 'api/event', ['post']]); apiResources.push(['agent-settings', 'api/getAgentSettings', ['get']]); apiResources.push(['scp', 'api/scp', ['post']]); apiResources.push(['sshShellSequence', 'api/sshShellSequence', ['post']]); apiResources.push(['tftp', 'api/tftp', ['post']]); apiResources.push(['command/:command', 'api/command', ['post']]); apiResources.push(['sandbox/run', 'api/sandboxRunner', ['post']]); apiResources.push(['setBindings', 'api/bindings/setBindings', ['post']]); apiResources.push(['bin_executor', 'api/bin/executor', ['post']]); apiResources.push(['invalidateBindings', 'api/bindings/invalidateBindings', ['post']]); apiResources.push(['netgear-api-token', 'api/firewall_netgear/api-token', ['get']]); apiResources.push(['netgear-create-rule', 'api/firewall_netgear/create-rule', ['post']]); apiResources.push(['netgear-get-rules', 'api/firewall_netgear/get-rules', ['get']]); apiResources.push(['netgear-delete-all-rules', 'api/firewall_netgear/delete-all-rules', ['post']]); } } else if (status.isRestricted()) { apiResources.push(['agent/credentials', 'api/credentials', ['put']]); } function _onSuccess(res, data, plain) { if (!data) { myConsole.debug('WEBAPP - Success, providing empty response'); res.status(204); } else if (plain) { myConsole.debug('WEBAPP - Success, providing as plaintext'); res.status(200).send(data); } else { if (data && data.headers) { myConsole.debug('WEBAPP - Success, providing as complex data'); var headersParsed = JSON.parse(JSON.stringify(data.headers)); headersParsed['content-encoding'] = undefined; for (var headerIndex in headersParsed) { res.header(headerIndex, headersParsed[headerIndex]); } var bodyParsed = data.body ? data.body : {}; res.status(200).send(bodyParsed); } else { myConsole.debug('WEBAPP - Success, providing as json'); res.header('Content-type', 'application/json'); res.status(200).send(JSON.stringify(data)); } } res.end(); } function _onException(res, err) { if (err === undefined) { err = { message: 'Unknown error, see logs for further details', status: 500, }; } myConsole.warn('WEBAPP - Internal Error: ' + err.status + ' - ' + JSON.stringify(err.message || '[ No error message ]')); res.status(err.status || 500).send(JSON.stringify(err.message || {})); res.end(); } myConsole.info('WEBAPP - +------------- INTERNAL API ----------------------+'); apiResources.forEach(function (route) { route[2].forEach(function (method) { var moduleName = '../request_handlers/' + route[1]; var path = route[0]; myConsole.info('WEBAPP - Create route: ' + method.toUpperCase() + ' ' + path + '. File: ' + moduleName); var handler = require(moduleName)[method](resourceLocator, _onException, _onSuccess); router[method]('/api/v1/' + path, handler); }); }); myConsole.info('WEBAPP - +-------------------------------------------------+'); return router; }; module.exports.create = create;