tunnel-ssh
Advanced tools
Comparing version 5.0.4 to 5.0.5
75
index.js
@@ -5,6 +5,6 @@ const net = require('net'); | ||
function autoClose(server, connection){ | ||
connection.on('close',()=>{ | ||
server.getConnections((error, count)=>{ | ||
if(count === 0){ | ||
function autoClose(server, connection) { | ||
connection.on('close', () => { | ||
server.getConnections((error, count) => { | ||
if (count === 0) { | ||
server.close(); | ||
@@ -16,13 +16,20 @@ } | ||
async function createServer(options){ | ||
return new Promise(function(resolve, reject){ | ||
async function createServer(options) { | ||
return new Promise((resolve, reject) => { | ||
let server = net.createServer(); | ||
let errorHandler = function (error) { | ||
reject(error); | ||
}; | ||
server.on('error', errorHandler); | ||
process.on('uncaughtException', errorHandler); | ||
server.listen(options); | ||
server.on('listening', ()=> resolve(server)); | ||
server.on('error', reject); | ||
server.on('listening', () => { | ||
process.removeListener('uncaughtException', errorHandler); | ||
resolve(server); | ||
}); | ||
}); | ||
} | ||
async function createClient(config){ | ||
return new Promise(function(resolve, reject){ | ||
async function createClient(config) { | ||
return new Promise(function (resolve, reject) { | ||
let conn = new Client(); | ||
@@ -35,27 +42,35 @@ conn.on('ready', () => resolve(conn)); | ||
async function createTunnel(tunnelOptions, serverOptions, sshOptions, forwardOptions){ | ||
async function createTunnel(tunnelOptions, serverOptions, sshOptions, forwardOptions) { | ||
return new Promise(function(resolve, reject){ | ||
createServer(serverOptions).then(function(server, reject){ | ||
createClient(sshOptions).then(function(conn){ | ||
server.on('connection',(connection)=>{ | ||
if(tunnelOptions.autoClose){ | ||
autoClose(server, connection); | ||
} | ||
return new Promise(async function (resolve, reject) { | ||
let server, conn; | ||
try { | ||
server = await createServer(serverOptions); | ||
} catch (e) { | ||
return reject(e); | ||
} | ||
conn.forwardOut( | ||
forwardOptions.srcAddr, | ||
forwardOptions.srcPort, | ||
forwardOptions.dstAddr, | ||
forwardOptions.dstPort, (err, stream) => { | ||
connection.pipe(stream).pipe(connection); | ||
}); | ||
try { | ||
conn = await createClient(sshOptions); | ||
} catch (e) { | ||
return reject(e); | ||
} | ||
server.on('connection', (connection) => { | ||
if (tunnelOptions.autoClose) { | ||
autoClose(server, connection); | ||
} | ||
conn.forwardOut( | ||
forwardOptions.srcAddr, | ||
forwardOptions.srcPort, | ||
forwardOptions.dstAddr, | ||
forwardOptions.dstPort, (err, stream) => { | ||
connection.pipe(stream).pipe(connection); | ||
}); | ||
server.on('close',()=> conn.end()); | ||
resolve([server, conn]); | ||
}); | ||
}); | ||
server.on('close', () => conn.end()); | ||
resolve([server, conn]); | ||
}); | ||
@@ -62,0 +77,0 @@ } |
{ | ||
"name": "tunnel-ssh", | ||
"version": "5.0.4", | ||
"version": "5.0.5", | ||
"description": "Easy extendable SSH tunnel", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
8765
63