UNPKG

domotz-remote-pawn

Version:

Domotz Agent

151 lines (133 loc) 5.91 kB
/** * Created by Fausto Lelli <fausto@domotz.com> on 23/08/19. */ var DEFAULT_REMOTE_PAWN_DIR = '/opt/domotz/lib/node_modules/domotz-remote-pawn'; function scriptName(platform) { if (platform.isWindows()) { return 'openvpn/domotzvpn_win.sh'; } else { return 'openvpn/domotzvpn.sh'; } } var generateOpenvpn = function (message, resourceLocator) { var path = resourceLocator.path; var lodash = resourceLocator.lodash; var cmdUtils = resourceLocator.utils.command; var myConsole = resourceLocator.log.decorateLogs(); var fileUtils = resourceLocator.utils.file; var childProcess = resourceLocator.utils.childProcess; var platform = resourceLocator.utils.platform; var NEW_CLIENT_TIMEOUT = 28000; var outputFile = path.join(process.env.DOMOTZ_CONF_DIR, 'openvpn/newclient.ovpn'); var script = '"' + path.join(process.env.DOMOTZ_REMOTE_PAWN_DIR || DEFAULT_REMOTE_PAWN_DIR, scriptName(platform)) + '"'; return { describe: function () { return message.correlationId + ' - generateVpnConfig - '; }, execute: function (callback) { myConsole.debug('execute - GENERATE VPN CONFIG: ' + ' parameters are ' + JSON.stringify(message.payload) + '; script path:' + script); var params = ['newclient']; if (lodash.get(message, 'payload.routing_policy', '') === 'local') { params.push('no-redirect-gw'); } if (platform.isWindows()) { var connectionDurationSeconds = lodash.get(message, 'payload.connection_duration_seconds', '3600'); params.push(connectionDurationSeconds.toString()); } var options = { timeout: NEW_CLIENT_TIMEOUT, shell: platform.isWindows() ? platform.getWindowsShell() : true, }; myConsole.info('execute - GENERATE VPN CONFIG: ' + ' spawn with parameters: %s', JSON.stringify(params)); var newClient = childProcess.spawn(script, params, options); newClient.on('exit', function (code) { if (code !== 0) { myConsole.error('ERROR while creating OpenVPN configuration file: ' + code); callback(cmdUtils.getResponse(null)); } else { var content = fileUtils.readFile(outputFile); if (!content) { callback(cmdUtils.getResponse(null, 'unable to read openvpn output file')); return; } myConsole.debug('execute - SENDING DATA'); callback(content.toString('base64'), null, { asBinary: true, 'content-type': 'plain/text', }); } myConsole.debug('child process exited with code ' + code); }); newClient.on('data', myConsole.debug); newClient.stdout.on('data', myConsole.debug); newClient.stderr.on('data', myConsole.error); }, }; }; var openvpnCheck = function (path, proc, logManager, fileManager, q, platform) { var myConsole = logManager.decorateLogs(); var versionStringPattern = 'DOMOTZ_VPN_SERVER=OPENVPN'; var script = path.join(process.env.DOMOTZ_REMOTE_PAWN_DIR || DEFAULT_REMOTE_PAWN_DIR, scriptName(platform)); var version = null; var RECONF_TIMEOUT = 50000; var CHECK_VERSION_TIMEOUT = 5000; function startConfiguration() { proc.spawn('sh', [script, 'reconf'], { timeout: RECONF_TIMEOUT }) .then(function (output) { myConsole.info('configured' + output); }) .catch(function (error) { myConsole.info('configuration failed ' + error); }); } function getVersion() { if (version === false || process.env.DPLATFORM === 'luxul') { return q.reject(); } if (version) { return q.resolve(version); } var deferred = q.defer(); proc.spawn('sh', [script, 'version'], { timeout: CHECK_VERSION_TIMEOUT }) .then(function (output) { myConsole.info('openvpn is available ' + output); version = false; if (output) { var lines = output.toUpperCase().split('\n'); var versionString = lines.filter(function (line) { if (line) { return line.indexOf(versionStringPattern, 0) === 0; } deferred.reject(); return false; }); if (versionString.length > 0) { version = versionString[0].replace(versionStringPattern, '').trim(); if (!version.match(/\d+\.\d+\.\d+/)) { myConsole.warn('openvpn version is malformed'); version = false; deferred.reject(); return; } myConsole.info('openvpn is available detected version' + version); return deferred.resolve(version); } deferred.reject(); } else { deferred.reject(); } }) .catch(function (error) { myConsole.info('openpvn is not available ' + error); version = false; deferred.reject(); }); return deferred.promise; } return { getVersion: getVersion, startConfiguration: startConfiguration, }; }; module.exports.command = generateOpenvpn; module.exports.factory = openvpnCheck;