Browse Source

First version of setting class

pull/1731/head
Ferdinando Papale 7 days ago
parent
commit
a715feffde
  1. 26
      src/MongoDB.Driver/ClusterKey.cs
  2. 5
      src/MongoDB.Driver/ClusterRegistry.cs
  3. 70
      src/MongoDB.Driver/Core/Configuration/TcpStreamSettings.cs
  4. 122
      src/MongoDB.Driver/Core/Connections/Socks5ProxySettings.cs
  5. 133
      src/MongoDB.Driver/MongoClientSettings.cs

26
src/MongoDB.Driver/ClusterKey.cs

@ -17,6 +17,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using MongoDB.Driver.Core.Configuration; using MongoDB.Driver.Core.Configuration;
using MongoDB.Driver.Core.Connections;
using MongoDB.Driver.Core.Servers; using MongoDB.Driver.Core.Servers;
using MongoDB.Shared; using MongoDB.Shared;
@ -46,10 +47,6 @@ namespace MongoDB.Driver
private readonly TimeSpan _maxConnectionLifeTime; private readonly TimeSpan _maxConnectionLifeTime;
private readonly int _maxConnectionPoolSize; private readonly int _maxConnectionPoolSize;
private readonly int _minConnectionPoolSize; 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 int _receiveBufferSize;
private readonly string _replicaSetName; private readonly string _replicaSetName;
private readonly ConnectionStringScheme _scheme; private readonly ConnectionStringScheme _scheme;
@ -59,6 +56,7 @@ namespace MongoDB.Driver
private readonly ServerMonitoringMode _serverMonitoringMode; private readonly ServerMonitoringMode _serverMonitoringMode;
private readonly TimeSpan _serverSelectionTimeout; private readonly TimeSpan _serverSelectionTimeout;
private readonly TimeSpan _socketTimeout; private readonly TimeSpan _socketTimeout;
private readonly Socks5ProxySettings _socks5ProxySettings;
private readonly int _srvMaxHosts; private readonly int _srvMaxHosts;
private readonly string _srvServiceName; private readonly string _srvServiceName;
private readonly SslSettings _sslSettings; private readonly SslSettings _sslSettings;
@ -88,10 +86,6 @@ namespace MongoDB.Driver
TimeSpan maxConnectionLifeTime, TimeSpan maxConnectionLifeTime,
int maxConnectionPoolSize, int maxConnectionPoolSize,
int minConnectionPoolSize, int minConnectionPoolSize,
string proxyHost,
int? proxyPort,
string proxyUsername,
string proxyPassword,
int receiveBufferSize, int receiveBufferSize,
string replicaSetName, string replicaSetName,
ConnectionStringScheme scheme, ConnectionStringScheme scheme,
@ -101,6 +95,7 @@ namespace MongoDB.Driver
ServerMonitoringMode serverMonitoringMode, ServerMonitoringMode serverMonitoringMode,
TimeSpan serverSelectionTimeout, TimeSpan serverSelectionTimeout,
TimeSpan socketTimeout, TimeSpan socketTimeout,
Socks5ProxySettings socks5ProxySettings,
int srvMaxHosts, int srvMaxHosts,
string srvServiceName, string srvServiceName,
SslSettings sslSettings, SslSettings sslSettings,
@ -128,10 +123,6 @@ namespace MongoDB.Driver
_maxConnectionLifeTime = maxConnectionLifeTime; _maxConnectionLifeTime = maxConnectionLifeTime;
_maxConnectionPoolSize = maxConnectionPoolSize; _maxConnectionPoolSize = maxConnectionPoolSize;
_minConnectionPoolSize = minConnectionPoolSize; _minConnectionPoolSize = minConnectionPoolSize;
_proxyHost = proxyHost;
_proxyPort = proxyPort;
_proxyUsername = proxyUsername;
_proxyPassword = proxyPassword;
_receiveBufferSize = receiveBufferSize; _receiveBufferSize = receiveBufferSize;
_replicaSetName = replicaSetName; _replicaSetName = replicaSetName;
_scheme = scheme; _scheme = scheme;
@ -141,6 +132,7 @@ namespace MongoDB.Driver
_serverMonitoringMode = serverMonitoringMode; _serverMonitoringMode = serverMonitoringMode;
_serverSelectionTimeout = serverSelectionTimeout; _serverSelectionTimeout = serverSelectionTimeout;
_socketTimeout = socketTimeout; _socketTimeout = socketTimeout;
_socks5ProxySettings = socks5ProxySettings;
_srvMaxHosts = srvMaxHosts; _srvMaxHosts = srvMaxHosts;
_srvServiceName = srvServiceName; _srvServiceName = srvServiceName;
_sslSettings = sslSettings; _sslSettings = sslSettings;
@ -172,10 +164,6 @@ namespace MongoDB.Driver
public TimeSpan MaxConnectionLifeTime { get { return _maxConnectionLifeTime; } } public TimeSpan MaxConnectionLifeTime { get { return _maxConnectionLifeTime; } }
public int MaxConnectionPoolSize { get { return _maxConnectionPoolSize; } } public int MaxConnectionPoolSize { get { return _maxConnectionPoolSize; } }
public int MinConnectionPoolSize { get { return _minConnectionPoolSize; } } 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 int ReceiveBufferSize { get { return _receiveBufferSize; } }
public string ReplicaSetName { get { return _replicaSetName; } } public string ReplicaSetName { get { return _replicaSetName; } }
public ConnectionStringScheme Scheme { get { return _scheme; } } public ConnectionStringScheme Scheme { get { return _scheme; } }
@ -185,6 +173,7 @@ namespace MongoDB.Driver
public ServerMonitoringMode ServerMonitoringMode { get { return _serverMonitoringMode; } } public ServerMonitoringMode ServerMonitoringMode { get { return _serverMonitoringMode; } }
public TimeSpan ServerSelectionTimeout { get { return _serverSelectionTimeout; } } public TimeSpan ServerSelectionTimeout { get { return _serverSelectionTimeout; } }
public TimeSpan SocketTimeout { get { return _socketTimeout; } } public TimeSpan SocketTimeout { get { return _socketTimeout; } }
public Socks5ProxySettings Socks5ProxySettings { get { return _socks5ProxySettings; } }
public int SrvMaxHosts { get { return _srvMaxHosts; } } public int SrvMaxHosts { get { return _srvMaxHosts; } }
public string SrvServiceName { get { return _srvServiceName; } } public string SrvServiceName { get { return _srvServiceName; } }
public SslSettings SslSettings { get { return _sslSettings; } } public SslSettings SslSettings { get { return _sslSettings; } }
@ -231,10 +220,6 @@ namespace MongoDB.Driver
_maxConnectionLifeTime == rhs._maxConnectionLifeTime && _maxConnectionLifeTime == rhs._maxConnectionLifeTime &&
_maxConnectionPoolSize == rhs._maxConnectionPoolSize && _maxConnectionPoolSize == rhs._maxConnectionPoolSize &&
_minConnectionPoolSize == rhs._minConnectionPoolSize && _minConnectionPoolSize == rhs._minConnectionPoolSize &&
_proxyHost == rhs._proxyHost &&
_proxyPort == rhs._proxyPort &&
_proxyUsername == rhs._proxyUsername &&
_proxyPassword == rhs._proxyPassword &&
_receiveBufferSize == rhs._receiveBufferSize && _receiveBufferSize == rhs._receiveBufferSize &&
_replicaSetName == rhs._replicaSetName && _replicaSetName == rhs._replicaSetName &&
_scheme == rhs._scheme && _scheme == rhs._scheme &&
@ -244,6 +229,7 @@ namespace MongoDB.Driver
_serverMonitoringMode == rhs._serverMonitoringMode && _serverMonitoringMode == rhs._serverMonitoringMode &&
_serverSelectionTimeout == rhs._serverSelectionTimeout && _serverSelectionTimeout == rhs._serverSelectionTimeout &&
_socketTimeout == rhs._socketTimeout && _socketTimeout == rhs._socketTimeout &&
object.Equals(_socks5ProxySettings, rhs._socks5ProxySettings) &&
_srvMaxHosts == rhs._srvMaxHosts && _srvMaxHosts == rhs._srvMaxHosts &&
_srvServiceName == rhs.SrvServiceName && _srvServiceName == rhs.SrvServiceName &&
object.Equals(_sslSettings, rhs._sslSettings) && object.Equals(_sslSettings, rhs._sslSettings) &&

5
src/MongoDB.Driver/ClusterRegistry.cs

@ -172,10 +172,7 @@ namespace MongoDB.Driver
receiveBufferSize: clusterKey.ReceiveBufferSize, receiveBufferSize: clusterKey.ReceiveBufferSize,
sendBufferSize: clusterKey.SendBufferSize, sendBufferSize: clusterKey.SendBufferSize,
writeTimeout: clusterKey.SocketTimeout, writeTimeout: clusterKey.SocketTimeout,
proxyHost: clusterKey.ProxyHost,
proxyPort: clusterKey.ProxyPort,
proxyUsername: clusterKey.ProxyUsername,
proxyPassword: clusterKey.ProxyPassword);
socks5ProxySettings: clusterKey.Socks5ProxySettings);
} }
internal IClusterInternal GetOrCreateCluster(ClusterKey clusterKey) internal IClusterInternal GetOrCreateCluster(ClusterKey clusterKey)

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

@ -16,6 +16,7 @@
using System; using System;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; using System.Threading;
using MongoDB.Driver.Core.Connections;
using MongoDB.Driver.Core.Misc; using MongoDB.Driver.Core.Misc;
namespace MongoDB.Driver.Core.Configuration namespace MongoDB.Driver.Core.Configuration
@ -32,11 +33,8 @@ namespace MongoDB.Driver.Core.Configuration
private readonly int _receiveBufferSize; private readonly int _receiveBufferSize;
private readonly int _sendBufferSize; private readonly int _sendBufferSize;
private readonly Action<Socket> _socketConfigurator; private readonly Action<Socket> _socketConfigurator;
private readonly Socks5ProxySettings _socks5ProxySettings;
private readonly TimeSpan? _writeTimeout; private readonly TimeSpan? _writeTimeout;
private readonly string _proxyHost;
private readonly int? _proxyPort;
private readonly string _proxyUsername;
private readonly string _proxyPassword;
// constructors // constructors
/// <summary> /// <summary>
@ -48,11 +46,8 @@ namespace MongoDB.Driver.Core.Configuration
/// <param name="receiveBufferSize">Size of the receive buffer.</param> /// <param name="receiveBufferSize">Size of the receive buffer.</param>
/// <param name="sendBufferSize">Size of the send buffer.</param> /// <param name="sendBufferSize">Size of the send buffer.</param>
/// <param name="socketConfigurator">The socket configurator.</param> /// <param name="socketConfigurator">The socket configurator.</param>
/// <param name="socks5ProxySettings">The SOCKS5 proxy settings.</param>
/// <param name="writeTimeout">The write timeout.</param> /// <param name="writeTimeout">The write timeout.</param>
/// <param name="proxyHost">//TODO</param>
/// <param name="proxyPort"></param>
/// <param name="proxyUsername"></param>
/// <param name="proxyPassword"></param>
public TcpStreamSettings( public TcpStreamSettings(
Optional<AddressFamily> addressFamily = default(Optional<AddressFamily>), Optional<AddressFamily> addressFamily = default(Optional<AddressFamily>),
Optional<TimeSpan> connectTimeout = default(Optional<TimeSpan>), Optional<TimeSpan> connectTimeout = default(Optional<TimeSpan>),
@ -61,10 +56,7 @@ namespace MongoDB.Driver.Core.Configuration
Optional<int> sendBufferSize = default(Optional<int>), Optional<int> sendBufferSize = default(Optional<int>),
Optional<Action<Socket>> socketConfigurator = default(Optional<Action<Socket>>), Optional<Action<Socket>> socketConfigurator = default(Optional<Action<Socket>>),
Optional<TimeSpan?> writeTimeout = default(Optional<TimeSpan?>), Optional<TimeSpan?> writeTimeout = default(Optional<TimeSpan?>),
Optional<string> proxyHost = default(Optional<string>),
Optional<int?> proxyPort = default(Optional<int?>),
Optional<string> proxyUsername = default(Optional<string>),
Optional<string> proxyPassword = default(Optional<string>))
Optional<Socks5ProxySettings> socks5ProxySettings = default(Optional<Socks5ProxySettings>))
{ {
_addressFamily = addressFamily.WithDefault(AddressFamily.InterNetwork); _addressFamily = addressFamily.WithDefault(AddressFamily.InterNetwork);
_connectTimeout = Ensure.IsInfiniteOrGreaterThanOrEqualToZero(connectTimeout.WithDefault(Timeout.InfiniteTimeSpan), "connectTimeout"); _connectTimeout = Ensure.IsInfiniteOrGreaterThanOrEqualToZero(connectTimeout.WithDefault(Timeout.InfiniteTimeSpan), "connectTimeout");
@ -73,10 +65,7 @@ namespace MongoDB.Driver.Core.Configuration
_sendBufferSize = Ensure.IsGreaterThanZero(sendBufferSize.WithDefault(64 * 1024), "sendBufferSize"); _sendBufferSize = Ensure.IsGreaterThanZero(sendBufferSize.WithDefault(64 * 1024), "sendBufferSize");
_socketConfigurator = socketConfigurator.WithDefault(null); _socketConfigurator = socketConfigurator.WithDefault(null);
_writeTimeout = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(writeTimeout.WithDefault(null), "writeTimeout"); _writeTimeout = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(writeTimeout.WithDefault(null), "writeTimeout");
_proxyHost = proxyHost.WithDefault(null);
_proxyPort = proxyPort.WithDefault(null);
_proxyUsername = proxyUsername.WithDefault(null);
_proxyPassword = proxyPassword.WithDefault(null);
_socks5ProxySettings = socks5ProxySettings.WithDefault(null);
} }
// /// <summary> // /// <summary>
@ -109,11 +98,8 @@ namespace MongoDB.Driver.Core.Configuration
_receiveBufferSize = other.ReceiveBufferSize; _receiveBufferSize = other.ReceiveBufferSize;
_sendBufferSize = other.SendBufferSize; _sendBufferSize = other.SendBufferSize;
_socketConfigurator = other.SocketConfigurator; _socketConfigurator = other.SocketConfigurator;
_socks5ProxySettings = other._socks5ProxySettings;
_writeTimeout = other.WriteTimeout; _writeTimeout = other.WriteTimeout;
_proxyHost = other._proxyHost;
_proxyPort = other._proxyPort;
_proxyUsername = other._proxyUsername;
_proxyPassword = other._proxyPassword;
} }
// properties // properties
@ -183,6 +169,11 @@ namespace MongoDB.Driver.Core.Configuration
get { return _socketConfigurator; } get { return _socketConfigurator; }
} }
/// <summary>
/// Gets the SOCKS5 proxy settings.
/// </summary>
public Socks5ProxySettings Socks5ProxySettings => _socks5ProxySettings;
/// <summary> /// <summary>
/// Gets the write timeout. /// Gets the write timeout.
/// </summary> /// </summary>
@ -194,28 +185,6 @@ namespace MongoDB.Driver.Core.Configuration
get { return _writeTimeout; } get { return _writeTimeout; }
} }
//TODO Add xml docs
/// <summary>
///
/// </summary>
public string ProxyHost => _proxyHost;
/// <summary>
///
/// </summary>
public int? ProxyPort => _proxyPort;
/// <summary>
///
/// </summary>
public string ProxyUsername => _proxyUsername;
/// <summary>
///
/// </summary>
public string ProxyPassword => _proxyPassword;
//TODO We can decide to remove this
internal bool UseProxy => !string.IsNullOrEmpty(_proxyHost) && _proxyPort.HasValue;
// methods // methods
/// <summary> /// <summary>
/// Returns a new TcpStreamSettings instance with some settings changed. /// Returns a new TcpStreamSettings instance with some settings changed.
@ -226,11 +195,8 @@ namespace MongoDB.Driver.Core.Configuration
/// <param name="receiveBufferSize">Size of the receive buffer.</param> /// <param name="receiveBufferSize">Size of the receive buffer.</param>
/// <param name="sendBufferSize">Size of the send buffer.</param> /// <param name="sendBufferSize">Size of the send buffer.</param>
/// <param name="socketConfigurator">The socket configurator.</param> /// <param name="socketConfigurator">The socket configurator.</param>
/// <param name="socks5ProxySettings">The SOCKS5 proxy settings.</param>
/// <param name="writeTimeout">The write timeout.</param> /// <param name="writeTimeout">The write timeout.</param>
/// <param name="proxyHost"> //TODO Add docs</param>
/// <param name="proxyPort"></param>
/// <param name="proxyUsername"></param>
/// <param name="proxyPassword"></param>
/// <returns>A new TcpStreamSettings instance.</returns> /// <returns>A new TcpStreamSettings instance.</returns>
public TcpStreamSettings With( public TcpStreamSettings With(
Optional<AddressFamily> addressFamily = default(Optional<AddressFamily>), Optional<AddressFamily> addressFamily = default(Optional<AddressFamily>),
@ -240,10 +206,7 @@ namespace MongoDB.Driver.Core.Configuration
Optional<int> sendBufferSize = default(Optional<int>), Optional<int> sendBufferSize = default(Optional<int>),
Optional<Action<Socket>> socketConfigurator = default(Optional<Action<Socket>>), Optional<Action<Socket>> socketConfigurator = default(Optional<Action<Socket>>),
Optional<TimeSpan?> writeTimeout = default(Optional<TimeSpan?>), Optional<TimeSpan?> writeTimeout = default(Optional<TimeSpan?>),
Optional<string> proxyHost = default(Optional<string>),
Optional<int?> proxyPort = default(Optional<int?>),
Optional<string> proxyUsername = default(Optional<string>),
Optional<string> proxyPassword = default(Optional<string>))
Optional<Socks5ProxySettings> socks5ProxySettings = default(Optional<Socks5ProxySettings>))
{ {
return new TcpStreamSettings( return new TcpStreamSettings(
addressFamily: addressFamily.WithDefault(_addressFamily), addressFamily: addressFamily.WithDefault(_addressFamily),
@ -252,11 +215,8 @@ namespace MongoDB.Driver.Core.Configuration
receiveBufferSize: receiveBufferSize.WithDefault(_receiveBufferSize), receiveBufferSize: receiveBufferSize.WithDefault(_receiveBufferSize),
sendBufferSize: sendBufferSize.WithDefault(_sendBufferSize), sendBufferSize: sendBufferSize.WithDefault(_sendBufferSize),
socketConfigurator: socketConfigurator.WithDefault(_socketConfigurator), socketConfigurator: socketConfigurator.WithDefault(_socketConfigurator),
writeTimeout: writeTimeout.WithDefault(_writeTimeout),
proxyHost: proxyHost.WithDefault(_proxyHost),
proxyPort: proxyPort.WithDefault(_proxyPort),
proxyUsername: proxyUsername.WithDefault(_proxyUsername),
proxyPassword: proxyPassword.WithDefault(_proxyPassword));
socks5ProxySettings: socks5ProxySettings.WithDefault(_socks5ProxySettings),
writeTimeout: writeTimeout.WithDefault(_writeTimeout));
} }
} }
} }

122
src/MongoDB.Driver/Core/Connections/Socks5ProxySettings.cs

@ -0,0 +1,122 @@
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using MongoDB.Driver.Core.Misc;
namespace MongoDB.Driver.Core.Connections
{
/// <summary>
/// Represents the settings for a SOCKS5 proxy connection.
/// </summary>
public class Socks5ProxySettings
{
/// <summary>
/// Gets the host of the SOCKS5 proxy.
/// </summary>
public string Host { get; }
/// <summary>
/// Gets the port of the SOCKS5 proxy.
/// </summary>
public int Port { get; }
/// <summary>
/// Gets the authentication settings of the SOCKS5 proxy.
/// </summary>
public Socks5AuthenticationSettings Authentication { get; }
private Socks5ProxySettings(string host, int port, Socks5AuthenticationSettings authentication)
{
Host = Ensure.IsNotNullOrEmpty(host, nameof(host));
Port = Ensure.IsBetween(port, 0, 65535, nameof(port));
Authentication = Ensure.IsNotNull(authentication, nameof(authentication));
}
internal static Socks5ProxySettings Create(string host, int? port, string username, string password)
{
Socks5AuthenticationSettings authentication;
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
{
authentication = Socks5AuthenticationSettings.UsernamePassword(username, password);
}
else
{
authentication = Socks5AuthenticationSettings.None;
}
return new Socks5ProxySettings(host, port ?? 1080, authentication);
}
/// <summary>
/// Creates a new instance of <see cref="Socks5ProxySettings"/>.
/// </summary>
/// <param name="host">The host.</param>
/// <param name="port">The port</param>
/// <param name="authentication">The authentication settings.</param>
/// <returns></returns>
public static Socks5ProxySettings Create(string host, int port = 1080, Socks5AuthenticationSettings authentication = null)
{
return new Socks5ProxySettings(host, port, authentication ?? Socks5AuthenticationSettings.None);
}
}
internal enum Socks5AuthenticationType
{
None,
UsernamePassword
}
/// <summary>
/// Represents the settings for SOCKS5 authentication.
/// </summary>
public abstract class Socks5AuthenticationSettings
{
internal abstract Socks5AuthenticationType Type { get; }
/// <summary>
/// Creates authentication settings that do not require any authentication.
/// </summary>
public static Socks5AuthenticationSettings None => new NoAuthenticationSettings();
/// <summary>
/// Creates authentication settings for username and password.
/// </summary>
/// <param name="username"></param>
/// <param name="password"></param>
/// <returns></returns>
public static Socks5AuthenticationSettings UsernamePassword(string username, string password)
=> new UsernamePasswordAuthenticationSettings(username, password);
private sealed class NoAuthenticationSettings : Socks5AuthenticationSettings
{
internal override Socks5AuthenticationType Type => Socks5AuthenticationType.None;
}
private sealed class UsernamePasswordAuthenticationSettings : Socks5AuthenticationSettings
{
internal override Socks5AuthenticationType Type => Socks5AuthenticationType.UsernamePassword;
public string Username { get; }
public string Password { get; }
internal UsernamePasswordAuthenticationSettings(string username, string password)
{
Username = Ensure.IsNotNullOrEmpty(username, nameof(username));
Password = Ensure.IsNotNullOrEmpty(password, nameof(password));
}
}
}
}

133
src/MongoDB.Driver/MongoClientSettings.cs

@ -20,9 +20,9 @@ using System.Linq;
using System.Text; using System.Text;
using MongoDB.Driver.Core.Compression; using MongoDB.Driver.Core.Compression;
using MongoDB.Driver.Core.Configuration; using MongoDB.Driver.Core.Configuration;
using MongoDB.Driver.Core.Connections;
using MongoDB.Driver.Core.Misc; using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Core.Servers; using MongoDB.Driver.Core.Servers;
using MongoDB.Driver.Encryption;
using MongoDB.Shared; using MongoDB.Shared;
namespace MongoDB.Driver namespace MongoDB.Driver
@ -59,10 +59,6 @@ namespace MongoDB.Driver
private TimeSpan _maxConnectionLifeTime; private TimeSpan _maxConnectionLifeTime;
private int _maxConnectionPoolSize; private int _maxConnectionPoolSize;
private int _minConnectionPoolSize; private int _minConnectionPoolSize;
private string _proxyHost;
private int? _proxyPort;
private string _proxyUsername;
private string _proxyPassword;
private ReadConcern _readConcern; private ReadConcern _readConcern;
private UTF8Encoding _readEncoding; private UTF8Encoding _readEncoding;
private ReadPreference _readPreference; private ReadPreference _readPreference;
@ -75,6 +71,7 @@ namespace MongoDB.Driver
private ServerMonitoringMode _serverMonitoringMode; private ServerMonitoringMode _serverMonitoringMode;
private TimeSpan _serverSelectionTimeout; private TimeSpan _serverSelectionTimeout;
private TimeSpan _socketTimeout; private TimeSpan _socketTimeout;
private Socks5ProxySettings _socks5ProxySettings;
private int _srvMaxHosts; private int _srvMaxHosts;
private string _srvServiceName; private string _srvServiceName;
private SslSettings _sslSettings; private SslSettings _sslSettings;
@ -114,10 +111,6 @@ namespace MongoDB.Driver
_maxConnectionLifeTime = MongoDefaults.MaxConnectionLifeTime; _maxConnectionLifeTime = MongoDefaults.MaxConnectionLifeTime;
_maxConnectionPoolSize = MongoDefaults.MaxConnectionPoolSize; _maxConnectionPoolSize = MongoDefaults.MaxConnectionPoolSize;
_minConnectionPoolSize = MongoDefaults.MinConnectionPoolSize; _minConnectionPoolSize = MongoDefaults.MinConnectionPoolSize;
_proxyHost = null;
_proxyPort = null;
_proxyUsername = null;
_proxyPassword = null;
_readConcern = ReadConcern.Default; _readConcern = ReadConcern.Default;
_readEncoding = null; _readEncoding = null;
_readPreference = ReadPreference.Primary; _readPreference = ReadPreference.Primary;
@ -130,6 +123,7 @@ namespace MongoDB.Driver
_serverMonitoringMode = ServerMonitoringMode.Auto; _serverMonitoringMode = ServerMonitoringMode.Auto;
_serverSelectionTimeout = MongoDefaults.ServerSelectionTimeout; _serverSelectionTimeout = MongoDefaults.ServerSelectionTimeout;
_socketTimeout = MongoDefaults.SocketTimeout; _socketTimeout = MongoDefaults.SocketTimeout;
_socks5ProxySettings = null;
_srvMaxHosts = 0; _srvMaxHosts = 0;
_srvServiceName = MongoInternalDefaults.MongoClientSettings.SrvServiceName; _srvServiceName = MongoInternalDefaults.MongoClientSettings.SrvServiceName;
_sslSettings = null; _sslSettings = null;
@ -437,60 +431,15 @@ namespace MongoDB.Driver
} }
/// <summary> /// <summary>
/// Gets or sets the proxy host.
/// Gets or sets the SOCKS5 proxy settings.
/// </summary> /// </summary>
public string ProxyHost
public Socks5ProxySettings Socks5ProxySettings
{ {
get => _proxyHost;
get => _socks5ProxySettings;
set set
{ {
if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); } if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
_proxyHost = value;
}
}
/// <summary>
/// Gets or sets the proxy port.
/// </summary>
public int? ProxyPort
{
get => _proxyPort;
set
{
if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
if (value is < 0 or > 65535)
{
throw new MongoConfigurationException("ProxyPort must be between 0 and 65535.");
}
_proxyPort = value;
}
}
/// <summary>
/// Gets or sets the proxy username.
/// </summary>
public string ProxyUsername
{
get => _proxyUsername;
set
{
if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
_proxyUsername = value;
}
}
/// <summary>
/// Gets or sets the proxy password.
/// </summary>
public string ProxyPassword
{
get => _proxyPassword;
set
{
if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
_proxyPassword = value;
_socks5ProxySettings = value;
} }
} }
@ -929,10 +878,6 @@ namespace MongoDB.Driver
clientSettings.MaxConnectionLifeTime = url.MaxConnectionLifeTime; clientSettings.MaxConnectionLifeTime = url.MaxConnectionLifeTime;
clientSettings.MaxConnectionPoolSize = ConnectionStringConversions.GetEffectiveMaxConnections(url.MaxConnectionPoolSize); clientSettings.MaxConnectionPoolSize = ConnectionStringConversions.GetEffectiveMaxConnections(url.MaxConnectionPoolSize);
clientSettings.MinConnectionPoolSize = url.MinConnectionPoolSize; clientSettings.MinConnectionPoolSize = url.MinConnectionPoolSize;
clientSettings.ProxyHost = url.ProxyHost;
clientSettings.ProxyPort = url.ProxyPort;
clientSettings.ProxyUsername = url.ProxyUsername;
clientSettings.ProxyPassword = url.ProxyPassword;
clientSettings.ReadConcern = new ReadConcern(url.ReadConcernLevel); clientSettings.ReadConcern = new ReadConcern(url.ReadConcernLevel);
clientSettings.ReadEncoding = null; // ReadEncoding must be provided in code clientSettings.ReadEncoding = null; // ReadEncoding must be provided in code
clientSettings.ReadPreference = (url.ReadPreference == null) ? ReadPreference.Primary : url.ReadPreference; clientSettings.ReadPreference = (url.ReadPreference == null) ? ReadPreference.Primary : url.ReadPreference;
@ -944,6 +889,10 @@ namespace MongoDB.Driver
clientSettings.ServerMonitoringMode = url.ServerMonitoringMode ?? ServerMonitoringMode.Auto; clientSettings.ServerMonitoringMode = url.ServerMonitoringMode ?? ServerMonitoringMode.Auto;
clientSettings.ServerSelectionTimeout = url.ServerSelectionTimeout; clientSettings.ServerSelectionTimeout = url.ServerSelectionTimeout;
clientSettings.SocketTimeout = url.SocketTimeout; clientSettings.SocketTimeout = url.SocketTimeout;
if (!string.IsNullOrEmpty(url.ProxyHost))
{
clientSettings.Socks5ProxySettings = Socks5ProxySettings.Create(url.ProxyHost, url.ProxyPort, url.ProxyUsername, url.ProxyPassword);
}
clientSettings.SrvMaxHosts = url.SrvMaxHosts.GetValueOrDefault(0); clientSettings.SrvMaxHosts = url.SrvMaxHosts.GetValueOrDefault(0);
clientSettings.SrvServiceName = url.SrvServiceName; clientSettings.SrvServiceName = url.SrvServiceName;
clientSettings.SslSettings = null; clientSettings.SslSettings = null;
@ -990,10 +939,6 @@ namespace MongoDB.Driver
clone._maxConnectionLifeTime = _maxConnectionLifeTime; clone._maxConnectionLifeTime = _maxConnectionLifeTime;
clone._maxConnectionPoolSize = _maxConnectionPoolSize; clone._maxConnectionPoolSize = _maxConnectionPoolSize;
clone._minConnectionPoolSize = _minConnectionPoolSize; clone._minConnectionPoolSize = _minConnectionPoolSize;
clone._proxyHost = _proxyHost;
clone._proxyPort = _proxyPort;
clone._proxyUsername = _proxyUsername;
clone._proxyPassword = _proxyPassword;
clone._readConcern = _readConcern; clone._readConcern = _readConcern;
clone._readEncoding = _readEncoding; clone._readEncoding = _readEncoding;
clone._readPreference = _readPreference; clone._readPreference = _readPreference;
@ -1006,6 +951,7 @@ namespace MongoDB.Driver
clone._serverMonitoringMode = _serverMonitoringMode; clone._serverMonitoringMode = _serverMonitoringMode;
clone._serverSelectionTimeout = _serverSelectionTimeout; clone._serverSelectionTimeout = _serverSelectionTimeout;
clone._socketTimeout = _socketTimeout; clone._socketTimeout = _socketTimeout;
clone._socks5ProxySettings = _socks5ProxySettings;
clone._srvMaxHosts = _srvMaxHosts; clone._srvMaxHosts = _srvMaxHosts;
clone._srvServiceName = _srvServiceName; clone._srvServiceName = _srvServiceName;
clone._sslSettings = (_sslSettings == null) ? null : _sslSettings.Clone(); clone._sslSettings = (_sslSettings == null) ? null : _sslSettings.Clone();
@ -1063,10 +1009,6 @@ namespace MongoDB.Driver
_maxConnectionLifeTime == rhs._maxConnectionLifeTime && _maxConnectionLifeTime == rhs._maxConnectionLifeTime &&
_maxConnectionPoolSize == rhs._maxConnectionPoolSize && _maxConnectionPoolSize == rhs._maxConnectionPoolSize &&
_minConnectionPoolSize == rhs._minConnectionPoolSize && _minConnectionPoolSize == rhs._minConnectionPoolSize &&
_proxyHost == rhs._proxyHost &&
_proxyPort == rhs._proxyPort &&
_proxyUsername == rhs._proxyUsername &&
_proxyPassword == rhs._proxyPassword &&
object.Equals(_readEncoding, rhs._readEncoding) && object.Equals(_readEncoding, rhs._readEncoding) &&
object.Equals(_readConcern, rhs._readConcern) && object.Equals(_readConcern, rhs._readConcern) &&
object.Equals(_readPreference, rhs._readPreference) && object.Equals(_readPreference, rhs._readPreference) &&
@ -1079,6 +1021,7 @@ namespace MongoDB.Driver
_serverMonitoringMode == rhs._serverMonitoringMode && _serverMonitoringMode == rhs._serverMonitoringMode &&
_serverSelectionTimeout == rhs._serverSelectionTimeout && _serverSelectionTimeout == rhs._serverSelectionTimeout &&
_socketTimeout == rhs._socketTimeout && _socketTimeout == rhs._socketTimeout &&
object.Equals(_socks5ProxySettings, rhs._socks5ProxySettings) &&
_srvMaxHosts == rhs._srvMaxHosts && _srvMaxHosts == rhs._srvMaxHosts &&
_srvServiceName == rhs._srvServiceName && _srvServiceName == rhs._srvServiceName &&
_sslSettings == rhs._sslSettings && _sslSettings == rhs._sslSettings &&
@ -1154,10 +1097,6 @@ namespace MongoDB.Driver
.Hash(_maxConnectionLifeTime) .Hash(_maxConnectionLifeTime)
.Hash(_maxConnectionPoolSize) .Hash(_maxConnectionPoolSize)
.Hash(_minConnectionPoolSize) .Hash(_minConnectionPoolSize)
.Hash(_proxyHost)
.Hash(_proxyPort)
.Hash(_proxyUsername)
.Hash(_proxyPassword)
.Hash(_readConcern) .Hash(_readConcern)
.Hash(_readEncoding) .Hash(_readEncoding)
.Hash(_readPreference) .Hash(_readPreference)
@ -1170,6 +1109,7 @@ namespace MongoDB.Driver
.Hash(_serverMonitoringMode) .Hash(_serverMonitoringMode)
.Hash(_serverSelectionTimeout) .Hash(_serverSelectionTimeout)
.Hash(_socketTimeout) .Hash(_socketTimeout)
.Hash(_socks5ProxySettings)
.Hash(_srvMaxHosts) .Hash(_srvMaxHosts)
.Hash(_srvServiceName) .Hash(_srvServiceName)
.Hash(_sslSettings) .Hash(_sslSettings)
@ -1188,7 +1128,6 @@ namespace MongoDB.Driver
/// <returns>A string representation of the settings.</returns> /// <returns>A string representation of the settings.</returns>
public override string ToString() public override string ToString()
{ {
//TODO Need to add proxy here
if (_isFrozen) if (_isFrozen)
{ {
return _frozenStringRepresentation; return _frozenStringRepresentation;
@ -1228,21 +1167,9 @@ namespace MongoDB.Driver
sb.AppendFormat("MaxConnectionLifeTime={0};", _maxConnectionLifeTime); sb.AppendFormat("MaxConnectionLifeTime={0};", _maxConnectionLifeTime);
sb.AppendFormat("MaxConnectionPoolSize={0};", _maxConnectionPoolSize); sb.AppendFormat("MaxConnectionPoolSize={0};", _maxConnectionPoolSize);
sb.AppendFormat("MinConnectionPoolSize={0};", _minConnectionPoolSize); sb.AppendFormat("MinConnectionPoolSize={0};", _minConnectionPoolSize);
if (_proxyHost != null)
if (_socks5ProxySettings!= null)
{ {
sb.AppendFormat("ProxyHost={0};", _proxyHost);
}
if (_proxyPort != null)
{
sb.AppendFormat("ProxyPort={0};", _proxyPort.Value);
}
if (_proxyUsername != null)
{
sb.AppendFormat("ProxyUsername={0};", _proxyUsername);
}
if (_proxyPassword != null)
{
sb.AppendFormat("ProxyPassword={0};", _proxyPassword);
sb.AppendFormat("ProxyHost={0};", _socks5ProxySettings);
} }
if (_readEncoding != null) if (_readEncoding != null)
{ {
@ -1311,10 +1238,6 @@ namespace MongoDB.Driver
_maxConnectionLifeTime, _maxConnectionLifeTime,
_maxConnectionPoolSize, _maxConnectionPoolSize,
_minConnectionPoolSize, _minConnectionPoolSize,
_proxyHost,
_proxyPort,
_proxyUsername,
_proxyPassword,
MongoDefaults.TcpReceiveBufferSize, // TODO: add ReceiveBufferSize to MongoClientSettings? MongoDefaults.TcpReceiveBufferSize, // TODO: add ReceiveBufferSize to MongoClientSettings?
_replicaSetName, _replicaSetName,
_scheme, _scheme,
@ -1324,6 +1247,7 @@ namespace MongoDB.Driver
_serverMonitoringMode, _serverMonitoringMode,
_serverSelectionTimeout, _serverSelectionTimeout,
_socketTimeout, _socketTimeout,
_socks5ProxySettings,
_srvMaxHosts, _srvMaxHosts,
_srvServiceName, _srvServiceName,
_sslSettings, _sslSettings,
@ -1400,29 +1324,6 @@ namespace MongoDB.Driver
throw new InvalidOperationException("Load balanced mode cannot be used with direct connection."); throw new InvalidOperationException("Load balanced mode cannot be used with direct connection.");
} }
} }
if (string.IsNullOrEmpty(_proxyHost))
{
if (_proxyPort is not null)
{
throw new InvalidOperationException("ProxyPort cannot be specified without ProxyHost.");
}
if (!string.IsNullOrEmpty(_proxyUsername))
{
throw new InvalidOperationException("ProxyUsername cannot be specified without ProxyHost.");
}
if (!string.IsNullOrEmpty(_proxyPassword))
{
throw new InvalidOperationException("ProxyPassword cannot be specified without ProxyHost.");
}
}
if (string.IsNullOrEmpty(_proxyUsername) != string.IsNullOrEmpty(_proxyPassword))
{
throw new InvalidOperationException("ProxyUsername and ProxyPassword must both be specified or neither.");
}
} }
} }
} }
Loading…
Cancel
Save