Skip to content

Commit 47bd496

Browse files
committed
Update to support Promise API v1 and v2
1 parent 01f62e7 commit 47bd496

1 file changed

Lines changed: 30 additions & 30 deletions

File tree

src/Client.php

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -156,32 +156,33 @@ public function getConnection($host, $port)
156156

157157
$loop = $this->loop;
158158
$that = $this;
159-
\react\promise\all(
160-
array(
161-
$this->connector->create($this->socksHost, $this->socksPort)->then(
162-
null,
163-
function ($error) {
164-
throw new Exception('Unable to connect to socks server', 0, $error);
165-
}
166-
),
167-
$this->resolve($host)->then(
168-
null,
159+
160+
// simultaneously start TCP connection and DNS resolution
161+
$connecting = $this->connector->create($this->socksHost, $this->socksPort);
162+
$resolving = $this->resolve($host);
163+
164+
$deferred->resolve($connecting->then(
165+
function (Stream $stream) use ($that, $resolving, $loop, $timerTimeout, $protocolVersion, $auth, $timestampTimeout, $port) {
166+
// connection established, wait for DNS resolver
167+
return $resolving->then(
168+
function ($host) use ($stream, $port, $timestampTimeout, $that, $loop, $timerTimeout, $protocolVersion, $auth) {
169+
// DNS resolver completed => cancel timeout
170+
$loop->cancelTimer($timerTimeout);
171+
172+
$timeout = max($timestampTimeout - microtime(true), 0.1);
173+
return $that->handleConnectedSocks($stream, $host, $port, $timeout, $protocolVersion, $auth);
174+
},
169175
function ($error) {
170176
throw new Exception('Unable to resolve remote hostname', 0, $error);
171177
}
172-
)
173-
),
174-
function ($fulfilled) use ($deferred, $port, $timestampTimeout, $that, $loop, $timerTimeout, $protocolVersion, $auth) {
175-
$loop->cancelTimer($timerTimeout);
176-
177-
$timeout = max($timestampTimeout - microtime(true), 0.1);
178-
$deferred->resolve($that->handleConnectedSocks($fulfilled[0], $fulfilled[1], $port, $timeout, $protocolVersion, $auth));
178+
);
179179
},
180-
function ($error) use ($deferred, $loop, $timerTimeout) {
180+
function ($error) use ($loop, $timerTimeout) {
181181
$loop->cancelTimer($timerTimeout);
182-
$deferred->reject(new Exception('Unable to connect to socks server', 0, $error));
182+
throw new Exception('Unable to connect to socks server', 0, $error);
183183
}
184-
);
184+
));
185+
185186
return $deferred->promise();
186187
}
187188

@@ -200,10 +201,9 @@ private function resolve($host)
200201
public function handleConnectedSocks(Stream $stream, $host, $port, $timeout, $protocolVersion, $auth=null)
201202
{
202203
$deferred = new Deferred();
203-
$resolver = $deferred->resolver();
204204

205-
$timerTimeout = $this->loop->addTimer($timeout, function () use ($resolver) {
206-
$resolver->reject(new Exception('Timeout while establishing socks session'));
205+
$timerTimeout = $this->loop->addTimer($timeout, function () use ($deferred) {
206+
$deferred->reject(new Exception('Timeout while establishing socks session'));
207207
});
208208

209209
$reader = new StreamReader($stream);
@@ -214,14 +214,14 @@ public function handleConnectedSocks(Stream $stream, $host, $port, $timeout, $pr
214214
} else {
215215
$promise = $this->handleSocks4($stream, $host, $port, $reader);
216216
}
217-
$promise->then(function () use ($resolver, $stream) {
218-
$resolver->resolve($stream);
219-
}, function($error) use ($resolver) {
220-
$resolver->reject(new Exception('Unable to communicate...', 0, $error));
217+
$promise->then(function () use ($deferred, $stream) {
218+
$deferred->resolve($stream);
219+
}, function($error) use ($deferred) {
220+
$deferred->reject(new Exception('Unable to communicate...', 0, $error));
221221
});
222222

223223
$loop = $this->loop;
224-
$deferred->then(
224+
$deferred->promise()->then(
225225
function (Stream $stream) use ($timerTimeout, $loop, $reader) {
226226
$loop->cancelTimer($timerTimeout);
227227
$stream->removeAllListeners('end');
@@ -240,8 +240,8 @@ function ($error) use ($stream, $timerTimeout, $loop, $reader) {
240240
}
241241
);
242242

243-
$stream->on('end', function (Stream $stream) use ($resolver) {
244-
$resolver->reject(new Exception('Premature end while establishing socks session'));
243+
$stream->on('end', function (Stream $stream) use ($deferred) {
244+
$deferred->reject(new Exception('Premature end while establishing socks session'));
245245
});
246246

247247
return $deferred->promise();

0 commit comments

Comments
 (0)