From e94af46fd813134d615fec4b42f816d2d8bbc872 Mon Sep 17 00:00:00 2001 From: samueleaton Date: Thu, 5 May 2016 18:14:31 -0600 Subject: [PATCH] add ability to set 'sshPort' to use a different port thn 22 --- README.md | 16 +++++++++++++++- index.js | 53 +++++++++++++++++++++++++++++++++++++++++----------- src/index.js | 40 ++++++++++++++++++++++++++++++--------- 3 files changed, 88 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 1366ccf..32156c3 100644 --- a/README.md +++ b/README.md @@ -209,4 +209,18 @@ autossh({ }); ``` -**Warning:** The max poll count is there to prevent `autossh` from infinitely polling the local port. Rather than disabling it, it may be wise to set it to a high number (e.g. `500`). \ No newline at end of file +**Warning:** The max poll count is there to prevent `autossh` from infinitely polling the local port. Rather than disabling it, it may be wise to set it to a high number (e.g. `500`). + +#### Specifying a Different SSH Port + +The designated port for SSH according to the Transmission Control Protocol (TCP) is port 22, but you can specify a different port if you are using a different port. Set the `sshPort` property in the object you pass to `autossh`. + +```javascript +autossh({ + host: '111.22.333.444', + username: 'root', + localPort: 'auto', + remotePort: 5432, + sshPort: 9999 +}); +``` \ No newline at end of file diff --git a/index.js b/index.js index 5d058f8..5a8b674 100644 --- a/index.js +++ b/index.js @@ -49,6 +49,8 @@ var AutoSSH = function (_EventEmitter) { _this.serverAliveCountMax = typeof conf.serverAliveCountMax === 'number' ? conf.serverAliveCountMax : 1; + _this.sshPort = typeof conf.sshPort === 'number' ? conf.sshPort : null; + _this.privateKey = conf.privateKey || null; setImmediate(function () { @@ -178,22 +180,50 @@ var AutoSSH = function (_EventEmitter) { /* */ + }, { + key: 'generateDefaultOptions', + value: function generateDefaultOptions() { + var exitOnFailure = '-o ExitOnForwardFailure=yes'; + var strictHostCheck = '-o StrictHostKeyChecking=no'; + return exitOnFailure + ' ' + strictHostCheck; + } + + /* + */ + + }, { + key: 'generateServerAliveOptions', + value: function generateServerAliveOptions() { + var serverAliveInterval = '-o ServerAliveInterval=' + this.serverAliveInterval; + var serverAliveCountMax = '-o ServerAliveCountMax=' + this.serverAliveCountMax; + return serverAliveInterval + ' ' + serverAliveCountMax; + } + + /* + */ + + }, { + key: 'generateExecOptions', + value: function generateExecOptions() { + var serverAliveOpts = this.generateServerAliveOptions(); + var defaultOpts = this.generateDefaultOptions(); + var privateKey = this.privateKey ? '-i ' + this.privateKey : ''; + var sshPort = this.sshPort ? '-p ' + this.sshPort : ''; + + return defaultOpts + ' ' + serverAliveOpts + ' ' + privateKey + ' ' + sshPort; + } + + /* + */ + }, { key: 'generateExecString', value: function generateExecString() { var bindAddress = this.localPort + ':localhost:' + this.remotePort; - var exitOnFailure = '-o ExitOnForwardFailure=yes'; - var serverAliveInterval = '-o ServerAliveInterval=' + this.serverAliveInterval; - var serverAliveCountMax = '-o ServerAliveCountMax=' + this.serverAliveCountMax; - var serverAliveOpts = serverAliveInterval + ' ' + serverAliveCountMax; - var strictHostCheck = '-o StrictHostKeyChecking=no'; - var options = exitOnFailure + ' ' + serverAliveOpts + ' ' + strictHostCheck; - var privateKey = this.privateKey ? '-i ' + this.privateKey : ''; + var options = this.generateExecOptions(); var userAtHost = this.username + '@' + this.host; - this.execString = 'ssh -NL ' + bindAddress + ' ' + options + ' ' + privateKey + ' ' + userAtHost; - - return this.execString; + return 'ssh -NL ' + bindAddress + ' ' + options + ' ' + userAtHost; } /* @@ -204,7 +234,8 @@ var AutoSSH = function (_EventEmitter) { value: function execTunnel(execTunnelCb) { var _this5 = this; - this.currentProcess = (0, _child_process.exec)(this.generateExecString(), function (execErr, stdout, stderr) { + this.execString = this.generateExecString(); + this.currentProcess = (0, _child_process.exec)(this.execString, function (execErr, stdout, stderr) { if (/Address already in use/i.test(stderr)) { _this5.kill(); _this5.emit('error', stderr); diff --git a/src/index.js b/src/index.js index 45d6b13..cc63521 100644 --- a/src/index.js +++ b/src/index.js @@ -25,6 +25,8 @@ class AutoSSH extends EventEmitter { this.serverAliveCountMax = typeof conf.serverAliveCountMax === 'number' ? conf.serverAliveCountMax : 1; + this.sshPort = typeof conf.sshPort === 'number' ? conf.sshPort : null; + this.privateKey = conf.privateKey || null; setImmediate(() => { @@ -148,26 +150,46 @@ class AutoSSH extends EventEmitter { /* */ - generateExecString() { - const bindAddress = `${this.localPort}:localhost:${this.remotePort}`; + generateDefaultOptions() { const exitOnFailure = '-o ExitOnForwardFailure=yes'; + const strictHostCheck = `-o StrictHostKeyChecking=no`; + return `${exitOnFailure} ${strictHostCheck}`; + } + + /* + */ + generateServerAliveOptions() { const serverAliveInterval = `-o ServerAliveInterval=${this.serverAliveInterval}`; const serverAliveCountMax = `-o ServerAliveCountMax=${this.serverAliveCountMax}`; - const serverAliveOpts = `${serverAliveInterval} ${serverAliveCountMax}`; - const strictHostCheck = `-o StrictHostKeyChecking=no`; - const options = `${exitOnFailure} ${serverAliveOpts} ${strictHostCheck}`; + return `${serverAliveInterval} ${serverAliveCountMax}`; + } + + /* + */ + generateExecOptions() { + const serverAliveOpts = this.generateServerAliveOptions(); + const defaultOpts = this.generateDefaultOptions(); const privateKey = this.privateKey ? `-i ${this.privateKey}` : ''; + const sshPort = this.sshPort ? `-p ${this.sshPort}` : ''; + + return `${defaultOpts} ${serverAliveOpts} ${privateKey} ${sshPort}`; + } + + /* + */ + generateExecString() { + const bindAddress = `${this.localPort}:localhost:${this.remotePort}`; + const options = this.generateExecOptions(); const userAtHost = `${this.username}@${this.host}`; - this.execString = `ssh -NL ${bindAddress} ${options} ${privateKey} ${userAtHost}`; - - return this.execString; + return `ssh -NL ${bindAddress} ${options} ${userAtHost}`; } /* */ execTunnel(execTunnelCb) { - this.currentProcess = exec(this.generateExecString(), (execErr, stdout, stderr) => { + this.execString = this.generateExecString(); + this.currentProcess = exec(this.execString, (execErr, stdout, stderr) => { if (/Address already in use/i.test(stderr)) { this.kill(); this.emit('error', stderr);