Merge pull request #6 from vale981/reverse-forwarding

Add forwarding for reverse tunnels.
This commit is contained in:
Sam Eaton 2017-08-11 21:12:43 -07:00 committed by GitHub
commit 044e72766d
3 changed files with 52 additions and 10 deletions

View file

@ -54,6 +54,7 @@ The `connect` event will fire only once when the initial ssh connection is made.
- `kill` - a method to kill autossh - `kill` - a method to kill autossh
- `pid` - the autossh process id - `pid` - the autossh process id
- `host` - `host`
- `localHost` - The host, to which the tunnel applies.
- `username` - `username`
- `remotePort` - `remotePort`
- `localPort` - `localPort`
@ -127,6 +128,27 @@ autossh({
<br /> <br />
#### Tunneling Ports from another Host
It is also possible to use the tunnel as gateway to another host in the local network (for example a webcam).
By default, the `localHost` property is set to `localhost`, but you can overwrite it.
**Note that setting this property to a value different from `localhost` will make the tunnel reverse automaticly.**
``` javascript
autossh({
host: '111.22.333.444',
localhost: '192.168.1.25',
username: 'root',
localPort: '64444',
remotePort: 5432
})
.on('connect', connection => {
console.log('connected: ', connection);
});
```
#### Killing the Autossh Process #### Killing the Autossh Process
The autossh process will automatically die if the node process is closed, but you can manually kill the process using `kill`. The autossh process will automatically die if the node process is closed, but you can manually kill the process using `kill`.

View file

@ -55,9 +55,10 @@ var AutoSSH = function (_EventEmitter) {
_createClass(AutoSSH, [{ _createClass(AutoSSH, [{
key: 'configure', key: 'configure',
value: function configure(conf) { value: function configure(conf) {
this.reverse = conf.reverse === true || false;
this.host = conf.host; this.host = conf.host;
this.localHost = conf.localHost || 'localhost';
this.reverse = conf.reverse === true || this.localHost !== 'localhost';
this.username = conf.username || 'root'; this.username = conf.username || 'root';
this.remotePort = conf.remotePort; this.remotePort = conf.remotePort;
@ -84,7 +85,7 @@ var AutoSSH = function (_EventEmitter) {
var _this2 = this; var _this2 = this;
var port = this.localPort === 'auto' ? this.generateRandomPort() : this.localPort; var port = this.localPort === 'auto' ? this.generateRandomPort() : this.localPort;
if (this.reverse) { if (this.reverse || this.localHost !== 'localhost') {
this.execTunnel(function () { this.execTunnel(function () {
_this2.pollConnection(); _this2.pollConnection();
}); });
@ -117,6 +118,7 @@ var AutoSSH = function (_EventEmitter) {
}, },
pid: null, pid: null,
host: this.host || null, host: this.host || null,
localHost: this.localHost || null,
username: this.username || null, username: this.username || null,
remotePort: parseInt(this.remotePort), remotePort: parseInt(this.remotePort),
localPort: parseInt(this.localPort), localPort: parseInt(this.localPort),
@ -193,11 +195,18 @@ var AutoSSH = function (_EventEmitter) {
value: function isConnectionEstablished(connEstablishedCb) { value: function isConnectionEstablished(connEstablishedCb) {
var _this6 = this; var _this6 = this;
if (this.localHost !== 'localhost' || this.reverse) {
connEstablishedCb(true);
return;
}
_portfinder2.default.getPort({ port: this.localPort }, function (portfinderErr, freePort) { _portfinder2.default.getPort({ port: this.localPort }, function (portfinderErr, freePort) {
if (portfinderErr) return connEstablishedCb(false); if (portfinderErr) return connEstablishedCb(false);
if (_this6.localPort === freePort) return connEstablishedCb(false);else return connEstablishedCb(true); if (_this6.localPort === freePort) return connEstablishedCb(false);else return connEstablishedCb(true);
}); });
return;
} }
/* parses the conf for errors /* parses the conf for errors
@ -272,8 +281,9 @@ var AutoSSH = function (_EventEmitter) {
var defaultOpts = this.generateDefaultOptions(); var defaultOpts = this.generateDefaultOptions();
var privateKey = this.privateKey ? '-i ' + this.privateKey : ''; var privateKey = this.privateKey ? '-i ' + this.privateKey : '';
var sshPort = this.sshPort === 22 ? '' : '-p ' + this.sshPort; var sshPort = this.sshPort === 22 ? '' : '-p ' + this.sshPort;
var gatewayPorts = this.localHost === 'localhost' ? '' : '-o GatewayPorts=yes';
return defaultOpts + ' ' + serverAliveOpts + ' ' + privateKey + ' ' + sshPort; return defaultOpts + ' ' + serverAliveOpts + ' ' + gatewayPorts + ' ' + privateKey + ' ' + sshPort;
} }
/* /*
@ -284,7 +294,7 @@ var AutoSSH = function (_EventEmitter) {
value: function generateExecString() { value: function generateExecString() {
var startPort = this.reverse ? this.remotePort : this.localPort; var startPort = this.reverse ? this.remotePort : this.localPort;
var endPort = this.reverse ? this.localPort : this.remotePort; var endPort = this.reverse ? this.localPort : this.remotePort;
var bindAddress = startPort + ':localhost:' + endPort; var bindAddress = startPort + ':' + this.localHost + ':' + endPort;
var options = this.generateExecOptions(); var options = this.generateExecOptions();
var userAtHost = this.username + '@' + this.host; var userAtHost = this.username + '@' + this.host;
var method = this.reverse ? 'R' : 'L'; var method = this.reverse ? 'R' : 'L';

View file

@ -27,9 +27,10 @@ class AutoSSH extends EventEmitter {
} }
configure(conf) { configure(conf) {
this.reverse = conf.reverse === true || false;
this.host = conf.host; this.host = conf.host;
this.localHost = conf.localHost || 'localhost';
this.reverse = conf.reverse === true || (this.localHost !== 'localhost');
this.username = conf.username || 'root'; this.username = conf.username || 'root';
this.remotePort = conf.remotePort; this.remotePort = conf.remotePort;
@ -56,7 +57,7 @@ class AutoSSH extends EventEmitter {
*/ */
connect(conf) { connect(conf) {
const port = this.localPort === 'auto' ? this.generateRandomPort() : this.localPort; const port = this.localPort === 'auto' ? this.generateRandomPort() : this.localPort;
if (this.reverse) { if (this.reverse || this.localHost !== 'localhost') {
this.execTunnel(() => { this.execTunnel(() => {
this.pollConnection(); this.pollConnection();
}); });
@ -87,6 +88,7 @@ class AutoSSH extends EventEmitter {
kill: () => this.kill, kill: () => this.kill,
pid: null, pid: null,
host: this.host || null, host: this.host || null,
localHost: this.localHost || null,
username: this.username || null, username: this.username || null,
remotePort: parseInt(this.remotePort), remotePort: parseInt(this.remotePort),
localPort: parseInt(this.localPort), localPort: parseInt(this.localPort),
@ -150,6 +152,11 @@ class AutoSSH extends EventEmitter {
/* checks if connection is established at port /* checks if connection is established at port
*/ */
isConnectionEstablished(connEstablishedCb) { isConnectionEstablished(connEstablishedCb) {
if (this.localHost !== 'localhost' || this.reverse) {
connEstablishedCb(true);
return;
}
portfinder.getPort({ port: this.localPort }, (portfinderErr, freePort) => { portfinder.getPort({ port: this.localPort }, (portfinderErr, freePort) => {
if (portfinderErr) if (portfinderErr)
return connEstablishedCb(false); return connEstablishedCb(false);
@ -159,6 +166,8 @@ class AutoSSH extends EventEmitter {
else else
return connEstablishedCb(true); return connEstablishedCb(true);
}); });
return;
} }
/* parses the conf for errors /* parses the conf for errors
@ -236,8 +245,9 @@ class AutoSSH extends EventEmitter {
const defaultOpts = this.generateDefaultOptions(); const defaultOpts = this.generateDefaultOptions();
const privateKey = this.privateKey ? `-i ${this.privateKey}` : ''; const privateKey = this.privateKey ? `-i ${this.privateKey}` : '';
const sshPort = this.sshPort === 22 ? '' : `-p ${this.sshPort}`; const sshPort = this.sshPort === 22 ? '' : `-p ${this.sshPort}`;
const gatewayPorts = this.localHost === 'localhost' ? '' : '-o GatewayPorts=yes';
return `${defaultOpts} ${serverAliveOpts} ${privateKey} ${sshPort}`; return `${defaultOpts} ${serverAliveOpts} ${gatewayPorts} ${privateKey} ${sshPort}`;
} }
/* /*
@ -245,7 +255,7 @@ class AutoSSH extends EventEmitter {
generateExecString() { generateExecString() {
const startPort = this.reverse ? this.remotePort : this.localPort; const startPort = this.reverse ? this.remotePort : this.localPort;
const endPort = this.reverse ? this.localPort : this.remotePort; const endPort = this.reverse ? this.localPort : this.remotePort;
const bindAddress = `${startPort}:localhost:${endPort}`; const bindAddress = `${startPort}:${this.localHost}:${endPort}`;
const options = this.generateExecOptions(); const options = this.generateExecOptions();
const userAtHost = `${this.username}@${this.host}`; const userAtHost = `${this.username}@${this.host}`;
const method = this.reverse ? 'R' : 'L'; const method = this.reverse ? 'R' : 'L';