Skip to content

Commit a76dc1d

Browse files
committed
Fixed auth
1 parent fda35a1 commit a76dc1d

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ public static void PerformSocks5Handshake(Stream stream, EndPoint endPoint, Sock
102102
stream.Write(buffer, 0, greetingRequestLength);
103103

104104
stream.ReadBytes(buffer, 0, 2, cancellationToken);
105-
ProcessGreetingResponse(buffer, useAuth, authenticationSettings, cancellationToken);
105+
var acceptsUsernamePasswordAuth = ProcessGreetingResponse(buffer, useAuth, authenticationSettings, cancellationToken);
106106

107-
if (useAuth)
107+
if (useAuth && acceptsUsernamePasswordAuth)
108108
{
109109
var authenticationRequestLength = CreateAuthenticationRequest(buffer, authenticationSettings, cancellationToken);
110110
stream.Write(buffer, 0, authenticationRequestLength);
@@ -137,9 +137,9 @@ public static async Task PerformSocks5HandshakeAsync(Stream stream, EndPoint end
137137
var greetingRequestLength = CreateGreetingRequest(buffer, useAuth);
138138
await stream.WriteAsync(buffer, 0, greetingRequestLength, cancellationToken).ConfigureAwait(false);
139139

140-
ProcessGreetingResponse(buffer, useAuth, authenticationSettings, cancellationToken);
140+
var acceptsUsernamePasswordAuth = ProcessGreetingResponse(buffer, useAuth, authenticationSettings, cancellationToken);
141141

142-
if (useAuth)
142+
if (useAuth && acceptsUsernamePasswordAuth)
143143
{
144144
var authenticationRequestLength = CreateAuthenticationRequest(buffer, authenticationSettings, cancellationToken);
145145
await stream.WriteAsync(buffer, 0, authenticationRequestLength, cancellationToken).ConfigureAwait(false);
@@ -178,7 +178,7 @@ private static int CreateGreetingRequest(byte[] buffer, bool useAuth)
178178
return 4;
179179
}
180180

181-
private static void ProcessGreetingResponse(byte[] buffer, bool useAuth, Socks5AuthenticationSettings authenticationSettings, CancellationToken cancellationToken)
181+
private static bool ProcessGreetingResponse(byte[] buffer, bool useAuth, Socks5AuthenticationSettings authenticationSettings, CancellationToken cancellationToken)
182182
{
183183
VerifyProtocolVersion(buffer[0]);
184184
var method = buffer[1];
@@ -188,11 +188,16 @@ private static void ProcessGreetingResponse(byte[] buffer, bool useAuth, Socks5A
188188
{
189189
throw new IOException("SOCKS5 proxy requires authentication, but no credentials were provided.");
190190
}
191+
192+
return true;
191193
}
192-
else if (method != MethodNoAuth)
194+
195+
if (method != MethodNoAuth)
193196
{
194197
throw new IOException("SOCKS5 proxy requires unsupported authentication method.");
195198
}
199+
200+
return false;
196201
}
197202

198203
private static int CreateAuthenticationRequest(byte[] buffer, Socks5AuthenticationSettings authenticationSettings, CancellationToken cancellationToken)

0 commit comments

Comments
 (0)