Skip to content

Commit 96f4cbb

Browse files
author
EspressoTrip-v2
committed
mongo patch
1 parent 425e49d commit 96f4cbb

5 files changed

Lines changed: 79 additions & 1 deletion

File tree

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,37 @@ but adapted to expose raw Sockets, instead of just http/https requests.
6363
}
6464

6565
run().catch(console.error);
66+
67+
68+
## Usage - MongoDB
69+
70+
import * as mongo from 'mongodb';
71+
import { useProxyForMongo } from '@journeyapps/https-proxy-socket';
72+
73+
const SRV_URI = 'mongodb+srv://<username>:<password>@cluster0.jzuewet.mongodb.net';
74+
const PROXY = 'us-cc-proxy.journeyapps.com'; // Or za-cc-proxy.journeyapps.com
75+
const PROXY_PORT = 443
76+
77+
// Register the proxy globally for MongoDB
78+
useProxyForMongo({
79+
proxy: PROXY,
80+
auth: <egress_token>
81+
});
82+
83+
async function run() {
84+
const client = new mongo.MongoClient(SRV_URI, {
85+
proxyPort: PROXY_PORT,
86+
proxyHost: PROXY,
87+
});
88+
try {
89+
const database = client.db('poc');
90+
const data = database.collection('data');
91+
92+
const results = await data.find({ index: { $lt: 5 } }).toArray();
93+
console.log(results);
94+
} finally {
95+
await client.close();
96+
}
97+
}
98+
99+
run().catch(console.error);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
},
2828
"main": "./lib/index.js",
2929
"dependencies": {
30-
"agent-base": "^6.0.0"
30+
"agent-base": "^6.0.0",
31+
"socks": "^2.8.7"
3132
},
3233
"files": [
3334
"/README.md",

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './HttpsProxySocket';
22
export * from './tediousPatch';
3+
export * from './mongoPatch';

src/mongoPatch.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as socks from 'socks';
2+
import {HttpsProxySocket} from './HttpsProxySocket';
3+
4+
interface Config {
5+
/** The journey apps cc egress token */
6+
auth: string;
7+
/** The journey apps cc egress proxy domain */
8+
proxy: string;
9+
}
10+
/**
11+
* The patch should be called before instantiating the MongoClient
12+
* @param config - The configuration for the proxy
13+
*/
14+
export function useProxyForMongo(config: Config) {
15+
socks.SocksClient.createConnection = async (options, callback) => {
16+
const proxy = new HttpsProxySocket(`https://${config.proxy}`, { auth: config.auth });
17+
return new Promise(async (resolve, reject) => {
18+
const socket = await proxy.connect({ host: options.destination.host, port: options.destination.port });
19+
resolve({
20+
socket,
21+
});
22+
});
23+
};
24+
}

yarn.lock

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,11 @@ inquirer@^7.0.0:
758758
strip-ansi "^5.1.0"
759759
through "^2.3.6"
760760

761+
ip-address@^10.0.1:
762+
version "10.0.1"
763+
resolved "https://registry.yarnpkg.com/ip-address/-/ip-address-10.0.1.tgz#a8180b783ce7788777d796286d61bce4276818ed"
764+
integrity sha512-NWv9YLW4PoW2B7xtzaS3NCot75m6nK7Icdv0o3lfMceJVRfSoQwqD4wEH5rLwoKJwUiZ/rfpiVBhnaF0FK4HoA==
765+
761766
is-buffer@~2.0.3:
762767
version "2.0.4"
763768
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-2.0.4.tgz#3e572f23c8411a5cfd9557c849e3665e0b290623"
@@ -1264,6 +1269,19 @@ signal-exit@^3.0.2:
12641269
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"
12651270
integrity sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0=
12661271

1272+
smart-buffer@^4.2.0:
1273+
version "4.2.0"
1274+
resolved "https://registry.yarnpkg.com/smart-buffer/-/smart-buffer-4.2.0.tgz#6e1d71fa4f18c05f7d0ff216dd16a481d0e8d9ae"
1275+
integrity sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg==
1276+
1277+
socks@^2.8.7:
1278+
version "2.8.7"
1279+
resolved "https://registry.yarnpkg.com/socks/-/socks-2.8.7.tgz#e2fb1d9a603add75050a2067db8c381a0b5669ea"
1280+
integrity sha512-HLpt+uLy/pxB+bum/9DzAgiKS8CX1EvbWxI4zlmgGCExImLdiad2iCwXT5Z4c9c3Eq8rP2318mPW2c+QbtjK8A==
1281+
dependencies:
1282+
ip-address "^10.0.1"
1283+
smart-buffer "^4.2.0"
1284+
12671285
source-map-support@^0.5.6:
12681286
version "0.5.16"
12691287
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.16.tgz#0ae069e7fe3ba7538c64c98515e35339eac5a042"

0 commit comments

Comments
 (0)