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 29 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
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