Skip to content

Commit d7867eb

Browse files
authored
feat: manage MongoDB SOCKS5 proxy settings (#11731)
1 parent 8692da2 commit d7867eb

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

‎src/driver/mongodb/MongoConnectionOptions.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,24 @@ export interface MongoConnectionOptions extends BaseDataSourceOptions {
303303
* Please use the {@link writeConcern} option instead
304304
*/
305305
readonly wtimeoutMS?: number
306+
307+
/**
308+
* Configures a Socks5 proxy host used for creating TCP connections.
309+
*/
310+
readonly proxyHost?: string
311+
312+
/**
313+
* Configures a Socks5 proxy port used for creating TCP connections.
314+
*/
315+
readonly proxyPort?: number
316+
317+
/**
318+
* Configures a Socks5 proxy username when the proxy in proxyHost requires username/password authentication.
319+
*/
320+
readonly proxyUsername?: string
321+
322+
/**
323+
* Configures a Socks5 proxy password when the proxy in proxyHost requires username/password authentication.
324+
*/
325+
readonly proxyPassword?: string
306326
}

‎src/driver/mongodb/MongoDriver.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ export class MongoDriver implements Driver {
203203
"w",
204204
"writeConcern",
205205
"wtimeoutMS",
206+
// Proxy configuration for Socks5
207+
"proxyHost",
208+
"proxyPort",
209+
"proxyUsername",
210+
"proxyPassword",
206211
// Undocumented deprecated options
207212
// todo: remove next major version
208213
"appname",

‎test/functional/driver/MongoDriver.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,42 @@ describe("MongoDriver", () => {
5858
expect(connectionUrl).to.eql(url)
5959
})
6060
})
61+
62+
describe("proxy options", () => {
63+
it("should pass proxy options to MongoClient.connect", async () => {
64+
const options = {
65+
type: "mongodb",
66+
host: "someHost",
67+
port: 27017,
68+
database: "myDatabase",
69+
proxyHost: "127.0.0.1",
70+
proxyPort: 1080,
71+
proxyUsername: "proxyUser",
72+
proxyPassword: "proxyPass",
73+
}
74+
75+
const driver = new MongoDriver({
76+
options,
77+
} as DataSource)
78+
79+
// replace MongoClient.connect with a fake that resolves so connect() completes
80+
const connect = sinon.fake.resolves({})
81+
driver.mongodb = {
82+
...driver.mongodb,
83+
MongoClient: {
84+
connect,
85+
},
86+
}
87+
88+
await driver.connect()
89+
90+
// the second argument passed to MongoClient.connect should contain our proxy settings
91+
const passedOptions = connect.args[0][1]
92+
93+
expect(passedOptions).to.have.property("proxyHost", "127.0.0.1")
94+
expect(passedOptions).to.have.property("proxyPort", 1080)
95+
expect(passedOptions).to.have.property("proxyUsername", "proxyUser")
96+
expect(passedOptions).to.have.property("proxyPassword", "proxyPass")
97+
})
98+
})
6199
})

0 commit comments

Comments
 (0)