@@ -203,10 +203,13 @@ public function handleConnectedSocks(Stream $stream, $host, $port, $timeout, $pr
203203 $ resolver ->reject (new Exception ('Timeout while establishing socks session ' ));
204204 });
205205
206+ $ reader = new StreamReader ($ stream );
207+ $ stream ->on ('data ' , array ($ reader , 'write ' ));
208+
206209 if ($ protocolVersion === '5 ' || $ auth !== null ) {
207- $ promise = $ this ->handleSocks5 ($ stream , $ host , $ port , $ auth );
210+ $ promise = $ this ->handleSocks5 ($ stream , $ host , $ port , $ auth, $ reader );
208211 } else {
209- $ promise = $ this ->handleSocks4 ($ stream , $ host , $ port );
212+ $ promise = $ this ->handleSocks4 ($ stream , $ host , $ port, $ reader );
210213 }
211214 $ promise ->then (function () use ($ resolver , $ stream ) {
212215 $ resolver ->resolve ($ stream );
@@ -216,14 +219,20 @@ public function handleConnectedSocks(Stream $stream, $host, $port, $timeout, $pr
216219
217220 $ loop = $ this ->loop ;
218221 $ deferred ->then (
219- function (Stream $ stream ) use ($ timerTimeout , $ loop ) {
222+ function (Stream $ stream ) use ($ timerTimeout , $ loop, $ reader ) {
220223 $ loop ->cancelTimer ($ timerTimeout );
221224 $ stream ->removeAllListeners ('end ' );
225+
226+ $ stream ->removeListener ('data ' , array ($ reader , 'write ' ));
227+
222228 return $ stream ;
223229 },
224- function ($ error ) use ($ stream , $ timerTimeout , $ loop ) {
230+ function ($ error ) use ($ stream , $ timerTimeout , $ loop, $ reader ) {
225231 $ loop ->cancelTimer ($ timerTimeout );
226232 $ stream ->close ();
233+
234+ $ stream ->removeListener ('data ' , array ($ reader , 'write ' ));
235+
227236 return $ error ;
228237 }
229238 );
@@ -235,7 +244,7 @@ function ($error) use ($stream, $timerTimeout, $loop) {
235244 return $ deferred ->promise ();
236245 }
237246
238- protected function handleSocks4 (Stream $ stream , $ host , $ port )
247+ protected function handleSocks4 (Stream $ stream , $ host , $ port, StreamReader $ reader )
239248 {
240249 // do not resolve hostname. only try to convert to IP
241250 $ ip = ip2long ($ host );
@@ -250,7 +259,6 @@ protected function handleSocks4(Stream $stream, $host, $port)
250259
251260 $ stream ->write ($ data );
252261
253- $ reader = new StreamReader ($ stream );
254262 return $ reader ->readBinary (array (
255263 'null ' => 'C ' ,
256264 'status ' => 'C ' ,
@@ -263,7 +271,7 @@ protected function handleSocks4(Stream $stream, $host, $port)
263271 });
264272 }
265273
266- protected function handleSocks5 (Stream $ stream , $ host , $ port , $ auth =null )
274+ protected function handleSocks5 (Stream $ stream , $ host , $ port , $ auth =null , StreamReader $ reader )
267275 {
268276 // protocol version 5
269277 $ data = pack ('C ' , 0x05 );
@@ -277,7 +285,7 @@ protected function handleSocks5(Stream $stream, $host, $port, $auth=null)
277285 $ stream ->write ($ data );
278286
279287 $ that = $ this ;
280- $ reader = new StreamReader ( $ stream );
288+
281289 return $ reader ->readBinary (array (
282290 'version ' => 'C ' ,
283291 'method ' => 'C '
0 commit comments