Skip to content

Commit 3d0675c

Browse files
author
Chris Cho
authored
DOCSP-31694: SOCKS5 proxy support (mongodb#439)
* DOCSP-31694: SOCKS5 proxy support
1 parent f4e1889 commit 3d0675c

File tree

5 files changed

+163
-25
lines changed

5 files changed

+163
-25
lines changed

config/redirects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ raw: ${prefix}/master -> ${base}/upcoming/
99
[*-v4.6]: ${prefix}/${version}/fundamentals/crud/read-operations/change-streams/ -> ${base}/${version}/fundamentals/crud/read-operations/retrieve/
1010
[*-master]: ${prefix}/${version}/fundamentals/csfle/ -> ${base}/${version}/fundamentals/encrypt-fields/
1111
[*-master]: ${prefix}/${version}/fundamentals/crud/write-operations/change-a-document/ -> ${base}/${version}/fundamentals/crud/write-operations/modify/
12+
[*-v4.10]: ${prefix}/${version}/fundamentals/connection/socks/ -> ${base}/${version}/

source/fundamentals/connection.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Connection Guide
1111
/fundamentals/connection/mongoclientsettings
1212
/fundamentals/connection/network-compression
1313
/fundamentals/connection/tls
14+
/fundamentals/connection/socks
1415
/fundamentals/connection/jndi
1516
Connect to MongoDB Atlas from AWS Lambda <https://www.mongodb.com/docs/atlas/manage-connections-aws-lambda/>
1617

@@ -32,7 +33,8 @@ sections:
3233
- :ref:`Specify Connection Behavior with the MongoClient Class <specify-mongoclient-settings>`
3334
- :ref:`Enable Network Compression <network-compression>`
3435
- :ref:`Enable TLS/SSL on a Connection <tls-ssl>`
35-
- :ref:`Connect to MongoDB Using a JNDI Datasource <jndi>`
36+
- :ref:`Connect to MongoDB by Using a SOCKS5 Proxy <java-connect-socks>`
37+
- :ref:`Connect to MongoDB by Using a JNDI Datasource <jndi>`
3638
- :atlas:`Connect to MongoDB Atlas from AWS Lambda </manage-connections-aws-lambda/>`
3739

3840
For information about authenticating with a MongoDB instance,

source/fundamentals/connection/connection-options.txt

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,21 @@ parameters of the connection URI to specify the behavior of the client.
7272

7373
* - **ssl**
7474
- boolean
75-
- Specifies that all communication with MongoDB instances should
75+
- Specifies that all communication with MongoDB instances must
7676
use TLS/SSL. Superseded by the **tls** option.
7777

7878
| **Default**: ``false``
7979

8080
* - **tls**
8181
- boolean
82-
- Specifies that all communication with MongoDB instances should
82+
- Specifies that all communication with MongoDB instances must
8383
use TLS. Supersedes the **ssl** option.
8484

8585
| **Default**: ``false``
8686

8787
* - **tlsInsecure**
8888
- boolean
89-
- Specifies that the driver should allow invalid hostnames for TLS
89+
- Specifies that the driver must allow invalid hostnames for TLS
9090
connections. Has the same effect as setting
9191
**tlsAllowInvalidHostnames** to ``true``. To configure TLS security
9292
constraints in other ways, use a
@@ -96,7 +96,7 @@ parameters of the connection URI to specify the behavior of the client.
9696

9797
* - **tlsAllowInvalidHostnames**
9898
- boolean
99-
- Specifies that the driver should allow invalid hostnames in the
99+
- Specifies that the driver must allow invalid hostnames in the
100100
certificate for TLS connections. Supersedes
101101
**sslInvalidHostNameAllowed**.
102102

@@ -186,14 +186,14 @@ parameters of the connection URI to specify the behavior of the client.
186186
is greater. For more information, see the server documentation for the
187187
:manual:`maxStalenessSeconds option </reference/connection-string/#urioption.maxStalenessSeconds>`.
188188
Not providing a parameter or explicitly specifying ``-1`` indicates
189-
that there should be no staleness check for secondaries.
189+
that there must be no staleness check for secondaries.
190190

191191
| **Default**: ``-1``
192192

193193
* - **authMechanism**
194194
- string
195195
- Specifies the :doc:`authentication mechanism
196-
</fundamentals/auth>` that the driver should use if a credential
196+
</fundamentals/auth>` that the driver uses if a credential
197197
was supplied.
198198

199199
| **Default**: By default, the client picks the most secure
@@ -204,9 +204,8 @@ parameters of the connection URI to specify the behavior of the client.
204204

205205
* - **authSource**
206206
- string
207-
- Specifies the database that the supplied credentials should be
208-
validated against.
209-
207+
- Specifies the database in which the supplied credentials are validated.
208+
210209
| **Default**: ``admin``
211210

212211
* - **authMechanismProperties**
@@ -239,7 +238,7 @@ parameters of the connection URI to specify the behavior of the client.
239238
* - **zlibCompressionLevel**
240239
- integer
241240
- Specifies the degree of compression that `Zlib <https://zlib.net/>`__
242-
should use to decrease the size of requests to the connected MongoDB
241+
uses to decrease the size of requests to the connected MongoDB
243242
instance. The level can range from ``-1`` to ``9``, with lower values
244243
compressing faster (but resulting in larger requests) and larger values
245244
compressing slower (but resulting in smaller requests).
@@ -249,15 +248,15 @@ parameters of the connection URI to specify the behavior of the client.
249248
* - **retryWrites**
250249
- boolean
251250
- Specifies that the driver must retry supported write operations
252-
if they fail due to a network error.
251+
if they are unable to complete due to a network error.
253252

254253
| **Default**: ``true``
255254

256255

257256
* - **retryReads**
258257
- boolean
259258
- Specifies that the driver must retry supported read operations
260-
if they fail due to a network error.
259+
if they are unable to complete due to a network error.
261260

262261
| **Default**: ``true``
263262

@@ -280,23 +279,23 @@ parameters of the connection URI to specify the behavior of the client.
280279
- integer
281280
- Specifies the maximum number of connections a pool may be establishing
282281
concurrently.
283-
282+
284283
| **Default**: ``2``
285284

286285
* - **srvServiceName**
287286
- string
288287
- Specifies the service name of the
289288
`SRV resource records <https://www.rfc-editor.org/rfc/rfc2782>`__
290-
the driver retrieves to construct your
289+
the driver retrieves to construct your
291290
:manual:`seed list </reference/glossary/#std-term-seed-list>`.
292-
You must use the
291+
You must use the
293292
:manual:`DNS Seed List Connection Format </reference/connection-string/#dns-seed-list-connection-format>`
294293
in your
295294
:ref:`connection URI <connection-uri>`
296-
to use this option.
297-
295+
to use this option.
296+
298297
| **Default**: ``mongodb``
299298

300299
For a complete list of options, see the
301300
`ConnectionString <{+api+}/apidocs/mongodb-driver-core/com/mongodb/ConnectionString.html>`__
302-
API reference page.
301+
API documentation.

source/fundamentals/connection/jndi.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.. _jndi:
22

3-
==========================================
4-
Connect to MongoDB Using a JNDI Datasource
5-
==========================================
3+
=============================================
4+
Connect to MongoDB by Using a JNDI Datasource
5+
=============================================
66

77
.. contents:: On this page
88
:local:
@@ -15,7 +15,7 @@ Overview
1515
--------
1616

1717
In this guide, you can learn how to connect the MongoDB Java driver
18-
to your MongoDB instance using a Java Naming and Directory Interface
18+
to your MongoDB instance by using a Java Naming and Directory Interface
1919
(JNDI) Datasource.
2020

2121
MongoClientFactory includes a `JNDI
@@ -61,7 +61,7 @@ JNDI Datasource.
6161
Replace the placeholder connection value in the ``property`` tag
6262
with a value that points to your MongoDB installation.
6363

64-
This makes a MongoClient instance accessible via the JNDI name
64+
This makes a MongoClient instance discoverable through the JNDI name
6565
``MyMongoClient`` in the ``java:global`` context.
6666

6767
.. tab:: Tomcat
@@ -106,7 +106,7 @@ JNDI Datasource.
106106
</res-auth>
107107
</resource-ref>
108108

109-
This makes a ``MongoClient`` instance accessible via the JNDI name
109+
This makes a ``MongoClient`` instance discoverable through the JNDI name
110110
``mongodb/MyMongoClient`` in the ``java:comp/env`` context.
111111

112112
.. seealso::
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
.. _java-connect-socks:
2+
3+
==========================================
4+
Connect to MongoDB by Using a SOCKS5 Proxy
5+
==========================================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
Overview
14+
--------
15+
16+
In this guide, you can learn how to connect to MongoDB by using a SOCKS5 proxy.
17+
SOCKS5 is a standardized protocol for communicating with network services
18+
through a proxy server.
19+
20+
.. tip::
21+
22+
To learn more about the SOCKS5 protocol, see the Uncyclopedia entry on
23+
:wikipedia:`SOCKS <w/index.php?title=SOCKS&oldid=1160087146>`.
24+
25+
.. _socks-proxy-settings:
26+
27+
SOCKS5 Proxy Settings
28+
---------------------
29+
30+
The proxy settings specify the SOCKS5 proxy server address and your
31+
authentication credentials. You can specify your settings in an instance of
32+
``MongoClientSettings`` or in your connection string.
33+
34+
.. important::
35+
36+
The driver ignores the proxy settings if you specify a custom
37+
``StreamFactoryFactory``, which by default creates instances of
38+
``SocketStreamFactory``.
39+
40+
The following table describes the SOCKS5 client options:
41+
42+
.. list-table::
43+
:header-rows: 1
44+
:widths: 15 20 65
45+
46+
* - Name
47+
- Accepted Values
48+
- Description
49+
50+
* - **proxyHost**
51+
- String
52+
- Specifies the SOCKS5 proxy IPv4 address, IPv6 address, or hostname.
53+
You must provide this value to connect to a SOCKS5 proxy.
54+
55+
* - **proxyPort**
56+
- non-negative Integer
57+
- Specifies the TCP port number of the SOCKS5 proxy server. This option
58+
defaults to ``1080`` when you set ``proxyHost``.
59+
60+
* - **proxyUsername**
61+
- String
62+
- Specifies the username for authentication to the SOCKS5 proxy server.
63+
The driver ignores ``null`` and empty string values for this setting.
64+
The driver requires that you pass values for both ``proxyUsername``
65+
and ``proxyPassword`` or that you omit both values.
66+
67+
* - **proxyPassword**
68+
- String
69+
- Specifies the password for authentication to the SOCKS5 proxy server.
70+
The driver ignores ``null`` and empty string values for this setting.
71+
The driver requires that you pass values for both ``proxyUsername``
72+
and ``proxyPassword`` or that you omit both values.
73+
74+
75+
Examples
76+
--------
77+
78+
The following examples show how to instantiate a ``MongoClient`` that connects
79+
to MongoDB by using a SOCKS5 proxy. The proxy settings can be specified in a
80+
``MongoClientSettings`` instance or a connection string. These examples use
81+
the placeholder values described in the :ref:`socks-proxy-settings` section.
82+
Replace the placeholders with your proxy settings.
83+
84+
Specify Proxy Settings in the MongoClientSettings
85+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
86+
87+
The following code example shows how to specify your SOCKS5 proxy settings by
88+
using the ``MongoClientSettings`` builder:
89+
90+
.. code-block:: java
91+
:emphasize-lines: 5-12
92+
93+
MongoClient mongoClient = MongoClients.create(
94+
MongoClientSettings.builder()
95+
.applyConnectionString(
96+
new ConnectionString("mongodb+srv://myDatabaseUser:[email protected]/"))
97+
.applyToSocketSettings(builder ->
98+
builder.applyToProxySettings(proxyBuilder ->
99+
proxyBuilder
100+
.host("<proxyHost>")
101+
.port(<proxyPort>)
102+
.username("<proxyUsername>")
103+
.password("<proxyPassword>")
104+
)
105+
).build());
106+
107+
Specify Proxy Settings in the Connection String
108+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
109+
110+
The following code example shows how to specify your SOCKS5 proxy settings in
111+
your connection string:
112+
113+
.. code-block:: java
114+
:emphasize-lines: 2-5
115+
116+
String connectionString = "mongodb+srv://myDatabaseUser:[email protected]/" +
117+
"?proxyHost=<proxyHost>" +
118+
"&proxyPort=<proxyPort>" +
119+
"&proxyUsername=<proxyUsername>" +
120+
"&proxyPassword=<proxyPassword>";
121+
122+
MongoClient mongoClient = MongoClients.create(connectionString);
123+
124+
API Documentation
125+
~~~~~~~~~~~~~~~~~
126+
127+
To learn more about the methods and types discussed in this guide, see the
128+
following API documentation:
129+
130+
- `MongoClientSettings.Builder <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html>`__
131+
- `SocketSettings.Builder <{+api+}/apidocs/mongodb-driver-core/com/mongodb/connection/SocketSettings.Builder.html>`__
132+
- `MongoClients.create() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoClients.html#create(com.mongodb.MongoClientSettings)>`__
133+
134+
.. TODO
135+
- provide the link for the proxysettings builder once the API docs are generated:
136+
- `ProxySettings.Builder <{+api+}/apidocs/mongodb-driver-core/com/mongodb/connection/ProxySettings.Builder.html>`__

0 commit comments

Comments
 (0)