| 1 |
<?php |
| 2 |
|
| 3 |
namespace React\Socket; |
| 4 |
|
| 5 |
use React\Dns\Resolver\ResolverInterface; |
| 6 |
use React\Promise; |
| 7 |
use React\Promise\PromiseInterface; |
| 8 |
|
| 9 |
final class DnsConnector implements ConnectorInterface |
| 10 |
{ |
| 11 |
private $connector; |
| 12 |
private $resolver; |
| 13 |
|
| 14 |
public function __construct(ConnectorInterface $connector, ResolverInterface $resolver) |
| 15 |
{ |
| 16 |
$this->connector = $connector; |
| 17 |
$this->resolver = $resolver; |
| 18 |
} |
| 19 |
|
| 20 |
public function connect($uri) |
| 21 |
{ |
| 22 |
$original = $uri; |
| 23 |
if (\strpos($uri, '://') === false) { |
| 24 |
$uri = 'tcp://' . $uri; |
| 25 |
$parts = \parse_url($uri); |
| 26 |
if (isset($parts['scheme'])) { |
| 27 |
unset($parts['scheme']); |
| 28 |
} |
| 29 |
} else { |
| 30 |
$parts = \parse_url($uri); |
| 31 |
} |
| 32 |
|
| 33 |
if (!$parts || !isset($parts['host'])) { |
| 34 |
return Promise\reject(new \InvalidArgumentException( |
| 35 |
'Given URI "' . $original . '" is invalid (EINVAL)', |
| 36 |
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : (\defined('PCNTL_EINVAL') ? \PCNTL_EINVAL : 22) |
| 37 |
)); |
| 38 |
} |
| 39 |
|
| 40 |
$host = \trim($parts['host'], '[]'); |
| 41 |
$connector = $this->connector; |
| 42 |
|
| 43 |
// skip DNS lookup / URI manipulation if this URI already contains an IP |
| 44 |
if (@\inet_pton($host) !== false) { |
| 45 |
return $connector->connect($original); |
| 46 |
} |
| 47 |
|
| 48 |
$promise = $this->resolver->resolve($host); |
| 49 |
$resolved = null; |
| 50 |
|
| 51 |
return new Promise\Promise( |
| 52 |
function ($resolve, $reject) use (&$promise, &$resolved, $uri, $connector, $host, $parts) { |
| 53 |
// resolve/reject with result of DNS lookup |
| 54 |
$promise->then(function ($ip) use (&$promise, &$resolved, $uri, $connector, $host, $parts) { |
| 55 |
$resolved = $ip; |
| 56 |
|
| 57 |
return $promise = $connector->connect( |
| 58 |
Connector::uri($parts, $host, $ip) |
| 59 |
)->then(null, function (\Exception $e) use ($uri) { |
| 60 |
if ($e instanceof \RuntimeException) { |
| 61 |
$message = \preg_replace('/^(Connection to [^ ]+)[&?]hostname=[^ &]+/', '$1', $e->getMessage()); |
| 62 |
$e = new \RuntimeException( |
| 63 |
'Connection to ' . $uri . ' failed: ' . $message, |
| 64 |
$e->getCode(), |
| 65 |
$e |
| 66 |
); |
| 67 |
|
| 68 |
// avoid garbage references by replacing all closures in call stack. |
| 69 |
// what a lovely piece of code! |
| 70 |
$r = new \ReflectionProperty('Exception', 'trace'); |
| 71 |
if (\PHP_VERSION_ID < 80100) { |
| 72 |
$r->setAccessible(true); |
| 73 |
} |
| 74 |
$trace = $r->getValue($e); |
| 75 |
|
| 76 |
// Exception trace arguments are not available on some PHP 7.4 installs |
| 77 |
// @codeCoverageIgnoreStart |
| 78 |
foreach ($trace as $ti => $one) { |
| 79 |
if (isset($one['args'])) { |
| 80 |
foreach ($one['args'] as $ai => $arg) { |
| 81 |
if ($arg instanceof \Closure) { |
| 82 |
$trace[$ti]['args'][$ai] = 'Object(' . \get_class($arg) . ')'; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
// @codeCoverageIgnoreEnd |
| 88 |
$r->setValue($e, $trace); |
| 89 |
} |
| 90 |
|
| 91 |
throw $e; |
| 92 |
}); |
| 93 |
}, function ($e) use ($uri, $reject) { |
| 94 |
$reject(new \RuntimeException('Connection to ' . $uri .' failed during DNS lookup: ' . $e->getMessage(), 0, $e)); |
| 95 |
})->then($resolve, $reject); |
| 96 |
}, |
| 97 |
function ($_, $reject) use (&$promise, &$resolved, $uri) { |
| 98 |
// cancellation should reject connection attempt |
| 99 |
// reject DNS resolution with custom reason, otherwise rely on connection cancellation below |
| 100 |
if ($resolved === null) { |
| 101 |
$reject(new \RuntimeException( |
| 102 |
'Connection to ' . $uri . ' cancelled during DNS lookup (ECONNABORTED)', |
| 103 |
\defined('SOCKET_ECONNABORTED') ? \SOCKET_ECONNABORTED : 103 |
| 104 |
)); |
| 105 |
} |
| 106 |
|
| 107 |
// (try to) cancel pending DNS lookup / connection attempt |
| 108 |
if ($promise instanceof PromiseInterface && \method_exists($promise, 'cancel')) { |
| 109 |
// overwrite callback arguments for PHP7+ only, so they do not show |
| 110 |
// up in the Exception trace and do not cause a possible cyclic reference. |
| 111 |
$_ = $reject = null; |
| 112 |
|
| 113 |
$promise->cancel(); |
| 114 |
$promise = null; |
| 115 |
} |
| 116 |
} |
| 117 |
); |
| 118 |
} |
| 119 |
} |