add ability to set 'sshPort' to use a different port thn 22

This commit is contained in:
samueleaton 2016-05-05 18:14:31 -06:00
parent 11741caf5a
commit e94af46fd8
3 changed files with 88 additions and 21 deletions

View file

@ -210,3 +210,17 @@ 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`).
#### 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
});
```

View file

@ -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);

View file

@ -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);