mirror of
https://github.com/vale981/autossh
synced 2025-03-04 09:01:37 -05:00
Merge pull request #6 from vale981/reverse-forwarding
Add forwarding for reverse tunnels.
This commit is contained in:
commit
044e72766d
3 changed files with 52 additions and 10 deletions
22
README.md
22
README.md
|
@ -54,6 +54,7 @@ The `connect` event will fire only once when the initial ssh connection is made.
|
|||
- `kill` - a method to kill autossh
|
||||
- `pid` - the autossh process id
|
||||
- `host`
|
||||
- `localHost` - The host, to which the tunnel applies.
|
||||
- `username`
|
||||
- `remotePort`
|
||||
- `localPort`
|
||||
|
@ -127,6 +128,27 @@ autossh({
|
|||
|
||||
<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
|
||||
|
||||
The autossh process will automatically die if the node process is closed, but you can manually kill the process using `kill`.
|
||||
|
|
20
index.js
20
index.js
|
@ -55,9 +55,10 @@ var AutoSSH = function (_EventEmitter) {
|
|||
_createClass(AutoSSH, [{
|
||||
key: 'configure',
|
||||
value: function configure(conf) {
|
||||
this.reverse = conf.reverse === true || false;
|
||||
|
||||
this.host = conf.host;
|
||||
this.localHost = conf.localHost || 'localhost';
|
||||
this.reverse = conf.reverse === true || this.localHost !== 'localhost';
|
||||
|
||||
this.username = conf.username || 'root';
|
||||
this.remotePort = conf.remotePort;
|
||||
|
||||
|
@ -84,7 +85,7 @@ var AutoSSH = function (_EventEmitter) {
|
|||
var _this2 = this;
|
||||
|
||||
var port = this.localPort === 'auto' ? this.generateRandomPort() : this.localPort;
|
||||
if (this.reverse) {
|
||||
if (this.reverse || this.localHost !== 'localhost') {
|
||||
this.execTunnel(function () {
|
||||
_this2.pollConnection();
|
||||
});
|
||||
|
@ -117,6 +118,7 @@ var AutoSSH = function (_EventEmitter) {
|
|||
},
|
||||
pid: null,
|
||||
host: this.host || null,
|
||||
localHost: this.localHost || null,
|
||||
username: this.username || null,
|
||||
remotePort: parseInt(this.remotePort),
|
||||
localPort: parseInt(this.localPort),
|
||||
|
@ -193,11 +195,18 @@ var AutoSSH = function (_EventEmitter) {
|
|||
value: function isConnectionEstablished(connEstablishedCb) {
|
||||
var _this6 = this;
|
||||
|
||||
if (this.localHost !== 'localhost' || this.reverse) {
|
||||
connEstablishedCb(true);
|
||||
return;
|
||||
}
|
||||
|
||||
_portfinder2.default.getPort({ port: this.localPort }, function (portfinderErr, freePort) {
|
||||
if (portfinderErr) return connEstablishedCb(false);
|
||||
|
||||
if (_this6.localPort === freePort) return connEstablishedCb(false);else return connEstablishedCb(true);
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* parses the conf for errors
|
||||
|
@ -272,8 +281,9 @@ var AutoSSH = function (_EventEmitter) {
|
|||
var defaultOpts = this.generateDefaultOptions();
|
||||
var privateKey = this.privateKey ? '-i ' + this.privateKey : '';
|
||||
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() {
|
||||
var startPort = this.reverse ? this.remotePort : this.localPort;
|
||||
var endPort = this.reverse ? this.localPort : this.remotePort;
|
||||
var bindAddress = startPort + ':localhost:' + endPort;
|
||||
var bindAddress = startPort + ':' + this.localHost + ':' + endPort;
|
||||
var options = this.generateExecOptions();
|
||||
var userAtHost = this.username + '@' + this.host;
|
||||
var method = this.reverse ? 'R' : 'L';
|
||||
|
|
20
src/index.js
20
src/index.js
|
@ -27,9 +27,10 @@ class AutoSSH extends EventEmitter {
|
|||
}
|
||||
|
||||
configure(conf) {
|
||||
this.reverse = conf.reverse === true || false;
|
||||
|
||||
this.host = conf.host;
|
||||
this.localHost = conf.localHost || 'localhost';
|
||||
this.reverse = conf.reverse === true || (this.localHost !== 'localhost');
|
||||
|
||||
this.username = conf.username || 'root';
|
||||
this.remotePort = conf.remotePort;
|
||||
|
||||
|
@ -56,7 +57,7 @@ class AutoSSH extends EventEmitter {
|
|||
*/
|
||||
connect(conf) {
|
||||
const port = this.localPort === 'auto' ? this.generateRandomPort() : this.localPort;
|
||||
if (this.reverse) {
|
||||
if (this.reverse || this.localHost !== 'localhost') {
|
||||
this.execTunnel(() => {
|
||||
this.pollConnection();
|
||||
});
|
||||
|
@ -87,6 +88,7 @@ class AutoSSH extends EventEmitter {
|
|||
kill: () => this.kill,
|
||||
pid: null,
|
||||
host: this.host || null,
|
||||
localHost: this.localHost || null,
|
||||
username: this.username || null,
|
||||
remotePort: parseInt(this.remotePort),
|
||||
localPort: parseInt(this.localPort),
|
||||
|
@ -150,6 +152,11 @@ class AutoSSH extends EventEmitter {
|
|||
/* checks if connection is established at port
|
||||
*/
|
||||
isConnectionEstablished(connEstablishedCb) {
|
||||
if (this.localHost !== 'localhost' || this.reverse) {
|
||||
connEstablishedCb(true);
|
||||
return;
|
||||
}
|
||||
|
||||
portfinder.getPort({ port: this.localPort }, (portfinderErr, freePort) => {
|
||||
if (portfinderErr)
|
||||
return connEstablishedCb(false);
|
||||
|
@ -159,6 +166,8 @@ class AutoSSH extends EventEmitter {
|
|||
else
|
||||
return connEstablishedCb(true);
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* parses the conf for errors
|
||||
|
@ -236,8 +245,9 @@ class AutoSSH extends EventEmitter {
|
|||
const defaultOpts = this.generateDefaultOptions();
|
||||
const privateKey = this.privateKey ? `-i ${this.privateKey}` : '';
|
||||
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() {
|
||||
const startPort = this.reverse ? this.remotePort : this.localPort;
|
||||
const endPort = this.reverse ? this.localPort : this.remotePort;
|
||||
const bindAddress = `${startPort}:localhost:${endPort}`;
|
||||
const bindAddress = `${startPort}:${this.localHost}:${endPort}`;
|
||||
const options = this.generateExecOptions();
|
||||
const userAtHost = `${this.username}@${this.host}`;
|
||||
const method = this.reverse ? 'R' : 'L';
|
||||
|
|
Loading…
Add table
Reference in a new issue