| 1 |
<?php |
| 2 |
|
| 3 |
namespace React\Socket; |
| 4 |
|
| 5 |
use React\EventLoop\Loop; |
| 6 |
use React\EventLoop\LoopInterface; |
| 7 |
use React\Promise; |
| 8 |
use BadMethodCallException; |
| 9 |
use InvalidArgumentException; |
| 10 |
use UnexpectedValueException; |
| 11 |
|
| 12 |
final class SecureConnector implements ConnectorInterface |
| 13 |
{ |
| 14 |
private $connector; |
| 15 |
private $streamEncryption; |
| 16 |
private $context; |
| 17 |
|
| 18 |
/** |
| 19 |
* @param ConnectorInterface $connector |
| 20 |
* @param ?LoopInterface $loop |
| 21 |
* @param array $context |
| 22 |
*/ |
| 23 |
public function __construct(ConnectorInterface $connector, $loop = null, array $context = array()) |
| 24 |
{ |
| 25 |
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1 |
| 26 |
throw new \InvalidArgumentException('Argument #2 ($loop) expected null|React\EventLoop\LoopInterface'); |
| 27 |
} |
| 28 |
|
| 29 |
$this->connector = $connector; |
| 30 |
$this->streamEncryption = new StreamEncryption($loop ?: Loop::get(), false); |
| 31 |
$this->context = $context; |
| 32 |
} |
| 33 |
|
| 34 |
public function connect($uri) |
| 35 |
{ |
| 36 |
if (!\function_exists('stream_socket_enable_crypto')) { |
| 37 |
return Promise\reject(new \BadMethodCallException('Encryption not supported on your platform (HHVM < 3.8?)')); // @codeCoverageIgnore |
| 38 |
} |
| 39 |
|
| 40 |
if (\strpos($uri, '://') === false) { |
| 41 |
$uri = 'tls://' . $uri; |
| 42 |
} |
| 43 |
|
| 44 |
$parts = \parse_url($uri); |
| 45 |
if (!$parts || !isset($parts['scheme']) || $parts['scheme'] !== 'tls') { |
| 46 |
return Promise\reject(new \InvalidArgumentException( |
| 47 |
'Given URI "' . $uri . '" is invalid (EINVAL)', |
| 48 |
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : (\defined('PCNTL_EINVAL') ? \PCNTL_EINVAL : 22) |
| 49 |
)); |
| 50 |
} |
| 51 |
|
| 52 |
$context = $this->context; |
| 53 |
$encryption = $this->streamEncryption; |
| 54 |
$connected = false; |
| 55 |
/** @var \React\Promise\PromiseInterface<ConnectionInterface> $promise */ |
| 56 |
$promise = $this->connector->connect( |
| 57 |
\str_replace('tls://', '', $uri) |
| 58 |
)->then(function (ConnectionInterface $connection) use ($context, $encryption, $uri, &$promise, &$connected) { |
| 59 |
// (unencrypted) TCP/IP connection succeeded |
| 60 |
$connected = true; |
| 61 |
|
| 62 |
if (!$connection instanceof Connection) { |
| 63 |
$connection->close(); |
| 64 |
throw new \UnexpectedValueException('Base connector does not use internal Connection class exposing stream resource'); |
| 65 |
} |
| 66 |
|
| 67 |
// set required SSL/TLS context options |
| 68 |
foreach ($context as $name => $value) { |
| 69 |
\stream_context_set_option($connection->stream, 'ssl', $name, $value); |
| 70 |
} |
| 71 |
|
| 72 |
// try to enable encryption |
| 73 |
return $promise = $encryption->enable($connection)->then(null, function ($error) use ($connection, $uri) { |
| 74 |
// establishing encryption failed => close invalid connection and return error |
| 75 |
$connection->close(); |
| 76 |
|
| 77 |
throw new \RuntimeException( |
| 78 |
'Connection to ' . $uri . ' failed during TLS handshake: ' . $error->getMessage(), |
| 79 |
$error->getCode() |
| 80 |
); |
| 81 |
}); |
| 82 |
}, function (\Exception $e) use ($uri) { |
| 83 |
if ($e instanceof \RuntimeException) { |
| 84 |
$message = \preg_replace('/^Connection to [^ ]+/', '', $e->getMessage()); |
| 85 |
$e = new \RuntimeException( |
| 86 |
'Connection to ' . $uri . $message, |
| 87 |
$e->getCode(), |
| 88 |
$e |
| 89 |
); |
| 90 |
|
| 91 |
// avoid garbage references by replacing all closures in call stack. |
| 92 |
// what a lovely piece of code! |
| 93 |
$r = new \ReflectionProperty('Exception', 'trace'); |
| 94 |
if (\PHP_VERSION_ID < 80100) { |
| 95 |
$r->setAccessible(true); |
| 96 |
} |
| 97 |
$trace = $r->getValue($e); |
| 98 |
|
| 99 |
// Exception trace arguments are not available on some PHP 7.4 installs |
| 100 |
// @codeCoverageIgnoreStart |
| 101 |
foreach ($trace as $ti => $one) { |
| 102 |
if (isset($one['args'])) { |
| 103 |
foreach ($one['args'] as $ai => $arg) { |
| 104 |
if ($arg instanceof \Closure) { |
| 105 |
$trace[$ti]['args'][$ai] = 'Object(' . \get_class($arg) . ')'; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
// @codeCoverageIgnoreEnd |
| 111 |
$r->setValue($e, $trace); |
| 112 |
} |
| 113 |
|
| 114 |
throw $e; |
| 115 |
}); |
| 116 |
|
| 117 |
return new \React\Promise\Promise( |
| 118 |
function ($resolve, $reject) use ($promise) { |
| 119 |
$promise->then($resolve, $reject); |
| 120 |
}, |
| 121 |
function ($_, $reject) use (&$promise, $uri, &$connected) { |
| 122 |
if ($connected) { |
| 123 |
$reject(new \RuntimeException( |
| 124 |
'Connection to ' . $uri . ' cancelled during TLS handshake (ECONNABORTED)', |
| 125 |
\defined('SOCKET_ECONNABORTED') ? \SOCKET_ECONNABORTED : 103 |
| 126 |
)); |
| 127 |
} |
| 128 |
|
| 129 |
$promise->cancel(); |
| 130 |
$promise = null; |
| 131 |
} |
| 132 |
); |
| 133 |
} |
| 134 |
} |