tunnel-ssh
Advanced tools
Comparing version 5.0.8 to 5.1.0
80
index.js
const net = require('net'); | ||
const { Client } = require('ssh2'); | ||
const os = require('os'); | ||
function autoClose(server, connection) { | ||
@@ -17,4 +17,4 @@ connection.on('close', () => { | ||
let serverOptions = Object.assign({}, options); | ||
if(!serverOptions.port && !serverOptions.path){ | ||
if (!serverOptions.port && !serverOptions.path) { | ||
serverOptions = null; | ||
@@ -30,3 +30,3 @@ } | ||
process.on('uncaughtException', errorHandler); | ||
server.listen(serverOptions); | ||
@@ -40,3 +40,3 @@ server.on('listening', () => { | ||
async function createClient(config) { | ||
async function createSSHConnection(config) { | ||
return new Promise(function (resolve, reject) { | ||
@@ -50,17 +50,18 @@ let conn = new Client(); | ||
async function createTunnel(tunnelOptions, serverOptions, sshOptions, forwardOptions) { | ||
async function createTunnel( tunnelOptions, serverOptions, sshOptions, forwardOptions ) { | ||
let forwardOptionsLocal = Object.assign({}, forwardOptions); | ||
let tunnelOptionsLocal = Object.assign({}, tunnelOptions || {}); | ||
let sshOptionslocal = Object.assign({ port: 22, username: 'root' }, sshOptions); | ||
let forwardOptionsLocal = Object.assign({ dstAddr: '0.0.0.0' }, forwardOptions); | ||
let tunnelOptionsLocal = Object.assign({ autoClose: false, reconnectOnError: false }, tunnelOptions || {}); | ||
let server, sshConnection; | ||
return new Promise(async function (resolve, reject) { | ||
let server, conn; | ||
try { | ||
server = await createServer(serverOptions); | ||
if(!forwardOptionsLocal.srcPort){ | ||
forwardOptionsLocal.srcPort = server.address().port; | ||
} | ||
if(!forwardOptionsLocal.srcAddr){ | ||
forwardOptionsLocal.srcAddr = server.address().address; | ||
} | ||
addListenerServer(server); | ||
} catch (e) { | ||
@@ -71,3 +72,4 @@ return reject(e); | ||
try { | ||
conn = await createClient(sshOptions); | ||
sshConnection = await createSSHConnection(sshOptionslocal); | ||
addListenerSshConnection(sshConnection); | ||
} catch (e) { | ||
@@ -79,9 +81,41 @@ if (server) { | ||
} | ||
server.on('connection', (connection) => { | ||
function addListenerSshConnection(sshConnection) { | ||
if (tunnelOptionsLocal.reconnectOnError) { | ||
sshConnection.on('error', async () => { | ||
sshConnection.isBroken = true; | ||
sshConnection = await createSSHConnection(sshOptionslocal); | ||
addEventListener(sshConnection); | ||
}); | ||
} | ||
} | ||
function addListenerServer(server) { | ||
if (tunnelOptionsLocal.reconnectOnError) { | ||
server.on('error', async () => { | ||
server = await createServer(serverOptions); | ||
addListenerServer(server); | ||
}); | ||
} | ||
server.on('connection', onConnectionHandler); | ||
server.on('close', () => sshConnection.end()); | ||
} | ||
function onConnectionHandler(clientConnection) { | ||
if (!forwardOptionsLocal.srcPort) { | ||
forwardOptionsLocal.srcPort = server.address().port; | ||
} | ||
if (!forwardOptionsLocal.srcAddr) { | ||
forwardOptionsLocal.srcAddr = server.address().address; | ||
} | ||
if (tunnelOptionsLocal.autoClose) { | ||
autoClose(server, connection); | ||
autoClose(server, clientConnection); | ||
} | ||
conn.forwardOut( | ||
if (sshConnection.isBroken) { | ||
return; | ||
} | ||
sshConnection.forwardOut( | ||
forwardOptionsLocal.srcAddr, | ||
@@ -97,10 +131,8 @@ forwardOptionsLocal.srcPort, | ||
} else { | ||
connection.pipe(stream).pipe(connection); | ||
clientConnection.pipe(stream).pipe(clientConnection); | ||
} | ||
}); | ||
}); | ||
server.on('close', () => conn.end()); | ||
resolve([server, conn]); | ||
} | ||
resolve([server, sshConnection]); | ||
}); | ||
@@ -107,0 +139,0 @@ } |
{ | ||
"name": "tunnel-ssh", | ||
"version": "5.0.8", | ||
"version": "5.1.0", | ||
"description": "Easy extendable SSH tunnel", | ||
@@ -9,3 +9,3 @@ "main": "index.js", | ||
}, | ||
"scripts": {}, | ||
"types": "./types/index.d.ts", | ||
"repository": { | ||
@@ -30,4 +30,4 @@ "type": "git", | ||
"dependencies": { | ||
"ssh2": "^1.13.0" | ||
"ssh2": "^1.14.0" | ||
} | ||
} |
@@ -31,16 +31,8 @@ Tunnel-SSH | ||
### Changelog | ||
##### 5.1.0 | ||
* Improved Typescript support | ||
* sshOptions.username default is root | ||
* forwardOptions.dstAddr default to 0.0.0.0 (all interfaces) | ||
### Latest Release 5.x.x | ||
### Breaking change in 5.0.0 | ||
Please note that release 5.0.0 uses a complete different approch for configuration and is not compatible to prio versions. | ||
#### New Features | ||
* Reuse of ssh client instead of creating a new one for each connection | ||
* Promise / Async Await support | ||
## Concept | ||
@@ -109,2 +101,22 @@ | ||
## SSH Agent additional information. | ||
The most common settings for the agent are : | ||
```js | ||
// for linux | ||
{ | ||
host:'myhost.com' | ||
agent:process.env.SSH_AUTH_SOCK | ||
} | ||
// for windows | ||
{ | ||
agent:'pageant' | ||
} | ||
// for windows with unix port (wsl docker | ||
{ | ||
agent:'\\\\.\\pipe\\openssh-ssh-agent' | ||
} | ||
``` | ||
Example: | ||
@@ -177,2 +189,14 @@ | ||
## Typescript | ||
Since 5.0.9 we added our own types to the project. | ||
For Typescript we export the configuration objects as well. | ||
The recommented way of import is as follows: | ||
```Typescript | ||
import {createTunnel, ForwardOptions, ServerOptions, SshOptions} from 'tunnel-ssh'; | ||
// please note that the ForwardingOptions, ServerOptions and SshOptions are Types | ||
``` | ||
The method retuns a promise containing the server and ssh-client instance. For most cases you will not need those instances. But in case you want to extend the functionallity you can use them to | ||
@@ -179,0 +203,0 @@ bind to there events like that: |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15644
174
289
Updatedssh2@^1.14.0