|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -var RSVP = require('rsvp'); |
4 |
| -var fs = require('fs'); |
5 |
| -var tunnelSsh = require('tunnel-ssh'); |
6 |
| -var untildify = require('untildify'); |
| 3 | +const RSVP = require('rsvp'); |
| 4 | +const fs = require('fs'); |
| 5 | +const { createTunnel } = require('tunnel-ssh'); |
| 6 | +const untildify = require('untildify'); |
7 | 7 |
|
8 |
| -var DeployPluginBase = require('ember-cli-deploy-plugin'); |
| 8 | +const DeployPluginBase = require('ember-cli-deploy-plugin'); |
9 | 9 |
|
10 |
| -var MAX_PORT_NUMBER = 65535; |
11 |
| -var MIN_PORT_NUMBER = 49151; |
| 10 | +const MAX_PORT_NUMBER = 65535; |
| 11 | +const MIN_PORT_NUMBER = 49151; |
12 | 12 |
|
13 | 13 | module.exports = {
|
14 | 14 | name: 'ember-cli-deploy-ssh-tunnel',
|
15 | 15 |
|
16 |
| - createDeployPlugin: function(options) { |
17 |
| - var DeployPlugin = DeployPluginBase.extend({ |
| 16 | + createDeployPlugin: function (options) { |
| 17 | + const DeployPlugin = DeployPluginBase.extend({ |
18 | 18 | name: options.name,
|
19 | 19 | defaultConfig: {
|
20 |
| - dstPort: 6379, |
21 |
| - port: 22, |
22 |
| - dstHost: 'localhost', |
23 |
| - srcPort: function() { |
24 |
| - var range = MAX_PORT_NUMBER - MIN_PORT_NUMBER + 1; |
25 |
| - return Math.floor(Math.random() * range) + MIN_PORT_NUMBER; |
26 |
| - }, |
27 |
| - tunnelClient: function(context) { |
28 |
| - // if you want to provide your own ssh client to be used instead of one from this plugin |
29 |
| - return context.tunnelClient || tunnelSsh; |
30 |
| - } |
| 20 | + dstPort: 6379, |
| 21 | + port: 22, |
| 22 | + dstHost: 'localhost', |
| 23 | + srcPort: function () { |
| 24 | + var range = MAX_PORT_NUMBER - MIN_PORT_NUMBER + 1; |
| 25 | + return Math.floor(Math.random() * range) + MIN_PORT_NUMBER; |
| 26 | + }, |
| 27 | + tunnelClient: function (context) { |
| 28 | + // if you want to provide your own ssh client to be used instead of one from this plugin, |
| 29 | + // must follow this signature |
| 30 | + // createTunnel( |
| 31 | + // tunnelOptions: TunnelOptions, |
| 32 | + // serverOptions: ServerOptions, |
| 33 | + // sshOptions: SshOptions, |
| 34 | + // forwardOptions: ForwardOptions |
| 35 | + // ): Promise<[Server, Client]>; |
| 36 | + // https://github.com/agebrock/tunnel-ssh/blob/master/types/index.d.ts |
| 37 | + return context.tunnelClient || createTunnel; |
| 38 | + } |
31 | 39 | },
|
32 | 40 |
|
33 | 41 | requiredConfig: ['host', 'username'],
|
34 | 42 |
|
35 |
| - setup: function(/* context */) { |
36 |
| - var srcPort = this.readConfig('srcPort'); |
| 43 | + setup: function (/* context */) { |
| 44 | + const srcPort = this.readConfig('srcPort'); |
37 | 45 |
|
38 | 46 | if (srcPort > MAX_PORT_NUMBER || srcPort < MIN_PORT_NUMBER) {
|
39 |
| - throw 'Port ' + srcPort + ' is not available to open a SSH connection on.\n' + 'Please choose a port between ' + MIN_PORT_NUMBER + ' and ' + MAX_PORT_NUMBER + '.'; |
| 47 | + throw ( |
| 48 | + 'Port ' + |
| 49 | + srcPort + |
| 50 | + ' is not available to open a SSH connection on.\n' + |
| 51 | + 'Please choose a port between ' + |
| 52 | + MIN_PORT_NUMBER + |
| 53 | + ' and ' + |
| 54 | + MAX_PORT_NUMBER + |
| 55 | + '.' |
| 56 | + ); |
40 | 57 | }
|
41 | 58 |
|
42 |
| - var sshConfig = { |
| 59 | + const tunnel = this.readConfig('tunnelClient'); |
| 60 | + |
| 61 | + let privateKey = this.readConfig('privateKey'); |
| 62 | + |
| 63 | + if (this.readConfig('privateKeyPath')) { |
| 64 | + privateKey = fs.readFileSync(untildify(this.readConfig('privateKeyPath'))); |
| 65 | + } |
| 66 | + |
| 67 | + const tunnelOptions = { |
| 68 | + autoClose: true |
| 69 | + }; |
| 70 | + |
| 71 | + const serverOptions = { |
| 72 | + port: srcPort |
| 73 | + }; |
| 74 | + |
| 75 | + const sshOptions = { |
43 | 76 | host: this.readConfig('host'),
|
44 | 77 | port: this.readConfig('port'),
|
45 |
| - dstPort: this.readConfig('dstPort'), |
46 |
| - dstHost: this.readConfig('dstHost'), |
47 | 78 | username: this.readConfig('username'),
|
48 |
| - localPort: srcPort |
| 79 | + password: this.readConfig('password'), |
| 80 | + privateKey, |
| 81 | + passphrase: this.readConfig('passphrase') |
49 | 82 | };
|
50 | 83 |
|
51 |
| - var password = this.readConfig('password'); |
52 |
| - var privateKey = this.readConfig('privateKeyPath'); |
53 |
| - var tunnel = this.readConfig('tunnelClient'); |
54 |
| - |
55 |
| - if (password) { |
56 |
| - sshConfig.password = password; |
57 |
| - } else if (privateKey) { |
58 |
| - sshConfig.privateKey = fs.readFileSync(untildify(privateKey)); |
59 |
| - } |
| 84 | + const forwardOptions = { |
| 85 | + srcAddr: 'localhost', |
| 86 | + srcPort: this.readConfig('srcPort'), |
| 87 | + dstAddr: this.readConfig('dstHost'), |
| 88 | + dstPort: this.readConfig('dstPort') |
| 89 | + }; |
60 | 90 |
|
61 |
| - return new RSVP.Promise(function(resolve, reject) { |
62 |
| - var sshTunnel = tunnel(sshConfig, function(error /*, result */) { |
63 |
| - if (error) { |
64 |
| - reject(error); |
65 |
| - } else { |
| 91 | + return new RSVP.Promise(function (resolve, reject) { |
| 92 | + tunnel(tunnelOptions, serverOptions, sshOptions, forwardOptions) |
| 93 | + .then(([server]) => { |
66 | 94 | resolve({
|
67 | 95 | tunnel: {
|
68 |
| - handler: sshTunnel, |
| 96 | + handler: server, |
69 | 97 | srcPort: srcPort
|
70 | 98 | }
|
71 | 99 | });
|
72 |
| - } |
73 |
| - }); |
| 100 | + }) |
| 101 | + .catch((error) => { |
| 102 | + reject(error); |
| 103 | + }); |
74 | 104 | });
|
75 | 105 | },
|
76 | 106 |
|
77 |
| - teardown: function(context) { |
| 107 | + teardown: function (context) { |
78 | 108 | context.tunnel.handler.close();
|
79 | 109 | }
|
80 | 110 | });
|
|
0 commit comments