domotz-remote-pawn
Version:
Domotz Agent
83 lines (79 loc) • 3.03 kB
JavaScript
const semver = require('semver');
var OPTIONS_MAP = {
bind_address: '-b',
cipher_spec: '-c',
cipher: '-c',
escape_char: '-e',
config_file: '-F',
dynamic_port_forwarding: '-D',
identity_file: '-i',
pkcs11: '-I',
local_port_forwarding: '-L',
mac_spec: '-m',
disable_remote_command: '-N',
host_key_checking: '-oStrictHostKeyChecking=',
user_known_host_file: '-oUserKnownHostsFile=',
server_alive_interval: '-oServerAliveInterval=',
server_alive_count_max: '-oServerAliveCountMax=',
pubkey_accepted_key_types: '-oPubkeyAcceptedKeyTypes=',
host_key_algorithms: '-oHostKeyAlgorithms=',
kex_algorithm: '-oKexAlgorithms=',
ctl_cmd: '-O',
port: '-p',
remote_port_forwarding: '-R',
ctl_path: '-S',
disable_pseudo_tty_alloc: '-T'
};
function patchOptions(options, sshInfo) {
options = options === undefined ? {} : options;
sshInfo = sshInfo === undefined ? {} : sshInfo;
var prefix = '+'
var openSSHVersion = sshInfo.openssh;
if (openSSHVersion && semver.clean(openSSHVersion)) {
//https://www.openssh.com/txt/release-7.0
if (semver.lt(semver.clean(openSSHVersion), '7.0.0')) {
prefix = ''
}
}
/*
* If the options are not provided we set the default values using
* all the supported algorithms by the current SSH version
* */
if (!sshInfo.KEY || !sshInfo.KEX || !sshInfo.CIPHER) {
return options;
}
options.pubkey_accepted_key_types = options.pubkey_accepted_key_types || prefix + sshInfo.KEY.join(',');
options.host_key_algorithms = options.host_key_algorithms || prefix + sshInfo.KEY.join(',');
options.kex_algorithm = options.kex_algorithm || prefix + sshInfo.KEX.join(',');
options.cipher = options.cipher || prefix + sshInfo.CIPHER.join(',');
return options;
}
module.exports.getArgsFromOptions = function getArgsFromOptions(options, optionsMap, sshInfo, logger) {
optionsMap = optionsMap || OPTIONS_MAP;
options = patchOptions(options, sshInfo);
var openSSHVersion = sshInfo.openssh;
if (openSSHVersion && semver.clean(openSSHVersion)) {
//https://www.openssh.com/txt/release-7.0
if (semver.lt(semver.clean(openSSHVersion), '7.0.0')) {
delete options['pubkey_accepted_key_types'];
delete options['host_key_algorithms'];
}
}
logger.debug('SSH SCP Options: %s', JSON.stringify(options));
var args = [];
Object.keys(options || {}).forEach(function (opt) {
if (optionsMap.hasOwnProperty(opt)) {
var optionName = optionsMap[opt],
optionValue = options[opt];
if (optionName.indexOf('-o') === 0) {
args.push(optionName + optionValue);
} else if (typeof optionValue !== 'boolean') {
args.push(optionName);
args.push(optionValue);
} else {
args.push(optionName);
}
}
});
return args;
};