-
Notifications
You must be signed in to change notification settings - Fork 40
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
DOCSP-31694: SOCKS5 proxy support #439
Merged
ccho-mongodb
merged 13 commits into
mongodb:master
from
ccho-mongodb:DOCSP-31694-socks5-proxy
Sep 25, 2023
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
579b162
DOCSP-31694: SOCKS5 proxy support
5b87624
add to ToC
ecd8f12
JNDI page title fix and vale fixes
8318bc1
split examples into separate sections
85f082d
remove default value column, add connection string placeholders
5201d05
fix placeholder
0c5fc6c
add redirect and API docs links
efdf2ab
fix api docs links
924b699
PRR fixes
5bf94ff
additional edits
3889dbe
PRR fixes
33845ea
SocketStreamFactory callout
3f97f98
vale fixes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
.. _java-connect-socks: | ||
|
||
========================================== | ||
Connect to MongoDB by Using a SOCKS5 Proxy | ||
========================================== | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn how to connect to MongoDB by using a SOCKS5 proxy. | ||
SOCKS5 is a standardized protocol for communicating with network services | ||
through a proxy server. | ||
|
||
.. tip:: | ||
|
||
To learn more about the SOCKS5 protocol, see the Wikipedia entry on | ||
:wikipedia:`SOCKS <w/index.php?title=SOCKS&oldid=1160087146>`. | ||
|
||
.. _socks-proxy-settings: | ||
|
||
SOCKS5 Proxy Settings | ||
--------------------- | ||
|
||
The proxy settings specify the SOCKS5 proxy server address and your | ||
authentication credentials. You can specify your settings in an instance of | ||
``MongoClientSettings`` or in your connection string. | ||
|
||
.. important:: | ||
|
||
The driver ignores the proxy settings if you specify a custom | ||
``StreamFactoryFactory``, which by default creates instances of | ||
``SocketStreamFactory``. | ||
|
||
The following table describes the SOCKS5 client options: | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:widths: 15 20 65 | ||
|
||
* - Name | ||
- Accepted Values | ||
- Description | ||
|
||
* - **proxyHost** | ||
- String | ||
- Specifies the SOCKS5 proxy IPv4 address, IPv6 address, or hostname. | ||
You must provide this value to connect to a SOCKS5 proxy. | ||
|
||
* - **proxyPort** | ||
- non-negative Integer | ||
- Specifies the TCP port number of the SOCKS5 proxy server. This option | ||
defaults to ``1080`` when you set ``proxyHost``. | ||
|
||
* - **proxyUsername** | ||
- String | ||
- Specifies the username for authentication to the SOCKS5 proxy server. | ||
The driver ignores ``null`` and empty string values for this setting. | ||
The driver requires that you pass values for both ``proxyUsername`` | ||
and ``proxyPassword`` or that you omit both values. | ||
|
||
* - **proxyPassword** | ||
- String | ||
- Specifies the password for authentication to the SOCKS5 proxy server. | ||
The driver ignores ``null`` and empty string values for this setting. | ||
The driver requires that you pass values for both ``proxyUsername`` | ||
and ``proxyPassword`` or that you omit both values. | ||
|
||
|
||
Examples | ||
-------- | ||
|
||
The following examples show how to instantiate a ``MongoClient`` that connects | ||
to MongoDB by using a SOCKS5 proxy. The proxy settings can be specified in a | ||
``MongoClientSettings`` instance or a connection string. These examples use | ||
the placeholder values described in the :ref:`socks-proxy-settings` section. | ||
Replace the placeholders with your proxy settings. | ||
|
||
Specify Proxy Settings in the MongoClientSettings | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The following code example shows how to specify your SOCKS5 proxy settings by | ||
using the ``MongoClientSettings`` builder: | ||
|
||
.. code-block:: java | ||
:emphasize-lines: 5-12 | ||
|
||
MongoClient mongoClient = MongoClients.create( | ||
MongoClientSettings.builder() | ||
.applyConnectionString( | ||
new ConnectionString("mongodb+srv://myDatabaseUser:[email protected]/")) | ||
.applyToSocketSettings(builder -> | ||
builder.applyToProxySettings(proxyBuilder -> | ||
proxyBuilder | ||
.host("<proxyHost>") | ||
.port(<proxyPort>) | ||
.username("<proxyUsername>") | ||
.password("<proxyPassword>") | ||
) | ||
).build()); | ||
|
||
Specify Proxy Settings in the Connection String | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The following code example shows how to specify your SOCKS5 proxy settings in | ||
your connection string: | ||
|
||
.. code-block:: java | ||
:emphasize-lines: 2-5 | ||
|
||
String connectionString = "mongodb+srv://myDatabaseUser:[email protected]/" + | ||
"?proxyHost=<proxyHost>" + | ||
"&proxyPort=<proxyPort>" + | ||
"&proxyUsername=<proxyUsername>" + | ||
"&proxyPassword=<proxyPassword>"; | ||
|
||
MongoClient mongoClient = MongoClients.create(connectionString); | ||
|
||
API Documentation | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
To learn more about the methods and types discussed in this guide, see the | ||
following API documentation: | ||
|
||
- `MongoClientSettings.Builder <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html>`__ | ||
- `SocketSettings.Builder <{+api+}/apidocs/mongodb-driver-core/com/mongodb/connection/SocketSettings.Builder.html>`__ | ||
- `MongoClients.create() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoClients.html#create(com.mongodb.MongoClientSettings)>`__ | ||
|
||
.. TODO | ||
- provide the link for the proxysettings builder once the API docs are generated: | ||
- `ProxySettings.Builder <{+api+}/apidocs/mongodb-driver-core/com/mongodb/connection/ProxySettings.Builder.html>`__ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it might be beneficial to mention that
proxyUsername
andproxyPassword
should always be specified together, as the driver requires both or none.