|
3 | 3 | namespace Clue\React\Socks; |
4 | 4 |
|
5 | 5 | use React\Socket\ServerInterface; |
6 | | -use React\Promise; |
7 | | -use React\Promise\Deferred; |
8 | 6 | use React\Promise\PromiseInterface; |
9 | 7 | use React\Socket\ConnectorInterface; |
10 | 8 | use React\Socket\Connector; |
@@ -71,13 +69,9 @@ public function setAuth($auth) |
71 | 69 |
|
72 | 70 | // wrap authentication callback in order to cast its return value to a promise |
73 | 71 | $this->auth = function($username, $password, $remote) use ($auth) { |
74 | | - $ret = call_user_func($auth, $username, $password, $remote); |
75 | | - if ($ret instanceof PromiseInterface) { |
76 | | - return $ret; |
77 | | - } |
78 | | - $deferred = new Deferred(); |
79 | | - $ret ? $deferred->resolve() : $deferred->reject(); |
80 | | - return $deferred->promise(); |
| 72 | + return \React\Promise\resolve( |
| 73 | + call_user_func($auth, $username, $password, $remote) |
| 74 | + ); |
81 | 75 | }; |
82 | 76 | } |
83 | 77 |
|
@@ -215,7 +209,7 @@ public function handleSocks4(ConnectionInterface $stream, StreamReader $reader) |
215 | 209 | } |
216 | 210 |
|
217 | 211 | /** @internal */ |
218 | | - public function handleSocks5(ConnectionInterface $stream, $auth=null, StreamReader $reader) |
| 212 | + public function handleSocks5(ConnectionInterface $stream, $auth, StreamReader $reader) |
219 | 213 | { |
220 | 214 | $remote = $stream->getRemoteAddress(); |
221 | 215 | if ($remote !== null) { |
@@ -255,13 +249,19 @@ public function handleSocks5(ConnectionInterface $stream, $auth=null, StreamRead |
255 | 249 | $remote = str_replace('://', '://' . rawurlencode($username) . ':' . rawurlencode($password) . '@', $remote); |
256 | 250 | } |
257 | 251 |
|
258 | | - return $auth($username, $password, $remote)->then(function () use ($stream) { |
259 | | - // accept |
260 | | - $stream->write(pack('C2', 0x01, 0x00)); |
261 | | - }, function() use ($stream) { |
262 | | - // reject => send any code but 0x00 |
| 252 | + return $auth($username, $password, $remote)->then(function ($authenticated) use ($stream) { |
| 253 | + if ($authenticated) { |
| 254 | + // accept auth |
| 255 | + $stream->write(pack('C2', 0x01, 0x00)); |
| 256 | + } else { |
| 257 | + // reject auth => send any code but 0x00 |
| 258 | + $stream->end(pack('C2', 0x01, 0xFF)); |
| 259 | + throw new UnexpectedValueException('Authentication denied'); |
| 260 | + } |
| 261 | + }, function ($e) use ($stream) { |
| 262 | + // reject failed authentication => send any code but 0x00 |
263 | 263 | $stream->end(pack('C2', 0x01, 0xFF)); |
264 | | - throw new UnexpectedValueException('Unable to authenticate'); |
| 264 | + throw new UnexpectedValueException('Authentication error', 0, $e); |
265 | 265 | }); |
266 | 266 | }); |
267 | 267 | }); |
@@ -336,7 +336,7 @@ public function connectTarget(ConnectionInterface $stream, array $target) |
336 | 336 | // validate URI so a string hostname can not pass excessive URI parts |
337 | 337 | $parts = parse_url('tcp://' . $uri); |
338 | 338 | if (!$parts || !isset($parts['scheme'], $parts['host'], $parts['port']) || count($parts) !== 3) { |
339 | | - return Promise\reject(new InvalidArgumentException('Invalid target URI given')); |
| 339 | + return \React\Promise\reject(new InvalidArgumentException('Invalid target URI given')); |
340 | 340 | } |
341 | 341 |
|
342 | 342 | if (isset($target[2])) { |
|
0 commit comments