MCPcopy
hub / github.com/mongodb/node-mongodb-native / makeSocks5Connection

Function makeSocks5Connection

src/cmap/connect.ts:488–538  ·  view source on GitHub ↗
(options: MakeConnectionOptions)

Source from the content-addressed store, hash-verified

486}
487
488async function makeSocks5Connection(options: MakeConnectionOptions): Promise<Stream> {
489 const hostAddress = HostAddress.fromHostPort(
490 options.proxyHost ?? '', // proxyHost is guaranteed to set here
491 options.proxyPort ?? 1080
492 );
493
494 // First, connect to the proxy server itself:
495 const rawSocket = await makeSocket({
496 ...options,
497 hostAddress,
498 tls: false,
499 proxyHost: undefined
500 });
501
502 const destination = parseConnectOptions(options) as net.TcpNetConnectOpts;
503 if (typeof destination.host !== 'string' || typeof destination.port !== 'number') {
504 throw new MongoInvalidArgumentError('Can only make Socks5 connections to TCP hosts');
505 }
506
507 socks ??= loadSocks();
508
509 let existingSocket: Stream;
510
511 try {
512 // Then, establish the Socks5 proxy connection:
513 const connection = await socks.SocksClient.createConnection({
514 existing_socket: rawSocket,
515 timeout: options.connectTimeoutMS,
516 command: 'connect',
517 destination: {
518 host: destination.host,
519 port: destination.port
520 },
521 proxy: {
522 // host and port are ignored because we pass existing_socket
523 host: 'iLoveJavaScript',
524 port: 0,
525 type: 5,
526 userId: options.proxyUsername || undefined,
527 password: options.proxyPassword || undefined
528 }
529 });
530 existingSocket = connection.socket;
531 } catch (cause) {
532 throw new MongoNetworkError(MongoError.buildErrorMessage(cause), { cause });
533 }
534
535 // Finally, now treat the resulting duplex stream as the
536 // socket over which we send and receive wire protocol messages:
537 return await makeSocket({ ...options, existingSocket, proxyHost: undefined });
538}

Callers 1

makeSocketFunction · 0.85

Calls 6

makeSocketFunction · 0.85
parseConnectOptionsFunction · 0.85
fromHostPortMethod · 0.80
createConnectionMethod · 0.80
buildErrorMessageMethod · 0.80
loadSocksFunction · 0.70

Tested by

no test coverage detected