Skip to content

Commit e73293e

Browse files
committed
Various small fixes
1 parent b78c38e commit e73293e

File tree

4 files changed

+13
-17
lines changed

4 files changed

+13
-17
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ private void ParseOption(string name, string value)
10711071
var proxyPortValue = ParseInt32(name, value);
10721072
if (proxyPortValue is < 1 or > 65535)
10731073
{
1074-
throw new MongoConfigurationException("proxyPort must be between 0 and 65535.");
1074+
throw new MongoConfigurationException($"Invalid proxy port: {proxyPortValue}: must be between 1 and 65535, inclusive.");
10751075
}
10761076
_proxyPort = proxyPortValue;
10771077
break;

src/MongoDB.Driver/Core/Connections/Socks5Helper.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ public static void PerformSocks5Handshake(Stream stream, EndPoint endPoint, Sock
106106
stream.Write(buffer, 0, greetingRequestLength);
107107

108108
stream.ReadBytes(buffer, 0, 2, cancellationToken);
109-
var acceptsUsernamePasswordAuth = ProcessGreetingResponse(buffer, useAuth);
109+
var requiresAuthenticationStep = ProcessGreetingResponse(buffer, useAuth);
110110

111-
// If we have username and password, but the proxy doesn't need them, we skip.
112-
if (useAuth && acceptsUsernamePasswordAuth)
111+
// If we have username and password, but the proxy doesn't need them, we skip the authentication step.
112+
if (requiresAuthenticationStep)
113113
{
114114
var authenticationRequestLength = CreateAuthenticationRequest(buffer, authenticationSettings);
115115
stream.Write(buffer, 0, authenticationRequestLength);
@@ -143,10 +143,10 @@ public static async Task PerformSocks5HandshakeAsync(Stream stream, EndPoint end
143143
await stream.WriteAsync(buffer, 0, greetingRequestLength, cancellationToken).ConfigureAwait(false);
144144

145145
await stream.ReadBytesAsync(buffer, 0, 2, cancellationToken).ConfigureAwait(false);
146-
var acceptsUsernamePasswordAuth = ProcessGreetingResponse(buffer, useAuth);
146+
var requiresAuthenticationStep = ProcessGreetingResponse(buffer, useAuth);
147147

148148
// If we have username and password, but the proxy doesn't need them, we skip.
149-
if (useAuth && acceptsUsernamePasswordAuth)
149+
if (requiresAuthenticationStep)
150150
{
151151
var authenticationRequestLength = CreateAuthenticationRequest(buffer, authenticationSettings);
152152
await stream.WriteAsync(buffer, 0, authenticationRequestLength, cancellationToken).ConfigureAwait(false);
@@ -202,7 +202,7 @@ private static bool ProcessGreetingResponse(byte[] buffer, bool useAuth)
202202

203203
if (acceptedMethod != MethodNoAuth)
204204
{
205-
throw new IOException("SOCKS5 proxy requires unsupported authentication method.");
205+
throw new IOException($"SOCKS5 proxy requires unsupported authentication method. Unsupported method: {acceptedMethod}");
206206
}
207207

208208
return false;
@@ -228,7 +228,7 @@ private static void ProcessAuthenticationResponse(byte[] buffer)
228228
{
229229
if (buffer[0] != SubnegotiationVersion || buffer[1] != Socks5Success)
230230
{
231-
throw new IOException("SOCKS5 authentication failed.");
231+
throw new IOException($"SOCKS5 authentication failed. Version: {buffer[0]}, Status: {buffer[1]}.");
232232
}
233233
}
234234

@@ -277,7 +277,7 @@ private static int ProcessConnectResponse(byte[] buffer)
277277

278278
if (buffer[1] != Socks5Success)
279279
{
280-
throw new IOException($"SOCKS5 connect failed");
280+
throw new IOException($"SOCKS5 connect failed. Error code: {buffer[1]}");
281281
}
282282

283283
// We skip the last bytes of the response as we do not need them.
@@ -297,7 +297,7 @@ private static void VerifyProtocolVersion(byte version)
297297
{
298298
if (version != ProtocolVersion5)
299299
{
300-
throw new IOException("Invalid SOCKS version in response.");
300+
throw new IOException($"Invalid SOCKS version in response. Expected version {ProtocolVersion5}, but received {version}.");
301301
}
302302
}
303303

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ public override string ToString()
135135

136136
sb.Append(Authentication switch
137137
{
138-
Socks5AuthenticationSettings.UsernamePasswordAuthenticationSettings up =>
139-
$"UsernamePassword (Username: {up.Username}, Password: {up.Password})",
138+
Socks5AuthenticationSettings.UsernamePasswordAuthenticationSettings =>
139+
"UsernamePassword",
140140
_ => "None"
141141
});
142142

src/MongoDB.Driver/MongoUrlBuilder.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -468,11 +468,7 @@ public int? ProxyPort
468468
get => _proxyPort;
469469
set
470470
{
471-
if (value is null)
472-
{
473-
throw new ArgumentNullException(nameof(ProxyPort));
474-
}
475-
_proxyPort = Ensure.IsBetween(value.Value, 1, 65535, nameof(ProxyPort));
471+
_proxyPort = Ensure.IsNullOrBetween(value, 1, 65535, nameof(ProxyPort));
476472
}
477473
}
478474

0 commit comments

Comments
 (0)