@mongodb-js/ssh-tunnel
Advanced tools
Comparing version 0.0.0-next-bb1a39e78c30cda5ef910174e54d2c9708186388 to 0.0.0-next-bc37cfc07b82d66ae76a8abc7104e13cc84c22eb
/// <reference types="node" /> | ||
import { EventEmitter } from 'events'; | ||
import type { ConnectConfig } from 'ssh2'; | ||
declare type LocalProxyServerConfig = { | ||
type LocalProxyServerConfig = { | ||
localAddr: string; | ||
@@ -10,3 +10,3 @@ localPort: number; | ||
}; | ||
export declare type SshTunnelConfig = ConnectConfig & LocalProxyServerConfig; | ||
export type SshTunnelConfig = ConnectConfig & LocalProxyServerConfig; | ||
export declare class SshTunnel extends EventEmitter { | ||
@@ -23,2 +23,3 @@ private connected; | ||
private forwardOut; | ||
private logCtx; | ||
constructor(config?: Partial<SshTunnelConfig>); | ||
@@ -25,0 +26,0 @@ get config(): SshTunnelConfig; |
@@ -10,7 +10,7 @@ "use strict"; | ||
const ssh2_1 = require("ssh2"); | ||
const debug_1 = __importDefault(require("debug")); | ||
const compass_logging_1 = require("@mongodb-js/compass-logging"); | ||
const server_1 = __importDefault(require("socksv5/lib/server")); | ||
const None_1 = __importDefault(require("socksv5/lib/auth/None")); | ||
const UserPassword_1 = __importDefault(require("socksv5/lib/auth/UserPassword")); | ||
const debug = (0, debug_1.default)('mongodb:ssh-tunnel'); | ||
const { log, mongoLogId, debug } = (0, compass_logging_1.createLogger)('COMPASS-SSH-TUNNEL'); | ||
function getConnectConfig(config) { | ||
@@ -29,2 +29,3 @@ const { localAddr, localPort, socks5Password, socks5Username, ...connectConfig } = config; | ||
} | ||
let idCounter = 0; | ||
class SshTunnel extends events_1.EventEmitter { | ||
@@ -36,6 +37,7 @@ constructor(config = {}) { | ||
this.connections = new Set(); | ||
this.logCtx = `tunnel-${idCounter++}`; | ||
this.rawConfig = getSshTunnelConfig(config); | ||
this.sshClient = new ssh2_1.Client(); | ||
this.sshClient.on('close', () => { | ||
debug('sshClient closed'); | ||
log.info(mongoLogId(1001000252), this.logCtx, 'sshClient closed'); | ||
this.connected = false; | ||
@@ -49,8 +51,8 @@ }); | ||
this.rawConfig.socks5Password === pass; | ||
debug('validating auth parameters', success); | ||
process.nextTick(cb, success); | ||
log.info(mongoLogId(1001000253), this.logCtx, 'Validated auth parameters', { success }); | ||
queueMicrotask(() => cb(success)); | ||
})); | ||
} | ||
else { | ||
debug('skipping auth setup for this server'); | ||
log.info(mongoLogId(1001000254), this.logCtx, 'Skipping auth setup'); | ||
this.server.useAuth((0, None_1.default)()); | ||
@@ -68,3 +70,3 @@ } | ||
...this.rawConfig, | ||
localPort: (typeof serverAddress !== 'string' && (serverAddress === null || serverAddress === void 0 ? void 0 : serverAddress.port)) || | ||
localPort: (typeof serverAddress !== 'string' && serverAddress?.port) || | ||
this.rawConfig.localPort, | ||
@@ -75,3 +77,3 @@ }; | ||
const { localPort, localAddr } = this.rawConfig; | ||
debug('starting to listen', { localAddr, localPort }); | ||
log.info(mongoLogId(1001000255), this.logCtx, 'Listening for Socks5 connections', { localAddr, localPort }); | ||
await this.serverListen(localPort, localAddr); | ||
@@ -81,3 +83,3 @@ await this.connectSsh(); | ||
async close() { | ||
debug('closing SSH tunnel'); | ||
log.info(mongoLogId(1001000256), this.logCtx, 'Closing SSH tunnel'); | ||
const [maybeError] = await Promise.all([ | ||
@@ -104,3 +106,3 @@ this.serverClose().catch((e) => e), | ||
} | ||
debug('creating SSH connection'); | ||
log.info(mongoLogId(1001000257), this.logCtx, 'Establishing new SSH connection'); | ||
this.connectingPromise = Promise.race([ | ||
@@ -120,3 +122,4 @@ (0, events_1.once)(this.sshClient, 'error').then(([err]) => { | ||
catch (err) { | ||
debug('failed to establish SSH connection', err); | ||
this.emit('forwardingError', err); | ||
log.error(mongoLogId(1001000258), this.logCtx, 'Failed to establish new SSH connection', { error: err?.stack ?? String(err) }); | ||
delete this.connectingPromise; | ||
@@ -128,3 +131,3 @@ await this.serverClose(); | ||
this.connected = true; | ||
debug('created SSH connection'); | ||
log.info(mongoLogId(1001000259), this.logCtx, 'Finished establishing new SSH connection'); | ||
} | ||
@@ -150,3 +153,7 @@ async closeSshClient() { | ||
async socks5Request(info, accept, deny) { | ||
debug('receiving socks5 forwarding request', info); | ||
const { srcAddr, srcPort, dstAddr, dstPort } = info; | ||
const logMetadata = { srcAddr, srcPort, dstAddr, dstPort }; | ||
log.info(mongoLogId(1001000260), this.logCtx, 'Received Socks5 fowarding request', { | ||
...logMetadata, | ||
}); | ||
let socket = null; | ||
@@ -157,3 +164,3 @@ try { | ||
try { | ||
channel = await this.forwardOut(info.srcAddr, info.srcPort, info.dstAddr, info.dstPort); | ||
channel = await this.forwardOut(srcAddr, srcPort, dstAddr, dstPort); | ||
} | ||
@@ -163,5 +170,8 @@ catch (err) { | ||
this.connected = false; | ||
debug('error forwarding. retrying..', info, err); | ||
log.error(mongoLogId(1001000261), this.logCtx, 'Error forwarding Socks5 request, retrying', { | ||
...logMetadata, | ||
error: err.stack, | ||
}); | ||
await this.connectSsh(); | ||
channel = await this.forwardOut(info.srcAddr, info.srcPort, info.dstAddr, info.dstPort); | ||
channel = await this.forwardOut(srcAddr, srcPort, dstAddr, dstPort); | ||
} | ||
@@ -172,13 +182,19 @@ else { | ||
} | ||
debug('channel opened, accepting socks5 request', info); | ||
log.info(mongoLogId(1001000262), this.logCtx, 'Opened SSH channel and accepting socks5 request', { | ||
...logMetadata, | ||
}); | ||
socket = accept(true); | ||
this.connections.add(socket); | ||
socket.on('error', (err) => { | ||
var _a; | ||
debug('error on socksv5 socket', info, err); | ||
err.origin = (_a = err.origin) !== null && _a !== void 0 ? _a : 'connection'; | ||
this.server.emit('error', err); | ||
log.error(mongoLogId(1001000263), this.logCtx, 'Error on Socks5 stream socket', { | ||
...logMetadata, | ||
error: err.stack, | ||
}); | ||
err.origin = err.origin ?? 'connection'; | ||
this.emit('forwardingError', err); | ||
}); | ||
socket.once('close', () => { | ||
debug('socksv5 socket closed, removing from set'); | ||
log.info(mongoLogId(1001000264), this.logCtx, 'Socks5 stream socket closed', { | ||
...logMetadata, | ||
}); | ||
this.connections.delete(socket); | ||
@@ -189,3 +205,7 @@ }); | ||
catch (err) { | ||
debug('caught error, rejecting socks5 request', info, err); | ||
this.emit('forwardingError', err); | ||
log.error(mongoLogId(1001000265), this.logCtx, 'Error establishing SSH channel for Socks5 request', { | ||
...logMetadata, | ||
error: err.stack, | ||
}); | ||
deny(); | ||
@@ -192,0 +212,0 @@ if (socket) { |
@@ -16,3 +16,3 @@ { | ||
"homepage": "https://github.com/mongodb-js/compass", | ||
"version": "0.0.0-next-bb1a39e78c30cda5ef910174e54d2c9708186388", | ||
"version": "0.0.0-next-bc37cfc07b82d66ae76a8abc7104e13cc84c22eb", | ||
"repository": { | ||
@@ -43,23 +43,22 @@ "type": "git", | ||
"lint": "npm run eslint . && npm run prettier -- --check .", | ||
"depcheck": "depcheck", | ||
"depcheck": "compass-scripts check-peer-deps && depcheck", | ||
"check": "npm run lint && npm run depcheck", | ||
"check-ci": "npm run check", | ||
"test": "mocha", | ||
"test-cov": "nyc -x \"**/*.spec.*\" --reporter=lcov --reporter=text --reporter=html npm run test", | ||
"test-cov": "nyc --compact=false --produce-source-map=false -x \"**/*.spec.*\" --reporter=lcov --reporter=text --reporter=html npm run test", | ||
"test-watch": "npm run test -- --watch", | ||
"test-ci": "npm run test-cov", | ||
"reformat": "npm run prettier -- --write ." | ||
"reformat": "npm run eslint . -- --fix && npm run prettier -- --write ." | ||
}, | ||
"devDependencies": { | ||
"@mongodb-js/eslint-config-compass": "0.0.0-next-bb1a39e78c30cda5ef910174e54d2c9708186388", | ||
"@mongodb-js/mocha-config-compass": "0.0.0-next-bb1a39e78c30cda5ef910174e54d2c9708186388", | ||
"@mongodb-js/prettier-config-compass": "0.0.0-next-bb1a39e78c30cda5ef910174e54d2c9708186388", | ||
"@mongodb-js/tsconfig-compass": "0.0.0-next-bb1a39e78c30cda5ef910174e54d2c9708186388", | ||
"@mongodb-js/eslint-config-compass": "0.0.0-next-bc37cfc07b82d66ae76a8abc7104e13cc84c22eb", | ||
"@mongodb-js/mocha-config-compass": "0.0.0-next-bc37cfc07b82d66ae76a8abc7104e13cc84c22eb", | ||
"@mongodb-js/prettier-config-compass": "0.0.0-next-bc37cfc07b82d66ae76a8abc7104e13cc84c22eb", | ||
"@mongodb-js/tsconfig-compass": "0.0.0-next-bc37cfc07b82d66ae76a8abc7104e13cc84c22eb", | ||
"@types/chai": "^4.2.21", | ||
"@types/chai-as-promised": "^7.1.4", | ||
"@types/debug": "^4.1.7", | ||
"@types/mocha": "^9.0.0", | ||
"@types/node-fetch": "^2.5.8", | ||
"@types/node-fetch": "^2.6.11", | ||
"@types/sinon-chai": "^3.2.5", | ||
"@types/ssh2": "^0.5.46", | ||
"@types/ssh2": "^1.11.8", | ||
"chai": "^4.3.4", | ||
@@ -70,16 +69,16 @@ "chai-as-promised": "^7.1.1", | ||
"gen-esm-wrapper": "^1.1.0", | ||
"mocha": "^8.4.0", | ||
"node-fetch": "^2.6.1", | ||
"mocha": "^10.2.0", | ||
"node-fetch": "^2.7.0", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.7.1", | ||
"sinon": "^9.2.3", | ||
"socks": "^2.6.1", | ||
"typescript": "^4.8.3" | ||
"socks": "^2.7.3", | ||
"typescript": "^5.0.4" | ||
}, | ||
"dependencies": { | ||
"debug": "^4.2.0", | ||
"@mongodb-js/compass-logging": "0.0.0-next-bc37cfc07b82d66ae76a8abc7104e13cc84c22eb", | ||
"socksv5": "0.0.6", | ||
"ssh2": "^0.8.9" | ||
"ssh2": "^1.12.0" | ||
}, | ||
"gitHead": "bb1a39e78c30cda5ef910174e54d2c9708186388" | ||
"gitHead": "bc37cfc07b82d66ae76a8abc7104e13cc84c22eb" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
50878
22
240
+ Added@mongodb-js/compass-logging@0.0.0-next-bc37cfc07b82d66ae76a8abc7104e13cc84c22eb
+ Added@babel/runtime@7.26.0(transitive)
+ Added@electron/get@2.0.3(transitive)
+ Added@mongodb-js/compass-logging@0.0.0-next-bc37cfc07b82d66ae76a8abc7104e13cc84c22eb(transitive)
+ Added@sindresorhus/is@4.6.0(transitive)
+ Added@szmarczak/http-timer@4.0.6(transitive)
+ Added@types/cacheable-request@6.0.3(transitive)
+ Added@types/hoist-non-react-statics@3.3.5(transitive)
+ Added@types/http-cache-semantics@4.0.4(transitive)
+ Added@types/keyv@3.1.4(transitive)
+ Added@types/node@20.17.6(transitive)
+ Added@types/prop-types@15.7.13(transitive)
+ Added@types/react@18.3.12(transitive)
+ Added@types/responselike@1.0.3(transitive)
+ Added@types/use-sync-external-store@0.0.3(transitive)
+ Added@types/yauzl@2.10.3(transitive)
+ Addedasap@2.0.6(transitive)
+ Addedboolean@3.2.0(transitive)
+ Addedbson@6.10.0(transitive)
+ Addedbuffer-crc32@0.2.13(transitive)
+ Addedbuildcheck@0.0.6(transitive)
+ Addedcacheable-lookup@5.0.4(transitive)
+ Addedcacheable-request@7.0.4(transitive)
+ Addedclone-response@1.0.3(transitive)
+ Addedcore-js@1.2.7(transitive)
+ Addedcpu-features@0.0.10(transitive)
+ Addedcreate-react-class@15.7.0(transitive)
+ Addedcsstype@3.1.3(transitive)
+ Addeddecompress-response@6.0.0(transitive)
+ Addeddefer-to-connect@2.0.1(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddefine-properties@1.2.1(transitive)
+ Addeddetect-node@2.1.0(transitive)
+ Addedelectron@29.4.6(transitive)
+ Addedencoding@0.1.13(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedenv-paths@2.2.1(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes6-error@4.1.1(transitive)
+ Addedescape-string-regexp@4.0.0(transitive)
+ Addedeventemitter3@1.2.04.0.7(transitive)
+ Addedextract-zip@2.0.1(transitive)
+ Addedfbjs@0.8.18(transitive)
+ Addedfd-slicer@1.1.0(transitive)
+ Addedfs-extra@8.1.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedget-stream@5.2.0(transitive)
+ Addedglobal-agent@3.0.0(transitive)
+ Addedglobalthis@1.0.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedgot@11.8.6(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhadron-app-registry@0.0.0-next-bc37cfc07b82d66ae76a8abc7104e13cc84c22eb(transitive)
+ Addedhadron-ipc@0.0.0-next-bc37cfc07b82d66ae76a8abc7104e13cc84c22eb(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedheap-js@2.5.0(transitive)
+ Addedhoist-non-react-statics@3.3.2(transitive)
+ Addedhttp-cache-semantics@4.1.1(transitive)
+ Addedhttp2-wrapper@1.0.3(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedis-electron-renderer@2.0.1(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedisomorphic-fetch@2.2.1(transitive)
+ Addedjs-tokens@4.0.0(transitive)
+ Addedjson-buffer@3.0.1(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjsonfile@4.0.0(transitive)
+ Addedkeyv@4.5.4(transitive)
+ Addedloose-envify@1.4.0(transitive)
+ Addedlowercase-keys@2.0.0(transitive)
+ Addedmatcher@3.0.0(transitive)
+ Addedmimic-response@1.0.13.1.0(transitive)
+ Addedmongodb-log-writer@1.4.2(transitive)
+ Addednan@2.22.0(transitive)
+ Addednode-fetch@1.7.3(transitive)
+ Addednormalize-url@6.1.0(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedp-cancelable@2.1.1(transitive)
+ Addedpend@1.2.0(transitive)
+ Addedprogress@2.0.3(transitive)
+ Addedpromise@7.3.1(transitive)
+ Addedprop-types@15.8.1(transitive)
+ Addedpump@3.0.2(transitive)
+ Addedquick-lru@5.1.1(transitive)
+ Addedreact@15.7.017.0.2(transitive)
+ Addedreact-is@16.13.118.3.1(transitive)
+ Addedreact-redux@8.1.3(transitive)
+ Addedredux@4.2.1(transitive)
+ Addedreflux@0.4.1(transitive)
+ Addedreflux-core@0.3.0(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)
+ Addedresolve-alpn@1.2.1(transitive)
+ Addedresponselike@2.0.1(transitive)
+ Addedroarr@2.15.4(transitive)
+ Addedsemver@6.3.17.6.3(transitive)
+ Addedsemver-compare@1.0.0(transitive)
+ Addedserialize-error@7.0.1(transitive)
+ Addedsetimmediate@1.0.5(transitive)
+ Addedsprintf-js@1.1.3(transitive)
+ Addedssh2@1.16.0(transitive)
+ Addedsumchecker@3.0.1(transitive)
+ Addedtype-fest@0.13.1(transitive)
+ Addedua-parser-js@0.7.39(transitive)
+ Addedundici-types@6.19.8(transitive)
+ Addeduniversalify@0.1.2(transitive)
+ Addeduse-sync-external-store@1.2.2(transitive)
+ Addedwhatwg-fetch@3.6.20(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedyauzl@2.10.0(transitive)
- Removeddebug@^4.2.0
- Removedssh2@0.8.9(transitive)
- Removedssh2-streams@0.4.10(transitive)
- Removedstreamsearch@0.1.2(transitive)
Updatedssh2@^1.12.0