@@ -51,6 +51,7 @@ const STATES = {
5151 INITIAL : 'initial' ,
5252 HANDSHAKING : 'handshaking' ,
5353 AUTHENTICATING : 'authenticating' ,
54+ AUTHENTICATED : 'authenticated' ,
5455 CONNECTING : 'connecting' ,
5556 CONNECTED : 'connected' ,
5657 ERROR : 'error' ,
@@ -139,6 +140,11 @@ class Socks5Client extends EventEmitter {
139140 }
140141 }
141142
143+ markAuthenticated ( ) {
144+ this . state = STATES . AUTHENTICATED
145+ this . emit ( 'authenticated' )
146+ }
147+
142148 /**
143149 * Start the SOCKS5 handshake
144150 */
@@ -189,7 +195,7 @@ class Socks5Client extends EventEmitter {
189195 debug ( 'server selected auth method' , method )
190196
191197 if ( method === AUTH_METHODS . NO_AUTH ) {
192- this . emit ( 'authenticated' )
198+ this . markAuthenticated ( )
193199 } else if ( method === AUTH_METHODS . USERNAME_PASSWORD ) {
194200 this . state = STATES . AUTHENTICATING
195201 this . sendAuthRequest ( )
@@ -254,7 +260,7 @@ class Socks5Client extends EventEmitter {
254260
255261 this . buffer = this . buffer . subarray ( 2 )
256262 debug ( 'authentication successful' )
257- this . emit ( 'authenticated' )
263+ this . markAuthenticated ( )
258264 }
259265
260266 /**
@@ -263,8 +269,12 @@ class Socks5Client extends EventEmitter {
263269 * @param {number } port - Target port
264270 */
265271 connect ( address , port ) {
266- if ( this . state === STATES . CONNECTED ) {
267- throw new InvalidArgumentError ( 'Already connected' )
272+ if ( this . state === STATES . CONNECTING || this . state === STATES . CONNECTED ) {
273+ throw new InvalidArgumentError ( 'Connection already in progress' )
274+ }
275+
276+ if ( this . state !== STATES . AUTHENTICATED ) {
277+ throw new InvalidArgumentError ( 'Client must be authenticated before CONNECT' )
268278 }
269279
270280 debug ( 'connecting to' , address , port )
0 commit comments