SecureConnector.php 5.0 KB

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