domotz-remote-pawn
Version:
Domotz Agent
117 lines (100 loc) • 4.21 kB
JavaScript
/**
* 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/>.
*
* Created by Iacopo Papalini <ipapalini@domotz.com> on 24/04/15.
*
*/
var startSshTunnel = function (message, resourceLocator) {
var MODULE = 'startSshTunnel - ';
var ssh = resourceLocator.networkTools.ssh;
var payload = message.payload;
var deviceHost = payload.device.host;
var devicePort = payload.device.port;
var user = payload.server.user;
var host = payload.server.host;
var port = payload.server.port;
var privateListeningPort = payload.server.private_listening_port;
var sshMaxRetries = payload.ssh_connection_max_retries || 10;
var sshRetryInterval = payload.ssh_connection_retry_interval || 1000;
var sshParams = payload.ssh_params || {};
var myConsole = resourceLocator.log.decorateLogs();
var async = resourceLocator.async;
var options = {
user: user,
password: payload.server.password,
private_key: payload.server.private_key,
host: host,
port: port,
remote_port_forwarding:
(payload.server.private_listening_iface || '0.0.0.0') + ':' + privateListeningPort + ':' + deviceHost + ':' + devicePort,
disable_remote_command: true,
ttl: payload.ttl * 1000,
log_prefix: message.correlationId,
host_key_checking: 'no',
server_alive_interval: 60,
server_alive_count_max: 5,
pubkey_accepted_key_types: _ensureSshRsaPubkey(sshParams.pubkey_accepted_key_types || ''),
host_key_algorithms: sshParams.host_key_algorithms,
kex_algorithm: sshParams.kex_algorithm,
cipher_spec: sshParams.cipher_spec,
};
function _ensureSshRsaPubkey(pubkeyTypes) {
if (pubkeyTypes && !pubkeyTypes.includes('+ssh-rsa')) {
return pubkeyTypes + ',+ssh-rsa';
}
return pubkeyTypes || '+ssh-rsa';
}
function describeTunnel() {
return [user, '@', host, ':', port, ' ', privateListeningPort, ':', deviceHost, ':', devicePort].join('');
}
return {
describe: function () {
// 'cid - startSshTunnel - domotz@zbx01.cloudserver.it:22 8080:192.168.5.64:5000'
return MODULE + describeTunnel();
},
execute: function (callback) {
var ERR = 'E',
SUCCESS = null;
function sshConnect(retryCb) {
var failed = false;
var optionsCopy = JSON.parse(JSON.stringify(options));
ssh.exec(null, optionsCopy).catch(function () {
failed = true;
});
setTimeout(function () {
if (failed) {
myConsole.info('ssh process exited after one second');
retryCb(ERR);
} else {
myConsole.info('ssh process is up after one second');
retryCb(SUCCESS);
}
}, sshRetryInterval);
}
async.retry({ times: sshMaxRetries }, sshConnect, function (err) {
if (err) {
myConsole.error('ssh process cannot start, close the RPC call with failure');
callback({ success: false });
return;
}
myConsole.info('ssh process started, close the RPC call with success');
callback({ success: true });
});
},
};
};
module.exports.command = startSshTunnel;