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 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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: {}
20 changes: 20 additions & 0 deletions src/MongoDB.Driver/ClusterKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ internal sealed class ClusterKey
private readonly TimeSpan _maxConnectionLifeTime;
private readonly int _maxConnectionPoolSize;
private readonly int _minConnectionPoolSize;
private readonly string _proxyHost;
private readonly int? _proxyPort;
private readonly string _proxyUsername;
private readonly string _proxyPassword;
private readonly int _receiveBufferSize;
private readonly string _replicaSetName;
private readonly ConnectionStringScheme _scheme;
Expand Down Expand Up @@ -84,6 +88,10 @@ public ClusterKey(
TimeSpan maxConnectionLifeTime,
int maxConnectionPoolSize,
int minConnectionPoolSize,
string proxyHost,
int? proxyPort,
string proxyUsername,
string proxyPassword,
int receiveBufferSize,
string replicaSetName,
ConnectionStringScheme scheme,
Expand Down Expand Up @@ -120,6 +128,10 @@ public ClusterKey(
_maxConnectionLifeTime = maxConnectionLifeTime;
_maxConnectionPoolSize = maxConnectionPoolSize;
_minConnectionPoolSize = minConnectionPoolSize;
_proxyHost = proxyHost;
_proxyPort = proxyPort;
_proxyUsername = proxyUsername;
_proxyPassword = proxyPassword;
_receiveBufferSize = receiveBufferSize;
_replicaSetName = replicaSetName;
_scheme = scheme;
Expand Down Expand Up @@ -160,6 +172,10 @@ public ClusterKey(
public TimeSpan MaxConnectionLifeTime { get { return _maxConnectionLifeTime; } }
public int MaxConnectionPoolSize { get { return _maxConnectionPoolSize; } }
public int MinConnectionPoolSize { get { return _minConnectionPoolSize; } }
public string ProxyHost { get { return _proxyHost; } }
public int? ProxyPort { get { return _proxyPort; } }
public string ProxyUsername { get { return _proxyUsername; } }
public string ProxyPassword { get { return _proxyPassword; } }
public int ReceiveBufferSize { get { return _receiveBufferSize; } }
public string ReplicaSetName { get { return _replicaSetName; } }
public ConnectionStringScheme Scheme { get { return _scheme; } }
Expand Down Expand Up @@ -215,6 +231,10 @@ public override bool Equals(object obj)
_maxConnectionLifeTime == rhs._maxConnectionLifeTime &&
_maxConnectionPoolSize == rhs._maxConnectionPoolSize &&
_minConnectionPoolSize == rhs._minConnectionPoolSize &&
_proxyHost == rhs._proxyHost &&
_proxyPort == rhs._proxyPort &&
_proxyUsername == rhs._proxyUsername &&
_proxyPassword == rhs._proxyPassword &&
_receiveBufferSize == rhs._receiveBufferSize &&
_replicaSetName == rhs._replicaSetName &&
_scheme == rhs._scheme &&
Expand Down
6 changes: 5 additions & 1 deletion src/MongoDB.Driver/ClusterRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ private TcpStreamSettings ConfigureTcp(TcpStreamSettings settings, ClusterKey cl
readTimeout: clusterKey.SocketTimeout,
receiveBufferSize: clusterKey.ReceiveBufferSize,
sendBufferSize: clusterKey.SendBufferSize,
writeTimeout: clusterKey.SocketTimeout);
writeTimeout: clusterKey.SocketTimeout,
proxyHost: clusterKey.ProxyHost,
proxyPort: clusterKey.ProxyPort,
proxyUsername: clusterKey.ProxyUsername,
proxyPassword: clusterKey.ProxyPassword);
}

internal IClusterInternal GetOrCreateCluster(ClusterKey clusterKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ public static ClusterBuilder ConfigureWithConnectionString(
builder = builder.ConfigureTcp(s => s.With(addressFamily: AddressFamily.InterNetworkV6));
}

if (connectionString.ProxyHost != null)
{
builder = builder.ConfigureTcp(s => s.With(
proxyHost: connectionString.ProxyHost,
proxyPort: connectionString.ProxyPort,
proxyUsername: connectionString.ProxyUsername,
proxyPassword: connectionString.ProxyPassword));
}

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