@@ -66,8 +66,8 @@ public function setAuth($auth)
6666 throw new UnexpectedValueException ('Authentication requires SOCKS5. Consider using protocol version 5 or waive authentication ' );
6767 }
6868 // wrap authentication callback in order to cast its return value to a promise
69- $ this ->auth = function ($ username , $ password ) use ($ auth ) {
70- $ ret = call_user_func ($ auth , $ username , $ password );
69+ $ this ->auth = function ($ username , $ password, $ remote ) use ($ auth ) {
70+ $ ret = call_user_func ($ auth , $ username , $ password, $ remote );
7171 if ($ ret instanceof PromiseInterface) {
7272 return $ ret ;
7373 }
@@ -237,9 +237,17 @@ public function handleSocks5(ConnectionInterface $stream, $auth=null, StreamRead
237237 return $ reader ->readByte ()->then (function ($ length ) use ($ reader ) {
238238 return $ reader ->readLength ($ length );
239239 })->then (function ($ password ) use ($ username , $ auth , $ stream ) {
240- // username and password known => authenticate
241- // echo 'auth: ' . $username.' : ' . $password . PHP_EOL;
242- return $ auth ($ username , $ password )->then (function () use ($ stream , $ username ) {
240+ // username and password given => authenticate
241+ $ remote = $ stream ->getRemoteAddress ();
242+ if ($ remote !== null ) {
243+ // remove transport scheme and prefix socks5:// instead
244+ if (($ pos = strpos ($ remote , ':// ' )) !== false ) {
245+ $ remote = substr ($ remote , $ pos + 3 );
246+ }
247+ $ remote = 'socks5:// ' . rawurlencode ($ username ) . ': ' . rawurlencode ($ password ) . '@ ' . $ remote ;
248+ }
249+
250+ return $ auth ($ username , $ password , $ remote )->then (function () use ($ stream , $ username ) {
243251 // accept
244252 $ stream ->emit ('auth ' , array ($ username ));
245253 $ stream ->write (pack ('C2 ' , 0x01 , 0x00 ));
0 commit comments