@@ -106,10 +106,10 @@ public static void PerformSocks5Handshake(Stream stream, EndPoint endPoint, Sock
106
106
stream . Write ( buffer , 0 , greetingRequestLength ) ;
107
107
108
108
stream . ReadBytes ( buffer , 0 , 2 , cancellationToken ) ;
109
- var acceptsUsernamePasswordAuth = ProcessGreetingResponse ( buffer , useAuth ) ;
109
+ var requiresAuthenticationStep = ProcessGreetingResponse ( buffer , useAuth ) ;
110
110
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 )
113
113
{
114
114
var authenticationRequestLength = CreateAuthenticationRequest ( buffer , authenticationSettings ) ;
115
115
stream . Write ( buffer , 0 , authenticationRequestLength ) ;
@@ -143,10 +143,10 @@ public static async Task PerformSocks5HandshakeAsync(Stream stream, EndPoint end
143
143
await stream . WriteAsync ( buffer , 0 , greetingRequestLength , cancellationToken ) . ConfigureAwait ( false ) ;
144
144
145
145
await stream . ReadBytesAsync ( buffer , 0 , 2 , cancellationToken ) . ConfigureAwait ( false ) ;
146
- var acceptsUsernamePasswordAuth = ProcessGreetingResponse ( buffer , useAuth ) ;
146
+ var requiresAuthenticationStep = ProcessGreetingResponse ( buffer , useAuth ) ;
147
147
148
148
// If we have username and password, but the proxy doesn't need them, we skip.
149
- if ( useAuth && acceptsUsernamePasswordAuth )
149
+ if ( requiresAuthenticationStep )
150
150
{
151
151
var authenticationRequestLength = CreateAuthenticationRequest ( buffer , authenticationSettings ) ;
152
152
await stream . WriteAsync ( buffer , 0 , authenticationRequestLength , cancellationToken ) . ConfigureAwait ( false ) ;
@@ -202,7 +202,7 @@ private static bool ProcessGreetingResponse(byte[] buffer, bool useAuth)
202
202
203
203
if ( acceptedMethod != MethodNoAuth )
204
204
{
205
- throw new IOException ( "SOCKS5 proxy requires unsupported authentication method." ) ;
205
+ throw new IOException ( $ "SOCKS5 proxy requires unsupported authentication method. Unsupported method: { acceptedMethod } ") ;
206
206
}
207
207
208
208
return false ;
@@ -228,7 +228,7 @@ private static void ProcessAuthenticationResponse(byte[] buffer)
228
228
{
229
229
if ( buffer [ 0 ] != SubnegotiationVersion || buffer [ 1 ] != Socks5Success )
230
230
{
231
- throw new IOException ( "SOCKS5 authentication failed." ) ;
231
+ throw new IOException ( $ "SOCKS5 authentication failed. Version: { buffer [ 0 ] } , Status: { buffer [ 1 ] } .") ;
232
232
}
233
233
}
234
234
@@ -277,7 +277,7 @@ private static int ProcessConnectResponse(byte[] buffer)
277
277
278
278
if ( buffer [ 1 ] != Socks5Success )
279
279
{
280
- throw new IOException ( $ "SOCKS5 connect failed") ;
280
+ throw new IOException ( $ "SOCKS5 connect failed. Error code: { buffer [ 1 ] } ") ;
281
281
}
282
282
283
283
// We skip the last bytes of the response as we do not need them.
@@ -297,7 +297,7 @@ private static void VerifyProtocolVersion(byte version)
297
297
{
298
298
if ( version != ProtocolVersion5 )
299
299
{
300
- throw new IOException ( "Invalid SOCKS version in response." ) ;
300
+ throw new IOException ( $ "Invalid SOCKS version in response. Expected version { ProtocolVersion5 } , but received { version } .") ;
301
301
}
302
302
}
303
303
0 commit comments