Skip to content

CSHARP-734: SOCKS5 Proxy Support #1731

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

Draft
wants to merge 30 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a09ba4f
CSHARP-734: SOCKS5 Proxy Support
papafe Jun 24, 2025
6696184
Small fix
papafe Jun 24, 2025
b3020b5
Added options to MongoClient settings
papafe Jun 24, 2025
7931f70
Added uri options test for proxy options
papafe Jun 24, 2025
186df67
Added additional checks on parsing.
papafe Jun 24, 2025
567bfa7
Small fix
papafe Jul 16, 2025
feaa948
Added base implementation
papafe Jul 17, 2025
f8bd6a3
Improvements plus initial tests
papafe Jul 17, 2025
3665a29
Various corrections
papafe Jul 18, 2025
3845931
Improvements
papafe Jul 18, 2025
83cace4
Various fixes
papafe Jul 18, 2025
fad7d42
Simplification
papafe Jul 18, 2025
2ca4b3f
Added comments
papafe Jul 21, 2025
a715fef
First version of setting class
papafe Jul 22, 2025
e710b13
Various fixes
papafe Jul 23, 2025
cbea41d
Small improvements
papafe Jul 23, 2025
d49fff5
Added async version
papafe Jul 23, 2025
74ade29
Simplifications
papafe Jul 23, 2025
b9ceea0
Removed unused
papafe Jul 23, 2025
d2cc5f9
Removed flush and simplified
papafe Jul 23, 2025
fda35a1
Simplification
papafe Jul 23, 2025
a76dc1d
Fixed auth
papafe Jul 23, 2025
8e7570f
Removed unused parameters
papafe Jul 23, 2025
9d23395
Fixed whitespace
papafe Jul 23, 2025
3cfe101
Added use of async
papafe Jul 23, 2025
896292d
Added comments and removed unused methods
papafe Jul 24, 2025
3d1d2bd
File scoped namespaces
papafe Jul 24, 2025
01dfdb1
Added connection string tests
papafe Jul 24, 2025
6064af9
Added MongoClientSettings test
papafe Jul 24, 2025
fb50e10
Removed unused using.
papafe Jul 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions specifications/uri-options/tests/proxy-options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{
"tests": [
{
"description": "proxyPort without proxyHost",
"uri": "mongodb://localhost/?proxyPort=1080",
"valid": false,
"warning": false,
"hosts": null,
"auth": null,
"options": null
},
{
"description": "proxyUsername without proxyHost",
"uri": "mongodb://localhost/?proxyUsername=abc",
"valid": false,
"warning": false,
"hosts": null,
"auth": null,
"options": null
},
{
"description": "proxyPassword without proxyHost",
"uri": "mongodb://localhost/?proxyPassword=def",
"valid": false,
"warning": false,
"hosts": null,
"auth": null,
"options": null
},
{
"description": "all other proxy options without proxyHost",
"uri": "mongodb://localhost/?proxyPort=1080&proxyUsername=abc&proxyPassword=def",
"valid": false,
"warning": false,
"hosts": null,
"auth": null,
"options": null
},
{
"description": "proxyUsername without proxyPassword",
"uri": "mongodb://localhost/?proxyHost=localhost&proxyUsername=abc",
"valid": false,
"warning": false,
"hosts": null,
"auth": null,
"options": null
},
{
"description": "proxyPassword without proxyUsername",
"uri": "mongodb://localhost/?proxyHost=localhost&proxyPassword=def",
"valid": false,
"warning": false,
"hosts": null,
"auth": null,
"options": null
},
{
"description": "multiple proxyHost parameters",
"uri": "mongodb://localhost/?proxyHost=localhost&proxyHost=localhost2",
"valid": false,
"warning": false,
"hosts": null,
"auth": null,
"options": null
},
{
"description": "multiple proxyPort parameters",
"uri": "mongodb://localhost/?proxyHost=localhost&proxyPort=1234&proxyPort=12345",
"valid": false,
"warning": false,
"hosts": null,
"auth": null,
"options": null
},
{
"description": "multiple proxyUsername parameters",
"uri": "mongodb://localhost/?proxyHost=localhost&proxyUsername=abc&proxyUsername=def&proxyPassword=123",
"valid": false,
"warning": false,
"hosts": null,
"auth": null,
"options": null
},
{
"description": "multiple proxyPassword parameters",
"uri": "mongodb://localhost/?proxyHost=localhost&proxyUsername=abc&proxyPassword=123&proxyPassword=456",
"valid": false,
"warning": false,
"hosts": null,
"auth": null,
"options": null
},
{
"description": "only host present",
"uri": "mongodb://localhost/?proxyHost=localhost",
"valid": true,
"warning": false,
"hosts": null,
"auth": null,
"options": {}
},
{
"description": "host and default port present",
"uri": "mongodb://localhost/?proxyHost=localhost&proxyPort=1080",
"valid": true,
"warning": false,
"hosts": null,
"auth": null,
"options": {}
},
{
"description": "host and non-default port present",
"uri": "mongodb://localhost/?proxyHost=localhost&proxyPort=12345",
"valid": true,
"warning": false,
"hosts": null,
"auth": null,
"options": {}
},
{
"description": "replicaset, host and non-default port present",
"uri": "mongodb://rs1,rs2,rs3/?proxyHost=localhost&proxyPort=12345",
"valid": true,
"warning": false,
"hosts": null,
"auth": null,
"options": {}
},
{
"description": "all options present",
"uri": "mongodb://rs1,rs2,rs3/?proxyHost=localhost&proxyPort=12345&proxyUsername=asdf&proxyPassword=qwerty",
"valid": true,
"warning": false,
"hosts": null,
"auth": null,
"options": {}
}
]
}
121 changes: 121 additions & 0 deletions specifications/uri-options/tests/proxy-options.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
tests:
-
description: "proxyPort without proxyHost"
uri: "mongodb://localhost/?proxyPort=1080"
valid: false
warning: false
hosts: ~
auth: ~
options: ~
-
description: "proxyUsername without proxyHost"
uri: "mongodb://localhost/?proxyUsername=abc"
valid: false
warning: false
hosts: ~
auth: ~
options: ~
-
description: "proxyPassword without proxyHost"
uri: "mongodb://localhost/?proxyPassword=def"
valid: false
warning: false
hosts: ~
auth: ~
options: ~
-
description: "all other proxy options without proxyHost"
uri: "mongodb://localhost/?proxyPort=1080&proxyUsername=abc&proxyPassword=def"
valid: false
warning: false
hosts: ~
auth: ~
options: ~
-
description: "proxyUsername without proxyPassword"
uri: "mongodb://localhost/?proxyHost=localhost&proxyUsername=abc"
valid: false
warning: false
hosts: ~
auth: ~
options: ~
-
description: "proxyPassword without proxyUsername"
uri: "mongodb://localhost/?proxyHost=localhost&proxyPassword=def"
valid: false
warning: false
hosts: ~
auth: ~
options: ~
-
description: "multiple proxyHost parameters"
uri: "mongodb://localhost/?proxyHost=localhost&proxyHost=localhost2"
valid: false
warning: false
hosts: ~
auth: ~
options: ~
-
description: "multiple proxyPort parameters"
uri: "mongodb://localhost/?proxyHost=localhost&proxyPort=1234&proxyPort=12345"
valid: false
warning: false
hosts: ~
auth: ~
options: ~
-
description: "multiple proxyUsername parameters"
uri: "mongodb://localhost/?proxyHost=localhost&proxyUsername=abc&proxyUsername=def&proxyPassword=123"
valid: false
warning: false
hosts: ~
auth: ~
options: ~
-
description: "multiple proxyPassword parameters"
uri: "mongodb://localhost/?proxyHost=localhost&proxyUsername=abc&proxyPassword=123&proxyPassword=456"
valid: false
warning: false
hosts: ~
auth: ~
options: ~
-
description: "only host present"
uri: "mongodb://localhost/?proxyHost=localhost"
valid: true
warning: false
hosts: ~
auth: ~
options: {}
-
description: "host and default port present"
uri: "mongodb://localhost/?proxyHost=localhost&proxyPort=1080"
valid: true
warning: false
hosts: ~
auth: ~
options: {}
-
description: "host and non-default port present"
uri: "mongodb://localhost/?proxyHost=localhost&proxyPort=12345"
valid: true
warning: false
hosts: ~
auth: ~
options: {}
-
description: "replicaset, host and non-default port present"
uri: "mongodb://rs1,rs2,rs3/?proxyHost=localhost&proxyPort=12345"
valid: true
warning: false
hosts: ~
auth: ~
options: {}
-
description: "all options present"
uri: "mongodb://rs1,rs2,rs3/?proxyHost=localhost&proxyPort=12345&proxyUsername=asdf&proxyPassword=qwerty"
valid: true
warning: false
hosts: ~
auth: ~
options: {}
6 changes: 6 additions & 0 deletions src/MongoDB.Driver/ClusterKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Collections.Generic;
using System.Linq;
using MongoDB.Driver.Core.Configuration;
using MongoDB.Driver.Core.Connections;
using MongoDB.Driver.Core.Servers;
using MongoDB.Shared;

Expand Down Expand Up @@ -55,6 +56,7 @@ internal sealed class ClusterKey
private readonly ServerMonitoringMode _serverMonitoringMode;
private readonly TimeSpan _serverSelectionTimeout;
private readonly TimeSpan _socketTimeout;
private readonly Socks5ProxySettings _socks5ProxySettings;
private readonly int _srvMaxHosts;
private readonly string _srvServiceName;
private readonly SslSettings _sslSettings;
Expand Down Expand Up @@ -93,6 +95,7 @@ public ClusterKey(
ServerMonitoringMode serverMonitoringMode,
TimeSpan serverSelectionTimeout,
TimeSpan socketTimeout,
Socks5ProxySettings socks5ProxySettings,
int srvMaxHosts,
string srvServiceName,
SslSettings sslSettings,
Expand Down Expand Up @@ -129,6 +132,7 @@ public ClusterKey(
_serverMonitoringMode = serverMonitoringMode;
_serverSelectionTimeout = serverSelectionTimeout;
_socketTimeout = socketTimeout;
_socks5ProxySettings = socks5ProxySettings;
_srvMaxHosts = srvMaxHosts;
_srvServiceName = srvServiceName;
_sslSettings = sslSettings;
Expand Down Expand Up @@ -169,6 +173,7 @@ public ClusterKey(
public ServerMonitoringMode ServerMonitoringMode { get { return _serverMonitoringMode; } }
public TimeSpan ServerSelectionTimeout { get { return _serverSelectionTimeout; } }
public TimeSpan SocketTimeout { get { return _socketTimeout; } }
public Socks5ProxySettings Socks5ProxySettings { get { return _socks5ProxySettings; } }
public int SrvMaxHosts { get { return _srvMaxHosts; } }
public string SrvServiceName { get { return _srvServiceName; } }
public SslSettings SslSettings { get { return _sslSettings; } }
Expand Down Expand Up @@ -224,6 +229,7 @@ public override bool Equals(object obj)
_serverMonitoringMode == rhs._serverMonitoringMode &&
_serverSelectionTimeout == rhs._serverSelectionTimeout &&
_socketTimeout == rhs._socketTimeout &&
object.Equals(_socks5ProxySettings, rhs._socks5ProxySettings) &&
_srvMaxHosts == rhs._srvMaxHosts &&
_srvServiceName == rhs.SrvServiceName &&
object.Equals(_sslSettings, rhs._sslSettings) &&
Expand Down
3 changes: 2 additions & 1 deletion src/MongoDB.Driver/ClusterRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ private TcpStreamSettings ConfigureTcp(TcpStreamSettings settings, ClusterKey cl
readTimeout: clusterKey.SocketTimeout,
receiveBufferSize: clusterKey.ReceiveBufferSize,
sendBufferSize: clusterKey.SendBufferSize,
writeTimeout: clusterKey.SocketTimeout);
writeTimeout: clusterKey.SocketTimeout,
socks5ProxySettings: clusterKey.Socks5ProxySettings);
}

internal IClusterInternal GetOrCreateCluster(ClusterKey clusterKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using MongoDB.Driver.Authentication;
using MongoDB.Driver.Authentication.Gssapi;
using MongoDB.Driver.Authentication.Oidc;
using MongoDB.Driver.Core.Connections;
using MongoDB.Driver.Core.Events.Diagnostics;
using MongoDB.Driver.Core.Misc;

Expand Down Expand Up @@ -118,6 +119,16 @@ public static ClusterBuilder ConfigureWithConnectionString(
builder = builder.ConfigureTcp(s => s.With(addressFamily: AddressFamily.InterNetworkV6));
}

if (connectionString.ProxyHost != null)
{
builder = builder.ConfigureTcp(s => s.With(
socks5ProxySettings: Socks5ProxySettings.Create(
connectionString.ProxyHost,
connectionString.ProxyPort,
connectionString.ProxyUsername,
connectionString.ProxyPassword)));
}

if (connectionString.SocketTimeout != null)
{
builder = builder.ConfigureTcp(s => s.With(
Expand Down
Loading