Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-5409)!: allow socks to be installed optionally #3782

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
},
"dependencies": {
"bson": "^5.4.0",
"mongodb-connection-string-url": "^2.6.0",
"socks": "^2.7.1"
"mongodb-connection-string-url": "^2.6.0"
},
"optionalDependencies": {
"saslprep": "^1.0.3"
Expand All @@ -38,7 +37,8 @@
"gcp-metadata": "^5.2.0",
"kerberos": "^2.0.1",
"mongodb-client-encryption": ">=6.0.0-alpha.0 <7",
"snappy": "^7.2.2"
"snappy": "^7.2.2",
"socks": "^2.7.1"
},
"peerDependenciesMeta": {
"@aws-sdk/credential-providers": {
Expand All @@ -58,6 +58,9 @@
},
"gcp-metadata": {
"optional": true
},
"socks": {
"optional": true
}
},
"devDependencies": {
Expand Down Expand Up @@ -102,6 +105,7 @@
"sinon": "^15.0.4",
"sinon-chai": "^3.7.0",
"snappy": "^7.2.2",
"socks": "^2.7.1",
"source-map-support": "^0.5.21",
"ts-node": "^10.9.1",
"tsd": "^0.28.1",
Expand Down
18 changes: 16 additions & 2 deletions src/client-side-encryption/stateMachine.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@ import * as tls from 'tls';
import * as net from 'net';
import * as fs from 'fs';
import { once } from 'events';
import { SocksClient } from 'socks';
import { MongoNetworkTimeoutError } from '../error';
import { debug, databaseNamespace, collectionNamespace } from './common';
import { MongoCryptError } from './errors';
import { BufferPool } from './buffer_pool';
import { serialize, deserialize } from '../bson';
import { getSocks } from '../deps';

/** @type {import('../deps').SocksLib | null} */
let socks = null;
function loadSocks() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary? makeModuleError returns a proxy that throws whenever a key that isn't kModuleError is accessed. So I'd expect we would just do the following (where it's used):

const socks = loadSocks();
await socks.SocksClient(...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm alinging with the approach taken for kerberos, zstd, snappy. The goal is to not invoke the import operation more than once, determined by the nullishness of the socks variable.

if (socks == null) {
const socksImport = getSocks();
if ('kModuleError' in socksImport) {
throw socksImport.kModuleError;
}
socks = socksImport;
}
return socks;
}

// libmongocrypt states
const MONGOCRYPT_CTX_ERROR = 0;
Expand Down Expand Up @@ -289,8 +302,9 @@ class StateMachine {
rawSocket.on('error', onerror);
try {
await once(rawSocket, 'connect');
socks ??= loadSocks();
options.socket = (
await SocksClient.createConnection({
await socks.SocksClient.createConnection({
existing_socket: rawSocket,
command: 'connect',
destination: { host: options.host, port: options.port },
Expand Down
24 changes: 21 additions & 3 deletions src/cmap/connect.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Socket, SocketConnectOpts } from 'net';
import * as net from 'net';
import { SocksClient } from 'socks';
import type { ConnectionOptions as TLSConnectionOpts, TLSSocket } from 'tls';
import * as tls from 'tls';

import type { Document } from '../bson';
import { LEGACY_HELLO_COMMAND } from '../constants';
import { getSocks, type SocksLib } from '../deps';
import {
MongoCompatibilityError,
MongoError,
Expand Down Expand Up @@ -419,6 +419,18 @@ function makeConnection(options: MakeConnectionOptions, _callback: Callback<Stre
}
}

let socks: SocksLib | null = null;
function loadSocks() {
if (socks == null) {
const socksImport = getSocks();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as in stateMachine.js.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto response

if ('kModuleError' in socksImport) {
throw socksImport.kModuleError;
}
socks = socksImport;
}
return socks;
}

function makeSocks5Connection(options: MakeConnectionOptions, callback: Callback<Stream>) {
const hostAddress = HostAddress.fromHostPort(
options.proxyHost ?? '', // proxyHost is guaranteed to set here
Expand All @@ -434,7 +446,7 @@ function makeSocks5Connection(options: MakeConnectionOptions, callback: Callback
proxyHost: undefined
},
(err, rawSocket) => {
if (err) {
if (err || !rawSocket) {
return callback(err);
}

Expand All @@ -445,8 +457,14 @@ function makeSocks5Connection(options: MakeConnectionOptions, callback: Callback
);
}

try {
socks ??= loadSocks();
} catch (error) {
return callback(error);
}

// Then, establish the Socks5 proxy connection:
SocksClient.createConnection({
socks.SocksClient.createConnection({
existing_socket: rawSocket,
timeout: options.connectTimeoutMS,
command: 'connect',
Expand Down
35 changes: 35 additions & 0 deletions src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import type { Document } from './bson';
import { type Stream } from './cmap/connect';
import type { ProxyOptions } from './cmap/connection';
import { MongoMissingDependencyError } from './error';
import type { MongoClient } from './mongo_client';
Expand Down Expand Up @@ -157,6 +158,40 @@ export function getSnappy(): SnappyLib | { kModuleError: MongoMissingDependencyE
}
}

export type SocksLib = {
SocksClient: {
createConnection(options: {
command: 'connect';
destination: { host: string; port: number };
proxy: {
/** host and port are ignored because we pass existing_socket */
host: 'iLoveJavaScript';
port: 0;
type: 5;
userId?: string;
password?: string;
};
timeout?: number;
/** We always create our own socket, and pass it to this API for proxy negotiation */
existing_socket: Stream;
}): Promise<{ socket: Stream }>;
};
};

export function getSocks(): SocksLib | { kModuleError: MongoMissingDependencyError } {
try {
// Ensure you always wrap an optional require in the try block NODE-3199
const value = require('socks');
return value;
} catch (cause) {
const kModuleError = new MongoMissingDependencyError(
'Optional module `socks` not found. Please install it to connections over a SOCKS5 proxy',
{ cause }
);
return { kModuleError };
}
}

export let saslprep: typeof import('saslprep') | { kModuleError: MongoMissingDependencyError } =
makeErrorModule(
new MongoMissingDependencyError(
Expand Down
10 changes: 4 additions & 6 deletions test/action/dependency.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { expect } from 'chai';
import { dependencies, peerDependencies, peerDependenciesMeta } from '../../package.json';
import { itInNodeProcess } from '../tools/utils';

const EXPECTED_DEPENDENCIES = ['bson', 'mongodb-connection-string-url', 'socks'];
const EXPECTED_DEPENDENCIES = ['bson', 'mongodb-connection-string-url'];
const EXPECTED_PEER_DEPENDENCIES = [
'@aws-sdk/credential-providers',
'@mongodb-js/zstd',
'kerberos',
'snappy',
'mongodb-client-encryption',
'gcp-metadata'
'gcp-metadata',
'socks'
];

describe('package.json', function () {
Expand Down Expand Up @@ -119,10 +120,7 @@ describe('package.json', function () {
'mongodb-connection-string-url',
'whatwg-url',
'webidl-conversions',
'tr46',
'socks',
'ip',
'smart-buffer'
'tr46'
];

describe('mongodb imports', () => {
Expand Down