diff --git a/build/actions/ssh.js b/build/actions/ssh.js index ab295ef4..35e6f737 100644 --- a/build/actions/ssh.js +++ b/build/actions/ssh.js @@ -15,14 +15,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -var _, bash, getSubShellCommand; - -_ = require('lodash'); - -bash = require('bash'); - -getSubShellCommand = require('../utils/helpers').getSubShellCommand; - module.exports = { signature: 'ssh [uuid]', description: '(beta) get a shell into the running app container of a device', @@ -47,15 +39,47 @@ module.exports = { } ], action: function(params, options, done) { - var Promise, child_process, patterns, resin, verbose; + var Promise, _, bash, child_process, getSshProxyCommand, getSubShellCommand, hasbin, patterns, proxyConfig, resin, useProxy, verbose; child_process = require('child_process'); Promise = require('bluebird'); resin = require('resin-sdk-preconfigured'); + _ = require('lodash'); + bash = require('bash'); + hasbin = require('hasbin'); + getSubShellCommand = require('../utils/helpers').getSubShellCommand; patterns = require('../utils/patterns'); if (options.port == null) { options.port = 22; } verbose = options.verbose ? '-vvv' : ''; + proxyConfig = global.PROXY_CONFIG; + useProxy = !!proxyConfig && !options.noproxy; + getSshProxyCommand = function(hasTunnelBin) { + var i, proxyAuth, proxyCommand, tunnelOptions; + if (!useProxy) { + return ''; + } + if (!hasTunnelBin) { + console.warn('Proxy is enabled but the `proxytunnel` binary cannot be found.\nPlease install it if you want to route the `resin ssh` requests through the proxy.\nAlternatively you can pass `--noproxy` param to the `resin ssh` command to ignore the proxy config\nfor the `ssh` requests.\n\nAttemmpting the unproxied request for now.'); + return ''; + } + tunnelOptions = { + proxy: proxyConfig.host + ":" + proxyConfig.port, + dest: '%h:%p' + }; + proxyAuth = proxyConfig.proxyAuth; + if (proxyAuth) { + i = proxyAuth.indexOf(':'); + _.assign(tunnelOptions, { + user: proxyAuth.substring(0, i), + pass: proxyAuth.substring(i + 1) + }); + } + proxyCommand = "proxytunnel " + (bash.args(tunnelOptions, '--', '=')); + return "-o " + (bash.args({ + ProxyCommand: proxyCommand + }, '', '=')); + }; return Promise["try"](function() { if (!params.uuid) { return false; @@ -77,35 +101,17 @@ module.exports = { username: resin.auth.whoami(), uuid: device.uuid, containerId: resin.models.device.getApplicationInfo(device.uuid).get('containerId'), - proxyUrl: resin.settings.get('proxyUrl') + proxyUrl: resin.settings.get('proxyUrl'), + hasTunnelBin: useProxy ? hasbin('proxytunnel') : null }).then(function(arg) { - var containerId, proxyUrl, username, uuid; - username = arg.username, uuid = arg.uuid, containerId = arg.containerId, proxyUrl = arg.proxyUrl; + var containerId, hasTunnelBin, proxyUrl, username, uuid; + username = arg.username, uuid = arg.uuid, containerId = arg.containerId, proxyUrl = arg.proxyUrl, hasTunnelBin = arg.hasTunnelBin; if (containerId == null) { throw new Error('Did not find running application container'); } return Promise["try"](function() { - var command, i, proxyAuth, proxyCommand, proxyConfig, sshProxyCommand, subShellCommand, tunnelOptions; - sshProxyCommand = ''; - proxyConfig = global.PROXY_CONFIG; - if (proxyConfig && !options.noproxy) { - tunnelOptions = { - proxy: proxyConfig.host + ":" + proxyConfig.port, - dest: '%h:%p' - }; - proxyAuth = proxyConfig.proxyAuth; - if (proxyAuth) { - i = proxyAuth.indexOf(':'); - _.assign(tunnelOptions, { - user: proxyAuth.substring(0, i), - pass: proxyAuth.substring(i + 1) - }); - } - proxyCommand = "proxytunnel " + (bash.args(tunnelOptions, '--', '=')); - sshProxyCommand = "-o " + (bash.args({ - ProxyCommand: proxyCommand - }, '', '=')); - } + var command, sshProxyCommand, subShellCommand; + sshProxyCommand = getSshProxyCommand(hasTunnelBin); command = "ssh " + verbose + " -t -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ControlMaster=no " + sshProxyCommand + " -p " + options.port + " " + username + "@ssh." + proxyUrl + " enter " + uuid + " " + containerId; subShellCommand = getSubShellCommand(command); return child_process.spawn(subShellCommand.program, subShellCommand.args, { diff --git a/lib/actions/ssh.coffee b/lib/actions/ssh.coffee index 7f4264c6..3e7d2ff2 100644 --- a/lib/actions/ssh.coffee +++ b/lib/actions/ssh.coffee @@ -14,10 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. ### -_ = require('lodash') -bash = require('bash') -{ getSubShellCommand } = require('../utils/helpers') - module.exports = signature: 'ssh [uuid]' description: '(beta) get a shell into the running app container of a device' @@ -58,12 +54,45 @@ module.exports = child_process = require('child_process') Promise = require('bluebird') resin = require('resin-sdk-preconfigured') + _ = require('lodash') + bash = require('bash') + hasbin = require('hasbin') + { getSubShellCommand } = require('../utils/helpers') patterns = require('../utils/patterns') options.port ?= 22 verbose = if options.verbose then '-vvv' else '' + proxyConfig = global.PROXY_CONFIG + useProxy = !!proxyConfig and not options.noproxy + + getSshProxyCommand = (hasTunnelBin) -> + return '' if not useProxy + + if not hasTunnelBin + console.warn(''' + Proxy is enabled but the `proxytunnel` binary cannot be found. + Please install it if you want to route the `resin ssh` requests through the proxy. + Alternatively you can pass `--noproxy` param to the `resin ssh` command to ignore the proxy config + for the `ssh` requests. + + Attemmpting the unproxied request for now. + ''') + return '' + + tunnelOptions = + proxy: "#{proxyConfig.host}:#{proxyConfig.port}" + dest: '%h:%p' + { proxyAuth } = proxyConfig + if proxyAuth + i = proxyAuth.indexOf(':') + _.assign tunnelOptions, + user: proxyAuth.substring(0, i) + pass: proxyAuth.substring(i + 1) + proxyCommand = "proxytunnel #{bash.args(tunnelOptions, '--', '=')}" + return "-o #{bash.args({ ProxyCommand: proxyCommand }, '', '=')}" + Promise.try -> return false if not params.uuid return resin.models.device.has(params.uuid) @@ -82,25 +111,12 @@ module.exports = # get full uuid containerId: resin.models.device.getApplicationInfo(device.uuid).get('containerId') proxyUrl: resin.settings.get('proxyUrl') - .then ({ username, uuid, containerId, proxyUrl }) -> + + hasTunnelBin: if useProxy then hasbin('proxytunnel') else null + .then ({ username, uuid, containerId, proxyUrl, hasTunnelBin }) -> throw new Error('Did not find running application container') if not containerId? Promise.try -> - sshProxyCommand = '' - - proxyConfig = global.PROXY_CONFIG - if proxyConfig and not options.noproxy - tunnelOptions = - proxy: "#{proxyConfig.host}:#{proxyConfig.port}" - dest: '%h:%p' - { proxyAuth } = proxyConfig - if proxyAuth - i = proxyAuth.indexOf(':') - _.assign tunnelOptions, - user: proxyAuth.substring(0, i) - pass: proxyAuth.substring(i + 1) - proxyCommand = "proxytunnel #{bash.args(tunnelOptions, '--', '=')}" - sshProxyCommand = "-o #{bash.args({ ProxyCommand: proxyCommand }, '', '=')}" - + sshProxyCommand = getSshProxyCommand(hasTunnelBin) command = "ssh #{verbose} -t \ -o LogLevel=ERROR \ -o StrictHostKeyChecking=no \ diff --git a/package.json b/package.json index 7c53bf05..2422c2d0 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "drivelist": "^5.0.16", "etcher-image-write": "^9.0.3", "global-tunnel-ng": "^2.1.0", + "hasbin": "^1.2.3", "inquirer": "^3.0.6", "is-root": "^1.0.0", "js-yaml": "^3.7.0",