@mongodb-js/ssh-tunnel
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -19,2 +19,6 @@ /// <reference types="node" /> | ||
private rawConfig; | ||
private sshClient; | ||
private serverListen; | ||
private serverClose; | ||
private forwardOut; | ||
constructor(config?: Partial<SshTunnelConfig>); | ||
@@ -24,4 +28,5 @@ get config(): SshTunnelConfig; | ||
close(): Promise<void>; | ||
private closeSshClient; | ||
private closeOpenConnections; | ||
} | ||
export default SshTunnel; |
@@ -25,3 +25,3 @@ "use strict"; | ||
localAddr: '127.0.0.1', | ||
localPort: connectConfig.port, | ||
localPort: 0, | ||
}, config); | ||
@@ -34,21 +34,6 @@ } | ||
this.rawConfig = getSshTunnelConfig(config); | ||
this.server = net_1.createServer((socket) => { | ||
const sshClient = new ssh2_1.Client(); | ||
const forwardOut = util_1.promisify(sshClient.forwardOut.bind(sshClient)); | ||
this.sshClient = new ssh2_1.Client(); | ||
this.forwardOut = util_1.promisify(this.sshClient.forwardOut.bind(this.sshClient)); | ||
this.server = net_1.createServer(async (socket) => { | ||
this.connections.add(socket); | ||
sshClient.on('ready', async () => { | ||
const { srcAddr, srcPort, dstAddr, dstPort } = this.rawConfig; | ||
try { | ||
const channel = await forwardOut(srcAddr, srcPort, dstAddr, dstPort); | ||
socket.pipe(channel).pipe(socket); | ||
} | ||
catch (err) { | ||
err.origin = 'ssh-client'; | ||
socket.destroy(err); | ||
} | ||
}); | ||
sshClient.on('error', (err) => { | ||
err.origin = 'ssh-client'; | ||
socket.destroy(err); | ||
}); | ||
socket.on('error', (err) => { | ||
@@ -60,9 +45,8 @@ var _a; | ||
socket.once('close', () => { | ||
sshClient.end(); | ||
}); | ||
socket.once('close', () => { | ||
this.connections.delete(socket); | ||
}); | ||
try { | ||
sshClient.connect(getConnectConfig(this.rawConfig)); | ||
const { srcAddr, srcPort, dstAddr, dstPort } = this.rawConfig; | ||
const channel = await this.forwardOut(srcAddr, srcPort, dstAddr, dstPort); | ||
socket.pipe(channel).pipe(socket); | ||
} | ||
@@ -74,2 +58,4 @@ catch (err) { | ||
}); | ||
this.serverListen = util_1.promisify(this.server.listen.bind(this.server)); | ||
this.serverClose = util_1.promisify(this.server.close.bind(this.server)); | ||
['close', 'connection', 'error', 'listening'].forEach((eventName) => { | ||
@@ -88,8 +74,22 @@ this.server.on(eventName, this.emit.bind(this, eventName)); | ||
async listen() { | ||
const serverListen = util_1.promisify(this.server.listen.bind(this.server)); | ||
const { localPort, localAddr } = this.rawConfig; | ||
await serverListen(localPort, localAddr); | ||
await this.serverListen(localPort, localAddr); | ||
try { | ||
await Promise.race([ | ||
events_1.once(this.sshClient, 'error').then(([err]) => { | ||
throw err; | ||
}), | ||
(() => { | ||
const waitForReady = events_1.once(this.sshClient, 'ready'); | ||
this.sshClient.connect(getConnectConfig(this.rawConfig)); | ||
return waitForReady; | ||
})(), | ||
]); | ||
} | ||
catch (err) { | ||
await this.serverClose(); | ||
throw err; | ||
} | ||
} | ||
async close() { | ||
const serverClose = util_1.promisify(this.server.close.bind(this.server)); | ||
const [maybeError] = await Promise.all([ | ||
@@ -99,3 +99,4 @@ // If we catch anything, just return the error instead of throwing, we | ||
// close error | ||
serverClose().catch((e) => e), | ||
this.serverClose().catch((e) => e), | ||
this.closeSshClient(), | ||
this.closeOpenConnections(), | ||
@@ -107,2 +108,10 @@ ]); | ||
} | ||
async closeSshClient() { | ||
try { | ||
return events_1.once(this.sshClient, 'close'); | ||
} | ||
finally { | ||
this.sshClient.end(); | ||
} | ||
} | ||
async closeOpenConnections() { | ||
@@ -109,0 +118,0 @@ const waitForClose = []; |
{ | ||
"name": "@mongodb-js/ssh-tunnel", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Yet another ssh tunnel based on ssh2", | ||
@@ -35,2 +35,3 @@ "main": "lib/index.js", | ||
"@types/jest": "^26.0.20", | ||
"@types/node-fetch": "^2.5.8", | ||
"@types/ssh2": "^0.5.46", | ||
@@ -42,2 +43,3 @@ "@typescript-eslint/eslint-plugin": "^4.15.1", | ||
"jest": "^26.6.3", | ||
"node-fetch": "^2.6.1", | ||
"prettier": "^2.2.1", | ||
@@ -44,0 +46,0 @@ "ts-jest": "^26.5.1", |
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
17328
153
14