Skip to content

Commit a715fef

Browse files
committed
First version of setting class
1 parent 2ca4b3f commit a715fef

File tree

5 files changed

+161
-195
lines changed

5 files changed

+161
-195
lines changed

src/MongoDB.Driver/ClusterKey.cs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Collections.Generic;
1818
using System.Linq;
1919
using MongoDB.Driver.Core.Configuration;
20+
using MongoDB.Driver.Core.Connections;
2021
using MongoDB.Driver.Core.Servers;
2122
using MongoDB.Shared;
2223

@@ -46,10 +47,6 @@ internal sealed class ClusterKey
4647
private readonly TimeSpan _maxConnectionLifeTime;
4748
private readonly int _maxConnectionPoolSize;
4849
private readonly int _minConnectionPoolSize;
49-
private readonly string _proxyHost;
50-
private readonly int? _proxyPort;
51-
private readonly string _proxyUsername;
52-
private readonly string _proxyPassword;
5350
private readonly int _receiveBufferSize;
5451
private readonly string _replicaSetName;
5552
private readonly ConnectionStringScheme _scheme;
@@ -59,6 +56,7 @@ internal sealed class ClusterKey
5956
private readonly ServerMonitoringMode _serverMonitoringMode;
6057
private readonly TimeSpan _serverSelectionTimeout;
6158
private readonly TimeSpan _socketTimeout;
59+
private readonly Socks5ProxySettings _socks5ProxySettings;
6260
private readonly int _srvMaxHosts;
6361
private readonly string _srvServiceName;
6462
private readonly SslSettings _sslSettings;
@@ -88,10 +86,6 @@ public ClusterKey(
8886
TimeSpan maxConnectionLifeTime,
8987
int maxConnectionPoolSize,
9088
int minConnectionPoolSize,
91-
string proxyHost,
92-
int? proxyPort,
93-
string proxyUsername,
94-
string proxyPassword,
9589
int receiveBufferSize,
9690
string replicaSetName,
9791
ConnectionStringScheme scheme,
@@ -101,6 +95,7 @@ public ClusterKey(
10195
ServerMonitoringMode serverMonitoringMode,
10296
TimeSpan serverSelectionTimeout,
10397
TimeSpan socketTimeout,
98+
Socks5ProxySettings socks5ProxySettings,
10499
int srvMaxHosts,
105100
string srvServiceName,
106101
SslSettings sslSettings,
@@ -128,10 +123,6 @@ public ClusterKey(
128123
_maxConnectionLifeTime = maxConnectionLifeTime;
129124
_maxConnectionPoolSize = maxConnectionPoolSize;
130125
_minConnectionPoolSize = minConnectionPoolSize;
131-
_proxyHost = proxyHost;
132-
_proxyPort = proxyPort;
133-
_proxyUsername = proxyUsername;
134-
_proxyPassword = proxyPassword;
135126
_receiveBufferSize = receiveBufferSize;
136127
_replicaSetName = replicaSetName;
137128
_scheme = scheme;
@@ -141,6 +132,7 @@ public ClusterKey(
141132
_serverMonitoringMode = serverMonitoringMode;
142133
_serverSelectionTimeout = serverSelectionTimeout;
143134
_socketTimeout = socketTimeout;
135+
_socks5ProxySettings = socks5ProxySettings;
144136
_srvMaxHosts = srvMaxHosts;
145137
_srvServiceName = srvServiceName;
146138
_sslSettings = sslSettings;
@@ -172,10 +164,6 @@ public ClusterKey(
172164
public TimeSpan MaxConnectionLifeTime { get { return _maxConnectionLifeTime; } }
173165
public int MaxConnectionPoolSize { get { return _maxConnectionPoolSize; } }
174166
public int MinConnectionPoolSize { get { return _minConnectionPoolSize; } }
175-
public string ProxyHost { get { return _proxyHost; } }
176-
public int? ProxyPort { get { return _proxyPort; } }
177-
public string ProxyUsername { get { return _proxyUsername; } }
178-
public string ProxyPassword { get { return _proxyPassword; } }
179167
public int ReceiveBufferSize { get { return _receiveBufferSize; } }
180168
public string ReplicaSetName { get { return _replicaSetName; } }
181169
public ConnectionStringScheme Scheme { get { return _scheme; } }
@@ -185,6 +173,7 @@ public ClusterKey(
185173
public ServerMonitoringMode ServerMonitoringMode { get { return _serverMonitoringMode; } }
186174
public TimeSpan ServerSelectionTimeout { get { return _serverSelectionTimeout; } }
187175
public TimeSpan SocketTimeout { get { return _socketTimeout; } }
176+
public Socks5ProxySettings Socks5ProxySettings { get { return _socks5ProxySettings; } }
188177
public int SrvMaxHosts { get { return _srvMaxHosts; } }
189178
public string SrvServiceName { get { return _srvServiceName; } }
190179
public SslSettings SslSettings { get { return _sslSettings; } }
@@ -231,10 +220,6 @@ public override bool Equals(object obj)
231220
_maxConnectionLifeTime == rhs._maxConnectionLifeTime &&
232221
_maxConnectionPoolSize == rhs._maxConnectionPoolSize &&
233222
_minConnectionPoolSize == rhs._minConnectionPoolSize &&
234-
_proxyHost == rhs._proxyHost &&
235-
_proxyPort == rhs._proxyPort &&
236-
_proxyUsername == rhs._proxyUsername &&
237-
_proxyPassword == rhs._proxyPassword &&
238223
_receiveBufferSize == rhs._receiveBufferSize &&
239224
_replicaSetName == rhs._replicaSetName &&
240225
_scheme == rhs._scheme &&
@@ -244,6 +229,7 @@ public override bool Equals(object obj)
244229
_serverMonitoringMode == rhs._serverMonitoringMode &&
245230
_serverSelectionTimeout == rhs._serverSelectionTimeout &&
246231
_socketTimeout == rhs._socketTimeout &&
232+
object.Equals(_socks5ProxySettings, rhs._socks5ProxySettings) &&
247233
_srvMaxHosts == rhs._srvMaxHosts &&
248234
_srvServiceName == rhs.SrvServiceName &&
249235
object.Equals(_sslSettings, rhs._sslSettings) &&

src/MongoDB.Driver/ClusterRegistry.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,7 @@ private TcpStreamSettings ConfigureTcp(TcpStreamSettings settings, ClusterKey cl
172172
receiveBufferSize: clusterKey.ReceiveBufferSize,
173173
sendBufferSize: clusterKey.SendBufferSize,
174174
writeTimeout: clusterKey.SocketTimeout,
175-
proxyHost: clusterKey.ProxyHost,
176-
proxyPort: clusterKey.ProxyPort,
177-
proxyUsername: clusterKey.ProxyUsername,
178-
proxyPassword: clusterKey.ProxyPassword);
175+
socks5ProxySettings: clusterKey.Socks5ProxySettings);
179176
}
180177

181178
internal IClusterInternal GetOrCreateCluster(ClusterKey clusterKey)

src/MongoDB.Driver/Core/Configuration/TcpStreamSettings.cs

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Net.Sockets;
1818
using System.Threading;
19+
using MongoDB.Driver.Core.Connections;
1920
using MongoDB.Driver.Core.Misc;
2021

2122
namespace MongoDB.Driver.Core.Configuration
@@ -32,11 +33,8 @@ public class TcpStreamSettings
3233
private readonly int _receiveBufferSize;
3334
private readonly int _sendBufferSize;
3435
private readonly Action<Socket> _socketConfigurator;
36+
private readonly Socks5ProxySettings _socks5ProxySettings;
3537
private readonly TimeSpan? _writeTimeout;
36-
private readonly string _proxyHost;
37-
private readonly int? _proxyPort;
38-
private readonly string _proxyUsername;
39-
private readonly string _proxyPassword;
4038

4139
// constructors
4240
/// <summary>
@@ -48,11 +46,8 @@ public class TcpStreamSettings
4846
/// <param name="receiveBufferSize">Size of the receive buffer.</param>
4947
/// <param name="sendBufferSize">Size of the send buffer.</param>
5048
/// <param name="socketConfigurator">The socket configurator.</param>
49+
/// <param name="socks5ProxySettings">The SOCKS5 proxy settings.</param>
5150
/// <param name="writeTimeout">The write timeout.</param>
52-
/// <param name="proxyHost">//TODO</param>
53-
/// <param name="proxyPort"></param>
54-
/// <param name="proxyUsername"></param>
55-
/// <param name="proxyPassword"></param>
5651
public TcpStreamSettings(
5752
Optional<AddressFamily> addressFamily = default(Optional<AddressFamily>),
5853
Optional<TimeSpan> connectTimeout = default(Optional<TimeSpan>),
@@ -61,10 +56,7 @@ public TcpStreamSettings(
6156
Optional<int> sendBufferSize = default(Optional<int>),
6257
Optional<Action<Socket>> socketConfigurator = default(Optional<Action<Socket>>),
6358
Optional<TimeSpan?> writeTimeout = default(Optional<TimeSpan?>),
64-
Optional<string> proxyHost = default(Optional<string>),
65-
Optional<int?> proxyPort = default(Optional<int?>),
66-
Optional<string> proxyUsername = default(Optional<string>),
67-
Optional<string> proxyPassword = default(Optional<string>))
59+
Optional<Socks5ProxySettings> socks5ProxySettings = default(Optional<Socks5ProxySettings>))
6860
{
6961
_addressFamily = addressFamily.WithDefault(AddressFamily.InterNetwork);
7062
_connectTimeout = Ensure.IsInfiniteOrGreaterThanOrEqualToZero(connectTimeout.WithDefault(Timeout.InfiniteTimeSpan), "connectTimeout");
@@ -73,10 +65,7 @@ public TcpStreamSettings(
7365
_sendBufferSize = Ensure.IsGreaterThanZero(sendBufferSize.WithDefault(64 * 1024), "sendBufferSize");
7466
_socketConfigurator = socketConfigurator.WithDefault(null);
7567
_writeTimeout = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(writeTimeout.WithDefault(null), "writeTimeout");
76-
_proxyHost = proxyHost.WithDefault(null);
77-
_proxyPort = proxyPort.WithDefault(null);
78-
_proxyUsername = proxyUsername.WithDefault(null);
79-
_proxyPassword = proxyPassword.WithDefault(null);
68+
_socks5ProxySettings = socks5ProxySettings.WithDefault(null);
8069
}
8170

8271
// /// <summary>
@@ -109,11 +98,8 @@ internal TcpStreamSettings(TcpStreamSettings other)
10998
_receiveBufferSize = other.ReceiveBufferSize;
11099
_sendBufferSize = other.SendBufferSize;
111100
_socketConfigurator = other.SocketConfigurator;
101+
_socks5ProxySettings = other._socks5ProxySettings;
112102
_writeTimeout = other.WriteTimeout;
113-
_proxyHost = other._proxyHost;
114-
_proxyPort = other._proxyPort;
115-
_proxyUsername = other._proxyUsername;
116-
_proxyPassword = other._proxyPassword;
117103
}
118104

119105
// properties
@@ -183,6 +169,11 @@ public Action<Socket> SocketConfigurator
183169
get { return _socketConfigurator; }
184170
}
185171

172+
/// <summary>
173+
/// Gets the SOCKS5 proxy settings.
174+
/// </summary>
175+
public Socks5ProxySettings Socks5ProxySettings => _socks5ProxySettings;
176+
186177
/// <summary>
187178
/// Gets the write timeout.
188179
/// </summary>
@@ -194,28 +185,6 @@ public TimeSpan? WriteTimeout
194185
get { return _writeTimeout; }
195186
}
196187

197-
//TODO Add xml docs
198-
/// <summary>
199-
///
200-
/// </summary>
201-
public string ProxyHost => _proxyHost;
202-
/// <summary>
203-
///
204-
/// </summary>
205-
public int? ProxyPort => _proxyPort;
206-
/// <summary>
207-
///
208-
/// </summary>
209-
public string ProxyUsername => _proxyUsername;
210-
/// <summary>
211-
///
212-
/// </summary>
213-
public string ProxyPassword => _proxyPassword;
214-
215-
//TODO We can decide to remove this
216-
internal bool UseProxy => !string.IsNullOrEmpty(_proxyHost) && _proxyPort.HasValue;
217-
218-
219188
// methods
220189
/// <summary>
221190
/// Returns a new TcpStreamSettings instance with some settings changed.
@@ -226,11 +195,8 @@ public TimeSpan? WriteTimeout
226195
/// <param name="receiveBufferSize">Size of the receive buffer.</param>
227196
/// <param name="sendBufferSize">Size of the send buffer.</param>
228197
/// <param name="socketConfigurator">The socket configurator.</param>
198+
/// <param name="socks5ProxySettings">The SOCKS5 proxy settings.</param>
229199
/// <param name="writeTimeout">The write timeout.</param>
230-
/// <param name="proxyHost"> //TODO Add docs</param>
231-
/// <param name="proxyPort"></param>
232-
/// <param name="proxyUsername"></param>
233-
/// <param name="proxyPassword"></param>
234200
/// <returns>A new TcpStreamSettings instance.</returns>
235201
public TcpStreamSettings With(
236202
Optional<AddressFamily> addressFamily = default(Optional<AddressFamily>),
@@ -240,10 +206,7 @@ public TcpStreamSettings With(
240206
Optional<int> sendBufferSize = default(Optional<int>),
241207
Optional<Action<Socket>> socketConfigurator = default(Optional<Action<Socket>>),
242208
Optional<TimeSpan?> writeTimeout = default(Optional<TimeSpan?>),
243-
Optional<string> proxyHost = default(Optional<string>),
244-
Optional<int?> proxyPort = default(Optional<int?>),
245-
Optional<string> proxyUsername = default(Optional<string>),
246-
Optional<string> proxyPassword = default(Optional<string>))
209+
Optional<Socks5ProxySettings> socks5ProxySettings = default(Optional<Socks5ProxySettings>))
247210
{
248211
return new TcpStreamSettings(
249212
addressFamily: addressFamily.WithDefault(_addressFamily),
@@ -252,11 +215,8 @@ public TcpStreamSettings With(
252215
receiveBufferSize: receiveBufferSize.WithDefault(_receiveBufferSize),
253216
sendBufferSize: sendBufferSize.WithDefault(_sendBufferSize),
254217
socketConfigurator: socketConfigurator.WithDefault(_socketConfigurator),
255-
writeTimeout: writeTimeout.WithDefault(_writeTimeout),
256-
proxyHost: proxyHost.WithDefault(_proxyHost),
257-
proxyPort: proxyPort.WithDefault(_proxyPort),
258-
proxyUsername: proxyUsername.WithDefault(_proxyUsername),
259-
proxyPassword: proxyPassword.WithDefault(_proxyPassword));
218+
socks5ProxySettings: socks5ProxySettings.WithDefault(_socks5ProxySettings),
219+
writeTimeout: writeTimeout.WithDefault(_writeTimeout));
260220
}
261221
}
262222
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using MongoDB.Driver.Core.Misc;
18+
19+
namespace MongoDB.Driver.Core.Connections
20+
{
21+
/// <summary>
22+
/// Represents the settings for a SOCKS5 proxy connection.
23+
/// </summary>
24+
public class Socks5ProxySettings
25+
{
26+
/// <summary>
27+
/// Gets the host of the SOCKS5 proxy.
28+
/// </summary>
29+
public string Host { get; }
30+
31+
/// <summary>
32+
/// Gets the port of the SOCKS5 proxy.
33+
/// </summary>
34+
public int Port { get; }
35+
36+
/// <summary>
37+
/// Gets the authentication settings of the SOCKS5 proxy.
38+
/// </summary>
39+
public Socks5AuthenticationSettings Authentication { get; }
40+
41+
private Socks5ProxySettings(string host, int port, Socks5AuthenticationSettings authentication)
42+
{
43+
Host = Ensure.IsNotNullOrEmpty(host, nameof(host));
44+
Port = Ensure.IsBetween(port, 0, 65535, nameof(port));
45+
Authentication = Ensure.IsNotNull(authentication, nameof(authentication));
46+
}
47+
48+
internal static Socks5ProxySettings Create(string host, int? port, string username, string password)
49+
{
50+
Socks5AuthenticationSettings authentication;
51+
52+
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
53+
{
54+
authentication = Socks5AuthenticationSettings.UsernamePassword(username, password);
55+
}
56+
else
57+
{
58+
authentication = Socks5AuthenticationSettings.None;
59+
}
60+
61+
return new Socks5ProxySettings(host, port ?? 1080, authentication);
62+
}
63+
64+
/// <summary>
65+
/// Creates a new instance of <see cref="Socks5ProxySettings"/>.
66+
/// </summary>
67+
/// <param name="host">The host.</param>
68+
/// <param name="port">The port</param>
69+
/// <param name="authentication">The authentication settings.</param>
70+
/// <returns></returns>
71+
public static Socks5ProxySettings Create(string host, int port = 1080, Socks5AuthenticationSettings authentication = null)
72+
{
73+
return new Socks5ProxySettings(host, port, authentication ?? Socks5AuthenticationSettings.None);
74+
}
75+
}
76+
77+
internal enum Socks5AuthenticationType
78+
{
79+
None,
80+
UsernamePassword
81+
}
82+
83+
/// <summary>
84+
/// Represents the settings for SOCKS5 authentication.
85+
/// </summary>
86+
public abstract class Socks5AuthenticationSettings
87+
{
88+
internal abstract Socks5AuthenticationType Type { get; }
89+
90+
/// <summary>
91+
/// Creates authentication settings that do not require any authentication.
92+
/// </summary>
93+
public static Socks5AuthenticationSettings None => new NoAuthenticationSettings();
94+
95+
/// <summary>
96+
/// Creates authentication settings for username and password.
97+
/// </summary>
98+
/// <param name="username"></param>
99+
/// <param name="password"></param>
100+
/// <returns></returns>
101+
public static Socks5AuthenticationSettings UsernamePassword(string username, string password)
102+
=> new UsernamePasswordAuthenticationSettings(username, password);
103+
104+
private sealed class NoAuthenticationSettings : Socks5AuthenticationSettings
105+
{
106+
internal override Socks5AuthenticationType Type => Socks5AuthenticationType.None;
107+
}
108+
109+
private sealed class UsernamePasswordAuthenticationSettings : Socks5AuthenticationSettings
110+
{
111+
internal override Socks5AuthenticationType Type => Socks5AuthenticationType.UsernamePassword;
112+
public string Username { get; }
113+
public string Password { get; }
114+
115+
internal UsernamePasswordAuthenticationSettings(string username, string password)
116+
{
117+
Username = Ensure.IsNotNullOrEmpty(username, nameof(username));
118+
Password = Ensure.IsNotNullOrEmpty(password, nameof(password));
119+
}
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)