friendship ended with social-app. php is my new best friend
1<?php
2
3namespace React\Socket;
4
5use React\EventLoop\Loop;
6use React\EventLoop\LoopInterface;
7use React\Promise;
8use BadMethodCallException;
9use InvalidArgumentException;
10use UnexpectedValueException;
11
12final 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 $r->setAccessible(true);
95 $trace = $r->getValue($e);
96
97 // Exception trace arguments are not available on some PHP 7.4 installs
98 // @codeCoverageIgnoreStart
99 foreach ($trace as $ti => $one) {
100 if (isset($one['args'])) {
101 foreach ($one['args'] as $ai => $arg) {
102 if ($arg instanceof \Closure) {
103 $trace[$ti]['args'][$ai] = 'Object(' . \get_class($arg) . ')';
104 }
105 }
106 }
107 }
108 // @codeCoverageIgnoreEnd
109 $r->setValue($e, $trace);
110 }
111
112 throw $e;
113 });
114
115 return new \React\Promise\Promise(
116 function ($resolve, $reject) use ($promise) {
117 $promise->then($resolve, $reject);
118 },
119 function ($_, $reject) use (&$promise, $uri, &$connected) {
120 if ($connected) {
121 $reject(new \RuntimeException(
122 'Connection to ' . $uri . ' cancelled during TLS handshake (ECONNABORTED)',
123 \defined('SOCKET_ECONNABORTED') ? \SOCKET_ECONNABORTED : 103
124 ));
125 }
126
127 $promise->cancel();
128 $promise = null;
129 }
130 );
131 }
132}