What is tunnel-ssh?
The tunnel-ssh npm package allows you to create SSH tunnels in Node.js applications. This can be useful for securely connecting to remote servers, databases, or other services that require SSH tunneling.
What are tunnel-ssh's main functionalities?
Basic SSH Tunnel
This code sample demonstrates how to set up a basic SSH tunnel using the tunnel-ssh package. The configuration object includes details such as the SSH username, host, port, destination host and port, local host and port, and the private key for authentication.
const tunnel = require('tunnel-ssh');
const config = {
username: 'user',
host: 'remote.server.com',
port: 22,
dstHost: '127.0.0.1',
dstPort: 27017,
localHost: '127.0.0.1',
localPort: 27017,
privateKey: require('fs').readFileSync('/path/to/private/key')
};
tunnel(config, function (error, server) {
if (error) {
console.error('SSH connection error: ', error);
} else {
console.log('SSH tunnel established');
}
});
SSH Tunnel with Forwarding
This code sample shows how to set up an SSH tunnel with port forwarding. The 'keepAlive' option is set to true to maintain the connection. This is useful for applications that need to keep a persistent connection to a remote database or service.
const tunnel = require('tunnel-ssh');
const config = {
username: 'user',
host: 'remote.server.com',
port: 22,
dstHost: '127.0.0.1',
dstPort: 3306,
localHost: '127.0.0.1',
localPort: 3306,
privateKey: require('fs').readFileSync('/path/to/private/key'),
keepAlive: true
};
tunnel(config, function (error, server) {
if (error) {
console.error('SSH connection error: ', error);
} else {
console.log('SSH tunnel with forwarding established');
}
});
Dynamic Port Forwarding
This code sample demonstrates how to set up a dynamic SSH tunnel, which can be used as a SOCKS proxy. The 'dynamic' option is set to true, allowing the tunnel to dynamically forward traffic to different destinations.
const tunnel = require('tunnel-ssh');
const config = {
username: 'user',
host: 'remote.server.com',
port: 22,
dstHost: '0.0.0.0',
dstPort: 0,
localHost: '127.0.0.1',
localPort: 1080,
privateKey: require('fs').readFileSync('/path/to/private/key'),
dynamic: true
};
tunnel(config, function (error, server) {
if (error) {
console.error('SSH connection error: ', error);
} else {
console.log('Dynamic SSH tunnel established');
}
});
Other packages similar to tunnel-ssh
ssh2
The ssh2 package is a more general-purpose SSH client for Node.js. It provides a wide range of SSH functionalities, including tunneling, executing commands, and file transfers. Compared to tunnel-ssh, ssh2 offers more flexibility and control over SSH connections but requires more setup and configuration.
node-ssh
The node-ssh package is a high-level SSH client for Node.js that simplifies the process of connecting to SSH servers, executing commands, and transferring files. It is easier to use than ssh2 but offers less control over the SSH connection. It is a good alternative to tunnel-ssh for users who need basic SSH functionalities without the complexity.
ssh2-sftp-client
The ssh2-sftp-client package is built on top of ssh2 and provides a simplified API for SFTP (SSH File Transfer Protocol) operations. While it focuses on file transfers, it can also be used to create SSH tunnels. It is a good choice for users who primarily need SFTP capabilities with occasional SSH tunneling.
Tunnel-SSH
One to connect them all !
Tunnel-ssh is based on the fantastic ssh2 library by Brian White.
Trouble ? Please study the ssh2 configuration.
v2.1.1 Released !
- Server now throws client exceptions see Example below Thx @joshbalfour
- Improved reverse proxy and example
##So what about next major version ?
With version 3 we will introduce a new feature to enable the developer to use
tunnel-ssh without wrapping your code, a feature i was asked very often for.
We think we have found a very good solution for that, and we already testing
the beta in production.
We want to make next release to be as stable as v2 but way more powerful and
easy to use. Until then we support both versions.
Keep digging !
Howto
####map remote port to localhost:
var tunnel = require('tunnel-ssh');
var server = tunnel({host: '172.16.0.8', dstPort: 3306}, function (error, result) {
console.log('connected');
});
####remap remote port to localhost
var config = {
host: '172.16.0.8',
username: 'root',
dstPort: 3306,
localPort: 3000
};
var server = tunnel(config, function (error, result) {
console.log('connected');
});
####catch SSH errors that occur outside of setup:
var tunnel = require('tunnel-ssh');
var server = tunnel({host: '172.16.0.8', dstPort: 3306}, function (error, result) {
console.log('connected');
});
server.on('error', function(err){
console.error('Something bad happened:', err);
});
####Reverse tunnel
The reverse tunnel can be used to bypass network restrictions,
or to listen to web-hocks on your local machine.
var tunnel = require('../');
var dstPort = 8000;
var host = 'tunneltest.com';
tunnel.reverse({
host: host,
username: 'root',
dstHost: '0.0.0.0',
dstPort: dstPort,
}, function(error, clientConnection) {
console.log(clientConnection._forwarding);
});
require('http').createServer(function(res, res){
res.end('SSH-TUNNEL: Gate to heaven !');
}).listen(localPort);
console.log('Tunnel created: http://'+host+':'+localPort);
Pro tip:
If you plan to expose a local port on a remote machine (external interface) you need to
enable the "GatewayPorts" option in your 'sshd_config'
Port 22
GatewayPorts yes
tunnel-ssh supports the default ssh2 configuration.
{
username: 'root',
port: 22,
srcPort: 0,
srcHost: 'localhost',
dstPort: null,
dstHost: 'localhost',
localHost: 'localhost'
agent : process.env.SSH_AUTH_SOCK,
privateKey:require('fs').readFileSync('/here/is/my/key'),
password:'secret'
}