@@ -102,9 +102,9 @@ public static void PerformSocks5Handshake(Stream stream, EndPoint endPoint, Sock
102
102
stream . Write ( buffer , 0 , greetingRequestLength ) ;
103
103
104
104
stream . ReadBytes ( buffer , 0 , 2 , cancellationToken ) ;
105
- ProcessGreetingResponse ( buffer , useAuth , authenticationSettings , cancellationToken ) ;
105
+ var acceptsUsernamePasswordAuth = ProcessGreetingResponse ( buffer , useAuth , authenticationSettings , cancellationToken ) ;
106
106
107
- if ( useAuth )
107
+ if ( useAuth && acceptsUsernamePasswordAuth )
108
108
{
109
109
var authenticationRequestLength = CreateAuthenticationRequest ( buffer , authenticationSettings , cancellationToken ) ;
110
110
stream . Write ( buffer , 0 , authenticationRequestLength ) ;
@@ -137,9 +137,9 @@ public static async Task PerformSocks5HandshakeAsync(Stream stream, EndPoint end
137
137
var greetingRequestLength = CreateGreetingRequest ( buffer , useAuth ) ;
138
138
await stream . WriteAsync ( buffer , 0 , greetingRequestLength , cancellationToken ) . ConfigureAwait ( false ) ;
139
139
140
- ProcessGreetingResponse ( buffer , useAuth , authenticationSettings , cancellationToken ) ;
140
+ var acceptsUsernamePasswordAuth = ProcessGreetingResponse ( buffer , useAuth , authenticationSettings , cancellationToken ) ;
141
141
142
- if ( useAuth )
142
+ if ( useAuth && acceptsUsernamePasswordAuth )
143
143
{
144
144
var authenticationRequestLength = CreateAuthenticationRequest ( buffer , authenticationSettings , cancellationToken ) ;
145
145
await stream . WriteAsync ( buffer , 0 , authenticationRequestLength , cancellationToken ) . ConfigureAwait ( false ) ;
@@ -178,7 +178,7 @@ private static int CreateGreetingRequest(byte[] buffer, bool useAuth)
178
178
return 4 ;
179
179
}
180
180
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 )
182
182
{
183
183
VerifyProtocolVersion ( buffer [ 0 ] ) ;
184
184
var method = buffer [ 1 ] ;
@@ -188,11 +188,16 @@ private static void ProcessGreetingResponse(byte[] buffer, bool useAuth, Socks5A
188
188
{
189
189
throw new IOException ( "SOCKS5 proxy requires authentication, but no credentials were provided." ) ;
190
190
}
191
+
192
+ return true ;
191
193
}
192
- else if ( method != MethodNoAuth )
194
+
195
+ if ( method != MethodNoAuth )
193
196
{
194
197
throw new IOException ( "SOCKS5 proxy requires unsupported authentication method." ) ;
195
198
}
199
+
200
+ return false ;
196
201
}
197
202
198
203
private static int CreateAuthenticationRequest ( byte [ ] buffer , Socks5AuthenticationSettings authenticationSettings , CancellationToken cancellationToken )
0 commit comments